CleanUpInstructionPass中优化branch和macro

backup
walon 2025-04-22 08:58:00 +08:00
parent 289e38f827
commit 945c6a074c
5 changed files with 42 additions and 10 deletions

View File

@ -1,4 +1,6 @@
using System; using dnlib.DotNet;
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -15,7 +17,22 @@ namespace Obfuz
public override void Start(ObfuscatorContext ctx) public override void Start(ObfuscatorContext ctx)
{ {
foreach (var ass in ctx.assemblies)
{
foreach (TypeDef type in ass.module.GetTypes())
{
foreach (MethodDef method in type.Methods)
{
if (method.HasBody)
{
CilBody body = method.Body;
body.SimplifyBranches();
body.OptimizeMacros();
body.OptimizeBranches();
}
}
}
}
} }
public override void Stop(ObfuscatorContext ctx) public override void Stop(ObfuscatorContext ctx)

View File

@ -1,7 +1,13 @@
namespace Obfuz.Virtualization using dnlib.DotNet;
using dnlib.DotNet.Emit;
using NUnit.Framework;
using System.Collections.Generic;
namespace Obfuz.Virtualization
{ {
public class CompileContext public class CompileContext
{ {
public MethodDef method;
public List<Instruction> obfuscatedInstructions;
} }
} }

View File

@ -16,12 +16,12 @@ namespace Obfuz.Virtualization
public override bool NeedObfuscateInt(MethodDef method, int value) public override bool NeedObfuscateInt(MethodDef method, int value)
{ {
return value > 10000; return value > 10000 || value < -10000;
} }
public override bool NeedObfuscateLong(MethodDef method, long value) public override bool NeedObfuscateLong(MethodDef method, long value)
{ {
return value > 10000; return value > 10000 || value < -10000;
} }
public override bool NeedObfuscateFloat(MethodDef method, float value) public override bool NeedObfuscateFloat(MethodDef method, float value)

View File

@ -137,9 +137,12 @@ namespace Obfuz.Virtualization
{ {
// current instruction may be the target of control flow instruction, so we can't remove it directly. // 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 // we replace it with nop now, then remove it in CleanUpInstructionPass
inst.OpCode = OpCodes.Nop; inst.OpCode = obfuscatedInstructions[0].OpCode;
inst.Operand = null; inst.Operand = obfuscatedInstructions[0].Operand;
resultInstructions.AddRange(obfuscatedInstructions); for (int k = 1; k < obfuscatedInstructions.Count; k++)
{
resultInstructions.Add(obfuscatedInstructions[k]);
}
} }
} }

View File

@ -12,7 +12,13 @@ namespace Obfuz.Virtualization
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions) public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions)
{ {
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value); IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value);
obfuscatedInstructions.Add(Instruction.CreateLdcI4(value)); var ctx = new CompileContext
{
method = method,
obfuscatedInstructions = obfuscatedInstructions,
};
node.Compile(ctx);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value));
} }
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions) public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)