Exemple #1
0
        private static void ProcessFile(string inputFile)
        {
            Console.WriteLine("Processing  File : " + inputFile);

            if (Directory.Exists(inputFile))
            {
                string directoryName = Path.GetDirectoryName(inputFile);
                if (directoryName != null)
                {
                    Environment.CurrentDirectory = directoryName;
                }
            }


            PEFile file = PEFile.ReadPEFile(inputFile);

            Method startLogMethod, endLogMethod;

            if (!MethodLoggerUtil.LocateLoggerMethods(out startLogMethod, out endLogMethod))
            {
                ShowHelp("未成功获取注入内容!");
                return;
            }

            //Diag.DiagOn = true;
            ClassDef[] classes = file.GetClasses();

            System.Array.ForEach(classes, delegate(ClassDef classDef)
            {
                ProcessClass(classDef, startLogMethod, endLogMethod);
            });

            file.WritePEFile(false);
            //Console.WriteLine("Processe end ,any key exit");
            //Console.ReadKey();
        }
Exemple #2
0
        internal static void ILSpyMtNoRt(ClassDef classDef, MethodDef methodDef, Method startLogMethod, Method endLogMethod)
        {
            string classNameString  = MethodLoggerUtil.GetQualifiedClassName(classDef);
            string methodNameString = methodDef.Name();
            string paramsString     = MethodLoggerUtil.GetParamsAsString(methodDef.GetParams());

            List <Local> CLRLocals = new List <Local>();

            CLRLocals.Clear();

            Param[] parms = methodDef.GetParams();


            if (methodDef.GetMaxStack() < 3)
            {
                methodDef.SetMaxStack(3);
            }
            string          strGuid      = Guid.NewGuid().ToString();
            CILInstructions instructions = methodDef.GetCodeBuffer();

            if (instructions == null)
            {
                return;
            }


            instructions.StartInsert();


            instructions.Inst(Op.nop);
            instructions.StartBlock(); // Try #1
            instructions.StartBlock(); // Try #2
            instructions.Inst(Op.nop);



            instructions.ldstr(strGuid);
            instructions.ldstr(classNameString);
            instructions.ldstr(methodNameString);
            instructions.ldstr(paramsString);
            instructions.MethInst(MethodOp.call, startLogMethod);
            instructions.EndInsert();


            while (instructions.GetNextInstruction().GetPos() < instructions.NumInstructions() - 2)
            {
                ;
            }



            instructions.StartInsert();
            instructions.Inst(Op.nop);
            CILLabel cel0 = instructions.NewLabel();
            CILLabel cel9 = instructions.NewLabel();

            instructions.Branch(BranchOp.leave_s, cel9);



            TryBlock tBlk2 = instructions.EndTryBlock(); // #2

            instructions.StartBlock();
            int istloc = 0;

            if (methodDef.GetLocals() != null)
            {
                istloc = methodDef.GetLocals().Length;
            }
            instructions.IntInst(IntOp.stloc_s, istloc);
            instructions.Inst(Op.nop);
            //instructions.Inst(Op.rethrow);

            CILLabel cel = instructions.NewLabel();



            instructions.CodeLabel(cel0);



            instructions.OpenScope();
            //start---add exceptiong to stocks variables
            Local loc = new Local("SpyMtNoRt", Runtime.SystemExceptionRef);

            if (methodDef.GetLocals() != null)
            {
                foreach (Local lab in methodDef.GetLocals())
                {
                    CLRLocals.Add(lab);
                }
            }
            CLRLocals.Add(loc);

            methodDef.AddLocals(CLRLocals.ToArray(), false);
            foreach (Local la in methodDef.GetLocals())
            {
                instructions.BindLocal(la);
            }
            //start---add exceptiong to stocks variables


            instructions.CloseScope();

            instructions.IntInst(IntOp.ldloc_s, istloc);
            Method LogException = null;

            MethodLoggerUtil.GetMethodsFromClass("LogException", out LogException);
            instructions.MethInst(MethodOp.call, LogException);
            instructions.Inst(Op.nop);

            instructions.Inst(Op.nop);
            instructions.Branch(BranchOp.leave_s, cel9);

            instructions.EndCatchBlock(Runtime.SystemExceptionRef, tBlk2);
            instructions.CodeLabel(cel9);



            instructions.Branch(BranchOp.leave_s, cel);



            TryBlock tBlk1 = instructions.EndTryBlock(); // #1

            instructions.StartBlock();                   // Finally
            instructions.Inst(Op.nop);



            instructions.ldstr(strGuid);
            instructions.ldstr(classNameString);
            instructions.ldstr(methodNameString);
            instructions.ldstr(paramsString);
            instructions.MethInst(MethodOp.call, endLogMethod);
            instructions.Inst(Op.nop);
            instructions.Inst(Op.nop);
            instructions.Inst(Op.endfinally);

            instructions.EndFinallyBlock(tBlk1);

            instructions.CodeLabel(cel);



            instructions.EndInsert();
        }