internal static MethodInfo FindReplacement(StackFrame frame) { var methodAddress = AccessTools.Field(typeof(StackFrame), "methodAddress"); if (methodAddress == null) { return(null); } var framePtr = (long)methodAddress.GetValue(frame); return(WithState(() => { return originals.Keys .FirstOrDefault(replacement => DetourHelper.GetNativeStart(replacement).ToInt64() == framePtr); })); }
public static void Start() { ModuleDefMD sampleCode = ModuleDefMD.Load(@"C:\Users\colton\source\repos\PackerTest\PackerTest\bin\Debug\PackerTest.exe"); // ModuleDefMD thisAssembly = ModuleDefMD.Load(System.Reflection.Assembly.GetExecutingAssembly().Location); var program = sampleCode.Types.Single(x => x.Name == "Program").Methods.Single(x => x.Name == "Main"); var validate = sampleCode.Types.Single(x => x.Name == "PersonValidator").Methods.Single(x => x.Name == "Validate"); var callInstructions = program.FindAllCallInstructions(); foreach (var instruction in callInstructions) { try { if ((MethodDef)instruction.Operand == validate) { Console.WriteLine(instruction.ToString()); } } catch (Exception ex) { // Console.WriteLine(instruction.ToString() + "DID NOT WORK!! EXCEPTION - " + ex.Message); } } CompiletimeObfuscater.ObfuscateMethodCalls(validate, 30, new List <ModuleDefMD> { sampleCode }); // validate.GetCalls(new List<ModuleDefMD>() { sampleCode}); var detourSource = sampleCode.Types.Single(x => x.Name == "PersonValidator").Methods.Single(x => x.Name == "DetourExample"); var emptyMethod = sampleCode.Types.Single(x => x.Name == "PersonValidator").Methods.Single(x => x.Name == "EmptyMethod"); DetourHelper.CompiletimeDetour(validate, detourSource); CompiletimePatcher.CreateMethodRestore(validate, detourSource, sampleCode); if (Process.GetProcessesByName("Obfuscated").Count() == 0) { Console.WriteLine("Saving"); sampleCode.Write(@"C:\Users\colton\source\repos\PackerTest\PackerTest\bin\Debug\Obfuscated.exe"); Console.WriteLine("Finsihed saving"); } Console.WriteLine("NetPackerTest.Start() is over"); }
public MethodBase GenerateTrampoline(MethodBase signature = null) { if (TrampolineMethod == null) { // Generate trampoline without applying the detour GenerateTrampolineInner(out _, out _); if (TrampolinePtr == IntPtr.Zero) { throw new InvalidOperationException("Trampoline pointer is not available"); } TrampolineMethod = DetourHelper.GenerateNativeProxy(TrampolinePtr, signature); } return(TrampolineMethod); }