Exemplo n.º 1
0
        public override void Process(ConfusionParameter parameter)
        {
            method = parameter.Target as MethodDefinition;
            if (!method.HasBody)
            {
                return;
            }

            int slv = 5;

            if (Array.IndexOf(parameter.Parameters.AllKeys, "level") != -1)
            {
                if (!int.TryParse(parameter.Parameters["level"], out slv) && (slv <= 0 || slv > 10))
                {
                    Log("Invaild level, 5 will be used.");
                    slv = 5;
                }
            }
            genJunk = false;
            if (method.Module.Architecture != TargetArchitecture.I386)
            {
                genJunk = false;
            }
            else if (Array.IndexOf(parameter.Parameters.AllKeys, "genjunk") != -1)
            {
                if (!bool.TryParse(parameter.Parameters["genjunk"], out genJunk))
                {
                    Log("Invaild junk code parameter, junk code would not generated.");
                    genJunk = false;
                }
            }
            double trueLv = slv / 10.0;

            MethodBody body = method.Body;

            body.ComputeHeader();
            body.MaxStackSize += 5;
            Dictionary <Instruction, Level> Ids = GetIds(body);

            Level[] lvs = GetLvs(Ids);
            List <Instruction[]> scopes = new List <Instruction[]>();

            foreach (Level lv in lvs)
            {
                scopes.Add(GetInstructionsByLv(lv, Ids));
            }

            body.Instructions.Clear();
            Dictionary <Instruction, Instruction> HdrTbl = new Dictionary <Instruction, Instruction>();

            for (int i = 0; i < scopes.Count; i++)
            {
                Instruction[]   scope = scopes[i];
                Instruction[][] blocks;
                blocks = BranchesSplit(scope, trueLv);
                AddBranches(body, ref blocks);
                Reorder(ref blocks);

                HdrTbl.Add(scope[0], blocks[0][0]);

                foreach (Instruction[] iblk in blocks)
                {
                    body.Instructions.Add(iblk[0]);
                    for (int ii = 1; ii < iblk.Length; ii++)
                    {
                        Instruction tmp;
                        if (iblk[ii].Operand is Instruction)
                        {
                            if (HdrTbl.TryGetValue(iblk[ii].Operand as Instruction, out tmp) && tmp != blocks[0][0])
                            {
                                iblk[ii].Operand = tmp;
                            }
                        }
                        else if (iblk[ii].Operand is Instruction[])
                        {
                            Instruction[] op = iblk[ii].Operand as Instruction[];
                            for (int iii = 0; iii < op.Length; iii++)
                            {
                                if (HdrTbl.TryGetValue(op[iii], out tmp) && tmp != blocks[0][0])
                                {
                                    op[iii] = tmp;
                                }
                            }
                            iblk[ii].Operand = op;
                        }
                        body.Instructions.Add(iblk[ii]);
                    }
                }
                SetLvHandler(lvs[i], body, blocks);
            }

            foreach (ExceptionHandler eh in body.ExceptionHandlers)
            {
                eh.TryEnd     = eh.TryEnd.Next;
                eh.HandlerEnd = eh.HandlerEnd.Next;
                //if ((eh.HandlerType & ExceptionHandlerType.Filter) == ExceptionHandlerType.Filter)
                //{
                //    eh.FilterEnd = eh.FilterEnd.Next;
                //}
            }

            body.ComputeOffsets();
            body.PreserveMaxStackSize = true;
        }
Exemplo n.º 2
0
        public override void Process(ConfusionParameter parameter)
        {
            MethodDefinition mtd = parameter.Target as MethodDefinition;

            if (!mtd.HasBody)
            {
                return;
            }

            int slv = 5;

            if (Array.IndexOf(parameter.Parameters.AllKeys, "level") != -1)
            {
                if (!int.TryParse(parameter.Parameters["level"], out slv) && (slv <= 0 || slv > 10))
                {
                    Log("Invaild level, 5 will be used.");
                    slv = 5;
                }
            }
            genJunk = false;
            if (mtd.Module.Architecture != TargetArchitecture.I386)
            {
                genJunk = false;
            }
            else if (Array.IndexOf(parameter.Parameters.AllKeys, "genjunk") != -1)
            {
                if (!bool.TryParse(parameter.Parameters["genjunk"], out genJunk))
                {
                    Log("Invaild junk code parameter, junk code would not generated.");
                    genJunk = false;
                }
            }
            double trueLv = slv / 10.0;

            MethodBody bdy = mtd.Body;

            bdy.SimplifyMacros();
            bdy.ComputeHeader();
            Dictionary <Instruction, Level> Ids = GetIds(bdy);

            Level[] lvs = GetLvs(Ids);
            List <Instruction[]> blks = new List <Instruction[]>();

            foreach (Level lv in lvs)
            {
                blks.Add(GetInstructionsByLv(lv, Ids));
            }

            bdy.Instructions.Clear();
            ILProcessor wkr = bdy.GetILProcessor();
            Dictionary <Instruction, Instruction> HdrTbl = new Dictionary <Instruction, Instruction>();

            for (int i = 0; i < blks.Count; i++)
            {
                Instruction[]   blk = blks[i];
                Instruction[][] iblks;
                if (Array.IndexOf(parameter.Parameters.AllKeys, "switch") == -1)
                {
                    iblks = BrizeSplit(blk, trueLv);
                    BrizeFlow(bdy, ref iblks);
                }
                else
                {
                    iblks = SwitcizeSplit(mtd, blk, trueLv);
                    if (iblks.Length == 1)
                    {
                        iblks = BrizeSplit(blk, trueLv);
                        BrizeFlow(bdy, ref iblks);
                    }
                    else
                    {
                        SwitcizeFlow(bdy, ref iblks);
                    }
                }
                Reorder(ref iblks);

                HdrTbl.Add(blk[0], iblks[0][0]);

                foreach (Instruction[] iblk in iblks)
                {
                    wkr.Append(iblk[0]);
                    for (int ii = 1; ii < iblk.Length; ii++)
                    {
                        Instruction tmp;
                        if (iblk[ii].Operand is Instruction)
                        {
                            if (HdrTbl.TryGetValue(iblk[ii].Operand as Instruction, out tmp) && tmp != iblks[0][0])
                            {
                                iblk[ii].Operand = tmp;
                            }
                        }
                        else if (iblk[ii].Operand is Instruction[])
                        {
                            Instruction[] op = iblk[ii].Operand as Instruction[];
                            for (int iii = 0; iii < op.Length; iii++)
                            {
                                if (HdrTbl.TryGetValue(op[iii], out tmp) && tmp != iblks[0][0])
                                {
                                    op[iii] = tmp;
                                }
                            }
                            iblk[ii].Operand = op;
                        }
                        wkr.Append(iblk[ii]);
                    }
                }
                SetLvHandler(lvs[i], bdy, iblks);
            }

            foreach (ExceptionHandler eh in bdy.ExceptionHandlers)
            {
                eh.TryEnd     = eh.TryEnd.Next;
                eh.HandlerEnd = eh.HandlerEnd.Next;
                //if ((eh.HandlerType & ExceptionHandlerType.Filter) == ExceptionHandlerType.Filter)
                //{
                //    eh.FilterEnd = eh.FilterEnd.Next;
                //}
            }

            bdy.OptimizeMacros();
            bdy.PreserveMaxStackSize = true;
        }