Esempio n. 1
0
        public void Mutate(Random rand, MethodBody body, ModuleDefinition mod)
        {
            
            MathGenerator mg = new MathGenerator(Environment.TickCount, mod);
            body.SimplifyMacros();
            ILProcessor ilp = body.GetILProcessor();

            //foreach (var i in body.Instructions)
            for (int x = 0; x < body.Instructions.Count; x++)
            {
                Instruction i = body.Instructions[x];
                FieldReference field = i.Operand as FieldReference;
                if (field != null && field.DeclaringType.FullName == "Mutation")
                {
                    switch (field.Name)
                    {
                        case "Key0I":
                            i.Operand = IntKeys[0]; goto case "I";
                        case "Key1I":
                            i.Operand = IntKeys[1]; goto case "I";
                        case "Key2I":
                            i.Operand = IntKeys[2]; goto case "I";
                        case "Key3I":
                            i.Operand = IntKeys[3]; goto case "I";
                        case "Key4I":
                            i.Operand = IntKeys[4]; goto case "I";
                        case "Key5I":
                            i.Operand = IntKeys[5]; goto case "I";
                        case "Key6I":
                            i.Operand = IntKeys[6]; goto case "I";
                        case "Key7I":
                            i.Operand = IntKeys[7]; goto case "I";

                        case "Key0L":
                            i.Operand = LongKeys[0]; goto case "L";
                        case "Key1L":
                            i.Operand = LongKeys[1]; goto case "L";
                        case "Key2L":
                            i.Operand = LongKeys[2]; goto case "L";
                        case "Key3L":
                            i.Operand = LongKeys[3]; goto case "L";
                        case "Key4L":
                            i.Operand = LongKeys[4]; goto case "L";
                        case "Key5L":
                            i.Operand = LongKeys[5]; goto case "L";
                        case "Key6L":
                            i.Operand = LongKeys[6]; goto case "L";
                        case "Key7L":
                            i.Operand = LongKeys[7]; goto case "L";

                        case "Key0S":
                            i.Operand = StringKeys[0]; goto case "S";
                        case "Key1S":
                            i.Operand = StringKeys[1]; goto case "S";
                        case "Key2S":
                            i.Operand = StringKeys[2]; goto case "S";
                        case "Key3S":
                            i.Operand = StringKeys[3]; goto case "S";

                        case "Key0Delayed":
                            if (IsDelayed)
                            {
                                i.Operand = DelayedKeys[0];
                                goto case "I";
                            }
                            else
                                Delayed0 = i;
                            break;
                        case "Key1Delayed":
                            if (IsDelayed)
                            {
                                i.Operand = DelayedKeys[1];
                                goto case "I";
                            }
                            else
                                Delayed1 = i;
                            break;


                        case "I":

                            i.OpCode = OpCodes.Ldc_I4;

                            int target = (int)i.Operand;

                            if (ObfuscationHelper.StringGen != null)
                            {
                                List<Instruction> keyInsts = new List<Instruction>();
                                if (ObfuscationHelper.StringGen.Module != mod)
                                {
                                    keyInsts = ObfuscationHelper.StringGen.GenerateLevels(body.Method, target, rand.Next(0, 2), 5, false, mod);
                                }
                                else
                                {
                                    keyInsts = ObfuscationHelper.StringGen.GenerateLevels(body.Method, target, rand.Next(0, 2), 5);
                                }

                                ilp.Replace(i, keyInsts[0]);

                                keyInsts = keyInsts.Skip(1).Reverse().ToList();
                                foreach (Instruction a in keyInsts)
                                {
                                    body.Instructions.Insert(x + 1, a);
                                }
                                x += keyInsts.Count;
                            }
                            else
                            {
                                List<Instruction> keyInsts = mg.GenerateLevels(target, OpCodes.Ldc_I4, rand.Next(0, 6));

                                ilp.Replace(i, keyInsts[0]);

                                keyInsts = keyInsts.Skip(1).Reverse().ToList();
                                foreach (Instruction a in keyInsts)
                                {
                                    body.Instructions.Insert(x + 1, a);
                                }
                                x += keyInsts.Count;
                            }

                            /*
                            List<Instruction> genInsts = mg.GenerateLevels(target, OpCodes.Ldc_I4, rand.Next(0, 6));

                            Instruction tmp = null;

                            foreach (Instruction a in genInsts)
                            {
                                if (genInsts[0] == a)
                                {
                                    // first statement
                                    ilp.Replace(i, a);
                                    tmp = a;
                                    continue;
                                }
                                ilp.InsertAfter(tmp, a);
                                tmp = a;
                            }
                            x += genInsts.Count;*/


                            break;
                        case "L":
                            i.OpCode = OpCodes.Ldc_I8; break;
                        case "S":
                            i.OpCode = OpCodes.Ldstr; break;
                    }
                }
                MethodReference method = i.Operand as MethodReference;
                if (method != null && method.DeclaringType.FullName == "Mutation")
                {
                    if (method.Name == "Placeholder")
                        Placeholder = i;
                    else if (method.Name == "DeclaringType")
                    {
                        i.OpCode = OpCodes.Ldtoken;
                        i.Operand = body.Method.DeclaringType;
                    }
                    else if (method.Name == "Method")
                    {
                        var str = (string)i.Previous.Operand;
                        i.OpCode = OpCodes.Ldtoken;
                        i.Operand = body.Method.DeclaringType.Methods.Single(m => m.Name == str);
                        i.Previous.OpCode = OpCodes.Nop;
                        i.Previous.Operand = null;
                    }
                    else if (method.Name == "Break")
                    {
                        i.OpCode = OpCodes.Break;
                        i.Operand = null;
                    }
                }
            }

            for (int i = 0; i < body.Variables.Count; i++)
            {
                int x = rand.Next(0, body.Variables.Count);
                var tmp = body.Variables[i];
                body.Variables[i] = body.Variables[x];
                body.Variables[x] = tmp;
            }

            int iteration = rand.Next(20, 35);
            while (iteration > 0)
            {
                MutateCode(rand, body);
                iteration--;
            }
        }
