支持CallObfus cacheCallIndex

backup
walon 2025-05-10 11:25:07 +08:00
parent 2166821d13
commit f1e3399c27
10 changed files with 64 additions and 30 deletions

View File

@ -70,7 +70,7 @@ namespace Obfuz.Data
{ {
_module.EnableTypeDefFindCache = false; _module.EnableTypeDefFindCache = false;
ITypeDefOrRef objectTypeRef = _module.Import(typeof(object)); ITypeDefOrRef objectTypeRef = _module.Import(typeof(object));
_holderTypeDef = new TypeDefUser("$Obfuz$ConstFieldHolder$", objectTypeRef); _holderTypeDef = new TypeDefUser($"$Obfuz$ConstFieldHolder${_holderTypeDefs.Count}", objectTypeRef);
_module.Types.Add(_holderTypeDef); _module.Types.Add(_holderTypeDef);
_holderTypeDefs.Add(_holderTypeDef); _holderTypeDefs.Add(_holderTypeDef);
_module.EnableTypeDefFindCache = true; _module.EnableTypeDefFindCache = true;

View File

@ -16,17 +16,14 @@ namespace Obfuz.ObfusPasses.CallObfus
public class CallObfusPass : BasicBlockObfuscationPassBase public class CallObfusPass : BasicBlockObfuscationPassBase
{ {
private readonly List<string> _configFiles; private readonly List<string> _configFiles;
private readonly IRandom _random; private IRandom _random;
private readonly IEncryptor _encryptor; private IEncryptor _encryptor;
private readonly IObfuscator _dynamicProxyObfuscator; private IObfuscator _dynamicProxyObfuscator;
private IObfuscationPolicy _dynamicProxyPolicy; private IObfuscationPolicy _dynamicProxyPolicy;
public CallObfusPass(CallObfusSettings settings) public CallObfusPass(CallObfusSettings settings)
{ {
_configFiles = settings.configFiles.ToList(); _configFiles = settings.configFiles.ToList();
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
_dynamicProxyObfuscator = new DefaultCallProxyObfuscator(_random, _encryptor);
} }
public override void Stop(ObfuscationPassContext ctx) public override void Stop(ObfuscationPassContext ctx)
@ -36,6 +33,9 @@ namespace Obfuz.ObfusPasses.CallObfus
public override void Start(ObfuscationPassContext ctx) public override void Start(ObfuscationPassContext ctx)
{ {
_random = ctx.random;
_encryptor = ctx.encryptor;
_dynamicProxyObfuscator = new DefaultCallProxyObfuscator(_random, _encryptor, ctx.constFieldAllocator);
_dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.toObfuscatedAssemblyNames, _configFiles); _dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
} }
@ -77,14 +77,14 @@ namespace Obfuz.ObfusPasses.CallObfus
default: return false; default: return false;
} }
ObfuscationCachePolicy cachePolicy = _dynamicProxyPolicy.GetMethodObfuscationCachePolicy(callerMethod); if (!_dynamicProxyPolicy.NeedObfuscateCalledMethod(callerMethod, calledMethod, callVir, block.inLoop))
bool cachedCallIndex = block.inLoop ? cachePolicy.cacheInLoop : cachePolicy.cacheNotInLoop;
if (!_dynamicProxyPolicy.NeedObfuscateCalledMethod(callerMethod, calledMethod, callVir, cachedCallIndex))
{ {
return false; return false;
} }
_dynamicProxyObfuscator.Obfuscate(callerMethod, calledMethod, callVir, outputInstructions);
ObfuscationCachePolicy cachePolicy = _dynamicProxyPolicy.GetMethodObfuscationCachePolicy(callerMethod);
bool cachedCallIndex = block.inLoop ? cachePolicy.cacheInLoop : cachePolicy.cacheNotInLoop;
_dynamicProxyObfuscator.Obfuscate(callerMethod, calledMethod, callVir, cachedCallIndex, outputInstructions);
return true; return true;
} }
} }

View File

