obfuz/Editor/ObfusPasses/CallObfus/CallObfusPass.cs

96 lines
3.3 KiB
C#
Raw Normal View History

2025-04-24 11:58:22 +08:00
using dnlib.DotNet;
using dnlib.DotNet.Emit;
2025-04-28 11:37:48 +08:00
using Obfuz.Utils;
using Obfuz.Emit;
2025-04-24 11:58:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.Assertions;
2025-05-09 20:18:24 +08:00
using Obfuz.Settings;
2025-04-24 11:58:22 +08:00
2025-05-04 19:55:10 +08:00
namespace Obfuz.ObfusPasses.CallObfus
2025-04-24 11:58:22 +08:00
{
2025-05-10 09:41:45 +08:00
public class CallObfusPass : BasicBlockObfuscationPassBase
2025-04-24 11:58:22 +08:00
{
2025-05-10 09:41:45 +08:00
private readonly List<string> _configFiles;
private readonly int _obfuscationLevel;
2025-05-10 11:25:07 +08:00
private IRandom _random;
private IEncryptor _encryptor;
private IObfuscator _dynamicProxyObfuscator;
2025-05-10 09:41:45 +08:00
private IObfuscationPolicy _dynamicProxyPolicy;
2025-04-24 11:58:22 +08:00
2025-05-12 22:01:35 +08:00
public override ObfuscationPassType Type => ObfuscationPassType.CallObfus;
2025-05-09 20:18:24 +08:00
public CallObfusPass(CallObfusSettings settings)
2025-04-24 11:58:22 +08:00
{
2025-05-10 09:41:45 +08:00
_configFiles = settings.configFiles.ToList();
_obfuscationLevel = settings.callObfuscationLevel;
2025-04-24 11:58:22 +08:00
}
2025-05-04 19:24:14 +08:00
public override void Stop(ObfuscationPassContext ctx)
2025-04-24 11:58:22 +08:00
{
2025-04-28 11:37:48 +08:00
_dynamicProxyObfuscator.Done();
2025-04-24 11:58:22 +08:00
}
2025-05-04 19:24:14 +08:00
public override void Start(ObfuscationPassContext ctx)
2025-04-24 11:58:22 +08:00
{
2025-05-10 11:25:07 +08:00
_random = ctx.random;
_encryptor = ctx.encryptor;
_dynamicProxyObfuscator = new DefaultCallProxyObfuscator(_random, _encryptor, ctx.constFieldAllocator, _obfuscationLevel);
2025-05-10 09:41:45 +08:00
_dynamicProxyPolicy = new ConfigurableObfuscationPolicy(ctx.toObfuscatedAssemblyNames, _configFiles);
2025-04-24 11:58:22 +08:00
}
protected override bool NeedObfuscateMethod(MethodDef method)
{
2025-05-10 11:04:25 +08:00
return _dynamicProxyPolicy.NeedObfuscateCallInMethod(method);
2025-04-24 11:58:22 +08:00
}
2025-05-10 09:41:45 +08:00
protected override bool TryObfuscateInstruction(MethodDef callerMethod, Instruction inst, BasicBlock block,
int instructionIndex, IList<Instruction> globalInstructions, List<Instruction> outputInstructions, List<Instruction> totalFinalInstructions)
2025-04-24 11:58:22 +08:00
{
2025-04-28 11:37:48 +08:00
IMethod calledMethod = inst.Operand as IMethod;
if (calledMethod == null || !calledMethod.IsMethod)
{
return false;
}
if (MetaUtil.ContainsContainsGenericParameter(calledMethod))
{
return false;
}
2025-05-10 09:41:45 +08:00
bool callVir;
2025-04-24 11:58:22 +08:00
switch (inst.OpCode.Code)
{
case Code.Call:
{
2025-05-10 09:41:45 +08:00
callVir = false;
break;
2025-04-24 11:58:22 +08:00
}
case Code.Callvirt:
{
2025-05-10 09:41:45 +08:00
if (instructionIndex > 0 && globalInstructions[instructionIndex - 1].OpCode.Code == Code.Constrained)
2025-04-24 11:58:22 +08:00
{
return false;
}
2025-05-10 09:41:45 +08:00
callVir = true;
break;
2025-04-24 11:58:22 +08:00
}
default: return false;
}
2025-05-10 09:41:45 +08:00
2025-05-10 11:25:07 +08:00
if (!_dynamicProxyPolicy.NeedObfuscateCalledMethod(callerMethod, calledMethod, callVir, block.inLoop))
2025-05-10 09:41:45 +08:00
{
return false;
}
2025-05-10 11:25:07 +08:00
ObfuscationCachePolicy cachePolicy = _dynamicProxyPolicy.GetMethodObfuscationCachePolicy(callerMethod);
bool cachedCallIndex = block.inLoop ? cachePolicy.cacheInLoop : cachePolicy.cacheNotInLoop;
_dynamicProxyObfuscator.Obfuscate(callerMethod, calledMethod, callVir, cachedCallIndex, outputInstructions);
2025-05-10 09:41:45 +08:00
return true;
2025-04-24 11:58:22 +08:00
}
}
}