2025-05-21 09:23:29 +08:00
|
|
|
|
using dnlib.DotNet;
|
|
|
|
|
using dnlib.DotNet.Emit;
|
2025-06-20 16:56:14 +08:00
|
|
|
|
using Obfuz.Data;
|
2025-06-17 20:21:28 +08:00
|
|
|
|
using Obfuz.Emit;
|
|
|
|
|
using Obfuz.ObfusPasses.ExprObfus.Obfuscators;
|
|
|
|
|
using Obfuz.Settings;
|
|
|
|
|
using Obfuz.Utils;
|
2025-05-21 09:23:29 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Obfuz.ObfusPasses.ExprObfus
|
|
|
|
|
{
|
2025-06-20 16:56:14 +08:00
|
|
|
|
class ObfusMethodContext
|
|
|
|
|
{
|
|
|
|
|
public MethodDef method;
|
|
|
|
|
public EvalStackCalculator evalStackCalculator;
|
|
|
|
|
public LocalVariableAllocator localVariableAllocator;
|
|
|
|
|
public IRandom localRandom;
|
|
|
|
|
public EncryptionScopeInfo encryptionScope;
|
|
|
|
|
public DefaultMetadataImporter importer;
|
|
|
|
|
public ModuleConstFieldAllocator constFieldAllocator;
|
2025-06-20 17:34:25 +08:00
|
|
|
|
public float obfuscationPercentage;
|
2025-06-20 16:56:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-17 20:21:28 +08:00
|
|
|
|
class ExprObfusPass : ObfuscationMethodPassBase
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-06-17 20:21:28 +08:00
|
|
|
|
private readonly ExprObfuscationSettingsFacade _settings;
|
2025-06-20 17:34:25 +08:00
|
|
|
|
private readonly IObfuscator _basicObfuscator;
|
|
|
|
|
private readonly IObfuscator _advancedObfuscator;
|
|
|
|
|
private readonly IObfuscator _mostAdvancedObfuscator;
|
|
|
|
|
|
2025-06-17 20:21:28 +08:00
|
|
|
|
private IObfuscationPolicy _obfuscationPolicy;
|
|
|
|
|
|
|
|
|
|
public ExprObfusPass(ExprObfuscationSettingsFacade settings)
|
|
|
|
|
{
|
|
|
|
|
_settings = settings;
|
2025-06-20 17:34:25 +08:00
|
|
|
|
_basicObfuscator = new BasicObfuscator();
|
|
|
|
|
_advancedObfuscator = new AdvancedObfuscator();
|
|
|
|
|
_mostAdvancedObfuscator = new MostAdvancedObfuscator();
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 09:23:29 +08:00
|
|
|
|
public override ObfuscationPassType Type => ObfuscationPassType.ExprObfus;
|
|
|
|
|
|
|
|
|
|
public override void Start()
|
|
|
|
|
{
|
2025-06-17 20:21:28 +08:00
|
|
|
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
|
|
|
|
_obfuscationPolicy = new ConfigurableObfuscationPolicy(
|
|
|
|
|
ctx.coreSettings.assembliesToObfuscate,
|
|
|
|
|
_settings.ruleFiles);
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
|
2025-06-20 17:34:25 +08:00
|
|
|
|
private IObfuscator GetObfuscator(ObfuscationLevel level)
|
2025-06-17 20:21:28 +08:00
|
|
|
|
{
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
case ObfuscationLevel.None: return null;
|
|
|
|
|
case ObfuscationLevel.Basic: return _basicObfuscator;
|
|
|
|
|
case ObfuscationLevel.Advanced: return _advancedObfuscator;
|
|
|
|
|
case ObfuscationLevel.MostAdvanced: return _mostAdvancedObfuscator;
|
2025-06-17 20:21:28 +08:00
|
|
|
|
default: throw new System.ArgumentOutOfRangeException(nameof(level), level, "Unknown obfuscation level");
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Stop()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool NeedObfuscateMethod(MethodDef method)
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return _obfuscationPolicy.NeedObfuscate(method);
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-22 10:39:31 +08:00
|
|
|
|
protected bool TryObfuscateInstruction(IObfuscator obfuscator, InstructionParameterInfo pi, Instruction inst, List<Instruction> outputInstructions, ObfusMethodContext ctx)
|
2025-05-21 09:23:29 +08:00
|
|
|
|
{
|
2025-06-20 16:56:14 +08:00
|
|
|
|
//Debug.Log($"Obfuscating instruction: {inst} in method: {ctx.method.FullName}");
|
2025-06-20 17:34:25 +08:00
|
|
|
|
IRandom localRandom = ctx.localRandom;
|
|
|
|
|
float obfuscationPercentage = ctx.obfuscationPercentage;
|
2025-06-17 20:21:28 +08:00
|
|
|
|
switch (inst.OpCode.Code)
|
|
|
|
|
{
|
|
|
|
|
case Code.Neg:
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return localRandom.NextInPercentage(obfuscationPercentage) && obfuscator.ObfuscateBasicUnaryOp(inst, pi.op1, pi.retType, outputInstructions, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
case Code.Add:
|
|
|
|
|
case Code.Sub:
|
|
|
|
|
case Code.Mul:
|
|
|
|
|
case Code.Div:
|
|
|
|
|
case Code.Div_Un:
|
|
|
|
|
case Code.Rem:
|
|
|
|
|
case Code.Rem_Un:
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return localRandom.NextInPercentage(obfuscationPercentage) && obfuscator.ObfuscateBasicBinOp(inst, pi.op1, pi.op2, pi.retType, outputInstructions, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
case Code.And:
|
|
|
|
|
case Code.Or:
|
|
|
|
|
case Code.Xor:
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return localRandom.NextInPercentage(obfuscationPercentage) && obfuscator.ObfuscateBinBitwiseOp(inst, pi.op1, pi.op2, pi.retType, outputInstructions, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
case Code.Not:
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return localRandom.NextInPercentage(obfuscationPercentage) && obfuscator.ObfuscateUnaryBitwiseOp(inst, pi.op1, pi.retType, outputInstructions, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
case Code.Shl:
|
|
|
|
|
case Code.Shr:
|
|
|
|
|
case Code.Shr_Un:
|
|
|
|
|
{
|
2025-06-20 17:34:25 +08:00
|
|
|
|
return localRandom.NextInPercentage(obfuscationPercentage) && obfuscator.ObfuscateBitShiftOp(inst, pi.op1, pi.op2, pi.retType, outputInstructions, ctx);
|
2025-06-17 20:21:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-06-17 20:21:28 +08:00
|
|
|
|
|
|
|
|
|
protected override void ObfuscateData(MethodDef method)
|
|
|
|
|
{
|
2025-06-21 10:59:39 +08:00
|
|
|
|
//Debug.Log($"Obfuscating method: {method.FullName} with ExprObfusPass");
|
2025-06-17 20:21:28 +08:00
|
|
|
|
IList<Instruction> instructions = method.Body.Instructions;
|
|
|
|
|
var outputInstructions = new List<Instruction>();
|
|
|
|
|
var totalFinalInstructions = new List<Instruction>();
|
2025-06-20 16:56:14 +08:00
|
|
|
|
|
|
|
|
|
ObfuscationPassContext ctx = ObfuscationPassContext.Current;
|
|
|
|
|
var calc = new EvalStackCalculator(method);
|
|
|
|
|
var encryptionScope = ctx.encryptionScopeProvider.GetScope(method.Module);
|
2025-06-20 17:34:25 +08:00
|
|
|
|
var ruleData = _obfuscationPolicy.GetObfuscationRuleData(method);
|
|
|
|
|
var obfuscator = GetObfuscator(ruleData.obfuscationLevel);
|
2025-06-20 16:56:14 +08:00
|
|
|
|
var obfusMethodCtx = new ObfusMethodContext
|
|
|
|
|
{
|
|
|
|
|
method = method,
|
|
|
|
|
evalStackCalculator = calc,
|
|
|
|
|
localVariableAllocator = new LocalVariableAllocator(method),
|
|
|
|
|
encryptionScope = encryptionScope,
|
2025-06-20 17:34:25 +08:00
|
|
|
|
constFieldAllocator = ctx.constFieldAllocator.GetModuleAllocator(method.Module),
|
2025-06-20 16:56:14 +08:00
|
|
|
|
localRandom = encryptionScope.localRandomCreator(MethodEqualityComparer.CompareDeclaringTypes.GetHashCode(method)),
|
|
|
|
|
importer = ctx.moduleEntityManager.GetDefaultModuleMetadataImporter(method.Module, ctx.encryptionScopeProvider),
|
2025-06-20 17:34:25 +08:00
|
|
|
|
obfuscationPercentage = ruleData.obfuscationPercentage,
|
2025-06-20 16:56:14 +08:00
|
|
|
|
};
|
2025-06-17 20:21:28 +08:00
|
|
|
|
for (int i = 0; i < instructions.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
Instruction inst = instructions[i];
|
|
|
|
|
bool add = false;
|
|
|
|
|
if (calc.TryGetParameterInfo(inst, out InstructionParameterInfo pi))
|
|
|
|
|
{
|
|
|
|
|
outputInstructions.Clear();
|
2025-06-20 17:34:25 +08:00
|
|
|
|
if (TryObfuscateInstruction(obfuscator, pi, inst, outputInstructions, obfusMethodCtx))
|
2025-06-17 20:21:28 +08:00
|
|
|
|
{
|
|
|
|
|
// current instruction may be the target of control flow instruction, so we can't remove it directly.
|
|
|
|
|
// we replace it with nop now, then remove it in CleanUpInstructionPass
|
|
|
|
|
inst.OpCode = outputInstructions[0].OpCode;
|
|
|
|
|
inst.Operand = outputInstructions[0].Operand;
|
|
|
|
|
totalFinalInstructions.Add(inst);
|
|
|
|
|
for (int k = 1; k < outputInstructions.Count; k++)
|
|
|
|
|
{
|
|
|
|
|
totalFinalInstructions.Add(outputInstructions[k]);
|
|
|
|
|
}
|
|
|
|
|
add = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!add)
|
|
|
|
|
{
|
|
|
|
|
totalFinalInstructions.Add(inst);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
instructions.Clear();
|
|
|
|
|
foreach (var obInst in totalFinalInstructions)
|
|
|
|
|
{
|
|
|
|
|
instructions.Add(obInst);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-21 09:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|