obfuz/Editor/ProxyCall/DynamicProxyPass.cs

85 lines
2.8 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;
2025-04-24 11:58:22 +08:00
using Obfuz.Virtualization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using UnityEngine.Assertions;
namespace Obfuz.DynamicProxy
{
public class DynamicProxyPass : MethodBodyObfuscationPassBase
{
2025-04-28 11:37:48 +08:00
private readonly IRandom _random;
2025-04-24 11:58:22 +08:00
private readonly IDynamicProxyPolicy _dynamicProxyPolicy;
private readonly IDynamicProxyObfuscator _dynamicProxyObfuscator;
public DynamicProxyPass()
{
2025-04-28 11:37:48 +08:00
_random = new RandomWithKey(new byte[] { 0x1, 0x2, 0x3, 0x4 }, 0x5);
2025-04-24 11:58:22 +08:00
_dynamicProxyPolicy = new ConfigDynamicProxyPolicy();
2025-04-28 11:37:48 +08:00
_dynamicProxyObfuscator = new DefaultDynamicProxyObfuscator(_random);
2025-04-24 11:58:22 +08:00
}
public override void Stop(ObfuscatorContext ctx)
{
2025-04-28 11:37:48 +08:00
_dynamicProxyObfuscator.Done();
2025-04-24 11:58:22 +08:00
}
public override void Start(ObfuscatorContext ctx)
{
}
protected override bool NeedObfuscateMethod(MethodDef method)
{
return _dynamicProxyPolicy.NeedDynamicProxyCallInMethod(method);
}
protected override bool TryObfuscateInstruction(MethodDef method, Instruction inst, IList<Instruction> instructions, int instructionIndex,
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-04-24 11:58:22 +08:00
switch (inst.OpCode.Code)
{
case Code.Call:
{
if (!_dynamicProxyPolicy.NeedDynamicProxyCalledMethod(calledMethod, false))
{
return false;
}
_dynamicProxyObfuscator.Obfuscate(method, calledMethod, false, outputInstructions);
2025-04-24 11:58:22 +08:00
return true;
}
case Code.Callvirt:
{
if (instructionIndex > 0 && instructions[instructionIndex - 1].OpCode.Code == Code.Constrained)
{
return false;
}
if (!_dynamicProxyPolicy.NeedDynamicProxyCalledMethod(calledMethod, true))
{
return false;
}
_dynamicProxyObfuscator.Obfuscate(method, calledMethod, true, outputInstructions);
2025-04-24 11:58:22 +08:00
return true;
}
default: return false;
}
}
}
}