新增 IntRotateShift

backup
walon 2025-04-23 10:28:27 +08:00
parent 6d786330d1
commit f23ea3a46c
10 changed files with 187 additions and 69 deletions

View File

@ -7,5 +7,7 @@
int NextInt(int max);
int NextInt();
long NextLong();
}
}

View File

@ -54,5 +54,10 @@ namespace Obfuz.Utils
{
return _random.Next() ^ GetNextKeyByte();
}
public long NextLong()
{
return ((long)NextInt() << 32) | (uint)NextInt();
}
}
}

View File

@ -40,9 +40,9 @@ namespace Obfuz.Virtualization
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));
IDataNode node = _nodeCreator.CreateRandom(DataNodeType.Int64, value);
CompileNode(node, method, obfuscatedInstructions);
//obfuscatedInstructions.Add(Instruction.Create(OpCodes.Ldc_I8, value));
}
public void ObfuscateFloat(MethodDef method, float value, List<Instruction> obfuscatedInstructions)

View File

@ -22,7 +22,7 @@ namespace Obfuz.Virtualization
CompileSelf(ctx, ctx.output);
}
public IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
public virtual IDataNode CreateExpr(DataNodeType type, object value, CreateExpressionOptions options)
{
var args = new List<ConstValue>();
CreateArguments(type, value, options, args);

View File

@ -1,30 +0,0 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class Int32FunctionAdd : FunctionBase
{
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
int value = (int)v;
int op1 = options.random.NextInt();
int op2 = value - op1;
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,30 +0,0 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class Int32FunctionXor : FunctionBase
{
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
int value = (int)v;
int op1 = options.random.NextInt();
int op2 = value ^ op1;
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.Xor));
}
}
}

View File

@ -0,0 +1,50 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class IntAdd : FunctionBase
{
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
switch (type)
{
case DataNodeType.Int32:
{
int value = (int)v;
int op1 = options.random.NextInt();
int op2 = value - op1;
args.Add(new ConstValue(DataNodeType.Int32, op1));
args.Add(new ConstValue(DataNodeType.Int32, op2));
break;
}
case DataNodeType.Int64:
{
long value = (long)v;
long op1 = options.random.NextLong();
long op2 = value - op1;
args.Add(new ConstValue(DataNodeType.Int64, op1));
args.Add(new ConstValue(DataNodeType.Int64, op2));
break;
}
default:
{
throw new NotSupportedException($"Unsupported type: {type}");
}
}
}
public override void CompileSelf(CompileContext ctx, List<Instruction> output)
{
output.Add(Instruction.Create(OpCodes.Add));
}
}
}

View File

@ -0,0 +1,73 @@
using dnlib.DotNet.Emit;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class IntRotateShift : FunctionBase
{
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
switch (type)
{
case DataNodeType.Int32:
{
int value = (int)v;
// (value >> amount) | (value << (32 - amount))
// << amount
// value = b31 b30 .. b0;
int leftShiftAmount = options.random.NextInt(8, 16);
uint op1 = (uint)value >> leftShiftAmount;
int rightShiftAmount = 32 - leftShiftAmount;
uint op2 = (uint)value << rightShiftAmount;
Assert.AreEqual((uint)value, (op1 << leftShiftAmount) | (op2 >> rightShiftAmount));
args.Add(new ConstValue(DataNodeType.Int32, (int)op1));
args.Add(new ConstValue(DataNodeType.Int32, leftShiftAmount));
args.Add(new ConstValue(DataNodeType.Int32, (int)op2));
args.Add(new ConstValue(DataNodeType.Int32, rightShiftAmount));
break;
}
case DataNodeType.Int64:
{
long value = (long)v;
int leftShiftAmount = options.random.NextInt(16, 32);
ulong op1 = (ulong)value >> leftShiftAmount;
int rightShiftAmount = 64 - leftShiftAmount;
ulong op2 = (ulong)value << rightShiftAmount;
Assert.AreEqual((ulong)value, (op1 << leftShiftAmount) | (op2 >> rightShiftAmount));
args.Add(new ConstValue(DataNodeType.Int64, (long)op1));
args.Add(new ConstValue(DataNodeType.Int32, leftShiftAmount));
args.Add(new ConstValue(DataNodeType.Int64, (long)op2));
args.Add(new ConstValue(DataNodeType.Int32, rightShiftAmount));
break;
}
default: throw new NotSupportedException($"Unsupported type {type} for IntRotateShift");
}
}
public override void CompileSelf(CompileContext ctx, List<Instruction> output)
{
output.Add(Instruction.Create(OpCodes.Xor));
}
public override void Compile(CompileContext ctx, List<IDataNode> inputs, ConstValue result)
{
Assert.AreEqual(inputs.Count, 4);
inputs[0].Compile(ctx);
inputs[1].Compile(ctx);
ctx.output.Add(Instruction.Create(OpCodes.Shl));
inputs[2].Compile(ctx);
inputs[3].Compile(ctx);
ctx.output.Add(Instruction.Create(OpCodes.Shr_Un));
ctx.output.Add(Instruction.Create(OpCodes.Or));
}
}
}

View File

@ -0,0 +1,46 @@
using dnlib.DotNet.Emit;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static UnityEngine.Networking.UnityWebRequest;
namespace Obfuz.Virtualization.Functions
{
public class IntXor : FunctionBase
{
public override void CreateArguments(DataNodeType type, object v, CreateExpressionOptions options, List<ConstValue> args)
{
switch (type)
{
case DataNodeType.Int32:
{
int value = (int)v;
int op1 = options.random.NextInt();
int op2 = value ^ op1;
args.Add(new ConstValue(DataNodeType.Int32, op1));
args.Add(new ConstValue(DataNodeType.Int32, op2));
break;
}
case DataNodeType.Int64:
{
long value = (long)v;
long op1 = options.random.NextLong();
long op2 = value ^ op1;
args.Add(new ConstValue(DataNodeType.Int64, op1));
args.Add(new ConstValue(DataNodeType.Int64, op2));
break;
}
default: throw new NotSupportedException($"Unsupported type {type} for Int32FunctionXor");
}
}
public override void CompileSelf(CompileContext ctx, List<Instruction> output)
{
output.Add(Instruction.Create(OpCodes.Xor));
}
}
}

View File

@ -16,12 +16,14 @@ namespace Obfuz.Virtualization
_random = random;
var int32Funcs = new List<IFunction>()
{
new Int32FunctionAdd(),
new Int32FunctionXor(),
//new IntAdd(),
//new IntXor(),
new IntRotateShift(),
//new ConstFromFieldRvaDataCreator(),
//new ConstDataCreator(),
};
_functions.Add(DataNodeType.Int32, int32Funcs);
_functions.Add(DataNodeType.Int64, int32Funcs);
}
public override IDataNode CreateRandom(DataNodeType type, object value, CreateExpressionOptions options)
@ -30,11 +32,11 @@ namespace Obfuz.Virtualization
{
throw new System.Exception($"No functions available for type {type}");
}
if (options.depth >= 2)
if (options.depth >= 3)
{
//return new ConstDataNode() { Type = type, Value = value };
//return _random.NextInt(100) < 50 ?
return true ?
return _random.NextInt(100) < 50 ?
//return true ?
new ConstFromFieldRvaDataNode() { Type = type, Value = value } :
new ConstDataNode() { Type = type, Value = value };
}