RandomDataNodeCreator的叶节点可以为ConstDataNode或者ConstFromFieldRvaDataNode

backup
walon 2025-04-22 20:09:16 +08:00
parent f1b3bd3329
commit 3fded4273c
11 changed files with 74 additions and 27 deletions

View File

@ -15,7 +15,7 @@ namespace Obfuz.Virtualization
{ {
this.function = function; this.function = function;
this.inputs = inputs; this.inputs = inputs;
Type = function.ReturnType; Type = result.type;
this.result = result; this.result = result;
} }

View File

@ -20,9 +20,8 @@ namespace Obfuz.Virtualization
_rvaDataAllocator = new RvaDataAllocator(_random); _rvaDataAllocator = new RvaDataAllocator(_random);
} }
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions) private void CompileNode(IDataNode node, MethodDef method, List<Instruction> obfuscatedInstructions)
{ {
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value);
var ctx = new CompileContext var ctx = new CompileContext
{ {
method = method, method = method,
@ -30,21 +29,33 @@ namespace Obfuz.Virtualization
rvaDataAllocator = _rvaDataAllocator, rvaDataAllocator = _rvaDataAllocator,
}; };
node.Compile(ctx); node.Compile(ctx);
}
public void ObfuscateInt(MethodDef method, int value, List<Instruction> obfuscatedInstructions)
{
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int32, value);
CompileNode(node, method, obfuscatedInstructions);
//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) public void ObfuscateLong(MethodDef method, long value, List<Instruction> obfuscatedInstructions)
{ {
//IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int64, value);
//CompileNode(node, method, obfuscatedInstructions);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value)); obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value));
} }
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions) public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)
{ {
//IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float32, value);
//CompileNode(node, method, obfuscatedInstructions);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, value)); obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R4, value));
} }
public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions) public void ObfuscateDouble(MethodDef method, double value, List<Instruction> obfuscatedInstructions)
{ {
//IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Float64, value);
//CompileNode(node, method, obfuscatedInstructions);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, value)); obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_R8, value));
} }
@ -56,6 +67,8 @@ namespace Obfuz.Virtualization
public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions) public void ObfuscateString(MethodDef method, string value, List<Instruction> obfuscatedInstructions)
{ {
//IDataNode node = _nodeCreator.CreateRandom(DataNodeType.String, value);
//CompileNode(node, method, obfuscatedInstructions);
obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value)); obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldstr, value));
} }

View File

@ -0,0 +1,17 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Virtualization.Functions
{
public class ConstDataCreator : NodeCreatorBase
{
public override IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
{
return new ConstDataNode { Type = type, Value = value };
}
}
}

View File

@ -0,0 +1,17 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Virtualization.Functions
{
public class ConstFromFieldRvaDataCreator : NodeCreatorBase
{
public override IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
{
return new ConstFromFieldRvaDataNode { Type = type, Value = value };
}
}
}

View File

@ -8,7 +8,6 @@ namespace Obfuz.Virtualization
public abstract class FunctionBase : IFunction public abstract class FunctionBase : IFunction
{ {
public abstract DataNodeType ReturnType { get; }
public abstract void CreateArguments(DataNodeType type, object value, CreateExpressionOptions options, List<ConstValue> args); public abstract void CreateArguments(DataNodeType type, object value, CreateExpressionOptions options, List<ConstValue> args);
@ -23,7 +22,7 @@ namespace Obfuz.Virtualization
CompileSelf(ctx, ctx.output); CompileSelf(ctx, ctx.output);
} }
public ConstExpression CreateExpr(DataNodeType type, object value, CreateExpressionOptions options) public IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
{ {
var args = new List<ConstValue>(); var args = new List<ConstValue>();
CreateArguments(type, value, options, args); CreateArguments(type, value, options, args);

View File

@ -9,9 +9,8 @@ using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions namespace Obfuz.Virtualization.Functions
{ {
public class Int32FunctionAdd : Int32FunctionBase public class Int32FunctionAdd : FunctionBase
{ {
public override DataNodeType ReturnType => DataNodeType.Int32;
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args) public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{ {

View File

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Virtualization.Functions
{
public abstract class Int32FunctionBase : FunctionBase
{
public override DataNodeType ReturnType => DataNodeType.Int32;
}
}

View File

@ -9,9 +9,8 @@ using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions namespace Obfuz.Virtualization.Functions
{ {
public class Int32FunctionXor : Int32FunctionBase public class Int32FunctionXor : FunctionBase
{ {
public override DataNodeType ReturnType => DataNodeType.Int32;
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args) public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{ {

View File

@ -0,0 +1,14 @@
using System.Collections.Generic;
namespace Obfuz.Virtualization
{
public abstract class NodeCreatorBase : IFunction
{
void IFunction.Compile(CompileContext ctx, List<IDataNode> inputs, ConstValue result)
{
throw new System.NotSupportedException("This function is not supported in this context.");
}
public abstract IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options);
}
}

View File

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

View File

@ -18,6 +18,8 @@ namespace Obfuz.Virtualization
{ {
new Int32FunctionAdd(), new Int32FunctionAdd(),
new Int32FunctionXor(), new Int32FunctionXor(),
//new ConstFromFieldRvaDataCreator(),
//new ConstDataCreator(),
}; };
_functions.Add(DataNodeType.Int32, int32Funcs); _functions.Add(DataNodeType.Int32, int32Funcs);
} }
@ -28,10 +30,12 @@ namespace Obfuz.Virtualization
{ {
throw new System.Exception($"No functions available for type {type}"); throw new System.Exception($"No functions available for type {type}");
} }
if (options.depth >= 2) if (options.depth >= 4)
{ {
//return new ConstDataNode() { Type = type, Value = value }; //return new ConstDataNode() { Type = type, Value = value };
return new ConstFromFieldRvaDataNode() { Type = type, Value = value }; return _random.NextInt(100) < 50 ?
new ConstFromFieldRvaDataNode() { Type = type, Value = value } :
new ConstDataNode() { Type = type, Value = value };
} }
var func = funcs[options.random.NextInt(funcs.Count)]; var func = funcs[options.random.NextInt(funcs.Count)];
++options.depth; ++options.depth;