Esempio n. 2
0
        public List <Instruction> Generate(MethodDefinition CurrentMethod, int target, int maxLength = 20, bool unsigned = false)
        {
            List <Instruction> builder = new List <Instruction>();

            DynamicGenerator.DynamicInfo dCall;

            if (target < 0)
            {
                return(new List <Instruction>()
                {
                    Instruction.Create(OpCodes.Ldc_I4, target)
                });
            }
            if (target <= maxLength)
            {
                // Make one string and use the length of int in the place of the target.
                Instruction strInst = Instruction.Create(OpCodes.Ldstr, ObfuscationHelper.GetRandomString(target));

                if (target == 0 && rand.Next(0, 100) % 2 == 0)
                {
                    strInst = StringEmpty;
                }

                builder.Add(strInst);
                builder.Add(LengthCall);
            }
            else
            {
                // Make one string (random length) and use the length of it, + rest (perhaps expression gen for that?).

                int take = rand.Next(0, 20);

                Instruction strInst = Instruction.Create(OpCodes.Ldstr, ObfuscationHelper.GetRandomString(take));

                if (take == 0 && rand.Next(0, 100) % 2 == 0)
                {
                    strInst = StringEmpty;
                }

                int remainder = target - take;


                bool hasAdded = false;

                if (rand.Next(0, 100) % 2 == 0)
                {
                    builder.Add(strInst);
                    builder.Add(LengthCall);
                    hasAdded = true;
                }

                if (rand.Next(0, 100) % 2 == 0)
                {
                    Expression ex = ExpGen.Generate(5);

                    int evald = ExpressionEvaluator.Evaluate(ex, remainder);

                    Expression exR = ExpressionInverser.InverseExpression(ex);

                    CecilVisitor cv = new Visitors.CecilVisitor(exR, new Instruction[] { Instruction.Create(OpCodes.Ldc_I4, evald) });

                    builder.AddRange(cv.GetInstructions());
                }
                else if (rand.Next(0, 100) % 2 == 0)
                {
                    builder.AddRange(MathGen.GenerateLevels(remainder, !unsigned ? OpCodes.Ldc_I4 : OpCodes.Ldc_I4_0, rand.Next(0, 3)));
                }
                else if (!CurrentMethod.Name.StartsWith("DYN___"))
                {
                    builder.AddRange(DynGen.Generate(CurrentMethod, remainder, !unsigned ? OpCodes.Ldc_I4 : OpCodes.Ldc_I4_0, out dCall));
                }
                else
                {
                    /*Expression ex = ExpGen.Generate(5);
                     *
                     * int evald = ExpressionEvaluator.Evaluate(ex, remainder);
                     *
                     * Expression exR = ExpressionInverser.InverseExpression(ex);
                     *
                     * CecilVisitor cv = new Visitors.CecilVisitor(exR, new Instruction[] { Instruction.Create(OpCodes.Ldc_I4, evald) });
                     *
                     * builder.AddRange(cv.GetInstructions());*/
                    builder.AddRange(MathGen.GenerateLevels(remainder, !unsigned ? OpCodes.Ldc_I4 : OpCodes.Ldc_I4_0, rand.Next(0, 4)));
                }

                if (!hasAdded)
                {
                    builder.Add(strInst);
                    builder.Add(LengthCall);
                }

                builder.Add(Instruction.Create(OpCodes.Add));

                if (unsigned)
                {
                    builder.Add(Instruction.Create(OpCodes.Conv_U4));
                }
            }


            return(builder);
        }