支持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;
ITypeDefOrRef objectTypeRef = _module.Import(typeof(object));
_holderTypeDef = new TypeDefUser("$Obfuz$ConstFieldHolder$", objectTypeRef);
_holderTypeDef = new TypeDefUser($"$Obfuz$ConstFieldHolder${_holderTypeDefs.Count}", objectTypeRef);
_module.Types.Add(_holderTypeDef);
_holderTypeDefs.Add(_holderTypeDef);
_module.EnableTypeDefFindCache = true;

View File

@ -16,17 +16,14 @@ namespace Obfuz.ObfusPasses.CallObfus
public class CallObfusPass : BasicBlockObfuscationPassBase
{
private readonly List<string> _configFiles;
private readonly IRandom _random;
private readonly IEncryptor _encryptor;
private readonly IObfuscator _dynamicProxyObfuscator;
private IRandom _random;
private IEncryptor _encryptor;
private IObfuscator _dynamicProxyObfuscator;
private IObfuscationPolicy _dynamicProxyPolicy;
public CallObfusPass(CallObfusSettings settings)
{
_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)
@ -36,6 +33,9 @@ namespace Obfuz.ObfusPasses.CallObfus
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);
}
@ -77,14 +77,14 @@ namespace Obfuz.ObfusPasses.CallObfus
default: return false;
}
ObfuscationCachePolicy cachePolicy = _dynamicProxyPolicy.GetMethodObfuscationCachePolicy(callerMethod);
bool cachedCallIndex = block.inLoop ? cachePolicy.cacheInLoop : cachePolicy.cacheNotInLoop;
if (!_dynamicProxyPolicy.NeedObfuscateCalledMethod(callerMethod, calledMethod, callVir, cachedCallIndex))
if (!_dynamicProxyPolicy.NeedObfuscateCalledMethod(callerMethod, calledMethod, callVir, block.inLoop))
{
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;
}
}

View File

@ -18,13 +18,15 @@ namespace Obfuz.ObfusPasses.CallObfus
public readonly int encryptOps;
public readonly int salt;
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.encryptOps = encryptOps;
this.salt = salt;
this.encryptedIndex = encryptedIndex;
this.index = index;
}
}
@ -198,7 +200,7 @@ namespace Obfuz.ObfusPasses.CallObfus
methodDispatcher.methods.Add(new CallInfo { method = method, callVir = callVir});
_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()

View File

@ -3,6 +3,8 @@ using dnlib.DotNet;
using System.Collections.Generic;
using Obfuz.Utils;
using Obfuz.Emit;
using Obfuz.Data;
using UnityEngine;
namespace Obfuz.ObfusPasses.CallObfus
{
@ -10,12 +12,14 @@ namespace Obfuz.ObfusPasses.CallObfus
{
private readonly IRandom _random;
private readonly IEncryptor _encryptor;
private readonly ConstFieldAllocator _constFieldAllocator;
private readonly CallProxyAllocator _proxyCallAllocator;
public DefaultCallProxyObfuscator(IRandom random, IEncryptor encryptor)
public DefaultCallProxyObfuscator(IRandom random, IEncryptor encryptor, ConstFieldAllocator constFieldAllocator)
{
_random = random;
_encryptor = encryptor;
_constFieldAllocator = constFieldAllocator;
_proxyCallAllocator = new CallProxyAllocator(random, _encryptor);
}
@ -24,15 +28,25 @@ namespace Obfuz.ObfusPasses.CallObfus
_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));
ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callingMethod.Module, calledMethod, callVir);
DefaultModuleMetadataImporter importer = MetadataImporter.Instance.GetDefaultModuleMetadataImporter(callingMethod.Module);
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));
ProxyCallMethodData proxyCallMethodData = _proxyCallAllocator.Allocate(callerMethod.Module, calledMethod, callVir);
DefaultModuleMetadataImporter importer = MetadataImporter.Instance.GetDefaultModuleMetadataImporter(callerMethod.Module);
if (needCacheCall)
{
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));
}
}

View File

@ -10,7 +10,7 @@ namespace Obfuz.ObfusPasses.CallObfus
{
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();
}

View File

@ -6,7 +6,7 @@ namespace Obfuz.ObfusPasses.CallObfus
{
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();
}
}

View File

@ -27,7 +27,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
public override void Start(ObfuscationPassContext ctx)
{
_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)

View File

@ -17,12 +17,12 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
private readonly ConstFieldAllocator _constFieldAllocator;
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);
_encryptor = new DefaultEncryptor(new byte[] { 0x1A, 0x2B, 0x3C, 0x4D });
_rvaDataAllocator = new RvaDataAllocator(_random, _encryptor);
_constFieldAllocator = new ConstFieldAllocator(_encryptor, _random, _rvaDataAllocator);
_random = random;
_encryptor = encryptor;
_rvaDataAllocator = rvaDataAllocator;
_constFieldAllocator = constFieldAllocator;
}
private int GenerateEncryptionOperations()

View File

@ -1,4 +1,5 @@
using dnlib.DotNet;
using Obfuz.Data;
using Obfuz.ObfusPasses.SymbolObfus;
using Obfuz.Utils;
using System;
@ -21,5 +22,10 @@ namespace Obfuz
public List<string> notObfuscatedAssemblyNamesReferencingObfuscated;
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.Protection;
using Obfuz.Data;
using Obfuz.Emit;
using Obfuz.ObfusPasses;
using Obfuz.Utils;
@ -57,6 +59,11 @@ namespace Obfuz
{
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
{
assemblyCache = _assemblyCache,
@ -65,6 +72,11 @@ namespace Obfuz
toObfuscatedAssemblyNames = _toObfuscatedAssemblyNames,
notObfuscatedAssemblyNamesReferencingObfuscated = _notObfuscatedAssemblyNamesReferencingObfuscated,
obfuscatedAssemblyOutputDir = _obfuscatedAssemblyOutputDir,
random = random,
encryptor = encryptor,
rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator,
};
_pipeline.Start(_ctx);
}