{"id":1212,"date":"2016-03-13T03:47:28","date_gmt":"2016-03-13T03:47:28","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=1212"},"modified":"2016-03-13T03:47:56","modified_gmt":"2016-03-13T03:47:56","slug":"detours-trampolines-and-code-caves","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2016\/03\/detours-trampolines-and-code-caves\/","title":{"rendered":"Detours, Trampolines, and Code Caves"},"content":{"rendered":"<p>Howdy fellow RE folk!<\/p>\n<p>Today I&#8217;d like to cover the basics of detours, trampolines, and code caves. <\/p>\n<p>Traditionally, if one wanted to expand functionality to a program, they have to look for whats called a &#8216;code cave&#8217;, which is just a contiguous piece of (executable) memory. You replace a piece of code (typically a function prologue) with a long jump to your code. Of course its never easy to do this &#8211; if the Import Address Table doesn&#8217;t have the reference to the function \/ dll pair, calling a function will fail. If you wanted to do this statically, you would have to modify the IAT (pain in the ass BTW) to include the functions you&#8217;re calling. This isn&#8217;t always feasible, especially with packed programs and other programs which depend on check sums. That said, its best to do this sort of thing at run time. <\/p>\n<p>For those of you wondering how to do this statically, I figured out how to do modify the IAT in C#. You never know if you&#8217;ll need this code:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0000ff\">private<\/span> <span style=\"color: #0000ff\">void<\/span> AddToIAT(<span style=\"color: #2b91af\">byte<\/span>[] codestuff, <span style=\"color: #2b91af\">string<\/span> path)\r\n{\r\n    PeHeaderReader lolpe = <span style=\"color: #0000ff\">new<\/span> PeHeaderReader(<span style=\"color: #0000ff\">this<\/span>.tbfilehere.Text);\r\n    UInt32 ImportDirectoryRVA = lolpe.OptionalHeader32.ImportTable.VirtualAddress; <span style=\"color: #008000\">\/\/ RVA<\/span>\r\n    FileStream fs = <span style=\"color: #0000ff\">new<\/span> FileStream(path, FileMode.Open);\r\n    BinaryWriter bw = <span style=\"color: #0000ff\">new<\/span> BinaryWriter(fs);\r\n    BinaryReader br = <span style=\"color: #0000ff\">new<\/span> BinaryReader(fs);\r\n    fs.Seek(0x3c, SeekOrigin.Begin); <span style=\"color: #008000\">\/\/ go to the value of e_lfanew<\/span>\r\n    <span style=\"color: #2b91af\">int<\/span> my_e_lfanew = br.ReadInt32();\r\n    UInt32 joe = RvaToOffsetJoe(lolpe, ImportDirectoryRVA) + (UInt32)my_e_lfanew;\r\n    <span style=\"color: #008000\">\/\/ try seeking to the imports area first dumbass or better yet, importdirectoryVA + the <\/span>\r\n\t<span style=\"color: #008000\">\/\/ length of the last entry (lolpe.OptionalHeader32.ImportTable.Size;)<\/span>\r\n\r\n    fs.Seek(RvaToOffsetJoe(lolpe, ImportDirectoryRVA), SeekOrigin.Begin); \r\n    bw.Write(ImportDirectoryRVA + 40);\r\n    bw.Write(0);\r\n    bw.Write(0);\r\n    bw.Write(ImportHintNameTableRVA(lolpe) + 14);\r\n    bw.Write(ImportDirectoryRVA);\r\n    bw.Write(<span style=\"color: #0000ff\">new<\/span> <span style=\"color: #2b91af\">byte<\/span>[20]); \r\n    bw.Write(ImportHintNameTableRVA(lolpe));\r\n    <span style=\"color: #2b91af\">int<\/span> size = 48;\r\n\t<span style=\"color: #0000ff\">if<\/span> (lolpe.FileHeader.Machine != 0x014C) <span style=\"color: #008000\">\/\/ if not 32 bit<\/span>\r\n\t{\r\n\tsize += 4;\r\n\tbw.Write(0);\r\n\t}\r\n\tbw.Write(0);\r\n\r\n\t<span style=\"color: #0000ff\">for<\/span> (<span style=\"color: #2b91af\">int<\/span> i = (<span style=\"color: #2b91af\">int<\/span>)(ImportHintNameTableRVA(lolpe) - \r\n\t(ImportDirectoryRVA + size)); i &gt; 0; i--)\r\n\t{\r\n\tbw.Write((<span style=\"color: #2b91af\">byte<\/span>)0);\r\n\t}\r\n\r\n    bw.Write((<span style=\"color: #2b91af\">ushort<\/span>)0);\r\n    bw.Write(System.Text.Encoding.ASCII.GetBytes(<span style=\"color: #a31515\">&quot;FatalAppExitA&quot;<\/span>));\r\n    bw.Write(0);\r\n    bw.Write(System.Text.Encoding.ASCII.GetBytes(<span style=\"color: #a31515\">&quot;kernel32.dll&quot;<\/span>));\r\n    bw.Write((<span style=\"color: #2b91af\">ushort<\/span>)0);\r\n    fs.Close();\r\n}\r\n\r\n\t\r\nUInt32 RvaToOffsetJoe(PeHeaderReader lolpe, UInt32 Rva)\r\n{\r\n\t<span style=\"color: #2b91af\">int<\/span> o;\r\n\t<span style=\"color: #2b91af\">int<\/span> secx = lolpe.FileHeader.NumberOfSections;\r\n\t<span style=\"color: #0000ff\">for<\/span>(o=0;o&lt;secx;o++)\r\n\t{\r\n\t\t<span style=\"color: #0000ff\">if<\/span> (Rva &gt;= lolpe.ImageSectionHeaders[o].VirtualAddress &amp;&amp; Rva\r\n\t\t&lt; lolpe.ImageSectionHeaders[o].VirtualAddress + \r\n\t\tMath.Max(lolpe.ImageSectionHeaders[o].VirtualSize, lolpe.ImageSectionHeaders[o].SizeOfRawData))\r\n\t\t{\r\n\t\t<span style=\"color: #0000ff\">return<\/span> (UInt32)(Rva - lolpe.ImageSectionHeaders[o].VirtualAddress) + \r\n\t\t(lolpe.ImageSectionHeaders[o].PointerToRawData);\r\n\t\t}\r\n\t\r\n\t}\r\n    <span style=\"color: #0000ff\">throw<\/span> <span style=\"color: #0000ff\">new<\/span> Exception(<span style=\"color: #a31515\">&quot;Failed to convert RVA&quot;<\/span>);\r\n}\r\n<\/pre>\n<\/div>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1453959088604.jpg\" rel=\"attachment wp-att-1220\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1453959088604.jpg\" alt=\"1453959088604\" width=\"299\" height=\"259\" class=\"alignnone size-full wp-image-1220\" \/><\/a><\/p>\n<p>How would this be done dynamically? Many video game trainers do this kind of work. You&#8217;re basically obtaining the base address and calculating the offset of code in memory and performing peeks and pokes (Read\/WriteProcessMemory) to modify program code. Unfortunately, this means you have to roll your own implementation system, essentially re-inventing the wheel. I ain&#8217;t about to do that. Enter detours.<\/p>\n<p>The egg-heads at Microsoft have shared with us the <a href=\"http:\/\/research.microsoft.com\/en-us\/downloads\/d36340fb-4d3c-4ddd-bf5b-1db25d03713d\/default.aspx\" target=\"_blank\">Detours library<\/a>. The library provides a way to patch binaries live in memory without the use of a debugger and all in glorious C. I&#8217;m all for that. How it works is it employs the use of &#8216;trampolines&#8217; that patch the first 5 bytes of a target api or sub procedure or function pointer to perform a long &#8216;jmp&#8217; to controlled code. The code of your choosing is patched, and when you return, the bytes are restored. Sounds simple enough right? Its much easier when you can just call a library function to do this for you. One of my friends is like &#8220;joe, just roll your own&#8221; and I hate re-inventing the wheel. <\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1457578413917.gif\" rel=\"attachment wp-att-1219\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1457578413917.gif\" alt=\"1457578413917\" width=\"471\" height=\"263\" class=\"alignnone size-full wp-image-1219\" \/><\/a><\/p>\n<p>Here&#8217;s a very basic program (compiled to a dll) that illustrates the use of the detours library:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0000ff\">#include &quot;stdafx.h&quot;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;detours.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#pragma comment(lib, &quot;detours.lib&quot;)<\/span>\r\n\r\n\r\n<span style=\"color: #008000\">\/\/ Target pointer for the uninstrumented Sleep API.<\/span>\r\n<span style=\"color: #0000ff\">static<\/span> VOID(WINAPI * TrueSleep)(DWORD dwMilliseconds) = Sleep;\r\n<span style=\"color: #008000\">\/\/ Detour function that replaces the Sleep API.<\/span>\r\nVOID WINAPI AltSleep(DWORD dwMilliseconds)\r\n{\r\n\tFatalAppExitA(0, <span style=\"color: #a31515\">&quot;I HATE SLEEPZ!&quot;<\/span>);\r\n}\r\n\r\n<span style=\"color: #008000\">\/\/ DllMain function attaches and detaches the AltSleep detour to the<\/span>\r\n<span style=\"color: #008000\">\/\/ Sleep target function.  The Sleep target function is referred to<\/span>\r\n<span style=\"color: #008000\">\/\/ through the TrueSleep target pointer.<\/span>\r\n<span style=\"color: #0000ff\">extern<\/span> <span style=\"color: #a31515\">&quot;C&quot;<\/span> <span style=\"color: #0000ff\">__declspec<\/span>(dllexport) <span style=\"color: #2b91af\">void<\/span> DoNothingAlready(<span style=\"color: #2b91af\">void<\/span>)\r\n{\r\n\tDWORD ayylmao = 20345;\r\n\t_asm\r\n\t{\r\n\t\txor eax, eax\r\n\t\t\txor ecx, ecx\r\n\t\t\tmov eax, ayylmao\r\n\t\t\tmov ecx, 0\r\n\t\ttestd:\r\n\t\tfnop\r\n\t\t\tinc ecx\r\n\t\t\tcmp eax, ecx\r\n\t\t\tjnz testd\r\n\t\t\tpop ebx\r\n\t\t\tnop\r\n\t}\r\n\t<span style=\"color: #0000ff\">return<\/span>;\r\n}\r\n\r\nBOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)\r\n{\r\n\t<span style=\"color: #0000ff\">if<\/span> (DetourIsHelperProcess()) {\r\n\t\t<span style=\"color: #0000ff\">return<\/span> TRUE;\r\n\t}\r\n\r\n\t<span style=\"color: #0000ff\">if<\/span> (dwReason == DLL_PROCESS_ATTACH) {\r\n\t\tDetourRestoreAfterWith();\r\n\r\n\t\tDetourTransactionBegin();\r\n\t\tDetourUpdateThread(GetCurrentThread());\r\n\t\tDetourAttach(&amp;(PVOID&amp;)TrueSleep, AltSleep);\r\n\t\tDetourTransactionCommit();\r\n\t}\r\n\t<span style=\"color: #0000ff\">else<\/span> <span style=\"color: #0000ff\">if<\/span> (dwReason == DLL_PROCESS_DETACH) {\r\n\t\tDetourTransactionBegin();\r\n\t\tDetourUpdateThread(GetCurrentThread());\r\n\t\tDetourDetach(&amp;(PVOID&amp;)TrueSleep, AltSleep);\r\n\t\tDetourTransactionCommit();\r\n\t}\r\n\t<span style=\"color: #0000ff\">return<\/span> TRUE;\r\n}\r\n<\/pre>\n<\/div>\n<p>Note the dummy external function &#8216;DoNothingAlready&#8217; in the dll. You need at least 1 export present for this to work. <\/p>\n<p>The program I&#8217;m going to be modifying is my old keygen solution from an older project. I&#8217;ll be catching the Sleep API within the &#8216;GetKey&#8217; function and replacing it with a call to &#8216;FatalAppExitA&#8217;.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0000ff\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;stdio.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#define LoWord(l) ((WORD)(l))<\/span>\r\n<span style=\"color: #0000ff\">#define HiWord(l) ((WORD)(((DWORD)(l) &gt;&gt; 16) &amp; 0xFFFF)) <\/span><span style=\"color: #008000\">\/\/ pelles doesnt support hi \/ lo words <\/span>\r\n<span style=\"color: #008000\">\/\/ which just splits and shifts the double word. whatever. <\/span>\r\n<span style=\"color: #2b91af\">unsigned<\/span> <span style=\"color: #2b91af\">long<\/span> GenKey(DWORD);\r\n\r\n<span style=\"color: #2b91af\">int<\/span> main(<span style=\"color: #2b91af\">int<\/span> argc, <span style=\"color: #2b91af\">char<\/span> *argv[])\r\n{\r\n\t<span style=\"color: #2b91af\">char<\/span> root[MAX_PATH] = <span style=\"color: #a31515\">&quot;C:\\\\&quot;<\/span>;\r\n\t<span style=\"color: #2b91af\">char<\/span> volname[MAX_PATH];\r\n\tDWORD serial, maxname, flags;\r\n\t<span style=\"color: #2b91af\">char<\/span> fsName[128];\r\n\tDWORD fsNameLength;\tfsNameLength = <span style=\"color: #0000ff\">sizeof<\/span>(fsName)-1;\r\n\tGetVolumeInformation(root, volname, MAX_PATH,&amp;serial, &amp;maxname, &amp;flags, fsName, fsNameLength);\r\n\tprintf(<span style=\"color: #a31515\">&quot;Name of System: %s\\r\\n&quot;<\/span>,fsName);\r\n\t<span style=\"color: #0000ff\">if<\/span>(volname[0] == <span style=\"color: #a31515\">&#39;\\0&#39;<\/span>) <span style=\"color: #008000\">\/\/ lol null<\/span>\r\n\t{\r\n\tprintf(<span style=\"color: #a31515\">&quot;No Volume name present\\r\\n&quot;<\/span>);\r\n\t}\r\n\t<span style=\"color: #0000ff\">else<\/span>\r\n\t{\r\n\tprintf(<span style=\"color: #a31515\">&quot;Volume Name: %s\\r\\n&quot;<\/span>,volname);\r\n\t}\r\n\tprintf(<span style=\"color: #a31515\">&quot;Volume Serial Number: %x\\r\\n&quot;<\/span>,serial);\r\n\tprintf(<span style=\"color: #a31515\">&quot;Your Special key: %x\\r\\n&quot;<\/span>,GenKey(serial));\r\n\tSleep(50000);\r\n\tsystem(<span style=\"color: #a31515\">&quot;pause&quot;<\/span>);\r\n\t<span style=\"color: #0000ff\">return<\/span> 0;\r\n}\r\n\r\n<span style=\"color: #2b91af\">unsigned<\/span> <span style=\"color: #2b91af\">long<\/span> GenKey(DWORD serial)\r\n{\r\nSleep(100000);\r\nDWORD seed1 = 4096;\r\nDWORD seed2 = HiWord(serial);\r\nDWORD retme = seed1 ^ serial * seed2;\r\n<span style=\"color: #0000ff\">return<\/span> retme;\r\n}\r\n<\/pre>\n<\/div>\n<p>How do we get our dll to run inside this program? Basic dll injection wont work, so we have to use a special API to launch the process along side our dll to make this run.<\/p>\n<p>Our launcher employs the &#8216;DetourCreateProcessWithDllEx&#8217; function from the detours lib to launch our special dll and program side by side:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0000ff\">#include &quot;stdafx.h&quot;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;Windows.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;detours.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#pragma comment(lib,&quot;detours.lib&quot;)<\/span>\r\n\r\n<span style=\"color: #2b91af\">int<\/span> _tmain(<span style=\"color: #2b91af\">int<\/span> argc, _TCHAR* argv[])\r\n{\r\n\t<span style=\"color: #0000ff\">if<\/span> (argc &lt; 3)\r\n\t{\r\n\t\tprintf(<span style=\"color: #a31515\">&quot;Error, usage is %s &#39;target process&#39; &#39;dll&#39;\\r\\n&quot;<\/span>, argv[0]);\r\n\t\t<span style=\"color: #0000ff\">return<\/span> 1;\r\n\t}\r\n\t_TCHAR *prog = argv[1];\r\n\t\r\n\tSTARTUPINFO si;\r\n\tPROCESS_INFORMATION pi;\r\n\r\n\tZeroMemory(&amp;si, <span style=\"color: #0000ff\">sizeof<\/span>(si));\r\n\tZeroMemory(&amp;pi, <span style=\"color: #0000ff\">sizeof<\/span>(pi));\r\n\tsi.cb = <span style=\"color: #0000ff\">sizeof<\/span>(si);\r\n\tsi.dwFlags = STARTF_USESHOWWINDOW;\r\n\tsi.wShowWindow = SW_SHOW;\r\n\t<span style=\"color: #0000ff\">if<\/span> (!DetourCreateProcessWithDllEx(prog,\r\n\t\tNULL, NULL, NULL, TRUE,\r\n\t\tCREATE_DEFAULT_ERROR_MODE | CREATE_SUSPENDED,\r\n\t\tNULL, NULL, &amp;si, &amp;pi,\r\n\t\t<span style=\"color: #a31515\">&quot;ayy.dll&quot;<\/span>, NULL))\r\n\t{\r\n\t\tMessageBox(0, <span style=\"color: #a31515\">L&quot;failed&quot;<\/span>, 0, 0);\r\n\t\t<span style=\"color: #0000ff\">return<\/span> 1;\r\n\t}\r\n\t<span style=\"color: #0000ff\">else<\/span>\r\n\t{\r\n\t\tResumeThread(pi.hThread);\r\n\r\n\t\tWaitForSingleObject(pi.hProcess, INFINITE);\r\n\r\n\t\tCloseHandle(&amp;si);\r\n\t\tCloseHandle(&amp;pi);\r\n\t}\r\n\r\n\t\r\n\t<span style=\"color: #0000ff\">return<\/span> 0;\r\n}\r\n<\/pre>\n<\/div>\n<p>Here&#8217;s a quick video of this in action:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/TlDpANEnXKI\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>This example shows how you modify programs where you have API and function names as well as source. Where&#8217;s the fun in that? Let&#8217;s modify some stuff without the benefit of source code. <\/p>\n<p>Here we have Audacity open in IDA. I chose the sub routine responsible for when you hit the about button. <\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad.png\" rel=\"attachment wp-att-1218\" target=\"_blank\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad-300x159.png\" alt=\"idad\" width=\"300\" height=\"159\" class=\"alignnone size-medium wp-image-1218\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad-300x159.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad-768x407.png 768w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad-1024x542.png 1024w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/idad.png 1532w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now that we have our address &#8216;0x40c910&#8217;, we can throw that into our detour dll and rock \/ roll. The code looks something like this:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #0000ff\">#include &quot;stdafx.h&quot;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#include &lt;detours.h&gt;<\/span>\r\n<span style=\"color: #0000ff\">#pragma comment(lib, &quot;detours.lib&quot;)<\/span>\r\n\r\n<span style=\"color: #0000ff\">typedef<\/span> int (WINAPI *pFunc)(<span style=\"color: #2b91af\">int<\/span>,<span style=\"color: #2b91af\">int<\/span>);\r\n<span style=\"color: #2b91af\">int<\/span> WINAPI MyFunc(<span style=\"color: #2b91af\">int<\/span>,<span style=\"color: #2b91af\">int<\/span>);\r\npFunc FuncToDetour = (pFunc)(0x40C910); <span style=\"color: #008000\">\/\/Set it at address to detour in<\/span>\r\n\r\n<span style=\"color: #2b91af\">int<\/span> WINAPI MyFunc(<span style=\"color: #2b91af\">int<\/span> a, <span style=\"color: #2b91af\">int<\/span> b)\r\n{\r\n\tMessageBox(NULL, <span style=\"color: #a31515\">L&quot;Audacity rocks!&quot;<\/span>, <span style=\"color: #a31515\">L&quot;Joe was here&quot;<\/span>, MB_OK);\r\n\t<span style=\"color: #0000ff\">return<\/span> 4;\r\n}\r\n<span style=\"color: #0000ff\">extern<\/span> <span style=\"color: #a31515\">&quot;C&quot;<\/span> <span style=\"color: #0000ff\">__declspec<\/span>(dllexport) <span style=\"color: #2b91af\">void<\/span> DoNothingAlready(<span style=\"color: #2b91af\">void<\/span>)\r\n{\r\n\tDWORD ayylmao = 20345;\r\n\t_asm\r\n\t{\r\n\t\txor eax, eax\r\n\t\t\txor ecx, ecx\r\n\t\t\tmov eax, ayylmao\r\n\t\t\tmov ecx, 0\r\n\t\ttestd:\r\n\t\tfnop\r\n\t\t\tinc ecx\r\n\t\t\tcmp eax, ecx\r\n\t\t\tjnz testd\r\n\t\t\tpop ebx\r\n\t\t\tnop\r\n\t}\r\n\t<span style=\"color: #0000ff\">return<\/span>;\r\n}\r\n\r\nBOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)\r\n{\r\n\t<span style=\"color: #0000ff\">if<\/span> (DetourIsHelperProcess()) {\r\n\t\t<span style=\"color: #0000ff\">return<\/span> TRUE;\r\n\t}\r\n\r\n\t<span style=\"color: #0000ff\">if<\/span> (dwReason == DLL_PROCESS_ATTACH) {\r\n\t\tDetourRestoreAfterWith();\r\n\t\tDetourTransactionBegin();\r\n\t\tDetourUpdateThread(GetCurrentThread());\r\n\t\tDetourAttach(&amp;(PVOID&amp;)FuncToDetour, MyFunc);\r\n\t\tDetourTransactionCommit();\r\n\t}\r\n\t<span style=\"color: #0000ff\">else<\/span> <span style=\"color: #0000ff\">if<\/span> (dwReason == DLL_PROCESS_DETACH) {\r\n\t\tDetourTransactionBegin();\r\n\t\tDetourUpdateThread(GetCurrentThread());\r\n\t\tDetourDetach(&amp;(PVOID&amp;)FuncToDetour, MyFunc);\r\n\t\tDetourTransactionCommit();\r\n\t}\r\n\t<span style=\"color: #0000ff\">return<\/span> TRUE;\r\n}\r\n<\/pre>\n<\/div>\n<p>Here we again have a video of what this looks like:<\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/BO2BVMjqzmw\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>There you have it. Maybe later I&#8217;ll show you all how to use detours to cheat at age of empires and piss off family members. <\/p>\n<p><a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1449004430529.jpg\" rel=\"attachment wp-att-1221\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1449004430529.jpg\" alt=\"1449004430529\" width=\"403\" height=\"312\" class=\"alignnone size-full wp-image-1221\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1449004430529.jpg 403w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2016\/03\/1449004430529-300x232.jpg 300w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Howdy fellow RE folk! Today I&#8217;d like to cover the basics of detours, trampolines, and code caves. Traditionally, if one wanted to expand functionality to a program, they have to look for whats called a &#8216;code cave&#8217;, which is just a contiguous piece of (executable) memory. You replace a piece of code (typically a function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[104,108],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1212"}],"collection":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/comments?post=1212"}],"version-history":[{"count":8,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1225,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1212\/revisions\/1225"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}