Exemplo n.º 1
0
        public int RandomNumberInModule(Instruction[] instructions, int module, bool divisible)
        {
            int Rnum = module * r.Next(1, 12);

            Rnum = divisible ? Rnum : Rnum + 1;
            int x = 0;
            List <Instruction> instsx = new List <Instruction>();

            while (instructions[x].OpCode != OpCodes.Ldarg_0)
            {
                instsx.Add(instructions[x]);
                x++;
            }
            instsx.Add(OpCodes.Ret.ToInstruction());
            int valuesx = DynamicCode.Emulate(instsx.ToArray(), 0);
            List <Instruction> instdx = new List <Instruction>();

            instdx.Add(OpCodes.Ldc_I4.ToInstruction(Rnum));
            for (int i = instructions.Length - 2; i > x + 2; i -= 2)
            {
                Instruction operation = ReverseOperation(instructions[i].OpCode).ToInstruction();
                Instruction value     = instructions[i - 1];
                instdx.Add(value);
                instdx.Add(operation);
            }
            instdx.Add(Instruction.Create(OpCodes.Ret));
            int                valuedx = DynamicCode.Emulate(instdx.ToArray(), 0);
            Instruction        ope     = ReverseOperation(instructions[x + 1].OpCode).ToInstruction();
            List <Instruction> final   = new List <Instruction>();

            final.Add(OpCodes.Ldc_I4.ToInstruction(valuedx));
            final.Add(OpCodes.Ldc_I4.ToInstruction(valuesx));
            final.Add(ope.OpCode == OpCodes.Add ? OpCodes.Sub.ToInstruction() : ope);
            final.Add(OpCodes.Ret.ToInstruction());
            int finalValue = DynamicCode.Emulate(final.ToArray(), 0);

            return(ope.OpCode == OpCodes.Add ? (finalValue * -1) : finalValue);
        }
Exemplo n.º 2
0
        public static Dictionary<MethodDef, Tuple<int[], int[]>> CreateMethods(ModuleDef loadedMod)
        {
            DynamicCode code = new DynamicCode(3);
            int[] modules = new int[4];
            for (int i = 0; i < modules.Length; i++)
                modules[i] = rand.Next(2, 25);
            Instruction[,] methods = new Instruction[4, 10];
            for (int i = 0; i < 4; i++)
            {
                Instruction[] methodBody = code.Create();
                for (int y = 0; y < methodBody.Length; y++)
                    methods[i, y] = methodBody[y];
            }

            List<Tuple<Instruction[], Tuple<int, Tuple<int[], int[]>>>> InstrToInt =
                           new List<Tuple<Instruction[], Tuple<int, Tuple<int[], int[]>>>>();

            for (int i = 0; i < 4; i++)
            {
                List<Instruction> instr = new List<Instruction>();
                int[] numbersTrue = new int[5];
                int[] numbersFalse = new int[5];
                for (int y = 0; y < 10; y++)
                    instr.Add(methods[i, y]);
                for (int y = 0; y < 5; y++)
                    numbersTrue[y] = code.RandomNumberInModule(instr.ToArray(), modules[i], true);
                for (int y = 0; y < 5; y++)
                    numbersFalse[y] = code.RandomNumberInModule(instr.ToArray(), modules[i], false);
                InstrToInt.Add(Tuple.Create(instr.ToArray(), Tuple.Create(modules[i], Tuple.Create(numbersTrue, numbersFalse))));
            }
            Dictionary<MethodDef, Tuple<int[], int[]>> final = new Dictionary<MethodDef, Tuple<int[], int[]>>();
            MethodAttributes methFlags = MethodAttributes.Public | MethodAttributes.Static
                | MethodAttributes.HideBySig | MethodAttributes.ReuseSlot;
            MethodImplAttributes methImplFlags = MethodImplAttributes.IL | MethodImplAttributes.Managed;
            for (int i = 0; i < 4; i++)
            {
                MethodDef methodDefs1 = new MethodDefUser(
                                     "",
                                     MethodSig.CreateStatic(loadedMod.CorLibTypes.Boolean, loadedMod.CorLibTypes.Int32),
                                     methImplFlags, methFlags);
                RenameTask.Rename(methodDefs1);
                methodDefs1.Body = new CilBody();
                methodDefs1.ParamDefs.Add(new ParamDefUser("lol", 0));
                List<Instruction> preInstr = new List<Instruction>(InstrToInt[i].Item1);
                int module = InstrToInt[i].Item2.Item1;
                //preInstr.RemoveAt(preInstr.Count - 1);
                preInstr.Insert(preInstr.Count - 1, Instruction.CreateLdcI4(module));
                preInstr.Insert(preInstr.Count - 1, OpCodes.Rem.ToInstruction());
                preInstr.Insert(preInstr.Count - 1, Instruction.CreateLdcI4(0));
                preInstr.Insert(preInstr.Count - 1, Instruction.Create(OpCodes.Ceq));
                //preInstr.Insert(preInstr.Count - 1, Instruction.Create(OpCodes.Ret));
                foreach (var item in preInstr)
                    methodDefs1.Body.Instructions.Add(item);
                final.Add(methodDefs1, InstrToInt[i].Item2.Item2);
            }

            TypeDef type1 = new TypeDefUser("", "", loadedMod.CorLibTypes.Object.TypeDefOrRef);
            RenameTask.Rename(type1);
            type1.Attributes = dnlib.DotNet.TypeAttributes.Public | dnlib.DotNet.TypeAttributes.AutoLayout |
            dnlib.DotNet.TypeAttributes.Class | dnlib.DotNet.TypeAttributes.AnsiClass;
            loadedMod.Types.Add(type1);
            foreach (var item in final)
                type1.Methods.Add(item.Key);
            return final;
        }