常量混淆

backup
walon 2025-04-22 10:42:58 +08:00
parent 945c6a074c
commit b19959488f
11 changed files with 60 additions and 49 deletions

View File

@ -11,11 +11,6 @@ namespace Obfuz
public class CleanUpInstructionPass : ObfuscationPassBase
{
public override void Process(ObfuscatorContext ctx)
{
// TODO remove all nop instructions
}
public override void Start(ObfuscatorContext ctx)
{
foreach (var ass in ctx.assemblies)
{
@ -29,12 +24,17 @@ namespace Obfuz
body.SimplifyBranches();
body.OptimizeMacros();
body.OptimizeBranches();
// TODO remove dup
}
}
}
}
}
public override void Start(ObfuscatorContext ctx)
{
}
public override void Stop(ObfuscatorContext ctx)
{

View File

@ -8,6 +8,6 @@ namespace Obfuz.Virtualization
public class CompileContext
{
public MethodDef method;
public List<Instruction> obfuscatedInstructions;
public List<Instruction> output;
}
}

View File

@ -1,4 +1,5 @@
using System;
using dnlib.DotNet.Emit;
using System;
namespace Obfuz.Virtualization
{
@ -17,48 +18,18 @@ namespace Obfuz.Virtualization
// so we only need to deal int32 and int64
switch (Type)
{
//case DataNodeType.Byte:
//{
// // create ldloc.i4
// break;
//}
case DataNodeType.Int32:
{
// create ldloc.i4
ctx.output.Add(Instruction.CreateLdcI4(IntValue));
break;
}
case DataNodeType.Int64:
{
// create ldloc.i8
ctx.output.Add(Instruction.Create(OpCodes.Ldc_I8, LongValue));
break;
}
//case DataNodeType.Float32:
//{
// // create ldloc.r4
// break;
//}
//case DataNodeType.Float64:
//{
// // create ldloc.r8
// break;
//}
//case DataNodeType.Null:
//{
// // create ldnull
// break;
//}
//case DataNodeType.String:
//{
// // create ldstr
// break;
//}
//case DataNodeType.Bytes:
//{
// // create ldstr
// // RuntimeHelpers.InitializeArray(array, fieldHandle);
// break;
//}
default:
{
throw new NotImplementedException($"Type:{Type} not implemented");

View File

@ -21,7 +21,7 @@ namespace Obfuz.Virtualization
public override void Compile(CompileContext ctx)
{
function.Compile(ctx, inputs, result);
}
}
}

View File

@ -6,6 +6,18 @@
public abstract object Value { get; set; }
public int IntValue => (int)Value;
public long LongValue => (long) Value;
public float FloatValue => (float)Value;
public double DoubleValue => (double)Value;
public string StringValue => (string)Value;
public byte[] BytesValue => (byte[])Value;
public abstract void Compile(CompileContext ctx);
}

View File

@ -15,10 +15,10 @@ namespace Obfuz.Virtualization
var ctx = new CompileContext
{
method = method,
obfuscatedInstructions = obfuscatedInstructions,
output = obfuscatedInstructions,
};
node.Compile(ctx);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value));
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I4, value));
}
public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)

View File

@ -1,4 +1,5 @@
using NUnit.Framework;
using dnlib.DotNet.Emit;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
@ -11,7 +12,18 @@ namespace Obfuz.Virtualization
public abstract void CreateArguments(DataNodeType type, object value, CreateExpressionOptions options, List<ConstValue> args);
public ConstExpression CreateCallable(DataNodeType type, object value, CreateExpressionOptions options)
public abstract void CompileSelf(CompileContext ctx, List<Instruction> output);
public virtual void Compile(CompileContext ctx, List<IDataNode> inputs, ConstValue result)
{
foreach (var input in inputs)
{
input.Compile(ctx);
}
CompileSelf(ctx, ctx.output);
}
public ConstExpression CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
{
var args = new List<ConstValue>();
CreateArguments(type, value, options, args);

View File

@ -1,4 +1,5 @@
using System;
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
@ -21,5 +22,10 @@ namespace Obfuz.Virtualization.Functions
args.Add(new ConstValue(DataNodeType.Int32, op1));
args.Add(new ConstValue(DataNodeType.Int32, op2));
}
public override void CompileSelf(CompileContext ctx, List<Instruction> output)
{
output.Add(Instruction.Create(OpCodes.Add));
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
@ -21,5 +22,10 @@ namespace Obfuz.Virtualization.Functions
args.Add(new ConstValue(DataNodeType.Int32, op1));
args.Add(new ConstValue(DataNodeType.Int32, op2));
}
public override void CompileSelf(CompileContext ctx, List<Instruction> output)
{
output.Add(Instruction.Create(OpCodes.And));
}
}
}

View File

@ -1,9 +1,13 @@
namespace Obfuz.Virtualization
using System.Collections.Generic;
namespace Obfuz.Virtualization
{
public interface IFunction
{
DataNodeType ReturnType { get; }
ConstExpression CreateCallable(DataNodeType type, object value, CreateExpressionOptions options);
ConstExpression CreateExpr(DataNodeType type, object value, CreateExpressionOptions options);
void Compile(CompileContext ctx, List<IDataNode> inputs, ConstValue result);
}
}

View File

@ -33,7 +33,7 @@ namespace Obfuz.Virtualization
}
var func = funcs[options.random.NextInt(funcs.Count)];
++options.depth;
return func.CreateCallable(type, value, options);
return func.CreateExpr(type, value, options);
}
public IDataNode CreateRandom(DataNodeType type, object value)