diff --git a/Editor/Utils/IRandom.cs b/Editor/Utils/IRandom.cs index b3f8730..e3a68b8 100644 --- a/Editor/Utils/IRandom.cs +++ b/Editor/Utils/IRandom.cs @@ -7,5 +7,7 @@ int NextInt(int max); int NextInt(); + + long NextLong(); } } diff --git a/Editor/Utils/RandomWithKey.cs b/Editor/Utils/RandomWithKey.cs index 292eb36..3a92df0 100644 --- a/Editor/Utils/RandomWithKey.cs +++ b/Editor/Utils/RandomWithKey.cs @@ -54,5 +54,10 @@ namespace Obfuz.Utils { return _random.Next() ^ GetNextKeyByte(); } + + public long NextLong() + { + return ((long)NextInt() << 32) | (uint)NextInt(); + } } } diff --git a/Editor/Virtualization/DefaultDataObfuscator.cs b/Editor/Virtualization/DefaultDataObfuscator.cs index 4d648d1..63ef5ee 100644 --- a/Editor/Virtualization/DefaultDataObfuscator.cs +++ b/Editor/Virtualization/DefaultDataObfuscator.cs @@ -40,9 +40,9 @@ namespace Obfuz.Virtualization public void ObfuscateLong(MethodDef method, long value, List 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 obfuscatedInstructions) diff --git a/Editor/Virtualization/Functions/FunctionBase.cs b/Editor/Virtualization/Functions/FunctionBase.cs index 194984a..f62ba30 100644 --- a/Editor/Virtualization/Functions/FunctionBase.cs +++ b/Editor/Virtualization/Functions/FunctionBase.cs @@ -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(); CreateArguments(type, value, options, args); diff --git a/Editor/Virtualization/Functions/Int32FunctionAdd.cs b/Editor/Virtualization/Functions/Int32FunctionAdd.cs deleted file mode 100644 index 1c50e48..0000000 --- a/Editor/Virtualization/Functions/Int32FunctionAdd.cs +++ /dev/null @@ -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 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 output) - { - output.Add(Instruction.Create(OpCodes.Add)); - } - } -} diff --git a/Editor/Virtualization/Functions/Int32FunctionXor.cs b/Editor/Virtualization/Functions/Int32FunctionXor.cs deleted file mode 100644 index 51771f8..0000000 --- a/Editor/Virtualization/Functions/Int32FunctionXor.cs +++ /dev/null @@ -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 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 output) - { - output.Add(Instruction.Create(OpCodes.Xor)); - } - } -} diff --git a/Editor/Virtualization/Functions/IntAdd.cs b/Editor/Virtualization/Functions/IntAdd.cs new file mode 100644 index 0000000..ea542ba --- /dev/null +++ b/Editor/Virtualization/Functions/IntAdd.cs @@ -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 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 output) + { + output.Add(Instruction.Create(OpCodes.Add)); + } + } +} diff --git a/Editor/Virtualization/Functions/IntRotateShift.cs b/Editor/Virtualization/Functions/IntRotateShift.cs new file mode 100644 index 0000000..dd95bdb --- /dev/null +++ b/Editor/Virtualization/Functions/IntRotateShift.cs @@ -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 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 output) + { + output.Add(Instruction.Create(OpCodes.Xor)); + } + + public override void Compile(CompileContext ctx, List 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)); + } + } +} diff --git a/Editor/Virtualization/Functions/IntXor.cs b/Editor/Virtualization/Functions/IntXor.cs new file mode 100644 index 0000000..93c7a13 --- /dev/null +++ b/Editor/Virtualization/Functions/IntXor.cs @@ -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 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 output) + { + output.Add(Instruction.Create(OpCodes.Xor)); + } + } +} diff --git a/Editor/Virtualization/RandomDataNodeCreator.cs b/Editor/Virtualization/RandomDataNodeCreator.cs index 2c9113e..8a167c6 100644 --- a/Editor/Virtualization/RandomDataNodeCreator.cs +++ b/Editor/Virtualization/RandomDataNodeCreator.cs @@ -16,12 +16,14 @@ namespace Obfuz.Virtualization _random = random; var int32Funcs = new List() { - 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 }; }