@ -18,13 +18,15 @@ namespace Obfuz.ObfusPasses.CallObfus
public readonly int encryptOps; public readonly int encryptOps;
public readonly int salt; public readonly int salt;
public readonly int encryptedIndex; public readonly int encryptedIndex;
public readonly int index;
public ProxyCallMethodData(MethodDef proxyMethod, int encryptOps, int salt, int encryptedIndex) public ProxyCallMethodData(MethodDef proxyMethod, int encryptOps, int salt, int encryptedIndex, int index)
{ {
this.proxyMethod = proxyMethod; this.proxyMethod = proxyMethod;
this.encryptOps = encryptOps; this.encryptOps = encryptOps;
this.salt = salt; this.salt = salt;
this.encryptedIndex = encryptedIndex; this.encryptedIndex = encryptedIndex;
this.index = index;
} }
} }
@ -198,7 +200,7 @@ namespace Obfuz.ObfusPasses.CallObfus
methodDispatcher.methods.Add(new CallInfo { method = method, callVir = callVir}); methodDispatcher.methods.Add(new CallInfo { method = method, callVir = callVir});
_methodProxys.Add(key, proxyInfo); _methodProxys.Add(key, proxyInfo);
} }
return new ProxyCallMethodData(proxyInfo.proxyMethod, proxyInfo.encryptedOps, proxyInfo.salt, proxyInfo.encryptedIndex); return new ProxyCallMethodData(proxyInfo.proxyMethod, proxyInfo.encryptedOps, proxyInfo.salt, proxyInfo.encryptedIndex, proxyInfo.index);
} }
public void Done() public void Done()

View File

@ -3,6 +3,8 @@ using dnlib.DotNet;
using System.Collections.Generic; using System.Collections.Generic;
using Obfuz.Utils; using Obfuz.Utils;
using Obfuz.Emit; using Obfuz.Emit;
using Obfuz.Data;
using UnityEngine;
namespace Obfuz.ObfusPasses.CallObfus namespace Obfuz.ObfusPasses.CallObfus
{ {
@ -10,12 +12,14 @@ namespace Obfuz.ObfusPasses.CallObfus
{ {
private readonly IRandom _random; private readonly IRandom _random;
private readonly IEncryptor _encryptor; private readonly IEncryptor _encryptor;
private readonly ConstFieldAllocator _constFieldAllocator;
private readonly CallProxyAllocator _proxyCallAllocator; private readonly CallProxyAllocator _proxyCallAllocator;
public DefaultCallProxyObfuscator(IRandom random, IEncryptor encryptor) public DefaultCallProxyObfuscator(IRandom random, IEncryptor encryptor, ConstFieldAllocator constFieldAllocator)
{ {
_random = random; _random = random;
_encryptor = encryptor; _encryptor = encryptor;
_constFieldAllocator = constFieldAllocator;
_proxyCallAllocator = new CallProxyAllocator(random, _encryptor); _proxyCallAllocator = new CallProxyAllocator(random, _encryptor);
} }
@ -24,15 +28,25 @@ namespace Obfuz.ObfusPasses.CallObfus
_proxyCallAllocator.Done(); _proxyCallAllocator.Done();
} }
public override void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions) public override void Obfuscate(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions)
{ {
MethodSig sharedMethodSig = MetaUtil.ToSharedMethodSig(calledMethod.Module.CorLibTypes, MetaUtil.GetInflatedMethodSig(calledMethod)); MethodSig sharedMethodSig = MetaUtil.ToSharedMethodSig(calledMethod.Module.CorLibTypes, MetaUtil.GetInflatedMethodSig(calledMethod));
ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callingMethod.Module, calledMethod, callVir); ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callerMethod.Module, calledMethod, callVir);
DefaultModuleMetadataImporter importer = MetadataImporter.Instance.GetDefaultModuleMetadataImporter(callingMethod.Module); DefaultModuleMetadataImporter importer = MetadataImporter.Instance.GetDefaultModuleMetadataImporter(callerMethod.Module);
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptedIndex));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptOps)); if (needCacheCall)
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.salt)); {
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt)); FieldDef cacheField = _constFieldAllocator.Allocate(callerMethod.Module, proxyCallMethodData.index);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldsfld, cacheField));
}
else
{
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptedIndex));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.encryptOps));
obfuscatedInstructions.Add(Instruction.CreateLdcI4(proxyCallMethodData.salt));
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, importer.DecryptInt));
}
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, proxyCallMethodData.proxyMethod)); obfuscatedInstructions.Add(Instruction.Create(OpCodes.Call, proxyCallMethodData.proxyMethod));
} }
} }

View File

