diff --git a/AsStrongAsFuck/Protections/ControlFlow/Block.cs b/AsStrongAsFuck/Protections/ControlFlow/Block.cs deleted file mode 100644 index 2a63f8e..0000000 --- a/AsStrongAsFuck/Protections/ControlFlow/Block.cs +++ /dev/null @@ -1,21 +0,0 @@ -using dnlib.DotNet.Emit; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsStrongAsFuck.ControlFlow -{ - public class Block - { - public Block() - { - Instructions = new List(); - } - public List Instructions { get; set; } - - public int Number { get; set; } - public int Next { get; set; } - } -} diff --git a/AsStrongAsFuck/Protections/ControlFlow/BlockParser.cs b/AsStrongAsFuck/Protections/ControlFlow/BlockParser.cs deleted file mode 100644 index 1cfb6ce..0000000 --- a/AsStrongAsFuck/Protections/ControlFlow/BlockParser.cs +++ /dev/null @@ -1,62 +0,0 @@ -using dnlib.DotNet; -using dnlib.DotNet.Emit; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace AsStrongAsFuck.ControlFlow -{ - public class BlockParser - { - public static List ParseMethod(MethodDef method) - { - List blocks = new List(); - List body = new List(method.Body.Instructions); - - //splitting into blocks (Thanks to CodeOfDark#6320) - Block block = new Block(); - int Id = 0; - int usage = 0; - block.Number = Id; - block.Instructions.Add(Instruction.Create(OpCodes.Nop)); - blocks.Add(block); - block = new Block(); - Stack handlers = new Stack(); - foreach (Instruction instruction in method.Body.Instructions) - { - foreach (var eh in method.Body.ExceptionHandlers) - { - if (eh.HandlerStart == instruction || eh.TryStart == instruction || eh.FilterStart == instruction) - handlers.Push(eh); - } - foreach (var eh in method.Body.ExceptionHandlers) - { - if (eh.HandlerEnd == instruction || eh.TryEnd == instruction) - handlers.Pop(); - } - int stacks, pops; - instruction.CalculateStackUsage(out stacks, out pops); - block.Instructions.Add(instruction); - usage += stacks - pops; - if (stacks == 0) - { - if (instruction.OpCode != OpCodes.Nop) - { - if ((usage == 0 || instruction.OpCode == OpCodes.Ret) && handlers.Count == 0) - { - - block.Number = ++Id; - blocks.Add(block); - block = new Block(); - } - } - } - } - - return blocks; - } - - } -} diff --git a/AsStrongAsFuck/Protections/ControlFlow/ControlFlowObfuscation.cs b/AsStrongAsFuck/Protections/ControlFlow/ControlFlowObfuscation.cs index 67192d9..b9e340f 100644 --- a/AsStrongAsFuck/Protections/ControlFlow/ControlFlowObfuscation.cs +++ b/AsStrongAsFuck/Protections/ControlFlow/ControlFlowObfuscation.cs @@ -1,8 +1,6 @@ -using AsStrongAsFuck.Runtime; using dnlib.DotNet; using dnlib.DotNet.Emit; -using System.Collections.Generic; -using System.Linq; +using System; namespace AsStrongAsFuck.ControlFlow { @@ -32,68 +30,34 @@ public void Execute(ModuleDefMD md) public void ExecuteMethod(MethodDef method) { - method.Body.SimplifyMacros(method.Parameters); - List blocks = BlockParser.ParseMethod(method); - blocks = Randomize(blocks); - method.Body.Instructions.Clear(); - Local local = new Local(Module.CorLibTypes.Int32); - method.Body.Variables.Add(local); - Instruction target = Instruction.Create(OpCodes.Nop); - Instruction instr = Instruction.Create(OpCodes.Br, target); - foreach (Instruction instruction in Calc(0)) - method.Body.Instructions.Add(instruction); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Stloc, local)); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Br, instr)); - method.Body.Instructions.Add(target); - foreach (Block block in blocks) + for (int i = 0; i < method.Body.Instructions.Count; i++) { - if (block != blocks.Single(x => x.Number == blocks.Count - 1)) + if (method.Body.Instructions[i].IsLdcI4()) { - method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, local)); - foreach (Instruction instruction in Calc(block.Number)) - method.Body.Instructions.Add(instruction); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Ceq)); - Instruction instruction4 = Instruction.Create(OpCodes.Nop); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Brfalse, instruction4)); - foreach (Instruction instruction in block.Instructions) - method.Body.Instructions.Add(instruction); - foreach (Instruction instruction in Calc(block.Number + 1)) - method.Body.Instructions.Add(instruction); - - method.Body.Instructions.Add(Instruction.Create(OpCodes.Stloc, local)); - method.Body.Instructions.Add(instruction4); + int numorig = new Random(Guid.NewGuid().GetHashCode()).Next(); + int div = new Random(Guid.NewGuid().GetHashCode()).Next(); + int num = numorig ^ div; + + Instruction nop = OpCodes.Nop.ToInstruction(); + + Local local = new Local(method.Module.ImportAsTypeSig(typeof(int))); + method.Body.Variables.Add(local); + + method.Body.Instructions.Insert(i + 1, OpCodes.Stloc.ToInstruction(local)); + method.Body.Instructions.Insert(i + 2, Instruction.Create(OpCodes.Ldc_I4, method.Body.Instructions[i].GetLdcI4Value() - sizeof(float))); + method.Body.Instructions.Insert(i + 3, Instruction.Create(OpCodes.Ldc_I4, num)); + method.Body.Instructions.Insert(i + 4, Instruction.Create(OpCodes.Ldc_I4, div)); + method.Body.Instructions.Insert(i + 5, Instruction.Create(OpCodes.Xor)); + method.Body.Instructions.Insert(i + 6, Instruction.Create(OpCodes.Ldc_I4, numorig)); + method.Body.Instructions.Insert(i + 7, Instruction.Create(OpCodes.Bne_Un, nop)); + method.Body.Instructions.Insert(i + 8, Instruction.Create(OpCodes.Ldc_I4, 2)); + method.Body.Instructions.Insert(i + 9, OpCodes.Stloc.ToInstruction(local)); + method.Body.Instructions.Insert(i + 10, Instruction.Create(OpCodes.Sizeof, method.Module.Import(typeof(float)))); + method.Body.Instructions.Insert(i + 11, Instruction.Create(OpCodes.Add)); + method.Body.Instructions.Insert(i + 12, nop); + i += 12; } } - method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldloc, local)); - foreach (Instruction instruction in Calc(blocks.Count - 1)) - method.Body.Instructions.Add(instruction); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Ceq)); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Brfalse, instr)); - method.Body.Instructions.Add(Instruction.Create(OpCodes.Br, blocks.Single(x => x.Number == blocks.Count - 1).Instructions[0])); - method.Body.Instructions.Add(instr); - foreach (Instruction lastBlock in blocks.Single(x => x.Number == blocks.Count - 1).Instructions) - method.Body.Instructions.Add(lastBlock); - } - - public List Randomize(List input) - { - List ret = new List(); - foreach (var group in input) - ret.Insert(RuntimeHelper.Random.Next(0, ret.Count), group); - return ret; - } - - - public List Calc(int value) - { - List instructions = new List(); - instructions.Add(Instruction.Create(OpCodes.Ldc_I4, value)); - return instructions; - } - - public void AddJump(IList instrs, Instruction target) - { - instrs.Add(Instruction.Create(OpCodes.Br, target)); } } }