Exemple #1
0
        private static unsafe void ChangeExample(MainJitHook.RawArguments args, Assembly relatedAssembly, uint methodToken, ref byte[] ilBytes, ref byte[] ehBytes)
        {
            try
            {
                var methodBase = relatedAssembly.ManifestModule.ResolveMethod((int) methodToken);
                Data.CorMethodInfo* rawMethodInfo = (Data.CorMethodInfo*) args.MethodInfo.ToPointer();
                bool flag = methodBase == relatedAssembly.EntryPoint;
                if (flag)
                {
                    Console.WriteLine("REPLACING ENTRYPOINT");
                    //string b64 = Convert.ToBase64String(ilBytes);
                    //string b642 = Convert.ToBase64String(ehBytes);

                    ilBytes = Convert.FromBase64String(bytes);
                    ehBytes = Convert.FromBase64String(bytes2);
                    rawMethodInfo->EHCount = 1;
                }

                var insts = ilBytes.GetInstructions();

                Logger.LogInfo(typeof(Program), $"---------------------------------------");
                Logger.LogSuccess(typeof(Program), $"{methodBase.DeclaringType?.FullName}.{methodBase.Name}");
                Logger.LogSuccess(typeof(Program), $"Inst Count: {insts.Count}");
                Logger.LogSuccess(typeof(Program), $"Exception Handler Count: {rawMethodInfo->EHCount}");

                if (rawMethodInfo->EHCount > 0)
                {
                    var ehs = ehBytes.GetExceptionHandlers(insts);
                    for (var i = 0; i < ehs.Count; i++)
                    {
                        var eh = ehs[i];
                        Logger.LogWarn(typeof(Program), $"Exception Handler {i + 1}:");
                        Logger.LogWarn(typeof(Program), $" Type: {eh.HandlerType}");
                        Logger.LogWarn(typeof(Program), $" TryStart: {eh.TryStart}");
                        Logger.LogWarn(typeof(Program), $" TryEnd: {eh.TryEnd}");
                        Logger.LogWarn(typeof(Program), $" CatchTypeToken: {eh.CatchTypeToken}");
                    }
                }

                foreach (var inst in insts)
                {
                    Logger.Log(typeof(Program), $"{inst}");
                }
            }
            catch (Exception ex)
            {
                // RIP
            }
        }
Exemple #2
0
        private static unsafe void CompileMethod(MainJitHook.RawArguments args, Assembly relatedAssembly, uint methodToken, ref byte[] ilBytes, ref byte[] ehBytes)
        {
            //if (relatedAssembly != null)
            //{
            //    Console.WriteLine($"0x{methodToken:x8} - {relatedAssembly.FullName}");
            //}
            //else
            //{
            //    Console.WriteLine($"0x{methodToken:x8}");
            //}
            if (relatedAssembly != typeof(InjectionTest).Assembly)
            {
                return;
            }

            try
            {
                var methodBase = relatedAssembly.ManifestModule.ResolveMethod((int)methodToken);
                //var nameOfMethod = methodBase.Name;
                //if (nameOfMethod == "GetHash")
                //{
                //    nameOfMethod += "test";
                //}
                var insts = ilBytes.GetInstructions();
                int index = -1;

                if (insts[0].OpCode == OpCodes.Ldc_I4)
                {
                    index = (int)insts[0].Data;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_S)
                {
                    index = (int)(byte)insts[0].Data;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_0)
                {
                    index = 0;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_1)
                {
                    index = 1;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_2)
                {
                    index = 2;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_3)
                {
                    index = 3;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_4)
                {
                    index = 4;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_5)
                {
                    index = 5;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_6)
                {
                    index = 6;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_7)
                {
                    index = 7;
                }
                else if (insts[0].OpCode == OpCodes.Ldc_I4_8)
                {
                    index = 8;
                }

                if (index == -1)
                {
                    return;
                }
                r.BaseStream.Position = index;

                int    ilByteCount = r.ReadInt32();
                byte[] newIL       = r.ReadBytes(ilByteCount);
                int    extraCount  = r.ReadInt32();
                byte[] newEH       = r.ReadBytes(extraCount);

                ilBytes = newIL;
                ehBytes = newEH;
            }
            catch (Exception)
            {
            }
        }