@ -10,7 +10,7 @@ namespace Obfuz.ObfusPasses.CallObfus
{ {
public interface IObfuscator public interface IObfuscator
{ {
void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions); void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions);
void Done(); void Done();
} }

View File

@ -6,7 +6,7 @@ namespace Obfuz.ObfusPasses.CallObfus
{ {
public abstract class ObfuscatorBase : IObfuscator public abstract class ObfuscatorBase : IObfuscator
{ {
public abstract void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, List<Instruction> obfuscatedInstructions); public abstract void Obfuscate(MethodDef callingMethod, IMethod calledMethod, bool callVir, bool needCacheCall, List<Instruction> obfuscatedInstructions);
public abstract void Done(); public abstract void Done();
} }
} }

View File

@ -27,7 +27,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
public override void Start(ObfuscationPassContext ctx) public override void Start(ObfuscationPassContext ctx)
{ {
_dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles); _dataObfuscatorPolicy = new ConfigurableEncryptPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
_dataObfuscator = new DefaultConstEncryptor(); _dataObfuscator = new DefaultConstEncryptor(ctx.random, ctx.encryptor, ctx.rvaDataAllocator, ctx.constFieldAllocator);
} }
public override void Stop(ObfuscationPassContext ctx) public override void Stop(ObfuscationPassContext ctx)

View File

@ -17,12 +17,12 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
private readonly ConstFieldAllocator _constFieldAllocator; private readonly ConstFieldAllocator _constFieldAllocator;
private readonly IEncryptor _encryptor; private readonly IEncryptor _encryptor;
public DefaultConstEncryptor() public DefaultConstEncryptor(IRandom random, IEncryptor encryptor, RvaDataAllocator rvaDataAllocator, ConstFieldAllocator constFieldAllocator)
{ {
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5); _random = random;
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D }); _encryptor = encryptor;
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor); _rvaDataAllocator = rvaDataAllocator;
_constFieldAllocator = new ConstFieldAllocator(_encryptor, _random, _rvaDataAllocator); _constFieldAllocator = constFieldAllocator;
} }
private int GenerateEncryptionOperations() private int GenerateEncryptionOperations()

View File

@ -1,4 +1,5 @@
using dnlib.DotNet; using dnlib.DotNet;
using Obfuz.Data;
using Obfuz.ObfusPasses.SymbolObfus; using Obfuz.ObfusPasses.SymbolObfus;
using Obfuz.Utils; using Obfuz.Utils;
using System; using System;
@ -21,5 +22,10 @@ namespace Obfuz
public List<string> notObfuscatedAssemblyNamesReferencingObfuscated; public List<string> notObfuscatedAssemblyNamesReferencingObfuscated;
public string obfuscatedAssemblyOutputDir; public string obfuscatedAssemblyOutputDir;
public IRandom random;
public IEncryptor encryptor;
public ConstFieldAllocator constFieldAllocator;
public RvaDataAllocator rvaDataAllocator;
} }
} }

View File

@ -1,4 +1,6 @@
using dnlib.DotNet; using dnlib.DotNet;
using dnlib.Protection;
using Obfuz.Data;
using Obfuz.Emit; using Obfuz.Emit;
using Obfuz.ObfusPasses; using Obfuz.ObfusPasses;
using Obfuz.Utils; using Obfuz.Utils;
@ -57,6 +59,11 @@ namespace Obfuz
{ {
LoadAssemblies(); LoadAssemblies();
var random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
var encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
var rvaDataAllocator = new RvaDataAllocator(random, encryptor);
var constFieldAllocator = new ConstFieldAllocator(encryptor, random, rvaDataAllocator);
_ctx = new ObfuscationPassContext _ctx = new ObfuscationPassContext
{ {
assemblyCache = _assemblyCache, assemblyCache = _assemblyCache,
@ -65,6 +72,11 @@ namespace Obfuz
toObfuscatedAssemblyNames = _toObfuscatedAssemblyNames, toObfuscatedAssemblyNames = _toObfuscatedAssemblyNames,
notObfuscatedAssemblyNamesReferencingObfuscated = _notObfuscatedAssemblyNamesReferencingObfuscated, notObfuscatedAssemblyNamesReferencingObfuscated = _notObfuscatedAssemblyNamesReferencingObfuscated,
obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir, obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir,
random = random,
encryptor = encryptor,
rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator,
}; };
_pipeline.Start(_ctx); _pipeline.Start(_ctx);
} }