重构一半
parent
0ca1f8fe41
commit
807ead7cfc
|
@ -9,7 +9,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
namespace Obfuz.Encryption
|
namespace Obfuz.Data
|
||||||
{
|
{
|
||||||
public struct RvaData
|
public struct RvaData
|
||||||
{
|
{
|
|
@ -2,7 +2,7 @@
|
||||||
using dnlib.DotNet.Emit;
|
using dnlib.DotNet.Emit;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using Obfuz.Emit;
|
using Obfuz.Emit;
|
||||||
using Obfuz.Encryption;
|
using Obfuz.Data;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Obfuz.Emit
|
namespace Obfuz.Emit
|
||||||
|
|
|
@ -9,7 +9,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using static UnityEngine.Networking.UnityWebRequest;
|
using static UnityEngine.Networking.UnityWebRequest;
|
||||||
using UnityEngine.UIElements;
|
using UnityEngine.UIElements;
|
||||||
using Obfuz.Encryption;
|
using Obfuz.Data;
|
||||||
|
|
||||||
namespace Obfuz.Emit
|
namespace Obfuz.Emit
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,14 +2,7 @@
|
||||||
using dnlib.DotNet.Emit;
|
using dnlib.DotNet.Emit;
|
||||||
using Obfuz.Emit;
|
using Obfuz.Emit;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using Obfuz.Data;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using static UnityEngine.Networking.UnityWebRequest;
|
|
||||||
using UnityEngine.UIElements;
|
|
||||||
using Obfuz.Encryption;
|
|
||||||
|
|
||||||
namespace Obfuz.Emit
|
namespace Obfuz.Emit
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,102 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
using System;
|
||||||
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
|
namespace Obfuz.Emit
|
||||||
|
{
|
||||||
|
public class DefaultModuleMetadataImporter : ModuleMetadataImporterBase
|
||||||
|
{
|
||||||
|
public DefaultModuleMetadataImporter() { }
|
||||||
|
|
||||||
|
public override void Init(ModuleDef mod)
|
||||||
|
{
|
||||||
|
_module = mod;
|
||||||
|
var constUtilityType = typeof(ConstUtility);
|
||||||
|
|
||||||
|
_castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat"));
|
||||||
|
Assert.IsNotNull(_castIntAsFloat, "CastIntAsFloat not found");
|
||||||
|
_castLongAsDouble = mod.Import(constUtilityType.GetMethod("CastLongAsDouble"));
|
||||||
|
Assert.IsNotNull(_castLongAsDouble, "CastLongAsDouble not found");
|
||||||
|
_castFloatAsInt = mod.Import(constUtilityType.GetMethod("CastFloatAsInt"));
|
||||||
|
Assert.IsNotNull(_castFloatAsInt, "CastFloatAsInt not found");
|
||||||
|
_castDoubleAsLong = mod.Import(constUtilityType.GetMethod("CastDoubleAsLong"));
|
||||||
|
Assert.IsNotNull(_castDoubleAsLong, "CastDoubleAsLong not found");
|
||||||
|
|
||||||
|
_initializeArray = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) }));
|
||||||
|
Assert.IsNotNull(_initializeArray);
|
||||||
|
_encryptBlock = mod.Import(typeof(EncryptionService).GetMethod("EncryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptBlock);
|
||||||
|
_decryptBlock = mod.Import(typeof(EncryptionService).GetMethod("DecryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptBlock);
|
||||||
|
_encryptInt = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(int), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptInt);
|
||||||
|
_decryptInt = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(int), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptInt);
|
||||||
|
_encryptLong = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(long), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptLong);
|
||||||
|
_decryptLong = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(long), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptLong);
|
||||||
|
_encryptFloat = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(float), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptFloat);
|
||||||
|
_decryptFloat = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(float), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptFloat);
|
||||||
|
_encryptDouble = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(double), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptDouble);
|
||||||
|
_decryptDouble = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(double), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptDouble);
|
||||||
|
_encryptString = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(string), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptString);
|
||||||
|
_decryptString = mod.Import(typeof(EncryptionService).GetMethod("DecryptString", new[] { typeof(int[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptString);
|
||||||
|
_encryptBytes = mod.Import(typeof(EncryptionService).GetMethod("Encrypt", new[] { typeof(byte[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_encryptBytes);
|
||||||
|
_decryptBytes = mod.Import(typeof(EncryptionService).GetMethod("Decrypt", new[] { typeof(int[]), typeof(int), typeof(int), typeof(int), typeof(int) }));
|
||||||
|
Assert.IsNotNull(_decryptBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ModuleDef _module;
|
||||||
|
private IMethod _castIntAsFloat;
|
||||||
|
private IMethod _castLongAsDouble;
|
||||||
|
private IMethod _castFloatAsInt;
|
||||||
|
private IMethod _castDoubleAsLong;
|
||||||
|
private IMethod _initializeArray;
|
||||||
|
|
||||||
|
private IMethod _encryptBlock;
|
||||||
|
private IMethod _decryptBlock;
|
||||||
|
private IMethod _encryptInt;
|
||||||
|
private IMethod _decryptInt;
|
||||||
|
private IMethod _encryptLong;
|
||||||
|
private IMethod _decryptLong;
|
||||||
|
private IMethod _encryptFloat;
|
||||||
|
private IMethod _decryptFloat;
|
||||||
|
private IMethod _encryptDouble;
|
||||||
|
private IMethod _decryptDouble;
|
||||||
|
private IMethod _encryptString;
|
||||||
|
private IMethod _decryptString;
|
||||||
|
private IMethod _encryptBytes;
|
||||||
|
private IMethod _decryptBytes;
|
||||||
|
|
||||||
|
public IMethod CastIntAsFloat => _castIntAsFloat;
|
||||||
|
public IMethod CastLongAsDouble => _castLongAsDouble;
|
||||||
|
public IMethod CastFloatAsInt => _castFloatAsInt;
|
||||||
|
public IMethod CastDoubleAsLong => _castDoubleAsLong;
|
||||||
|
|
||||||
|
public IMethod InitializedArrayMethod => _initializeArray;
|
||||||
|
|
||||||
|
public IMethod EncryptBlock => _encryptBlock;
|
||||||
|
public IMethod DecryptBlock => _decryptBlock;
|
||||||
|
|
||||||
|
public IMethod EncryptInt => _encryptInt;
|
||||||
|
public IMethod DecryptInt => _decryptInt;
|
||||||
|
public IMethod EncryptLong => _encryptLong;
|
||||||
|
public IMethod DecryptLong => _decryptLong;
|
||||||
|
public IMethod EncryptFloat => _encryptFloat;
|
||||||
|
public IMethod DecryptFloat => _decryptFloat;
|
||||||
|
public IMethod EncryptDouble => _encryptDouble;
|
||||||
|
public IMethod DecryptDouble => _decryptDouble;
|
||||||
|
public IMethod EncryptString => _encryptString;
|
||||||
|
public IMethod DecryptString => _decryptString;
|
||||||
|
public IMethod EncryptBytes => _encryptBytes;
|
||||||
|
public IMethod DecryptBytes => _decryptBytes;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,6 @@ using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using UnityEngine.Assertions;
|
|
||||||
|
|
||||||
namespace Obfuz.Emit
|
namespace Obfuz.Emit
|
||||||
{
|
{
|
||||||
|
@ -18,54 +17,6 @@ namespace Obfuz.Emit
|
||||||
public abstract void Init(ModuleDef mod);
|
public abstract void Init(ModuleDef mod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DefaultModuleMetadataImporter : ModuleMetadataImporterBase
|
|
||||||
{
|
|
||||||
public override void Init(ModuleDef mod)
|
|
||||||
{
|
|
||||||
_module = mod;
|
|
||||||
var constUtilityType = typeof(ConstUtility);
|
|
||||||
|
|
||||||
_castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat"));
|
|
||||||
Assert.IsNotNull(_castIntAsFloat, "CastIntAsFloat not found");
|
|
||||||
_castLongAsDouble = mod.Import(constUtilityType.GetMethod("CastLongAsDouble"));
|
|
||||||
Assert.IsNotNull(_castLongAsDouble, "CastLongAsDouble not found");
|
|
||||||
_castFloatAsInt = mod.Import(constUtilityType.GetMethod("CastFloatAsInt"));
|
|
||||||
Assert.IsNotNull(_castFloatAsInt, "CastFloatAsInt not found");
|
|
||||||
_castDoubleAsLong = mod.Import(constUtilityType.GetMethod("CastDoubleAsLong"));
|
|
||||||
Assert.IsNotNull(_castDoubleAsLong, "CastDoubleAsLong not found");
|
|
||||||
|
|
||||||
_initializeArray = mod.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) }));
|
|
||||||
Assert.IsNotNull(_initializeArray);
|
|
||||||
_encryptBlock = mod.Import(typeof(EncryptionService).GetMethod("EncryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) }));
|
|
||||||
Assert.IsNotNull(_encryptBlock);
|
|
||||||
_decryptBlock = mod.Import(typeof(EncryptionService).GetMethod("DecryptBlock", new[] { typeof(byte[]), typeof(long), typeof(int) }));
|
|
||||||
Assert.IsNotNull(_decryptBlock);
|
|
||||||
}
|
|
||||||
|
|
||||||
private ModuleDef _module;
|
|
||||||
private IMethod _castIntAsFloat;
|
|
||||||
private IMethod _castLongAsDouble;
|
|
||||||
private IMethod _castFloatAsInt;
|
|
||||||
private IMethod _castDoubleAsLong;
|
|
||||||
private IMethod _initializeArray;
|
|
||||||
private IMethod _encryptBlock;
|
|
||||||
private IMethod _decryptBlock;
|
|
||||||
|
|
||||||
public IMethod CastIntAsFloat => _castIntAsFloat;
|
|
||||||
|
|
||||||
public IMethod CastLongAsDouble => _castLongAsDouble;
|
|
||||||
|
|
||||||
public IMethod CastFloatAsInt => _castFloatAsInt;
|
|
||||||
|
|
||||||
public IMethod CastDoubleAsLong => _castDoubleAsLong;
|
|
||||||
|
|
||||||
public IMethod InitializedArrayMethod => _initializeArray;
|
|
||||||
|
|
||||||
public IMethod EncryptBlock => _encryptBlock;
|
|
||||||
|
|
||||||
public IMethod DecryptBlock => _decryptBlock;
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MetadataImporter
|
public class MetadataImporter
|
||||||
{
|
{
|
||||||
private readonly Dictionary<(ModuleDef, Type), IModuleMetadataImporter> _customModuleMetadataImporters = new Dictionary<(ModuleDef, Type), IModuleMetadataImporter>();
|
private readonly Dictionary<(ModuleDef, Type), IModuleMetadataImporter> _customModuleMetadataImporters = new Dictionary<(ModuleDef, Type), IModuleMetadataImporter>();
|
||||||
|
@ -98,7 +49,7 @@ namespace Obfuz.Emit
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
importer = (IModuleMetadataImporter)Activator.CreateInstance(typeof(T), module);
|
importer = (IModuleMetadataImporter)Activator.CreateInstance(typeof(T));
|
||||||
}
|
}
|
||||||
importer.Init(module);
|
importer.Init(module);
|
||||||
_customModuleMetadataImporters[key] = importer;
|
_customModuleMetadataImporters[key] = importer;
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace Obfuz.ObfusPasses.ConstObfus
|
||||||
public override void Start(ObfuscationPassContext ctx)
|
public override void Start(ObfuscationPassContext ctx)
|
||||||
{
|
{
|
||||||
_dataObfuscatorPolicy = new RuleBasedObfuscationPolicy();
|
_dataObfuscatorPolicy = new RuleBasedObfuscationPolicy();
|
||||||
_dataObfuscator = new DefaultDataObfuscator();
|
_dataObfuscator = new DefaultConstObfuscator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Stop(ObfuscationPassContext ctx)
|
public override void Stop(ObfuscationPassContext ctx)
|
||||||
|
|
|
@ -0,0 +1,132 @@
|
||||||
|
using dnlib.DotNet;
|
||||||
|
using dnlib.DotNet.Emit;
|
||||||
|
using Obfuz.Emit;
|
||||||
|
using Obfuz.Data;
|
||||||
|
using Obfuz.Utils;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using NUnit.Framework;
|
||||||
|
|
||||||
|
namespace Obfuz.ObfusPasses.ConstObfus
|
||||||
|
{
|
||||||
|
public class DefaultConstObfuscator : IDataObfuscator
|
||||||
|
{
|
||||||
|
private readonly IRandom _random;
|
||||||
|
private readonly RandomDataNodeCreator _nodeCreator;
|
||||||
|
private readonly RvaDataAllocator _rvaDataAllocator;
|
||||||
|
private readonly ConstFieldAllocator _constFieldAllocator;
|
||||||
|
private readonly IEncryptor _encryptor;
|
||||||
|
|
||||||
|
public DefaultConstObfuscator()
|
||||||
|
{
|
||||||
|
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
|
||||||
|
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
|
||||||
|
_nodeCreator = new RandomDataNodeCreator(_random);
|
||||||
|
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor);
|
||||||
|
_constFieldAllocator = new ConstFieldAllocator(_random);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CompileNode(IDataNode node, MethodDef method, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
var ctx = new CompileContext
|
||||||
|
{
|
||||||
|
method = method,
|
||||||
|
output = obfuscatedInstructions,
|
||||||
|
rvaDataAllocator = _rvaDataAllocator,
|
||||||
|
constFieldAllocator = _constFieldAllocator,
|
||||||
|
};
|
||||||
|
node.Compile(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int GenerateEncryptionOperations()
|
||||||
|
{
|
||||||
|
return _random.NextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GenerateSalt()
|
||||||
|
{
|
||||||
|
return _random.NextInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
private DefaultModuleMetadataImporter GetModuleMetadataImporter(MethodDef method)
|
||||||
|
{
|
||||||
|
return MetadataImporter.Instance.GetDefaultModuleMetadataImporter(method.Module);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
int ops = GenerateEncryptionOperations();
|
||||||
|
int salt = GenerateSalt();
|
||||||
|
int encryptedValue = _encryptor.Encrypt(value, ops, salt);
|
||||||
|
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
||||||
|
|
||||||
|
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(encryptedValue));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
int ops = GenerateEncryptionOperations();
|
||||||
|
int salt = GenerateSalt();
|
||||||
|
long encryptedValue = _encryptor.Encrypt(value, ops, salt);
|
||||||
|
|
||||||
|
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, encryptedValue));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptLong));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
int ops = GenerateEncryptionOperations();
|
||||||
|
int salt = GenerateSalt();
|
||||||
|
float encryptedValue = _encryptor.Encrypt(value, ops, salt);
|
||||||
|
|
||||||
|
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, encryptedValue));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptFloat));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
int ops = GenerateEncryptionOperations();
|
||||||
|
int salt = GenerateSalt();
|
||||||
|
double encryptedValue = _encryptor.Encrypt(value, ops, salt);
|
||||||
|
DefaultModuleMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, encryptedValue));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(salt));
|
||||||
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptDouble));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateBytes(MethodDef method, byte[] value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
int ops = GenerateEncryptionOperations();
|
||||||
|
int salt = GenerateSalt();
|
||||||
|
|
||||||
|
Assert.IsTrue(value.Length % 4 == 0);
|
||||||
|
int[] intArr = new int[value.Length / 4];
|
||||||
|
Buffer.BlockCopy(value, 0, intArr, 0, value.Length);
|
||||||
|
byte[] encryptedValue = _encryptor.Decrypt(intArr, 0, value.Length, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions)
|
||||||
|
{
|
||||||
|
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value);
|
||||||
|
CompileNode(node, method, obfuscatedInstructions);
|
||||||
|
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Done()
|
||||||
|
{
|
||||||
|
_rvaDataAllocator.Done();
|
||||||
|
_constFieldAllocator.Done();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,89 +0,0 @@
|
||||||
using dnlib.DotNet;
|
|
||||||
using dnlib.DotNet.Emit;
|
|
||||||
using Obfuz.Emit;
|
|
||||||
using Obfuz.Encryption;
|
|
||||||
using Obfuz.Utils;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace Obfuz.ObfusPasses.ConstObfus
|
|
||||||
{
|
|
||||||
public class DefaultDataObfuscator : IDataObfuscator
|
|
||||||
{
|
|
||||||
private readonly IRandom _random;
|
|
||||||
private readonly RandomDataNodeCreator _nodeCreator;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
private readonly ConstFieldAllocator _constFieldAllocator;
|
|
||||||
private readonly IEncryptor _encryptor;
|
|
||||||
|
|
||||||
public DefaultDataObfuscator()
|
|
||||||
{
|
|
||||||
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
|
|
||||||
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
|
|
||||||
_nodeCreator = new RandomDataNodeCreator(_random);
|
|
||||||
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor);
|
|
||||||
_constFieldAllocator = new ConstFieldAllocator(_random);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CompileNode(IDataNode node, MethodDef method, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
var ctx = new CompileContext
|
|
||||||
{
|
|
||||||
method = method,
|
|
||||||
output = obfuscatedInstructions,
|
|
||||||
rvaDataAllocator = _rvaDataAllocator,
|
|
||||||
constFieldAllocator = _constFieldAllocator,
|
|
||||||
};
|
|
||||||
node.Compile(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int64, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float32, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float64, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateBytes(MethodDef method, Array value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Bytes, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//throw new NotSupportedException();
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value.Length));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions)
|
|
||||||
{
|
|
||||||
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value);
|
|
||||||
CompileNode(node, method, obfuscatedInstructions);
|
|
||||||
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Done()
|
|
||||||
{
|
|
||||||
_rvaDataAllocator.Done();
|
|
||||||
_constFieldAllocator.Done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -17,7 +17,7 @@ namespace Obfuz.ObfusPasses.ConstObfus
|
||||||
|
|
||||||
void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions);
|
void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions);
|
||||||
|
|
||||||
void ObfuscateBytes(MethodDef method, Array value, List<Instruction> obfuscatedInstructions);
|
void ObfuscateBytes(MethodDef method, byte[] value, List<Instruction> obfuscatedInstructions);
|
||||||
|
|
||||||
void Done();
|
void Done();
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Unity.Collections.LowLevel.Unsafe;
|
using Unity.Collections.LowLevel.Unsafe;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
namespace Obfuz
|
namespace Obfuz
|
||||||
{
|
{
|
||||||
|
@ -41,6 +42,14 @@ namespace Obfuz
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int[] GetInts(byte[] data, int offset, int byteLength)
|
||||||
|
{
|
||||||
|
Assert.IsTrue(byteLength % 4 == 0);
|
||||||
|
int[] result = new int[byteLength >> 2];
|
||||||
|
Buffer.BlockCopy(data, offset, result, 0, byteLength);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static void InitializeArray(Array array, byte[] data, int offset, int length)
|
public static void InitializeArray(Array array, byte[] data, int offset, int length)
|
||||||
{
|
{
|
||||||
Buffer.BlockCopy(data, offset, array, 0, length);
|
Buffer.BlockCopy(data, offset, array, 0, length);
|
||||||
|
|
|
@ -30,5 +30,72 @@ namespace Obfuz
|
||||||
data[i] ^= (byte)(_key[i % _key.Length] ^ salt);
|
data[i] ^= (byte)(_key[i % _key.Length] ^ salt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int Encrypt(int value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int Decrypt(int value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long Encrypt(long value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long Decrypt(long value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Encrypt(float value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float Decrypt(float value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double Encrypt(double value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double Decrypt(double value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
|
||||||
|
{
|
||||||
|
var intArr = new int[(length + 3) / 4];
|
||||||
|
Buffer.BlockCopy(value, offset, intArr, 0, length);
|
||||||
|
return intArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt)
|
||||||
|
{
|
||||||
|
byte[] byteArr = new byte[byteLength];
|
||||||
|
Buffer.BlockCopy(value, 0, byteArr, 0, byteLength);
|
||||||
|
return byteArr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int[] Encrypt(string value, int ops, int salt)
|
||||||
|
{
|
||||||
|
byte[] bytes = Encoding.UTF8.GetBytes(value);
|
||||||
|
return Encrypt(bytes, 0, bytes.Length, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt)
|
||||||
|
{
|
||||||
|
byte[] bytes = new byte[stringBytesLength];
|
||||||
|
Buffer.BlockCopy(value, 0, bytes, 0, stringBytesLength);
|
||||||
|
return Encoding.UTF8.GetString(bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,5 +19,96 @@ namespace Obfuz
|
||||||
{
|
{
|
||||||
_encryptor.DecryptBlock(data, ops, salt);
|
_encryptor.DecryptBlock(data, ops, salt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int Encrypt(int value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int Decrypt(int value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Decrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long Encrypt(long value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long Decrypt(long value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Decrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float Encrypt(float value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float Decrypt(float value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Decrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double Encrypt(double value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double Decrypt(double value, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Decrypt(value, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] Encrypt(byte[] value, int offset, int length, int opts, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, offset, length, opts, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Decrypt(value, offset, byteLength, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] Encrypt(string value, int ops, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.Encrypt(value, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt)
|
||||||
|
{
|
||||||
|
return _encryptor.DecryptString(value, offset, stringBytesLength, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static int DecryptFromRvaInt(byte[] data, int offset, int ops, int salt)
|
||||||
|
{
|
||||||
|
int encryptedValue = ConstUtility.GetInt(data, offset);
|
||||||
|
return Decrypt(encryptedValue, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static long DecryptFromRvaLong(byte[] data, int offset, int ops, int salt)
|
||||||
|
{
|
||||||
|
long encryptedValue = ConstUtility.GetLong(data, offset);
|
||||||
|
return Decrypt(encryptedValue, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static float DecryptFromRvaFloat(byte[] data, int offset, int ops, int salt)
|
||||||
|
{
|
||||||
|
int encryptedValue = ConstUtility.GetInt(data, offset);
|
||||||
|
return Decrypt(encryptedValue, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static double DecryptFromRvaDouble(byte[] data, int offset, int ops, int salt)
|
||||||
|
{
|
||||||
|
long encryptedValue = ConstUtility.GetLong(data, offset);
|
||||||
|
return Decrypt(encryptedValue, ops, salt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string DecryptFromRvaString(byte[] data, int offset, int stringBytesLength, int ops, int salt)
|
||||||
|
{
|
||||||
|
int[] encryptedValue = ConstUtility.GetBytes(data, offset, stringBytesLength);
|
||||||
|
return DecryptString(encryptedValue, 0, stringBytesLength, ops, salt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,23 @@ namespace Obfuz
|
||||||
{
|
{
|
||||||
void EncryptBlock(byte[] data, long ops, int salt);
|
void EncryptBlock(byte[] data, long ops, int salt);
|
||||||
void DecryptBlock(byte[] data, long ops, int salt);
|
void DecryptBlock(byte[] data, long ops, int salt);
|
||||||
|
|
||||||
|
int Encrypt(int value, int opts, int salt);
|
||||||
|
int Decrypt(int value, int opts, int salt);
|
||||||
|
|
||||||
|
long Encrypt(long value, int opts, int salt);
|
||||||
|
long Decrypt(long value, int opts, int salt);
|
||||||
|
|
||||||
|
float Encrypt(float value, int opts, int salt);
|
||||||
|
float Decrypt(float value, int opts, int salt);
|
||||||
|
|
||||||
|
double Encrypt(double value, int opts, int salt);
|
||||||
|
double Decrypt(double value, int opts, int salt);
|
||||||
|
|
||||||
|
int[] Encrypt(byte[] value, int offset, int length, int opts, int salt);
|
||||||
|
public byte[] Decrypt(int[] value, int offset, int byteLength, int ops, int salt);
|
||||||
|
|
||||||
|
public int[] Encrypt(string value, int ops, int salt);
|
||||||
|
public string DecryptString(int[] value, int offset, int stringBytesLength, int ops, int salt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue