重构GroupByModuleEntityManager代码
parent
4b0c5d7521
commit
e3d9d7a08e
|
@ -10,13 +10,8 @@ using UnityEngine.Assertions;
|
||||||
|
|
||||||
namespace Obfuz.Data
|
namespace Obfuz.Data
|
||||||
{
|
{
|
||||||
public class ModuleConstFieldAllocator : IGroupByModuleEntity
|
public class ConstFieldAllocator : GroupByModuleEntityBase
|
||||||
{
|
{
|
||||||
private ModuleDef _module;
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
private EncryptionScopeInfo _encryptionScope;
|
|
||||||
private RandomCreator _randomCreator;
|
private RandomCreator _randomCreator;
|
||||||
private IEncryptor _encryptor;
|
private IEncryptor _encryptor;
|
||||||
|
|
||||||
|
@ -57,19 +52,14 @@ namespace Obfuz.Data
|
||||||
private bool _done;
|
private bool _done;
|
||||||
|
|
||||||
|
|
||||||
public ModuleConstFieldAllocator(EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, GroupByModuleEntityManager moduleEntityManager)
|
public ConstFieldAllocator()
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_rvaDataAllocator = rvaDataAllocator;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(ModuleDef mod)
|
public override void Init()
|
||||||
{
|
{
|
||||||
_module = mod;
|
_randomCreator = EncryptionScope.localRandomCreator;
|
||||||
_encryptionScope = _encryptionScopeProvider.GetScope(mod);
|
_encryptor = EncryptionScope.encryptor;
|
||||||
_randomCreator = _encryptionScope.localRandomCreator;
|
|
||||||
_encryptor = _encryptionScope.encryptor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const int maxFieldCount = 1000;
|
const int maxFieldCount = 1000;
|
||||||
|
@ -77,30 +67,32 @@ namespace Obfuz.Data
|
||||||
|
|
||||||
private TypeSig GetTypeSigOfValue(object value)
|
private TypeSig GetTypeSigOfValue(object value)
|
||||||
{
|
{
|
||||||
|
ModuleDef mod = Module;
|
||||||
if (value is int)
|
if (value is int)
|
||||||
return _module.CorLibTypes.Int32;
|
return mod.CorLibTypes.Int32;
|
||||||
if (value is long)
|
if (value is long)
|
||||||
return _module.CorLibTypes.Int64;
|
return mod.CorLibTypes.Int64;
|
||||||
if (value is float)
|
if (value is float)
|
||||||
return _module.CorLibTypes.Single;
|
return mod.CorLibTypes.Single;
|
||||||
if (value is double)
|
if (value is double)
|
||||||
return _module.CorLibTypes.Double;
|
return mod.CorLibTypes.Double;
|
||||||
if (value is string)
|
if (value is string)
|
||||||
return _module.CorLibTypes.String;
|
return mod.CorLibTypes.String;
|
||||||
if (value is byte[])
|
if (value is byte[])
|
||||||
return new SZArraySig(_module.CorLibTypes.Byte);
|
return new SZArraySig(mod.CorLibTypes.Byte);
|
||||||
throw new NotSupportedException($"Unsupported type: {value.GetType()}");
|
throw new NotSupportedException($"Unsupported type: {value.GetType()}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private ConstFieldInfo CreateConstFieldInfo(object value)
|
private ConstFieldInfo CreateConstFieldInfo(object value)
|
||||||
{
|
{
|
||||||
|
ModuleDef mod = Module;
|
||||||
if (_holderTypeDef == null || _holderTypeDef.Fields.Count >= maxFieldCount)
|
if (_holderTypeDef == null || _holderTypeDef.Fields.Count >= maxFieldCount)
|
||||||
{
|
{
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
using (var scope = new DisableTypeDefFindCacheScope(mod))
|
||||||
{
|
{
|
||||||
ITypeDefOrRef objectTypeRef = _module.Import(typeof(object));
|
ITypeDefOrRef objectTypeRef = mod.Import(typeof(object));
|
||||||
_holderTypeDef = new TypeDefUser($"{ConstValues.ObfuzInternalSymbolNamePrefix}ConstFieldHolder${_holderTypeDefs.Count}", objectTypeRef);
|
_holderTypeDef = new TypeDefUser($"{ConstValues.ObfuzInternalSymbolNamePrefix}ConstFieldHolder${_holderTypeDefs.Count}", objectTypeRef);
|
||||||
_module.Types.Add(_holderTypeDef);
|
mod.Types.Add(_holderTypeDef);
|
||||||
_holderTypeDefs.Add(_holderTypeDef);
|
_holderTypeDefs.Add(_holderTypeDef);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -159,28 +151,22 @@ namespace Obfuz.Data
|
||||||
return AllocateAny(value);
|
return AllocateAny(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultMetadataImporter GetModuleMetadataImporter()
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetDefaultModuleMetadataImporter(_module, _encryptionScopeProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void CreateCCtorOfRvaTypeDef(TypeDef type)
|
private void CreateCCtorOfRvaTypeDef(TypeDef type)
|
||||||
{
|
{
|
||||||
|
ModuleDef mod = Module;
|
||||||
var cctor = new MethodDefUser(".cctor",
|
var cctor = new MethodDefUser(".cctor",
|
||||||
MethodSig.CreateStatic(_module.CorLibTypes.Void),
|
MethodSig.CreateStatic(mod.CorLibTypes.Void),
|
||||||
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
||||||
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
||||||
cctor.DeclaringType = type;
|
cctor.DeclaringType = type;
|
||||||
//_rvaTypeDef.Methods.Add(cctor);
|
|
||||||
var body = new CilBody();
|
var body = new CilBody();
|
||||||
cctor.Body = body;
|
cctor.Body = body;
|
||||||
var ins = body.Instructions;
|
var ins = body.Instructions;
|
||||||
|
|
||||||
//IMethod method = _module.Import(typeof(System.Runtime.CompilerServices.RuntimeHelpers).GetMethod("InitializeArray", new[] { typeof(Array), typeof(RuntimeFieldHandle) }));
|
|
||||||
//Assert.IsNotNull(method);
|
|
||||||
|
|
||||||
|
DefaultMetadataImporter importer = this.GetDefaultModuleMetadataImporter();
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter();
|
RvaDataAllocator rvaDataAllocator = GetEntity<RvaDataAllocator>();
|
||||||
// TODO. obfuscate init codes
|
// TODO. obfuscate init codes
|
||||||
foreach (var field in type.Fields)
|
foreach (var field in type.Fields)
|
||||||
{
|
{
|
||||||
|
@ -193,7 +179,7 @@ namespace Obfuz.Data
|
||||||
case int i:
|
case int i:
|
||||||
{
|
{
|
||||||
int encryptedValue = _encryptor.Encrypt(i, ops, salt);
|
int encryptedValue = _encryptor.Encrypt(i, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(ops));
|
ins.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -204,7 +190,7 @@ namespace Obfuz.Data
|
||||||
case long l:
|
case long l:
|
||||||
{
|
{
|
||||||
long encryptedValue = _encryptor.Encrypt(l, ops, salt);
|
long encryptedValue = _encryptor.Encrypt(l, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(ops));
|
ins.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -215,7 +201,7 @@ namespace Obfuz.Data
|
||||||
case float f:
|
case float f:
|
||||||
{
|
{
|
||||||
float encryptedValue = _encryptor.Encrypt(f, ops, salt);
|
float encryptedValue = _encryptor.Encrypt(f, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(ops));
|
ins.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -226,7 +212,7 @@ namespace Obfuz.Data
|
||||||
case double d:
|
case double d:
|
||||||
{
|
{
|
||||||
double encryptedValue = _encryptor.Encrypt(d, ops, salt);
|
double encryptedValue = _encryptor.Encrypt(d, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(ops));
|
ins.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -237,7 +223,7 @@ namespace Obfuz.Data
|
||||||
case string s:
|
case string s:
|
||||||
{
|
{
|
||||||
byte[] encryptedValue = _encryptor.Encrypt(s, ops, salt);
|
byte[] encryptedValue = _encryptor.Encrypt(s, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
Assert.AreEqual(encryptedValue.Length, rvaData.size);
|
Assert.AreEqual(encryptedValue.Length, rvaData.size);
|
||||||
|
@ -251,7 +237,7 @@ namespace Obfuz.Data
|
||||||
{
|
{
|
||||||
byte[] encryptedValue = _encryptor.Encrypt(bs, 0, bs.Length, ops, salt);
|
byte[] encryptedValue = _encryptor.Encrypt(bs, 0, bs.Length, ops, salt);
|
||||||
Assert.AreEqual(encryptedValue.Length, bs.Length);
|
Assert.AreEqual(encryptedValue.Length, bs.Length);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(bs.Length));
|
ins.Add(Instruction.CreateLdcI4(bs.Length));
|
||||||
|
@ -267,7 +253,7 @@ namespace Obfuz.Data
|
||||||
ins.Add(Instruction.Create(OpCodes.Ret));
|
ins.Add(Instruction.Create(OpCodes.Ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Done()
|
public override void Done()
|
||||||
{
|
{
|
||||||
if (_done)
|
if (_done)
|
||||||
{
|
{
|
||||||
|
@ -280,61 +266,4 @@ namespace Obfuz.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ConstFieldAllocator
|
|
||||||
{
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
|
|
||||||
public ConstFieldAllocator(EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, GroupByModuleEntityManager moduleEntityManager)
|
|
||||||
{
|
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_rvaDataAllocator = rvaDataAllocator;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModuleConstFieldAllocator GetModuleAllocator(ModuleDef mod)
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetEntity<ModuleConstFieldAllocator>(mod, () => new ModuleConstFieldAllocator(_encryptionScopeProvider, _rvaDataAllocator, _moduleEntityManager));
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, int value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, long value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, float value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, double value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, byte[] value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public FieldDef Allocate(ModuleDef mod, string value)
|
|
||||||
{
|
|
||||||
return GetModuleAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Done()
|
|
||||||
{
|
|
||||||
foreach (var moduleAllocator in _moduleEntityManager.GetEntities<ModuleConstFieldAllocator>())
|
|
||||||
{
|
|
||||||
moduleAllocator.Done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ using Obfuz.Emit;
|
||||||
using Obfuz.Utils;
|
using Obfuz.Utils;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
|
@ -23,16 +24,11 @@ namespace Obfuz.Data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ModuleRvaDataAllocator : GroupByModuleEntityBase
|
public class RvaDataAllocator : GroupByModuleEntityBase
|
||||||
{
|
{
|
||||||
// randomized
|
// randomized
|
||||||
const int maxRvaDataSize = 0x1000;
|
const int maxRvaDataSize = 0x1000;
|
||||||
|
|
||||||
private ModuleDef _module;
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
|
|
||||||
private EncryptionScopeInfo _encryptionScope;
|
|
||||||
private IRandom _random;
|
private IRandom _random;
|
||||||
|
|
||||||
class RvaField
|
class RvaField
|
||||||
|
@ -71,28 +67,23 @@ namespace Obfuz.Data
|
||||||
private readonly Dictionary<int, TypeDef> _dataHolderTypeBySizes = new Dictionary<int, TypeDef>();
|
private readonly Dictionary<int, TypeDef> _dataHolderTypeBySizes = new Dictionary<int, TypeDef>();
|
||||||
private bool _done;
|
private bool _done;
|
||||||
|
|
||||||
public ModuleRvaDataAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager)
|
public RvaDataAllocator()
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Init(ModuleDef mod)
|
public override void Init()
|
||||||
{
|
{
|
||||||
_module = mod;
|
_random = EncryptionScope.localRandomCreator(HashUtil.ComputeHash(Module.Name));
|
||||||
_encryptionScope = _encryptionScopeProvider.GetScope(mod);
|
|
||||||
_random = _encryptionScope.localRandomCreator(HashUtil.ComputeHash(mod.Name));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private (FieldDef, FieldDef) CreateDataHolderRvaField(TypeDef dataHolderType)
|
private (FieldDef, FieldDef) CreateDataHolderRvaField(TypeDef dataHolderType)
|
||||||
{
|
{
|
||||||
if (_rvaTypeDef == null)
|
if (_rvaTypeDef == null)
|
||||||
{
|
{
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
using (var scope = new DisableTypeDefFindCacheScope(Module))
|
||||||
{
|
{
|
||||||
ITypeDefOrRef objectTypeRef = _module.Import(typeof(object));
|
_rvaTypeDef = new TypeDefUser("$Obfuz$RVA$", Module.CorLibTypes.Object.ToTypeDefOrRef());
|
||||||
_rvaTypeDef = new TypeDefUser("$Obfuz$RVA$", objectTypeRef);
|
Module.Types.Add(_rvaTypeDef);
|
||||||
_module.Types.Add(_rvaTypeDef);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +91,7 @@ namespace Obfuz.Data
|
||||||
var holderField = new FieldDefUser($"$RVA_Data{_rvaFields.Count}", new FieldSig(dataHolderType.ToTypeSig()), FieldAttributes.InitOnly | FieldAttributes.Static | FieldAttributes.HasFieldRVA);
|
var holderField = new FieldDefUser($"$RVA_Data{_rvaFields.Count}", new FieldSig(dataHolderType.ToTypeSig()), FieldAttributes.InitOnly | FieldAttributes.Static | FieldAttributes.HasFieldRVA);
|
||||||
holderField.DeclaringType = _rvaTypeDef;
|
holderField.DeclaringType = _rvaTypeDef;
|
||||||
|
|
||||||
var runtimeValueField = new FieldDefUser($"$RVA_Value{_rvaFields.Count}", new FieldSig(new SZArraySig(_module.CorLibTypes.Byte)), FieldAttributes.Static | FieldAttributes.Public);
|
var runtimeValueField = new FieldDefUser($"$RVA_Value{_rvaFields.Count}", new FieldSig(new SZArraySig(Module.CorLibTypes.Byte)), FieldAttributes.Static | FieldAttributes.Public);
|
||||||
runtimeValueField.DeclaringType = _rvaTypeDef;
|
runtimeValueField.DeclaringType = _rvaTypeDef;
|
||||||
return (holderField, runtimeValueField);
|
return (holderField, runtimeValueField);
|
||||||
}
|
}
|
||||||
|
@ -111,15 +102,15 @@ namespace Obfuz.Data
|
||||||
if (_dataHolderTypeBySizes.TryGetValue(size, out var type))
|
if (_dataHolderTypeBySizes.TryGetValue(size, out var type))
|
||||||
return type;
|
return type;
|
||||||
|
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
using (var scope = new DisableTypeDefFindCacheScope(Module))
|
||||||
{
|
{
|
||||||
var dataHolderType = new TypeDefUser($"$ObfuzRVA$DataHolder{size}", _module.Import(typeof(ValueType)));
|
var dataHolderType = new TypeDefUser($"$ObfuzRVA$DataHolder{size}", Module.Import(typeof(ValueType)));
|
||||||
dataHolderType.Attributes = TypeAttributes.Public | TypeAttributes.Sealed;
|
dataHolderType.Attributes = TypeAttributes.Public | TypeAttributes.Sealed;
|
||||||
dataHolderType.Layout = TypeAttributes.ExplicitLayout;
|
dataHolderType.Layout = TypeAttributes.ExplicitLayout;
|
||||||
dataHolderType.PackingSize = 1;
|
dataHolderType.PackingSize = 1;
|
||||||
dataHolderType.ClassSize = (uint)size;
|
dataHolderType.ClassSize = (uint)size;
|
||||||
_dataHolderTypeBySizes.Add(size, dataHolderType);
|
_dataHolderTypeBySizes.Add(size, dataHolderType);
|
||||||
_module.Types.Add(dataHolderType);
|
Module.Types.Add(dataHolderType);
|
||||||
return dataHolderType;
|
return dataHolderType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -230,10 +221,11 @@ namespace Obfuz.Data
|
||||||
private void AddVerifyCodes(IList<Instruction> insts, DefaultMetadataImporter importer)
|
private void AddVerifyCodes(IList<Instruction> insts, DefaultMetadataImporter importer)
|
||||||
{
|
{
|
||||||
int verifyIntValue = 0x12345678;
|
int verifyIntValue = 0x12345678;
|
||||||
IRandom verifyRandom = _encryptionScope.localRandomCreator(verifyIntValue);
|
EncryptionScopeInfo encryptionScope = this.EncryptionScope;
|
||||||
int verifyOps = EncryptionUtil.GenerateEncryptionOpCodes(verifyRandom, _encryptionScope.encryptor, 4);
|
IRandom verifyRandom = encryptionScope.localRandomCreator(verifyIntValue);
|
||||||
|
int verifyOps = EncryptionUtil.GenerateEncryptionOpCodes(verifyRandom, encryptionScope.encryptor, 4);
|
||||||
int verifySalt = verifyRandom.NextInt();
|
int verifySalt = verifyRandom.NextInt();
|
||||||
int encryptedVerifyIntValue = _encryptionScope.encryptor.Encrypt(verifyIntValue, verifyOps, verifySalt);
|
int encryptedVerifyIntValue = encryptionScope.encryptor.Encrypt(verifyIntValue, verifyOps, verifySalt);
|
||||||
|
|
||||||
insts.Add(Instruction.Create(OpCodes.Ldc_I4, verifyIntValue));
|
insts.Add(Instruction.Create(OpCodes.Ldc_I4, verifyIntValue));
|
||||||
insts.Add(Instruction.CreateLdcI4(encryptedVerifyIntValue));
|
insts.Add(Instruction.CreateLdcI4(encryptedVerifyIntValue));
|
||||||
|
@ -252,7 +244,7 @@ namespace Obfuz.Data
|
||||||
}
|
}
|
||||||
ModuleDef mod = _rvaTypeDef.Module;
|
ModuleDef mod = _rvaTypeDef.Module;
|
||||||
var cctorMethod = new MethodDefUser(".cctor",
|
var cctorMethod = new MethodDefUser(".cctor",
|
||||||
MethodSig.CreateStatic(_module.CorLibTypes.Void),
|
MethodSig.CreateStatic(Module.CorLibTypes.Void),
|
||||||
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
||||||
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
||||||
cctorMethod.DeclaringType = _rvaTypeDef;
|
cctorMethod.DeclaringType = _rvaTypeDef;
|
||||||
|
@ -261,7 +253,7 @@ namespace Obfuz.Data
|
||||||
cctorMethod.Body = body;
|
cctorMethod.Body = body;
|
||||||
var ins = body.Instructions;
|
var ins = body.Instructions;
|
||||||
|
|
||||||
DefaultMetadataImporter importer = _moduleEntityManager.GetDefaultModuleMetadataImporter(mod, _encryptionScopeProvider);
|
DefaultMetadataImporter importer = this.GetDefaultModuleMetadataImporter();
|
||||||
AddVerifyCodes(ins, importer);
|
AddVerifyCodes(ins, importer);
|
||||||
foreach (var field in _rvaFields)
|
foreach (var field in _rvaFields)
|
||||||
{
|
{
|
||||||
|
@ -298,7 +290,7 @@ namespace Obfuz.Data
|
||||||
field.FillPaddingToEnd();
|
field.FillPaddingToEnd();
|
||||||
}
|
}
|
||||||
byte[] data = field.bytes.ToArray();
|
byte[] data = field.bytes.ToArray();
|
||||||
_encryptionScope.encryptor.EncryptBlock(data, field.encryptionOps, field.salt);
|
EncryptionScope.encryptor.EncryptBlock(data, field.encryptionOps, field.salt);
|
||||||
field.holderDataField.InitialValue = data;
|
field.holderDataField.InitialValue = data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -314,59 +306,4 @@ namespace Obfuz.Data
|
||||||
CreateCCtorOfRvaTypeDef();
|
CreateCCtorOfRvaTypeDef();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RvaDataAllocator
|
|
||||||
{
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
|
|
||||||
public RvaDataAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager)
|
|
||||||
{
|
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ModuleRvaDataAllocator GetModuleRvaDataAllocator(ModuleDef mod)
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetEntity<ModuleRvaDataAllocator>(mod, () => new ModuleRvaDataAllocator(_encryptionScopeProvider, _moduleEntityManager));
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, int value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, long value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, float value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, double value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, string value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public RvaData Allocate(ModuleDef mod, byte[] value)
|
|
||||||
{
|
|
||||||
return GetModuleRvaDataAllocator(mod).Allocate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Done()
|
|
||||||
{
|
|
||||||
foreach (var allocator in _moduleEntityManager.GetEntities<ModuleRvaDataAllocator>())
|
|
||||||
{
|
|
||||||
allocator.Done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,23 +111,20 @@ namespace Obfuz.Emit
|
||||||
|
|
||||||
public class DefaultMetadataImporter : GroupByModuleEntityBase
|
public class DefaultMetadataImporter : GroupByModuleEntityBase
|
||||||
{
|
{
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private EncryptionScopeInfo _encryptionScope;
|
|
||||||
private EncryptionServiceMetadataImporter _defaultEncryptionServiceMetadataImporter;
|
private EncryptionServiceMetadataImporter _defaultEncryptionServiceMetadataImporter;
|
||||||
|
|
||||||
|
|
||||||
private EncryptionServiceMetadataImporter _staticDefaultEncryptionServiceMetadataImporter;
|
private EncryptionServiceMetadataImporter _staticDefaultEncryptionServiceMetadataImporter;
|
||||||
private EncryptionServiceMetadataImporter _dynamicDefaultEncryptionServiceMetadataImporter;
|
private EncryptionServiceMetadataImporter _dynamicDefaultEncryptionServiceMetadataImporter;
|
||||||
|
|
||||||
public DefaultMetadataImporter(EncryptionScopeProvider encryptionScopeProvider)
|
public DefaultMetadataImporter()
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Init(ModuleDef mod)
|
public override void Init()
|
||||||
{
|
{
|
||||||
_module = mod;
|
ModuleDef mod = Module;
|
||||||
_encryptionScope = _encryptionScopeProvider.GetScope(mod);
|
|
||||||
var constUtilityType = typeof(ConstUtility);
|
var constUtilityType = typeof(ConstUtility);
|
||||||
|
|
||||||
_castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat"));
|
_castIntAsFloat = mod.Import(constUtilityType.GetMethod("CastIntAsFloat"));
|
||||||
|
@ -238,7 +235,7 @@ namespace Obfuz.Emit
|
||||||
|
|
||||||
_staticDefaultEncryptionServiceMetadataImporter = new EncryptionServiceMetadataImporter(mod, typeof(EncryptionService<DefaultStaticEncryptionScope>));
|
_staticDefaultEncryptionServiceMetadataImporter = new EncryptionServiceMetadataImporter(mod, typeof(EncryptionService<DefaultStaticEncryptionScope>));
|
||||||
_dynamicDefaultEncryptionServiceMetadataImporter = new EncryptionServiceMetadataImporter(mod, typeof(EncryptionService<DefaultDynamicEncryptionScope>));
|
_dynamicDefaultEncryptionServiceMetadataImporter = new EncryptionServiceMetadataImporter(mod, typeof(EncryptionService<DefaultDynamicEncryptionScope>));
|
||||||
if (_encryptionScopeProvider.IsDynamicSecretAssembly(mod))
|
if (EncryptionScopeProvider.IsDynamicSecretAssembly(mod))
|
||||||
{
|
{
|
||||||
_defaultEncryptionServiceMetadataImporter = _dynamicDefaultEncryptionServiceMetadataImporter;
|
_defaultEncryptionServiceMetadataImporter = _dynamicDefaultEncryptionServiceMetadataImporter;
|
||||||
}
|
}
|
||||||
|
@ -255,7 +252,7 @@ namespace Obfuz.Emit
|
||||||
|
|
||||||
public EncryptionServiceMetadataImporter GetEncryptionServiceMetadataImporterOfModule(ModuleDef mod)
|
public EncryptionServiceMetadataImporter GetEncryptionServiceMetadataImporterOfModule(ModuleDef mod)
|
||||||
{
|
{
|
||||||
return _encryptionScopeProvider.IsDynamicSecretAssembly(mod) ? _dynamicDefaultEncryptionServiceMetadataImporter : _staticDefaultEncryptionServiceMetadataImporter;
|
return EncryptionScopeProvider.IsDynamicSecretAssembly(mod) ? _dynamicDefaultEncryptionServiceMetadataImporter : _staticDefaultEncryptionServiceMetadataImporter;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ModuleDef _module;
|
private ModuleDef _module;
|
||||||
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
namespace Obfuz.Emit
|
||||||
|
{
|
||||||
|
public static class EntityExtensions
|
||||||
|
{
|
||||||
|
public static T GetEntity<T>(this IGroupByModuleEntity entity) where T : IGroupByModuleEntity, new()
|
||||||
|
{
|
||||||
|
return entity.Manager.GetEntity<T>(entity.Module);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DefaultMetadataImporter GetDefaultModuleMetadataImporter(this IGroupByModuleEntity entity)
|
||||||
|
{
|
||||||
|
return entity.GetEntity<DefaultMetadataImporter>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 6e9557733f180764692756653eb60f88
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -6,14 +6,35 @@ namespace Obfuz.Emit
|
||||||
{
|
{
|
||||||
public interface IGroupByModuleEntity
|
public interface IGroupByModuleEntity
|
||||||
{
|
{
|
||||||
void Init(ModuleDef mod);
|
GroupByModuleEntityManager Manager { get; set; }
|
||||||
|
|
||||||
|
ModuleDef Module { get; set; }
|
||||||
|
|
||||||
|
public EncryptionScopeProvider EncryptionScopeProvider { get; }
|
||||||
|
|
||||||
|
EncryptionScopeInfo EncryptionScope { get; set; }
|
||||||
|
|
||||||
|
void Init();
|
||||||
|
|
||||||
void Done();
|
void Done();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class GroupByModuleEntityBase : IGroupByModuleEntity
|
public abstract class GroupByModuleEntityBase : IGroupByModuleEntity
|
||||||
{
|
{
|
||||||
public abstract void Init(ModuleDef mod);
|
public GroupByModuleEntityManager Manager { get; set; }
|
||||||
|
|
||||||
|
public ModuleDef Module { get; set; }
|
||||||
|
|
||||||
|
public EncryptionScopeInfo EncryptionScope { get; set; }
|
||||||
|
|
||||||
|
public EncryptionScopeProvider EncryptionScopeProvider => Manager.EncryptionScopeProvider;
|
||||||
|
|
||||||
|
public T GetEntity<T>() where T : IGroupByModuleEntity, new()
|
||||||
|
{
|
||||||
|
return Manager.GetEntity<T>(Module);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void Init();
|
||||||
|
|
||||||
public abstract void Done();
|
public abstract void Done();
|
||||||
}
|
}
|
||||||
|
@ -22,7 +43,9 @@ namespace Obfuz.Emit
|
||||||
{
|
{
|
||||||
private readonly Dictionary<(ModuleDef, Type), IGroupByModuleEntity> _moduleEntityManagers = new Dictionary<(ModuleDef, Type), IGroupByModuleEntity>();
|
private readonly Dictionary<(ModuleDef, Type), IGroupByModuleEntity> _moduleEntityManagers = new Dictionary<(ModuleDef, Type), IGroupByModuleEntity>();
|
||||||
|
|
||||||
public T GetEntity<T>(ModuleDef mod, Func<T> creator = null) where T : IGroupByModuleEntity
|
public EncryptionScopeProvider EncryptionScopeProvider { get; set; }
|
||||||
|
|
||||||
|
public T GetEntity<T>(ModuleDef mod) where T : IGroupByModuleEntity, new()
|
||||||
{
|
{
|
||||||
var key = (mod, typeof(T));
|
var key = (mod, typeof(T));
|
||||||
if (_moduleEntityManagers.TryGetValue(key, out var emitManager))
|
if (_moduleEntityManagers.TryGetValue(key, out var emitManager))
|
||||||
|
@ -31,22 +54,17 @@ namespace Obfuz.Emit
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
T newEmitManager;
|
T newEmitManager = new T();
|
||||||
if (creator != null)
|
newEmitManager.Manager = this;
|
||||||
{
|
newEmitManager.Module = mod;
|
||||||
newEmitManager = creator();
|
newEmitManager.EncryptionScope = EncryptionScopeProvider.GetScope(mod);
|
||||||
}
|
newEmitManager.Init();
|
||||||
else
|
|
||||||
{
|
|
||||||
newEmitManager = (T)Activator.CreateInstance(typeof(T));
|
|
||||||
}
|
|
||||||
newEmitManager.Init(mod);
|
|
||||||
_moduleEntityManagers[key] = newEmitManager;
|
_moduleEntityManagers[key] = newEmitManager;
|
||||||
return newEmitManager;
|
return newEmitManager;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<T> GetEntities<T>() where T : IGroupByModuleEntity
|
public List<T> GetEntities<T>() where T : IGroupByModuleEntity, new()
|
||||||
{
|
{
|
||||||
var managers = new List<T>();
|
var managers = new List<T>();
|
||||||
foreach (var kv in _moduleEntityManagers)
|
foreach (var kv in _moduleEntityManagers)
|
||||||
|
@ -59,19 +77,13 @@ namespace Obfuz.Emit
|
||||||
return managers;
|
return managers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Done<T>() where T : IGroupByModuleEntity
|
public void Done<T>() where T : IGroupByModuleEntity, new()
|
||||||
{
|
{
|
||||||
var managers = GetEntities<T>();
|
var managers = GetEntities<T>();
|
||||||
foreach (var manager in managers)
|
foreach (var manager in managers)
|
||||||
{
|
{
|
||||||
manager.Done();
|
manager.Done();
|
||||||
}
|
}
|
||||||
_moduleEntityManagers.Remove((default(ModuleDef), typeof(T)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public DefaultMetadataImporter GetDefaultModuleMetadataImporter(ModuleDef module, EncryptionScopeProvider encryptionScopeProvider)
|
|
||||||
{
|
|
||||||
return GetEntity<DefaultMetadataImporter>(module, () => new DefaultMetadataImporter(encryptionScopeProvider));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,8 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
public class CallObfusPass : ObfuscationMethodPassBase
|
public class CallObfusPass : ObfuscationMethodPassBase
|
||||||
{
|
{
|
||||||
|
public static CallObfuscationSettingsFacade CurrentSettings { get; private set; }
|
||||||
|
|
||||||
private readonly CallObfuscationSettingsFacade _settings;
|
private readonly CallObfuscationSettingsFacade _settings;
|
||||||
private readonly SpecialWhiteListMethodCalculator _specialWhiteListMethodCache;
|
private readonly SpecialWhiteListMethodCalculator _specialWhiteListMethodCache;
|
||||||
|
|
||||||
|
@ -28,6 +30,8 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
public CallObfusPass(CallObfuscationSettingsFacade settings)
|
public CallObfusPass(CallObfuscationSettingsFacade settings)
|
||||||
{
|
{
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
|
CurrentSettings = settings;
|
||||||
|
|
||||||
_specialWhiteListMethodCache = new SpecialWhiteListMethodCalculator(settings.obfuscateCallToMethodInMscorlib);
|
_specialWhiteListMethodCache = new SpecialWhiteListMethodCalculator(settings.obfuscateCallToMethodInMscorlib);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,9 +52,9 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
switch (mode)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case ProxyMode.Dispatch:
|
case ProxyMode.Dispatch:
|
||||||
return new DispatchProxyObfuscator(ctx.encryptionScopeProvider, ctx.constFieldAllocator, ctx.moduleEntityManager, _settings);
|
return new DispatchProxyObfuscator(ctx.moduleEntityManager);
|
||||||
case ProxyMode.Delegate:
|
case ProxyMode.Delegate:
|
||||||
return new DelegateProxyObfuscator(ctx.encryptionScopeProvider, ctx.moduleEntityManager, ctx.rvaDataAllocator, _settings);
|
return new DelegateProxyObfuscator(ctx.moduleEntityManager);
|
||||||
default:
|
default:
|
||||||
throw new System.NotSupportedException($"Unsupported proxy mode: {mode}");
|
throw new System.NotSupportedException($"Unsupported proxy mode: {mode}");
|
||||||
}
|
}
|
||||||
|
@ -66,7 +70,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
var totalFinalInstructions = new List<Instruction>();
|
var totalFinalInstructions = new List<Instruction>();
|
||||||
|
|
||||||
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
||||||
var encryptionScope = ctx.encryptionScopeProvider.GetScope(method.Module);
|
var encryptionScope = ctx.moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
||||||
var omc = new ObfusMethodContext
|
var omc = new ObfusMethodContext
|
||||||
{
|
{
|
||||||
|
|
|
@ -24,17 +24,11 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ModuleDelegateProxyAllocator : IGroupByModuleEntity
|
class DelegateProxyAllocator : GroupByModuleEntityBase
|
||||||
{
|
{
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
private readonly CallObfuscationSettingsFacade _settings;
|
|
||||||
private readonly CachedDictionary<MethodSig, TypeDef> _delegateTypes;
|
private readonly CachedDictionary<MethodSig, TypeDef> _delegateTypes;
|
||||||
private readonly HashSet<string> _allocatedDelegateNames = new HashSet<string>();
|
private readonly HashSet<string> _allocatedDelegateNames = new HashSet<string>();
|
||||||
|
|
||||||
private ModuleDef _module;
|
|
||||||
|
|
||||||
private TypeDef _delegateInstanceHolderType;
|
private TypeDef _delegateInstanceHolderType;
|
||||||
private bool _done;
|
private bool _done;
|
||||||
|
|
||||||
|
@ -53,19 +47,13 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
}
|
}
|
||||||
private readonly Dictionary<MethodKey, CallInfo> _callMethods = new Dictionary<MethodKey, CallInfo>();
|
private readonly Dictionary<MethodKey, CallInfo> _callMethods = new Dictionary<MethodKey, CallInfo>();
|
||||||
|
|
||||||
public ModuleDelegateProxyAllocator(GroupByModuleEntityManager moduleEntityManager, EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, CallObfuscationSettingsFacade settings)
|
public DelegateProxyAllocator()
|
||||||
{
|
{
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_rvaDataAllocator = rvaDataAllocator;
|
|
||||||
_settings = settings;
|
|
||||||
_delegateTypes = new CachedDictionary<MethodSig, TypeDef>(SignatureEqualityComparer.Instance, CreateDelegateForSignature);
|
_delegateTypes = new CachedDictionary<MethodSig, TypeDef>(SignatureEqualityComparer.Instance, CreateDelegateForSignature);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(ModuleDef mod)
|
public override void Init()
|
||||||
{
|
{
|
||||||
_module = mod;
|
|
||||||
|
|
||||||
_delegateInstanceHolderType = CreateDelegateInstanceHolderTypeDef();
|
_delegateInstanceHolderType = CreateDelegateInstanceHolderTypeDef();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,19 +77,20 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
private TypeDef CreateDelegateForSignature(MethodSig delegateInvokeSig)
|
private TypeDef CreateDelegateForSignature(MethodSig delegateInvokeSig)
|
||||||
{
|
{
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
ModuleDef mod = Module;
|
||||||
|
using (var scope = new DisableTypeDefFindCacheScope(mod))
|
||||||
{
|
{
|
||||||
|
|
||||||
string typeName = AllocateDelegateTypeName(delegateInvokeSig);
|
string typeName = AllocateDelegateTypeName(delegateInvokeSig);
|
||||||
_module.Import(typeof(MulticastDelegate));
|
mod.Import(typeof(MulticastDelegate));
|
||||||
|
|
||||||
TypeDef delegateType = new TypeDefUser("", typeName, _module.CorLibTypes.GetTypeRef("System", "MulticastDelegate"));
|
TypeDef delegateType = new TypeDefUser("", typeName, mod.CorLibTypes.GetTypeRef("System", "MulticastDelegate"));
|
||||||
delegateType.Attributes = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public;
|
delegateType.Attributes = TypeAttributes.Class | TypeAttributes.Sealed | TypeAttributes.Public;
|
||||||
_module.Types.Add(delegateType);
|
mod.Types.Add(delegateType);
|
||||||
|
|
||||||
MethodDef ctor = new MethodDefUser(
|
MethodDef ctor = new MethodDefUser(
|
||||||
".ctor",
|
".ctor",
|
||||||
MethodSig.CreateInstance(_module.CorLibTypes.Void, _module.CorLibTypes.Object, _module.CorLibTypes.IntPtr),
|
MethodSig.CreateInstance(mod.CorLibTypes.Void, mod.CorLibTypes.Object, mod.CorLibTypes.IntPtr),
|
||||||
MethodImplAttributes.Runtime,
|
MethodImplAttributes.Runtime,
|
||||||
MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public
|
MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.HideBySig | MethodAttributes.Public
|
||||||
);
|
);
|
||||||
|
@ -121,12 +110,13 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
private TypeDef CreateDelegateInstanceHolderTypeDef()
|
private TypeDef CreateDelegateInstanceHolderTypeDef()
|
||||||
{
|
{
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
ModuleDef mod = Module;
|
||||||
|
using (var scope = new DisableTypeDefFindCacheScope(mod))
|
||||||
{
|
{
|
||||||
string typeName = "$Obfuz$DelegateInstanceHolder";
|
string typeName = "$Obfuz$DelegateInstanceHolder";
|
||||||
TypeDef holderType = new TypeDefUser("", typeName, _module.CorLibTypes.Object.ToTypeDefOrRef());
|
TypeDef holderType = new TypeDefUser("", typeName, mod.CorLibTypes.Object.ToTypeDefOrRef());
|
||||||
holderType.Attributes = TypeAttributes.Class | TypeAttributes.Public;
|
holderType.Attributes = TypeAttributes.Class | TypeAttributes.Public;
|
||||||
_module.Types.Add(holderType);
|
mod.Types.Add(holderType);
|
||||||
return holderType;
|
return holderType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,7 +182,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
return new DelegateProxyMethodData(callInfo.delegateInstanceField, callInfo.delegateInvokeMethod);
|
return new DelegateProxyMethodData(callInfo.delegateInstanceField, callInfo.delegateInvokeMethod);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Done()
|
public override void Done()
|
||||||
{
|
{
|
||||||
if (_done)
|
if (_done)
|
||||||
{
|
{
|
||||||
|
@ -200,12 +190,14 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
}
|
}
|
||||||
_done = true;
|
_done = true;
|
||||||
|
|
||||||
|
ModuleDef mod = Module;
|
||||||
|
|
||||||
// for stable order, we sort methods by name
|
// for stable order, we sort methods by name
|
||||||
List<CallInfo> callMethodList = _callMethods.Values.ToList();
|
List<CallInfo> callMethodList = _callMethods.Values.ToList();
|
||||||
callMethodList.Sort((a, b) => a.key1.CompareTo(b.key1));
|
callMethodList.Sort((a, b) => a.key1.CompareTo(b.key1));
|
||||||
|
|
||||||
var cctor = new MethodDefUser(".cctor",
|
var cctor = new MethodDefUser(".cctor",
|
||||||
MethodSig.CreateStatic(_module.CorLibTypes.Void),
|
MethodSig.CreateStatic(mod.CorLibTypes.Void),
|
||||||
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
MethodImplAttributes.IL | MethodImplAttributes.Managed,
|
||||||
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName | MethodAttributes.Private);
|
||||||
cctor.DeclaringType = _delegateInstanceHolderType;
|
cctor.DeclaringType = _delegateInstanceHolderType;
|
||||||
|
@ -219,7 +211,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
// arr[index] = d;
|
// arr[index] = d;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
ins.Add(Instruction.CreateLdcI4(callMethodList.Count));
|
ins.Add(Instruction.CreateLdcI4(callMethodList.Count));
|
||||||
ins.Add(Instruction.Create(OpCodes.Newarr, _module.CorLibTypes.Object));
|
ins.Add(Instruction.Create(OpCodes.Newarr, mod.CorLibTypes.Object));
|
||||||
foreach (CallInfo ci in callMethodList)
|
foreach (CallInfo ci in callMethodList)
|
||||||
{
|
{
|
||||||
ci.index = index;
|
ci.index = index;
|
||||||
|
@ -240,8 +232,9 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
List<CallInfo> callMethodList2 = callMethodList.ToList();
|
List<CallInfo> callMethodList2 = callMethodList.ToList();
|
||||||
callMethodList2.Sort((a, b) => a.key2.CompareTo(b.key2));
|
callMethodList2.Sort((a, b) => a.key2.CompareTo(b.key2));
|
||||||
|
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(_module);
|
EncryptionScopeInfo encryptionScope = EncryptionScope;
|
||||||
DefaultMetadataImporter importer = _moduleEntityManager.GetDefaultModuleMetadataImporter(_module, _encryptionScopeProvider);
|
DefaultMetadataImporter importer = this.GetDefaultModuleMetadataImporter();
|
||||||
|
RvaDataAllocator rvaDataAllocator = this.GetEntity<RvaDataAllocator>();
|
||||||
foreach (CallInfo ci in callMethodList2)
|
foreach (CallInfo ci in callMethodList2)
|
||||||
{
|
{
|
||||||
_delegateInstanceHolderType.Fields.Add(ci.delegateInstanceField);
|
_delegateInstanceHolderType.Fields.Add(ci.delegateInstanceField);
|
||||||
|
@ -254,7 +247,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
int salt = localRandom.NextInt();
|
int salt = localRandom.NextInt();
|
||||||
|
|
||||||
int encryptedValue = encryptionScope.encryptor.Encrypt(ci.index, ops, salt);
|
int encryptedValue = encryptionScope.encryptor.Encrypt(ci.index, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(_module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
ins.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
ins.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
ins.Add(Instruction.CreateLdcI4(ops));
|
ins.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -268,33 +261,4 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
ins.Add(Instruction.Create(OpCodes.Ret));
|
ins.Add(Instruction.Create(OpCodes.Ret));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DelegateProxyAllocator
|
|
||||||
{
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
private readonly CallObfuscationSettingsFacade _settings;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
|
|
||||||
public DelegateProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, RvaDataAllocator rvaDataAllocator, CallObfuscationSettingsFacade settings)
|
|
||||||
{
|
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
_rvaDataAllocator = rvaDataAllocator;
|
|
||||||
_settings = settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ModuleDelegateProxyAllocator GetModuleAllocator(ModuleDef mod)
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetEntity<ModuleDelegateProxyAllocator>(mod, () => new ModuleDelegateProxyAllocator(_moduleEntityManager, _encryptionScopeProvider, _rvaDataAllocator, _settings));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Done()
|
|
||||||
{
|
|
||||||
foreach (var allocator in _moduleEntityManager.GetEntities<ModuleDelegateProxyAllocator>())
|
|
||||||
{
|
|
||||||
allocator.Done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,16 +12,16 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
public class DelegateProxyObfuscator : ObfuscatorBase
|
public class DelegateProxyObfuscator : ObfuscatorBase
|
||||||
{
|
{
|
||||||
private readonly DelegateProxyAllocator _delegateProxyAllocator;
|
private readonly GroupByModuleEntityManager _entityManager;
|
||||||
|
|
||||||
public DelegateProxyObfuscator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, RvaDataAllocator rvaDataAllocator, CallObfuscationSettingsFacade settings)
|
public DelegateProxyObfuscator(GroupByModuleEntityManager moduleEntityManager)
|
||||||
{
|
{
|
||||||
_delegateProxyAllocator = new DelegateProxyAllocator(encryptionScopeProvider, moduleEntityManager, rvaDataAllocator, settings);
|
_entityManager = moduleEntityManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Done()
|
public override void Done()
|
||||||
{
|
{
|
||||||
_delegateProxyAllocator.Done();
|
_entityManager.Done<DelegateProxyAllocator>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private MethodSig CreateProxyMethodSig(ModuleDef module, IMethod method)
|
private MethodSig CreateProxyMethodSig(ModuleDef module, IMethod method)
|
||||||
|
@ -47,7 +47,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
public override bool Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions)
|
public override bool Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
ModuleDelegateProxyAllocator allocator = _delegateProxyAllocator.GetModuleAllocator(callingMethod.Module);
|
DelegateProxyAllocator allocator = _entityManager.GetEntity<DelegateProxyAllocator>(callingMethod.Module);
|
||||||
LocalVariableAllocator localVarAllocator = new LocalVariableAllocator(callingMethod);
|
LocalVariableAllocator localVarAllocator = new LocalVariableAllocator(callingMethod);
|
||||||
MethodSig methodSig = CreateProxyMethodSig(callingMethod.Module, calledMethod);
|
MethodSig methodSig = CreateProxyMethodSig(callingMethod.Module, calledMethod);
|
||||||
DelegateProxyMethodData proxyData = allocator.Allocate(calledMethod, callVir, methodSig);
|
DelegateProxyMethodData proxyData = allocator.Allocate(calledMethod, callVir, methodSig);
|
||||||
|
|
|
@ -31,14 +31,10 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ModuleDispatchProxyAllocator : IGroupByModuleEntity
|
class ModuleDispatchProxyAllocator : GroupByModuleEntityBase
|
||||||
{
|
{
|
||||||
private ModuleDef _module;
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly CallObfuscationSettingsFacade _settings;
|
|
||||||
|
|
||||||
private EncryptionScopeInfo _encryptionScope;
|
|
||||||
private bool _done;
|
private bool _done;
|
||||||
|
private CallObfuscationSettingsFacade _settings;
|
||||||
|
|
||||||
|
|
||||||
class MethodProxyInfo
|
class MethodProxyInfo
|
||||||
|
@ -71,25 +67,23 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
private TypeDef _proxyTypeDef;
|
private TypeDef _proxyTypeDef;
|
||||||
|
|
||||||
public ModuleDispatchProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, CallObfuscationSettingsFacade settings)
|
public ModuleDispatchProxyAllocator()
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_settings = settings;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Init(ModuleDef mod)
|
public override void Init()
|
||||||
{
|
{
|
||||||
_module = mod;
|
_settings = CallObfusPass.CurrentSettings;
|
||||||
_encryptionScope = _encryptionScopeProvider.GetScope(mod);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private TypeDef CreateProxyTypeDef()
|
private TypeDef CreateProxyTypeDef()
|
||||||
{
|
{
|
||||||
using (var scope = new DisableTypeDefFindCacheScope(_module))
|
ModuleDef mod = Module;
|
||||||
|
using (var scope = new DisableTypeDefFindCacheScope(mod))
|
||||||
{
|
{
|
||||||
var typeDef = new TypeDefUser($"{ConstValues.ObfuzInternalSymbolNamePrefix}ProxyCall", _module.CorLibTypes.Object.ToTypeDefOrRef());
|
var typeDef = new TypeDefUser($"{ConstValues.ObfuzInternalSymbolNamePrefix}ProxyCall", mod.CorLibTypes.Object.ToTypeDefOrRef());
|
||||||
typeDef.Attributes = TypeAttributes.NotPublic | TypeAttributes.Sealed;
|
typeDef.Attributes = TypeAttributes.NotPublic | TypeAttributes.Sealed;
|
||||||
_module.Types.Add(typeDef);
|
mod.Types.Add(typeDef);
|
||||||
return typeDef;
|
return typeDef;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -135,24 +129,25 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
private MethodSig CreateDispatchMethodSig(IMethod method)
|
private MethodSig CreateDispatchMethodSig(IMethod method)
|
||||||
{
|
{
|
||||||
MethodSig methodSig = MetaUtil.ToSharedMethodSig(_module.CorLibTypes, MetaUtil.GetInflatedMethodSig(method, null));
|
ModuleDef mod = Module;
|
||||||
|
MethodSig methodSig = MetaUtil.ToSharedMethodSig(mod.CorLibTypes, MetaUtil.GetInflatedMethodSig(method, null));
|
||||||
//MethodSig methodSig = MetaUtil.GetInflatedMethodSig(method).Clone();
|
//MethodSig methodSig = MetaUtil.GetInflatedMethodSig(method).Clone();
|
||||||
//methodSig.Params
|
//methodSig.Params
|
||||||
switch (MetaUtil.GetThisArgType(method))
|
switch (MetaUtil.GetThisArgType(method))
|
||||||
{
|
{
|
||||||
case ThisArgType.Class:
|
case ThisArgType.Class:
|
||||||
{
|
{
|
||||||
methodSig.Params.Insert(0, _module.CorLibTypes.Object);
|
methodSig.Params.Insert(0, mod.CorLibTypes.Object);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ThisArgType.ValueType:
|
case ThisArgType.ValueType:
|
||||||
{
|
{
|
||||||
methodSig.Params.Insert(0, _module.CorLibTypes.IntPtr);
|
methodSig.Params.Insert(0, mod.CorLibTypes.IntPtr);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// extra param for index
|
// extra param for index
|
||||||
methodSig.Params.Add(_module.CorLibTypes.Int32);
|
methodSig.Params.Add(mod.CorLibTypes.Int32);
|
||||||
return MethodSig.CreateStatic(methodSig.RetType, methodSig.Params.ToArray());
|
return MethodSig.CreateStatic(methodSig.RetType, methodSig.Params.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,7 +158,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
private int GenerateEncryptOps(IRandom random)
|
private int GenerateEncryptOps(IRandom random)
|
||||||
{
|
{
|
||||||
return EncryptionUtil.GenerateEncryptionOpCodes(random, _encryptionScope.encryptor, _settings.obfuscationLevel);
|
return EncryptionUtil.GenerateEncryptionOpCodes(random, EncryptionScope.encryptor, _settings.obfuscationLevel);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DispatchMethodInfo GetDispatchMethod(IMethod method)
|
private DispatchMethodInfo GetDispatchMethod(IMethod method)
|
||||||
|
@ -188,7 +183,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
private IRandom CreateRandomForMethod(IMethod method, bool callVir)
|
private IRandom CreateRandomForMethod(IMethod method, bool callVir)
|
||||||
{
|
{
|
||||||
int seed = MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method);
|
int seed = MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method);
|
||||||
return _encryptionScope.localRandomCreator(seed);
|
return EncryptionScope.localRandomCreator(seed);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ProxyCallMethodData Allocate(IMethod method, bool callVir)
|
public ProxyCallMethodData Allocate(IMethod method, bool callVir)
|
||||||
|
@ -206,7 +201,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
IRandom localRandom = CreateRandomForMethod(method, callVir);
|
IRandom localRandom = CreateRandomForMethod(method, callVir);
|
||||||
int encryptOps = GenerateEncryptOps(localRandom);
|
int encryptOps = GenerateEncryptOps(localRandom);
|
||||||
int salt = GenerateSalt(localRandom);
|
int salt = GenerateSalt(localRandom);
|
||||||
int encryptedIndex = _encryptionScope.encryptor.Encrypt(index, encryptOps, salt);
|
int encryptedIndex = EncryptionScope.encryptor.Encrypt(index, encryptOps, salt);
|
||||||
proxyInfo = new MethodProxyInfo()
|
proxyInfo = new MethodProxyInfo()
|
||||||
{
|
{
|
||||||
proxyMethod = methodDispatcher.methodDef,
|
proxyMethod = methodDispatcher.methodDef,
|
||||||
|
@ -221,7 +216,7 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
return new ProxyCallMethodData(proxyInfo.proxyMethod, proxyInfo.encryptedOps, proxyInfo.salt, proxyInfo.encryptedIndex, proxyInfo.index);
|
return new ProxyCallMethodData(proxyInfo.proxyMethod, proxyInfo.encryptedOps, proxyInfo.salt, proxyInfo.encryptedIndex, proxyInfo.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Done()
|
public override void Done()
|
||||||
{
|
{
|
||||||
if (_done)
|
if (_done)
|
||||||
{
|
{
|
||||||
|
@ -275,37 +270,4 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class DispatchProxyAllocator
|
|
||||||
{
|
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private GroupByModuleEntityManager _moduleEntityManager;
|
|
||||||
private readonly CallObfuscationSettingsFacade _settings;
|
|
||||||
|
|
||||||
public DispatchProxyAllocator(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, CallObfuscationSettingsFacade settings)
|
|
||||||
{
|
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
|
||||||
_settings = settings;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ModuleDispatchProxyAllocator GetModuleAllocator(ModuleDef mod)
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetEntity<ModuleDispatchProxyAllocator>(mod, () => new ModuleDispatchProxyAllocator(_encryptionScopeProvider, _settings));
|
|
||||||
}
|
|
||||||
|
|
||||||
public ProxyCallMethodData Allocate(ModuleDef mod, IMethod method, bool callVir)
|
|
||||||
{
|
|
||||||
ModuleDispatchProxyAllocator allocator = GetModuleAllocator(mod);
|
|
||||||
return allocator.Allocate(method, callVir);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Done()
|
|
||||||
{
|
|
||||||
foreach (var allocator in _moduleEntityManager.GetEntities<ModuleDispatchProxyAllocator>())
|
|
||||||
{
|
|
||||||
allocator.Done();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,30 +11,24 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
|
|
||||||
public class DispatchProxyObfuscator : ObfuscatorBase
|
public class DispatchProxyObfuscator : ObfuscatorBase
|
||||||
{
|
{
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly ConstFieldAllocator _constFieldAllocator;
|
|
||||||
private readonly DispatchProxyAllocator _proxyCallAllocator;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
||||||
|
|
||||||
public DispatchProxyObfuscator(EncryptionScopeProvider encryptionScopeProvider, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, CallObfuscationSettingsFacade settings)
|
public DispatchProxyObfuscator(GroupByModuleEntityManager moduleEntityManager)
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_constFieldAllocator = constFieldAllocator;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
_moduleEntityManager = moduleEntityManager;
|
||||||
_proxyCallAllocator = new DispatchProxyAllocator(encryptionScopeProvider, moduleEntityManager, settings);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Done()
|
public override void Done()
|
||||||
{
|
{
|
||||||
_proxyCallAllocator.Done();
|
_moduleEntityManager.Done<ModuleDispatchProxyAllocator>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool Obfuscate(MethodDef callerMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions)
|
public override bool Obfuscate(MethodDef callerMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
|
ModuleDispatchProxyAllocator proxyCallAllocator = _moduleEntityManager.GetEntity<ModuleDispatchProxyAllocator>(callerMethod.Module);
|
||||||
MethodSig sharedMethodSig = MetaUtil.ToSharedMethodSig(calledMethod.Module.CorLibTypes, MetaUtil.GetInflatedMethodSig(calledMethod, null));
|
MethodSig sharedMethodSig = MetaUtil.ToSharedMethodSig(calledMethod.Module.CorLibTypes, MetaUtil.GetInflatedMethodSig(calledMethod, null));
|
||||||
ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callerMethod.Module, calledMethod, callVir);
|
ProxyCallMethodData proxyCallMethodData = proxyCallAllocator.Allocate(calledMethod, callVir);
|
||||||
DefaultMetadataImporter importer = _moduleEntityManager.GetDefaultModuleMetadataImporter(callerMethod.Module, _encryptionScopeProvider);
|
DefaultMetadataImporter importer = proxyCallAllocator.GetDefaultModuleMetadataImporter();
|
||||||
|
|
||||||
//if (needCacheCall)
|
//if (needCacheCall)
|
||||||
//{
|
//{
|
||||||
|
@ -49,7 +43,8 @@ namespace Obfuz.ObfusPasses.CallObfus
|
||||||
// obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt));
|
// obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt));
|
||||||
//}
|
//}
|
||||||
|
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(callerMethod.Module, proxyCallMethodData.index);
|
ConstFieldAllocator constFieldAllocator = proxyCallAllocator.GetEntity<ConstFieldAllocator>();
|
||||||
|
FieldDef cacheField = constFieldAllocator.Allocate(proxyCallMethodData.index);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, proxyCallMethodData.proxyMethod));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, proxyCallMethodData.proxyMethod));
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -23,7 +23,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
var ctx = ObfuscationPassContext.Current;
|
var ctx = ObfuscationPassContext.Current;
|
||||||
_dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
|
_dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
|
||||||
_dataObfuscator = new DefaultConstEncryptor(ctx.encryptionScopeProvider, ctx.rvaDataAllocator, ctx.constFieldAllocator, ctx.moduleEntityManager, _settings);
|
_dataObfuscator = new DefaultConstEncryptor(ctx.moduleEntityManager, _settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void Stop()
|
public override void Stop()
|
||||||
|
|
|
@ -12,17 +12,11 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
public class DefaultConstEncryptor : IConstEncryptor
|
public class DefaultConstEncryptor : IConstEncryptor
|
||||||
{
|
{
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly RvaDataAllocator _rvaDataAllocator;
|
|
||||||
private readonly ConstFieldAllocator _constFieldAllocator;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
||||||
private readonly ConstEncryptionSettingsFacade _settings;
|
private readonly ConstEncryptionSettingsFacade _settings;
|
||||||
|
|
||||||
public DefaultConstEncryptor(EncryptionScopeProvider encryptionScopeProvider, RvaDataAllocator rvaDataAllocator, ConstFieldAllocator constFieldAllocator, GroupByModuleEntityManager moduleEntityManager, ConstEncryptionSettingsFacade settings)
|
public DefaultConstEncryptor(GroupByModuleEntityManager moduleEntityManager, ConstEncryptionSettingsFacade settings)
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_rvaDataAllocator = rvaDataAllocator;
|
|
||||||
_constFieldAllocator = constFieldAllocator;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
_moduleEntityManager = moduleEntityManager;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
}
|
}
|
||||||
|
@ -44,20 +38,23 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
private DefaultMetadataImporter GetModuleMetadataImporter(MethodDef method)
|
private DefaultMetadataImporter GetModuleMetadataImporter(MethodDef method)
|
||||||
{
|
{
|
||||||
return _moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, _encryptionScopeProvider);
|
return _moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ObfuscateInt(MethodDef method, bool needCacheValue, int value, List<Instruction> obfuscatedInstructions)
|
public void ObfuscateInt(MethodDef method, bool needCacheValue, int value, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(method.Module);
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
||||||
ModuleConstFieldAllocator moduleConstFieldAllocator = _constFieldAllocator.GetModuleAllocator(method.Module);
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
||||||
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
||||||
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
|
||||||
switch (random.NextInt(5))
|
switch (random.NextInt(5))
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
// = c = encrypted static field
|
// = c = encrypted static field
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -67,7 +64,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
int a = random.NextInt();
|
int a = random.NextInt();
|
||||||
int b = value - a;
|
int b = value - a;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +75,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
int ra = MathUtil.ModInverse32(a);
|
int ra = MathUtil.ModInverse32(a);
|
||||||
int b = ra * value;
|
int b = ra * value;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -88,7 +85,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
int a = random.NextInt();
|
int a = random.NextInt();
|
||||||
int b = a ^ value;
|
int b = a ^ value;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoInt(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -96,16 +93,14 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
if (needCacheValue)
|
if (needCacheValue)
|
||||||
{
|
{
|
||||||
FieldDef cacheField = moduleConstFieldAllocator.Allocate(value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
int encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
int encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -120,16 +115,18 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
public void ObfuscateLong(MethodDef method, bool needCacheValue, long value, List<Instruction> obfuscatedInstructions)
|
public void ObfuscateLong(MethodDef method, bool needCacheValue, long value, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(method.Module);
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
||||||
ModuleConstFieldAllocator moduleConstFieldAllocator = _constFieldAllocator.GetModuleAllocator(method.Module);
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
||||||
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
||||||
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
|
||||||
switch (random.NextInt(5))
|
switch (random.NextInt(5))
|
||||||
{
|
{
|
||||||
case 0:
|
case 0:
|
||||||
{
|
{
|
||||||
// c = encrypted static field
|
// c = encrypted static field
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -139,7 +136,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
long a = random.NextLong();
|
long a = random.NextLong();
|
||||||
long b = value - a;
|
long b = value - a;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Add));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -150,7 +147,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
long ra = MathUtil.ModInverse64(a);
|
long ra = MathUtil.ModInverse64(a);
|
||||||
long b = ra * value;
|
long b = ra * value;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Mul));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -160,7 +157,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
long a = random.NextLong();
|
long a = random.NextLong();
|
||||||
long b = a ^ value;
|
long b = a ^ value;
|
||||||
float constProbability = 0.5f;
|
float constProbability = 0.5f;
|
||||||
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, moduleConstFieldAllocator, obfuscatedInstructions);
|
ConstObfusUtil.LoadConstTwoLong(a, b, random, constProbability, constFieldAllocator, obfuscatedInstructions);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Xor));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +165,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
if (needCacheValue)
|
if (needCacheValue)
|
||||||
{
|
{
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -176,9 +173,8 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
long encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
long encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -193,21 +189,25 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
public void ObfuscateFloat(MethodDef method, bool needCacheValue, float value, List<Instruction> obfuscatedInstructions)
|
public void ObfuscateFloat(MethodDef method, bool needCacheValue, float value, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
||||||
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
||||||
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
||||||
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
|
||||||
if (needCacheValue)
|
if (needCacheValue)
|
||||||
{
|
{
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(method.Module);
|
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
float encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
float encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -217,21 +217,25 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
public void ObfuscateDouble(MethodDef method, bool needCacheValue, double value, List<Instruction> obfuscatedInstructions)
|
public void ObfuscateDouble(MethodDef method, bool needCacheValue, double value, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
||||||
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
||||||
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
||||||
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
|
||||||
if (needCacheValue)
|
if (needCacheValue)
|
||||||
{
|
{
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(method.Module);
|
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
double encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
double encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(ops));
|
||||||
|
@ -264,7 +268,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
{
|
{
|
||||||
if (!_encryptedRvaFields.TryGetValue(fieldDef, out var encryptedRvaData))
|
if (!_encryptedRvaFields.TryGetValue(fieldDef, out var encryptedRvaData))
|
||||||
{
|
{
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(fieldDef.Module);
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(fieldDef.Module);
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, FieldEqualityComparer.CompareDeclaringTypes.GetHashCode(fieldDef));
|
IRandom random = CreateRandomForValue(encryptionScope, FieldEqualityComparer.CompareDeclaringTypes.GetHashCode(fieldDef));
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
|
@ -297,23 +301,26 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
|
||||||
|
|
||||||
public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions)
|
public void ObfuscateString(MethodDef method, bool needCacheValue, string value, List<Instruction> obfuscatedInstructions)
|
||||||
{
|
{
|
||||||
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
|
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
||||||
|
ConstFieldAllocator constFieldAllocator = _moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module);
|
||||||
|
RvaDataAllocator rvaDataAllocator = _moduleEntityManager.GetEntity<RvaDataAllocator>(method.Module);
|
||||||
|
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
||||||
|
|
||||||
if (needCacheValue)
|
if (needCacheValue)
|
||||||
{
|
{
|
||||||
FieldDef cacheField = _constFieldAllocator.Allocate(method.Module, value);
|
FieldDef cacheField = constFieldAllocator.Allocate(value);
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(method.Module);
|
|
||||||
IRandom random = CreateRandomForValue(encryptionScope, value.GetHashCode());
|
|
||||||
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
int ops = GenerateEncryptionOperations(encryptionScope, random);
|
||||||
int salt = GenerateSalt(random);
|
int salt = GenerateSalt(random);
|
||||||
int stringByteLength = Encoding.UTF8.GetByteCount(value);
|
int stringByteLength = Encoding.UTF8.GetByteCount(value);
|
||||||
byte[] encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
byte[] encryptedValue = encryptionScope.encryptor.Encrypt(value, ops, salt);
|
||||||
Assert.AreEqual(stringByteLength, encryptedValue.Length);
|
Assert.AreEqual(stringByteLength, encryptedValue.Length);
|
||||||
RvaData rvaData = _rvaDataAllocator.Allocate(method.Module, encryptedValue);
|
RvaData rvaData = rvaDataAllocator.Allocate(encryptedValue);
|
||||||
|
|
||||||
DefaultMetadataImporter importer = GetModuleMetadataImporter(method);
|
|
||||||
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, rvaData.field));
|
||||||
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
obfuscatedInstructions.Add(Instruction.CreateLdcI4(rvaData.offset));
|
||||||
// should use stringByteLength, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length.
|
// should use stringByteLength, can't use rvaData.size, because rvaData.size is align to 4, it's not the actual length.
|
||||||
|
|
|
@ -13,7 +13,7 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus
|
||||||
public IRandom localRandom;
|
public IRandom localRandom;
|
||||||
public EncryptionScopeInfo encryptionScope;
|
public EncryptionScopeInfo encryptionScope;
|
||||||
public DefaultMetadataImporter importer;
|
public DefaultMetadataImporter importer;
|
||||||
public ModuleConstFieldAllocator constFieldAllocator;
|
public ConstFieldAllocator constFieldAllocator;
|
||||||
public int minInstructionCountOfBasicBlockToObfuscate;
|
public int minInstructionCountOfBasicBlockToObfuscate;
|
||||||
|
|
||||||
public IRandom CreateRandom()
|
public IRandom CreateRandom()
|
||||||
|
@ -60,7 +60,8 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus
|
||||||
//Debug.Log($"Obfuscating method: {method.FullName} with EvalStackObfusPass");
|
//Debug.Log($"Obfuscating method: {method.FullName} with EvalStackObfusPass");
|
||||||
|
|
||||||
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
||||||
var encryptionScope = ctx.encryptionScopeProvider.GetScope(method.Module);
|
GroupByModuleEntityManager moduleEntityManager = ctx.moduleEntityManager;
|
||||||
|
var encryptionScope = moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
||||||
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
||||||
var obfusMethodCtx = new ObfusMethodContext
|
var obfusMethodCtx = new ObfusMethodContext
|
||||||
|
@ -68,9 +69,9 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus
|
||||||
method = method,
|
method = method,
|
||||||
localVariableAllocator = new LocalVariableAllocator(method),
|
localVariableAllocator = new LocalVariableAllocator(method),
|
||||||
encryptionScope = encryptionScope,
|
encryptionScope = encryptionScope,
|
||||||
constFieldAllocator = ctx.constFieldAllocator.GetModuleAllocator(method.Module),
|
constFieldAllocator = moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module),
|
||||||
localRandom = localRandom,
|
localRandom = localRandom,
|
||||||
importer = ctx.moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, ctx.encryptionScopeProvider),
|
importer = moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module),
|
||||||
minInstructionCountOfBasicBlockToObfuscate = _settings.minInstructionCountOfBasicBlockToObfuscate,
|
minInstructionCountOfBasicBlockToObfuscate = _settings.minInstructionCountOfBasicBlockToObfuscate,
|
||||||
};
|
};
|
||||||
_obfuscator.Obfuscate(method, obfusMethodCtx);
|
_obfuscator.Obfuscate(method, obfusMethodCtx);
|
||||||
|
|
|
@ -6,7 +6,6 @@ using Obfuz.Utils;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Runtime.ConstrainedExecution;
|
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using UnityEngine.Assertions;
|
using UnityEngine.Assertions;
|
||||||
|
|
||||||
|
@ -155,11 +154,11 @@ namespace Obfuz.ObfusPasses.ControlFlowObfus
|
||||||
|
|
||||||
private readonly MethodDef _method;
|
private readonly MethodDef _method;
|
||||||
private readonly IRandom _random;
|
private readonly IRandom _random;
|
||||||
private readonly ModuleConstFieldAllocator _constFieldAllocator;
|
private readonly ConstFieldAllocator _constFieldAllocator;
|
||||||
private readonly int _minInstructionCountOfBasicBlockToObfuscate;
|
private readonly int _minInstructionCountOfBasicBlockToObfuscate;
|
||||||
private readonly BasicBlockInfo _bbHead;
|
private readonly BasicBlockInfo _bbHead;
|
||||||
|
|
||||||
public MethodControlFlowCalculator(MethodDef method, IRandom random, ModuleConstFieldAllocator constFieldAllocator, int minInstructionCountOfBasicBlockToObfuscate)
|
public MethodControlFlowCalculator(MethodDef method, IRandom random, ConstFieldAllocator constFieldAllocator, int minInstructionCountOfBasicBlockToObfuscate)
|
||||||
{
|
{
|
||||||
_method = method;
|
_method = method;
|
||||||
_random = random;
|
_random = random;
|
||||||
|
|
|
@ -16,7 +16,7 @@ namespace Obfuz.ObfusPasses.EvalStackObfus
|
||||||
public IRandom localRandom;
|
public IRandom localRandom;
|
||||||
public EncryptionScopeInfo encryptionScope;
|
public EncryptionScopeInfo encryptionScope;
|
||||||
public DefaultMetadataImporter importer;
|
public DefaultMetadataImporter importer;
|
||||||
public ModuleConstFieldAllocator constFieldAllocator;
|
public ConstFieldAllocator constFieldAllocator;
|
||||||
public float obfuscationPercentage;
|
public float obfuscationPercentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,7 +74,9 @@ namespace Obfuz.ObfusPasses.EvalStackObfus
|
||||||
|
|
||||||
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
||||||
var calc = new EvalStackCalculator(method);
|
var calc = new EvalStackCalculator(method);
|
||||||
var encryptionScope = ctx.encryptionScopeProvider.GetScope(method.Module);
|
|
||||||
|
GroupByModuleEntityManager moduleEntityManager = ctx.moduleEntityManager;
|
||||||
|
var encryptionScope = moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
||||||
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
var localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method));
|
||||||
var obfusMethodCtx = new ObfusMethodContext
|
var obfusMethodCtx = new ObfusMethodContext
|
||||||
|
@ -83,9 +85,9 @@ namespace Obfuz.ObfusPasses.EvalStackObfus
|
||||||
evalStackCalculator = calc,
|
evalStackCalculator = calc,
|
||||||
localVariableAllocator = new LocalVariableAllocator(method),
|
localVariableAllocator = new LocalVariableAllocator(method),
|
||||||
encryptionScope = encryptionScope,
|
encryptionScope = encryptionScope,
|
||||||
constFieldAllocator = ctx.constFieldAllocator.GetModuleAllocator(method.Module),
|
constFieldAllocator = moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module),
|
||||||
localRandom = localRandom,
|
localRandom = localRandom,
|
||||||
importer = ctx.moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, ctx.encryptionScopeProvider),
|
importer = moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module),
|
||||||
obfuscationPercentage = ruleData.obfuscationPercentage,
|
obfuscationPercentage = ruleData.obfuscationPercentage,
|
||||||
};
|
};
|
||||||
for (int i = 0; i < instructions.Count; i++)
|
for (int i = 0; i < instructions.Count; i++)
|
||||||
|
|
|
@ -17,7 +17,7 @@ namespace Obfuz.ObfusPasses.ExprObfus
|
||||||
public IRandom localRandom;
|
public IRandom localRandom;
|
||||||
public EncryptionScopeInfo encryptionScope;
|
public EncryptionScopeInfo encryptionScope;
|
||||||
public DefaultMetadataImporter importer;
|
public DefaultMetadataImporter importer;
|
||||||
public ModuleConstFieldAllocator constFieldAllocator;
|
public ConstFieldAllocator constFieldAllocator;
|
||||||
public float obfuscationPercentage;
|
public float obfuscationPercentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,7 +120,9 @@ namespace Obfuz.ObfusPasses.ExprObfus
|
||||||
|
|
||||||
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
||||||
var calc = new EvalStackCalculator(method);
|
var calc = new EvalStackCalculator(method);
|
||||||
var encryptionScope = ctx.encryptionScopeProvider.GetScope(method.Module);
|
|
||||||
|
GroupByModuleEntityManager moduleEntityManager = ctx.moduleEntityManager;
|
||||||
|
var encryptionScope = moduleEntityManager.EncryptionScopeProvider.GetScope(method.Module);
|
||||||
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
||||||
var obfuscator = GetObfuscator(ruleData.obfuscationLevel);
|
var obfuscator = GetObfuscator(ruleData.obfuscationLevel);
|
||||||
var obfusMethodCtx = new ObfusMethodContext
|
var obfusMethodCtx = new ObfusMethodContext
|
||||||
|
@ -129,9 +131,9 @@ namespace Obfuz.ObfusPasses.ExprObfus
|
||||||
evalStackCalculator = calc,
|
evalStackCalculator = calc,
|
||||||
localVariableAllocator = new LocalVariableAllocator(method),
|
localVariableAllocator = new LocalVariableAllocator(method),
|
||||||
encryptionScope = encryptionScope,
|
encryptionScope = encryptionScope,
|
||||||
constFieldAllocator = ctx.constFieldAllocator.GetModuleAllocator(method.Module),
|
constFieldAllocator = moduleEntityManager.GetEntity<ConstFieldAllocator>(method.Module),
|
||||||
localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method)),
|
localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method)),
|
||||||
importer = ctx.moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, ctx.encryptionScopeProvider),
|
importer = moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module),
|
||||||
obfuscationPercentage = ruleData.obfuscationPercentage,
|
obfuscationPercentage = ruleData.obfuscationPercentage,
|
||||||
};
|
};
|
||||||
for (int i = 0; i < instructions.Count; i++)
|
for (int i = 0; i < instructions.Count; i++)
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace Obfuz.ObfusPasses.ExprObfus.Obfuscators
|
||||||
protected bool GenerateIdentityTransformForArgument(Instruction inst, EvalDataType op, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
protected bool GenerateIdentityTransformForArgument(Instruction inst, EvalDataType op, List<Instruction> outputInsts, ObfusMethodContext ctx)
|
||||||
{
|
{
|
||||||
IRandom random = ctx.localRandom;
|
IRandom random = ctx.localRandom;
|
||||||
ModuleConstFieldAllocator constFieldAllocator = ctx.constFieldAllocator;
|
ConstFieldAllocator constFieldAllocator = ctx.constFieldAllocator;
|
||||||
switch (op)
|
switch (op)
|
||||||
{
|
{
|
||||||
case EvalDataType.Int32:
|
case EvalDataType.Int32:
|
||||||
|
|
|
@ -11,22 +11,15 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
|
||||||
{
|
{
|
||||||
public class DefaultFieldEncryptor : FieldEncryptorBase
|
public class DefaultFieldEncryptor : FieldEncryptorBase
|
||||||
{
|
{
|
||||||
private readonly EncryptionScopeProvider _encryptionScopeProvider;
|
|
||||||
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
private readonly GroupByModuleEntityManager _moduleEntityManager;
|
||||||
private readonly FieldEncryptionSettingsFacade _settings;
|
private readonly FieldEncryptionSettingsFacade _settings;
|
||||||
|
|
||||||
public DefaultFieldEncryptor(EncryptionScopeProvider encryptionScopeProvider, GroupByModuleEntityManager moduleEntityManager, FieldEncryptionSettingsFacade settings)
|
public DefaultFieldEncryptor(GroupByModuleEntityManager moduleEntityManager, FieldEncryptionSettingsFacade settings)
|
||||||
{
|
{
|
||||||
_encryptionScopeProvider = encryptionScopeProvider;
|
|
||||||
_moduleEntityManager = moduleEntityManager;
|
_moduleEntityManager = moduleEntityManager;
|
||||||
_settings = settings;
|
_settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private DefaultMetadataImporter GetMetadataImporter(MethodDef method)
|
|
||||||
{
|
|
||||||
return _moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, _encryptionScopeProvider);
|
|
||||||
}
|
|
||||||
|
|
||||||
class FieldEncryptInfo
|
class FieldEncryptInfo
|
||||||
{
|
{
|
||||||
public int encryptOps;
|
public int encryptOps;
|
||||||
|
@ -77,7 +70,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
|
||||||
{
|
{
|
||||||
return info;
|
return info;
|
||||||
}
|
}
|
||||||
EncryptionScopeInfo encryptionScope = _encryptionScopeProvider.GetScope(field.Module);
|
EncryptionScopeInfo encryptionScope = _moduleEntityManager.EncryptionScopeProvider.GetScope(field.Module);
|
||||||
|
|
||||||
IRandom random = CreateRandomForField(encryptionScope.localRandomCreator, field);
|
IRandom random = CreateRandomForField(encryptionScope.localRandomCreator, field);
|
||||||
IEncryptor encryptor = encryptionScope.encryptor;
|
IEncryptor encryptor = encryptionScope.encryptor;
|
||||||
|
@ -99,7 +92,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
|
||||||
|
|
||||||
public override void Encrypt(MethodDef method, FieldDef field, List<Instruction> outputInstructions, Instruction currentInstruction)
|
public override void Encrypt(MethodDef method, FieldDef field, List<Instruction> outputInstructions, Instruction currentInstruction)
|
||||||
{
|
{
|
||||||
DefaultMetadataImporter importer = GetMetadataImporter(method);
|
DefaultMetadataImporter importer = _moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module);
|
||||||
EncryptionServiceMetadataImporter encryptionServiceMetadataImporter = importer.GetEncryptionServiceMetadataImporterOfModule(field.Module);
|
EncryptionServiceMetadataImporter encryptionServiceMetadataImporter = importer.GetEncryptionServiceMetadataImporterOfModule(field.Module);
|
||||||
FieldEncryptInfo fei = GetFieldEncryptInfo(field);
|
FieldEncryptInfo fei = GetFieldEncryptInfo(field);
|
||||||
if (fei.fieldType == ElementType.I4 || fei.fieldType == ElementType.U4 || fei.fieldType == ElementType.R4)
|
if (fei.fieldType == ElementType.I4 || fei.fieldType == ElementType.U4 || fei.fieldType == ElementType.R4)
|
||||||
|
@ -154,7 +147,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
|
||||||
public override void Decrypt(MethodDef method, FieldDef field, List<Instruction> outputInstructions, Instruction currentInstruction)
|
public override void Decrypt(MethodDef method, FieldDef field, List<Instruction> outputInstructions, Instruction currentInstruction)
|
||||||
{
|
{
|
||||||
outputInstructions.Add(currentInstruction.Clone());
|
outputInstructions.Add(currentInstruction.Clone());
|
||||||
DefaultMetadataImporter importer = GetMetadataImporter(method);
|
DefaultMetadataImporter importer = _moduleEntityManager.GetEntity<DefaultMetadataImporter>(method.Module);
|
||||||
EncryptionServiceMetadataImporter encryptionServiceMetadataImporter = importer.GetEncryptionServiceMetadataImporterOfModule(field.Module);
|
EncryptionServiceMetadataImporter encryptionServiceMetadataImporter = importer.GetEncryptionServiceMetadataImporterOfModule(field.Module);
|
||||||
FieldEncryptInfo fei = GetFieldEncryptInfo(field);
|
FieldEncryptInfo fei = GetFieldEncryptInfo(field);
|
||||||
if (fei.fieldType == ElementType.I4 || fei.fieldType == ElementType.U4 || fei.fieldType == ElementType.R4)
|
if (fei.fieldType == ElementType.I4 || fei.fieldType == ElementType.U4 || fei.fieldType == ElementType.R4)
|
||||||
|
|
|
@ -24,7 +24,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
|
||||||
public override void Start()
|
public override void Start()
|
||||||
{
|
{
|
||||||
var ctx = ObfuscationPassContext.Current;
|
var ctx = ObfuscationPassContext.Current;
|
||||||
_memoryEncryptor = new DefaultFieldEncryptor(ctx.encryptionScopeProvider, ctx.moduleEntityManager, _settings);
|
_memoryEncryptor = new DefaultFieldEncryptor(ctx.moduleEntityManager, _settings);
|
||||||
_encryptionPolicy = new ConfigurableEncryptPolicy(ctx.obfuzIgnoreScopeComputeCache, ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
|
_encryptionPolicy = new ConfigurableEncryptPolicy(ctx.obfuzIgnoreScopeComputeCache, ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
using dnlib.DotNet;
|
using dnlib.DotNet;
|
||||||
using dnlib.DotNet.Emit;
|
using dnlib.DotNet.Emit;
|
||||||
using Obfuz.Editor;
|
using Obfuz.Editor;
|
||||||
|
using Obfuz.Emit;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
@ -89,7 +90,7 @@ namespace Obfuz.ObfusPasses.Instinct
|
||||||
}
|
}
|
||||||
|
|
||||||
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
||||||
var importer = ctx.moduleEntityManager.GetDefaultModuleMetadataImporter(callingMethod.Module, ctx.encryptionScopeProvider);
|
var importer = ctx.moduleEntityManager.GetEntity<DefaultMetadataImporter>(callingMethod.Module);
|
||||||
|
|
||||||
string methodName = methodDef.Name;
|
string methodName = methodDef.Name;
|
||||||
switch (methodName)
|
switch (methodName)
|
||||||
|
|
|
@ -14,7 +14,6 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
{
|
{
|
||||||
public class SymbolRename
|
public class SymbolRename
|
||||||
{
|
{
|
||||||
private readonly bool _debug;
|
|
||||||
private readonly bool _useConsistentNamespaceObfuscation;
|
private readonly bool _useConsistentNamespaceObfuscation;
|
||||||
private readonly bool _detectReflectionCompatibility;
|
private readonly bool _detectReflectionCompatibility;
|
||||||
private readonly List<string> _obfuscationRuleFiles;
|
private readonly List<string> _obfuscationRuleFiles;
|
||||||
|
@ -43,7 +42,6 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
public SymbolRename(SymbolObfuscationSettingsFacade settings)
|
public SymbolRename(SymbolObfuscationSettingsFacade settings)
|
||||||
{
|
{
|
||||||
_debug = settings.debug;
|
|
||||||
_useConsistentNamespaceObfuscation = settings.useConsistentNamespaceObfuscation;
|
_useConsistentNamespaceObfuscation = settings.useConsistentNamespaceObfuscation;
|
||||||
_detectReflectionCompatibility = settings.detectReflectionCompatibility;
|
_detectReflectionCompatibility = settings.detectReflectionCompatibility;
|
||||||
_mappingXmlPath = settings.symbolMappingFile;
|
_mappingXmlPath = settings.symbolMappingFile;
|
||||||
|
@ -275,7 +273,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
private void RenameTypes()
|
private void RenameTypes()
|
||||||
{
|
{
|
||||||
Debug.Log("RenameTypes begin");
|
//Debug.Log("RenameTypes begin");
|
||||||
|
|
||||||
RetargetTypeRefInCustomAttributes();
|
RetargetTypeRefInCustomAttributes();
|
||||||
|
|
||||||
|
@ -295,7 +293,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
// clean cache
|
// clean cache
|
||||||
_assemblyCache.EnableTypeDefCache = true;
|
_assemblyCache.EnableTypeDefCache = true;
|
||||||
Debug.Log("Rename Types end");
|
//Debug.Log("Rename Types end");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -398,7 +396,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
private void RenameFields()
|
private void RenameFields()
|
||||||
{
|
{
|
||||||
Debug.Log("Rename fields begin");
|
//Debug.Log("Rename fields begin");
|
||||||
var refFieldMetasMap = new Dictionary<FieldDef, RefFieldMetas>();
|
var refFieldMetasMap = new Dictionary<FieldDef, RefFieldMetas>();
|
||||||
BuildRefFieldMetasMap(refFieldMetasMap);
|
BuildRefFieldMetasMap(refFieldMetasMap);
|
||||||
|
|
||||||
|
@ -415,7 +413,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.Log("Rename fields end");
|
//Debug.Log("Rename fields end");
|
||||||
}
|
}
|
||||||
|
|
||||||
class RefMethodMetas
|
class RefMethodMetas
|
||||||
|
@ -509,8 +507,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
private void RenameMethods()
|
private void RenameMethods()
|
||||||
{
|
{
|
||||||
Debug.Log("Rename methods begin");
|
//Debug.Log("Rename methods begin");
|
||||||
Debug.Log("Rename not virtual methods begin");
|
//Debug.Log("Rename not virtual methods begin");
|
||||||
var virtualMethods = new List<MethodDef>();
|
var virtualMethods = new List<MethodDef>();
|
||||||
var refMethodMetasMap = new Dictionary<MethodDef, RefMethodMetas>();
|
var refMethodMetasMap = new Dictionary<MethodDef, RefMethodMetas>();
|
||||||
BuildRefMethodMetasMap(refMethodMetasMap);
|
BuildRefMethodMetasMap(refMethodMetasMap);
|
||||||
|
@ -547,10 +545,10 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Debug.Log("Rename not virtual methods end");
|
//Debug.Log("Rename not virtual methods end");
|
||||||
|
|
||||||
|
|
||||||
Debug.Log("Rename virtual methods begin");
|
//Debug.Log("Rename virtual methods begin");
|
||||||
var visitedVirtualMethods = new HashSet<MethodDef>();
|
var visitedVirtualMethods = new HashSet<MethodDef>();
|
||||||
var groupNeedRenames = new Dictionary<VirtualMethodGroup, bool>();
|
var groupNeedRenames = new Dictionary<VirtualMethodGroup, bool>();
|
||||||
foreach (var method in virtualMethods)
|
foreach (var method in virtualMethods)
|
||||||
|
@ -614,8 +612,8 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
throw new Exception($"group:{group} method:{method} not found in rename record map");
|
throw new Exception($"group:{group} method:{method} not found in rename record map");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.Log("Rename virtual methods end");
|
//Debug.Log("Rename virtual methods end");
|
||||||
Debug.Log("Rename methods end");
|
//Debug.Log("Rename methods end");
|
||||||
}
|
}
|
||||||
|
|
||||||
class RefPropertyMetas
|
class RefPropertyMetas
|
||||||
|
@ -669,7 +667,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
|
|
||||||
private void RenameProperties()
|
private void RenameProperties()
|
||||||
{
|
{
|
||||||
Debug.Log("Rename properties begin");
|
//Debug.Log("Rename properties begin");
|
||||||
var refPropertyMetasMap = new Dictionary<PropertyDef, RefPropertyMetas>();
|
var refPropertyMetasMap = new Dictionary<PropertyDef, RefPropertyMetas>();
|
||||||
BuildRefPropertyMetasMap(refPropertyMetasMap);
|
BuildRefPropertyMetasMap(refPropertyMetasMap);
|
||||||
foreach (ModuleDef mod in _toObfuscatedModules)
|
foreach (ModuleDef mod in _toObfuscatedModules)
|
||||||
|
@ -685,12 +683,12 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.Log("Rename properties end");
|
//Debug.Log("Rename properties end");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RenameEvents()
|
private void RenameEvents()
|
||||||
{
|
{
|
||||||
Debug.Log("Rename events begin");
|
//Debug.Log("Rename events begin");
|
||||||
foreach (ModuleDef mod in _toObfuscatedModules)
|
foreach (ModuleDef mod in _toObfuscatedModules)
|
||||||
{
|
{
|
||||||
foreach (TypeDef type in mod.GetTypes())
|
foreach (TypeDef type in mod.GetTypes())
|
||||||
|
@ -704,7 +702,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Debug.Log("Rename events begin");
|
//Debug.Log("Rename events begin");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Rename(TypeDef type, RefTypeDefMetas refTypeDefMeta)
|
private void Rename(TypeDef type, RefTypeDefMetas refTypeDefMeta)
|
||||||
|
|
|
@ -64,9 +64,6 @@ namespace Obfuz
|
||||||
public List<ModuleDef> allObfuscationRelativeModules;
|
public List<ModuleDef> allObfuscationRelativeModules;
|
||||||
public ObfuzIgnoreScopeComputeCache obfuzIgnoreScopeComputeCache;
|
public ObfuzIgnoreScopeComputeCache obfuzIgnoreScopeComputeCache;
|
||||||
|
|
||||||
public EncryptionScopeProvider encryptionScopeProvider;
|
|
||||||
public ConstFieldAllocator constFieldAllocator;
|
|
||||||
public RvaDataAllocator rvaDataAllocator;
|
|
||||||
public ObfuscationMethodWhitelist whiteList;
|
public ObfuscationMethodWhitelist whiteList;
|
||||||
public ConfigurablePassPolicy passPolicy;
|
public ConfigurablePassPolicy passPolicy;
|
||||||
}
|
}
|
||||||
|
|
|
@ -283,23 +283,22 @@ namespace Obfuz
|
||||||
LoadAssemblies(assemblyCache, modulesToObfuscate, allObfuscationRelativeModules);
|
LoadAssemblies(assemblyCache, modulesToObfuscate, allObfuscationRelativeModules);
|
||||||
|
|
||||||
EncryptionScopeProvider encryptionScopeProvider = CreateEncryptionScopeProvider();
|
EncryptionScopeProvider encryptionScopeProvider = CreateEncryptionScopeProvider();
|
||||||
var moduleEntityManager = new GroupByModuleEntityManager();
|
var moduleEntityManager = new GroupByModuleEntityManager()
|
||||||
|
{
|
||||||
|
EncryptionScopeProvider = encryptionScopeProvider,
|
||||||
|
};
|
||||||
var obfuzIgnoreScopeComputeCache = new ObfuzIgnoreScopeComputeCache();
|
var obfuzIgnoreScopeComputeCache = new ObfuzIgnoreScopeComputeCache();
|
||||||
var rvaDataAllocator = new RvaDataAllocator(encryptionScopeProvider, moduleEntityManager);
|
|
||||||
var constFieldAllocator = new ConstFieldAllocator(encryptionScopeProvider, rvaDataAllocator, moduleEntityManager);
|
|
||||||
_ctx = new ObfuscationPassContext
|
_ctx = new ObfuscationPassContext
|
||||||
{
|
{
|
||||||
coreSettings = _coreSettings,
|
coreSettings = _coreSettings,
|
||||||
assemblyCache = assemblyCache,
|
assemblyCache = assemblyCache,
|
||||||
modulesToObfuscate = modulesToObfuscate,
|
modulesToObfuscate = modulesToObfuscate,
|
||||||
allObfuscationRelativeModules = allObfuscationRelativeModules,
|
allObfuscationRelativeModules = allObfuscationRelativeModules,
|
||||||
|
|
||||||
moduleEntityManager = moduleEntityManager,
|
moduleEntityManager = moduleEntityManager,
|
||||||
|
|
||||||
encryptionScopeProvider = encryptionScopeProvider,
|
|
||||||
obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache,
|
obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache,
|
||||||
|
|
||||||
rvaDataAllocator = rvaDataAllocator,
|
|
||||||
constFieldAllocator = constFieldAllocator,
|
|
||||||
whiteList = new ObfuscationMethodWhitelist(obfuzIgnoreScopeComputeCache),
|
whiteList = new ObfuscationMethodWhitelist(obfuzIgnoreScopeComputeCache),
|
||||||
passPolicy = _passPolicy,
|
passPolicy = _passPolicy,
|
||||||
};
|
};
|
||||||
|
@ -345,8 +344,8 @@ namespace Obfuz
|
||||||
{
|
{
|
||||||
pipeline.Stop();
|
pipeline.Stop();
|
||||||
|
|
||||||
_ctx.constFieldAllocator.Done();
|
_ctx.moduleEntityManager.Done<ConstFieldAllocator>();
|
||||||
_ctx.rvaDataAllocator.Done();
|
_ctx.moduleEntityManager.Done<RvaDataAllocator>();
|
||||||
WriteAssemblies();
|
WriteAssemblies();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ namespace Obfuz.Utils
|
||||||
{
|
{
|
||||||
internal static class ConstObfusUtil
|
internal static class ConstObfusUtil
|
||||||
{
|
{
|
||||||
public static void LoadConstInt(int a, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstInt(int a, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
|
@ -22,7 +22,7 @@ namespace Obfuz.Utils
|
||||||
outputInsts.Add(inst);
|
outputInsts.Add(inst);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstLong(long a, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstLong(long a, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
|
@ -37,7 +37,7 @@ namespace Obfuz.Utils
|
||||||
outputInsts.Add(inst);
|
outputInsts.Add(inst);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstFloat(float a, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstFloat(float a, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
|
@ -52,7 +52,7 @@ namespace Obfuz.Utils
|
||||||
outputInsts.Add(inst);
|
outputInsts.Add(inst);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstDouble(double a, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstDouble(double a, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
Instruction inst;
|
Instruction inst;
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
|
@ -68,7 +68,7 @@ namespace Obfuz.Utils
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void LoadConstTwoInt(int a, int b, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstTwoInt(int a, int b, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
{
|
{
|
||||||
|
@ -96,7 +96,7 @@ namespace Obfuz.Utils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstTwoLong(long a, long b, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstTwoLong(long a, long b, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
{
|
{
|
||||||
|
@ -122,7 +122,7 @@ namespace Obfuz.Utils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstTwoFloat(float a, float b, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstTwoFloat(float a, float b, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
{
|
{
|
||||||
|
@ -148,7 +148,7 @@ namespace Obfuz.Utils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LoadConstTwoDouble(double a, double b, IRandom random, float constProbability, ModuleConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
public static void LoadConstTwoDouble(double a, double b, IRandom random, float constProbability, ConstFieldAllocator constFieldAllocator, List<Instruction> outputInsts)
|
||||||
{
|
{
|
||||||
if (random.NextInPercentage(constProbability))
|
if (random.NextInPercentage(constProbability))
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue