public IAlgorithm Compile(InstructionInfo[] Instructions) { lock (GlobalLock) { Stopwatch sw = Stopwatch.StartNew(); typeBuilder = modBuilder.DefineType("AlgoClass_" + CompiledClasses, TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit | TypeAttributes.AutoLayout, typeof(object), new Type[] { typeof(IAlgorithm) }); CreateConstructor(typeBuilder); CreateDeconstructor(typeBuilder); CreateUlongMethod(typeBuilder, Instructions); //CreateByteMethod(typeBuilder); Type InitType = typeBuilder.CreateType(); Algorithm = (IAlgorithm)InitType.GetConstructor(new Type[] { }).Invoke(new object[] { }); //asmBuilder.Save("Dderp.dll"); //ulong test = Algorithm.CalculateULong(1); CompiledClasses++; sw.Stop(); return Algorithm; } }
private void CreateUlongMethod(TypeBuilder typeBuilder, InstructionInfo[] instructions) { MethodBuilder mb = typeBuilder.DefineMethod("CalculateULong", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.NewSlot | MethodAttributes.Virtual | MethodAttributes.Final, typeof(ulong), new Type[] { typeof(ulong) }); ILGenerator gen = mb.GetILGenerator(); if (IsDecryptState) { for (int i = instructions.Length - 1; i >= 0; i--) { ProcessUlongInstruction(instructions[i], gen); } } else { for (int i = 0; i < instructions.Length; i++) { ProcessUlongInstruction(instructions[i], gen); } } gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ret); MethodInfo CalculateULongMethod = typeof(IAlgorithm).GetMethod("CalculateULong"); typeBuilder.DefineMethodOverride(mb, CalculateULongMethod); }
private void ProcessUlongInstruction(InstructionInfo inst, ILGenerator gen) { switch (inst.Inst) { case WopEx.Instruction.Plus: { gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I8, (long)inst.Value_Long); gen.Emit(OpCodes.Add); gen.Emit(OpCodes.Starg_S, 1); break; } case WopEx.Instruction.Minus: { gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I8, (long)inst.Value_Long); gen.Emit(OpCodes.Sub); gen.Emit(OpCodes.Starg_S, 1); break; } case WopEx.Instruction.BitLeft: { byte ShiftLeftVal = (byte)inst.Value_Long; gen.Emit(OpCodes.Ldarg_1); //might improve performance if we check the number if (ShiftLeftVal == 0) gen.Emit(OpCodes.Ldc_I4_0); else if (ShiftLeftVal == 1) gen.Emit(OpCodes.Ldc_I4_1); else if (ShiftLeftVal == 2) gen.Emit(OpCodes.Ldc_I4_2); else if (ShiftLeftVal == 3) gen.Emit(OpCodes.Ldc_I4_3); else if (ShiftLeftVal == 4) gen.Emit(OpCodes.Ldc_I4_4); else if (ShiftLeftVal == 5) gen.Emit(OpCodes.Ldc_I4_5); else if (ShiftLeftVal == 6) gen.Emit(OpCodes.Ldc_I4_6); else if (ShiftLeftVal == 7) gen.Emit(OpCodes.Ldc_I4_7); else if (ShiftLeftVal == 8) gen.Emit(OpCodes.Ldc_I4_8); else gen.Emit(OpCodes.Ldc_I4_S, ShiftLeftVal); gen.Emit(OpCodes.Shl); gen.Emit(OpCodes.Starg_S, 1); break; } case WopEx.Instruction.BitRight: { byte ShiftRightVal = (byte)inst.Value_Long; gen.Emit(OpCodes.Ldarg_1); //might improve performance if we check the number if (ShiftRightVal == 0) gen.Emit(OpCodes.Ldc_I4_0); else if (ShiftRightVal == 1) gen.Emit(OpCodes.Ldc_I4_1); else if (ShiftRightVal == 2) gen.Emit(OpCodes.Ldc_I4_2); else if (ShiftRightVal == 3) gen.Emit(OpCodes.Ldc_I4_3); else if (ShiftRightVal == 4) gen.Emit(OpCodes.Ldc_I4_4); else if (ShiftRightVal == 5) gen.Emit(OpCodes.Ldc_I4_5); else if (ShiftRightVal == 6) gen.Emit(OpCodes.Ldc_I4_6); else if (ShiftRightVal == 7) gen.Emit(OpCodes.Ldc_I4_7); else if (ShiftRightVal == 8) gen.Emit(OpCodes.Ldc_I4_8); else gen.Emit(OpCodes.Ldc_I4_S, ShiftRightVal); gen.Emit(OpCodes.Shr_Un); gen.Emit(OpCodes.Starg_S, 1); break; } case WopEx.Instruction.XOR: { gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I8, (long)inst.Value_Long); gen.Emit(OpCodes.Xor); gen.Emit(OpCodes.Starg_S, 1); break; } case WopEx.Instruction.SwapBits: { //this could be more optimized, but let's just keep it so it runs unchecked { //(0x00000000000000FF) & (value >> 56) gen.Emit(OpCodes.Ldc_I4, (int)0x00000000000000FF); gen.Emit(OpCodes.Conv_I8); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)56); // >> 56 gen.Emit(OpCodes.Shr_Un); gen.Emit(OpCodes.And); //(0x000000000000FF00) & (value >> 40) | gen.Emit(OpCodes.Ldc_I4, (int)0x000000000000FF00); gen.Emit(OpCodes.Conv_I8); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)40); // >> 40 gen.Emit(OpCodes.Shr_Un); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0x0000000000FF0000) & (value >> 24) | gen.Emit(OpCodes.Ldc_I4, (int)0x0000000000FF0000); gen.Emit(OpCodes.Conv_I8); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)24); // >> 24 gen.Emit(OpCodes.Shr_Un); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0x00000000FF000000) & (value >> 8) | gen.Emit(OpCodes.Ldc_I4, (int)0x00000000FF000000); gen.Emit(OpCodes.Conv_U8); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_8); // >> 8 gen.Emit(OpCodes.Shr_Un); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0x000000FF00000000) & (value << 8) | gen.Emit(OpCodes.Ldc_I8, (long)0x000000FF00000000); //64bit INT gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_8); // << 8 gen.Emit(OpCodes.Shl); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0x0000FF0000000000) & (value << 24) | gen.Emit(OpCodes.Ldc_I8, (long)0x0000FF0000000000); //64bit INT gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)24); // << 24 gen.Emit(OpCodes.Shl); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0x00FF000000000000) & (value << 40) | gen.Emit(OpCodes.Ldc_I8, (long)0x00FF000000000000); //64bit INT gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)40); // << 40 gen.Emit(OpCodes.Shl); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); //(0xFF00000000000000) & (value << 56) gen.Emit(OpCodes.Ldc_I8, (long)0xFF00000000000000); //64bit INT gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Ldc_I4_S, (byte)56); // << 56 gen.Emit(OpCodes.Shl); gen.Emit(OpCodes.And); gen.Emit(OpCodes.Or); gen.Emit(OpCodes.Starg_S, 1); } break; } } }
private void ShuffleInstructions(InstructionInfo[] insts, int Seed) { FastRandom rnd = new FastRandom(Seed); for (int i = insts.Length, j = 0; i > 1; i--, j++) { int pos = rnd.Next(i); // 0 <= j <= i-1 InstructionInfo tmp = insts[pos]; insts[pos] = insts[i - 1]; insts[i - 1] = tmp; } }
private BigInteger ExecuteInstruction(BigInteger value, InstructionInfo inf, ref bool Executed, bool IsDecrypt) { Executed = true; switch (inf.Inst) { //case Instruction.SwapBits: // return SwapBits(value); case Instruction.Plus: return value + inf.Value; case Instruction.Minus: return value - inf.Value; /*case Instruction.RotateLeft_Big: return RotateLeft(value, (int)inf.Value); case Instruction.RotateRight_Big: return RotateRight(value, (int)inf.Value); case Instruction.BitLeft: return value << 1; case Instruction.BitRight: return value >> 1;*/ case Instruction.XOR: return value ^= inf.Value; case Instruction.ForLoop_PlusMinus: { /* Heavy "Algorithm" */ /*uint decValue = inf.Value; uint incValue = inf.Value2; uint loops = inf.Value3 >> 1; if (IsDecrypt) { for (int i = 0; i < loops; i++) { value += decValue; value -= incValue; } } else { for (int i = 0; i < loops; i++) { value -= decValue; value += incValue; } } return value;*/ break; } } Executed = false; return 0; }
private byte ExecuteInstruction(byte value, InstructionInfo inf, ref bool Executed, bool IsDecrypt) { Executed = true; byte InstVal = inf.Value_Byte; switch (inf.Inst) { //case Instruction.SwapBits: // return SwapBits(value); case Instruction.Plus: return (byte)(value + InstVal); case Instruction.Minus: return (byte)(value - InstVal); case Instruction.RotateLeft_Big: return RotateLeft(value); case Instruction.RotateRight_Big: return RotateRight(value); /*case Instruction.BitLeft: return (byte)(value << 1); case Instruction.BitRight: return (byte)(value >> 1);*/ case Instruction.XOR: return value ^= InstVal; } Executed = false; return 0; }
private static byte[] WriteInstruction(InstructionInfo Instruction) { using (PayloadWriter pw = new PayloadWriter()) using (MemoryStream ms = new MemoryStream()) { Serializer.Serialize(ms, Instruction); pw.WriteByte((byte)ms.Length); pw.WriteBytes(ms.ToArray()); return pw.ToByteArray(); } }
private static void GetNextRandomInstruction(FastRandom rnd, ref InstructionInfo EncInstruction, ref InstructionInfo DecInstruction) { lock (RndInstLock) { Instruction[] InstructionList = new Instruction[] { //Instruction.BitLeft, //unstable do not use Instruction.Minus, Instruction.Plus, //Instruction.ForLoop_PlusMinus, //Instruction.RotateLeft_Big, //Instruction.RotateLeft_Small, Instruction.SwapBits, Instruction.XOR }; Instruction inst = InstructionList[rnd.Next(0, InstructionList.Length)]; switch (inst) { case Instruction.BitLeft: { int bitSize = rnd.Next(1, 3); //maybe needs to be higher ? EncInstruction = new InstructionInfo(inst, bitSize); DecInstruction = new InstructionInfo(Instruction.BitRight, bitSize); break; } case Instruction.Minus: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(Instruction.Plus, new BigInteger(TempDate)); break; } case Instruction.Plus: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(Instruction.Minus, new BigInteger(TempDate)); break; } case Instruction.ForLoop_PlusMinus: { int size = rnd.Next(); int size2 = rnd.Next(); int loops = rnd.Next(2, 255); EncInstruction = new InstructionInfo(inst, (uint)size, (uint)size2, loops); DecInstruction = new InstructionInfo(inst, (uint)size, (uint)size2, loops); break; } case Instruction.RotateLeft_Big: { byte bitSize = (byte)rnd.Next(1, 60); EncInstruction = new InstructionInfo(inst, (uint)bitSize); DecInstruction = new InstructionInfo(Instruction.RotateRight_Big, (uint)bitSize); break; } case Instruction.RotateLeft_Small: { byte bitSize = (byte)rnd.Next(1, 30); EncInstruction = new InstructionInfo(inst, (uint)bitSize); DecInstruction = new InstructionInfo(Instruction.RotateRight_Small, (uint)bitSize); break; } case Instruction.SwapBits: { EncInstruction = new InstructionInfo(inst, 0); DecInstruction = new InstructionInfo(inst, 0); break; } case Instruction.XOR: { byte[] TempDate = new byte[32]; rnd.NextBytes(TempDate); EncInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); DecInstruction = new InstructionInfo(inst, new BigInteger(TempDate)); break; } default: { break; } } } }