
Olá! Não é segredo que existem muitos programas para hackear jogos e aplicativos. Existem também muitas maneiras de hackear. Por exemplo, descompilação e modificação do código-fonte (com a publicação subsequente de APKs personalizados, por exemplo, com ouro infinito e todas as compras pagas). Ou a forma mais versátil é escanear, filtrar e editar os valores na RAM. Como lidar com o último, vou te dizer sob o corte.
Em geral, temos um perfil de jogador com vários parâmetros, que é serializado no Jogo Salvo e carregado / salvo quando o jogo começa / termina. E se for muito simples adicionar criptografia durante a serialização, proteger o mesmo perfil na RAM é um pouco mais difícil. Vou tentar dar um exemplo simples:
var money = 100; // "100" is present in RAM now (as four-byte integer value). Cheat apps can find, filter and replace it since it was declared.
money += 20; // Cheat apps can scan RAM for "120" values, filter them and discover the RAM address of our "money" variable.
Debug.Log(money); // We expect to see "120" in console. But cheat apps can deceive us!
ProtectedInt experience = 500; // four XOR-encrypted bytes are present in RAM now. Cheat apps can't find our value in RAM.
experience += 100;
Debug.Log(experience); // We can see "600" in console;
Debug.Log(JsonUtility.ToJson(experience)); // We can see four XOR-encrypted bytes here: {"_":[96,96,102,53]}. Our "experience" is hidden.
O segundo ponto que merece atenção é que a implementação da nova proteção deve ocorrer com o mínimo de alterações no código-fonte do jogo, onde tudo já funciona bem e foi testado várias vezes. No meu método, será suficiente substituir os tipos int / long / float por ProtectedInt / ProtectedLong / ProtectedFloat . Em seguida, fornecerei comentários e código.
A classe base Protected armazena uma matriz criptografada de bytes no campo "_", ela também é responsável por criptografar e descriptografar os dados. A criptografia é primitiva - XOR com chave . Esta criptografia é rápida, então você pode trabalhar com variáveis mesmo no Update... A classe base funciona com matrizes de bytes. As classes filhas são responsáveis por converter seu tipo em uma matriz de bytes. Mas o mais importante, eles são "disfarçados" como tipos simples usando o operador implícito , de modo que o desenvolvedor pode nem perceber que o tipo das variáveis mudou. Você também pode notar os atributos em alguns dos métodos e propriedades que são necessários para serialização com JsonUtility e Newtonsoft.Json (ambos são suportados ao mesmo tempo). Se você não estiver usando Newtonsoft.Json, será necessário remover o #define NEWTONSOFT_JSON .
#define NEWTONSOFT_JSON
using System;
using UnityEngine;
#if NEWTONSOFT_JSON
using Newtonsoft.Json;
#endif
namespace Assets
{
[Serializable]
public class ProtectedInt : Protected
{
#if NEWTONSOFT_JSON
[JsonConstructor]
#endif
private ProtectedInt()
{
}
protected ProtectedInt(byte[] bytes) : base(bytes)
{
}
public static implicit operator ProtectedInt(int value)
{
return new ProtectedInt(BitConverter.GetBytes(value));
}
public static implicit operator int(ProtectedInt value) => value == null ? 0 : BitConverter.ToInt32(value.DecodedBytes, 0);
public override string ToString()
{
return ((int) this).ToString();
}
}
[Serializable]
public class ProtectedFloat : Protected
{
#if NEWTONSOFT_JSON
[JsonConstructor]
#endif
private ProtectedFloat()
{
}
protected ProtectedFloat(byte[] bytes) : base(bytes)
{
}
public static implicit operator ProtectedFloat(int value)
{
return new ProtectedFloat(BitConverter.GetBytes(value));
}
public static implicit operator float(ProtectedFloat value) => value == null ? 0 : BitConverter.ToSingle(value.DecodedBytes, 0);
public override string ToString()
{
return ((float) this).ToString(System.Globalization.CultureInfo.InvariantCulture);
}
}
public abstract class Protected
{
#if NEWTONSOFT_JSON
[JsonProperty]
#endif
[SerializeField]
private byte[] _;
private static readonly byte[] Key = System.Text.Encoding.UTF8.GetBytes("8bf5b15ffef1f485f673ceb874fd6ef0");
protected Protected()
{
}
protected Protected(byte[] bytes)
{
_ = Encode(bytes);
}
private static byte[] Encode(byte[] bytes)
{
var encoded = new byte[bytes.Length];
for (var i = 0; i < bytes.Length; i++)
{
encoded[i] = (byte) (bytes[i] ^ Key[i % Key.Length]);
}
return encoded;
}
protected byte[] DecodedBytes
{
get
{
var decoded = new byte[_.Length];
for (var i = 0; i < decoded.Length; i++)
{
decoded[i] = (byte) (_[i] ^ Key[i % Key.Length]);
}
return decoded;
}
}
}
}
Se você se esqueceu ou errou em algum lugar, escreva nos comentários =) Boa sorte com o desenvolvimento!
PS. Não é meu gato, foto de CatCosplay.
UPD. Nos comentários feitos as seguintes observações sobre o caso:
- Melhor mudar para uma estrutura para tornar o código mais previsível (ainda mais se nos disfarçarmos como tipos de valor simples).
- A pesquisa na RAM pode ser realizada não por valores específicos, mas por todas as variáveis alteradas. XOR não vai ajudar aqui. Como alternativa, insira uma soma de verificação.
- BitConverter é lento (em uma escala micro, é claro). Melhor se livrar dele (por int, acabou, por float - estou esperando suas sugestões).
Abaixo está uma versão atualizada do código. ProtectedInt e ProtectedFloat agora são estruturas. Eu me livrei das matrizes de bytes. Além disso, introduziu a soma de verificação _h como uma solução para o segundo problema. Testei a serialização de ambas as maneiras.
[Serializable]
public struct ProtectedInt
{
#if NEWTONSOFT_JSON
[JsonProperty]
#endif
[SerializeField]
private int _;
#if NEWTONSOFT_JSON
[JsonProperty]
#endif
[SerializeField]
private byte _h;
private const int XorKey = 514229;
private ProtectedInt(int value)
{
_ = value ^ XorKey;
_h = GetHash(_);
}
public static implicit operator ProtectedInt(int value)
{
return new ProtectedInt(value);
}
public static implicit operator int(ProtectedInt value) => value._ == 0 && value._h == 0 || value._h != GetHash(value._) ? 0 : value._ ^ XorKey;
public override string ToString()
{
return ((int) this).ToString();
}
private static byte GetHash(int value)
{
return (byte) (255 - value % 256);
}
}
[Serializable]
public struct ProtectedFloat
{
#if NEWTONSOFT_JSON
[JsonProperty]
#endif
[SerializeField]
private int _;
#if NEWTONSOFT_JSON
[JsonProperty]
#endif
[SerializeField]
private byte _h;
private const int XorKey = 514229;
private ProtectedFloat(int value)
{
_ = value ^ XorKey;
_h = GetHash(_);
}
public static implicit operator ProtectedFloat(float value)
{
return new ProtectedFloat(BitConverter.ToInt32(BitConverter.GetBytes(value), 0));
}
public static implicit operator float(ProtectedFloat value) => value._ == 0 && value._h == 0 || value._h != GetHash(value._) ? 0f : BitConverter.ToSingle(BitConverter.GetBytes(value._ ^ XorKey), 0);
public override string ToString()
{
return ((float) this).ToString(CultureInfo.InvariantCulture);
}
private static byte GetHash(int value)
{
return (byte) (255 - value % 256);
}
}