{"id":1449,"date":"2017-07-17T07:48:35","date_gmt":"2017-07-17T07:48:35","guid":{"rendered":"http:\/\/www.gironsec.com\/blog\/?p=1449"},"modified":"2017-07-17T07:48:35","modified_gmt":"2017-07-17T07:48:35","slug":"intel-pin-cheatz-hax-and-detection-part-2","status":"publish","type":"post","link":"https:\/\/www.gironsec.com\/blog\/2017\/07\/intel-pin-cheatz-hax-and-detection-part-2\/","title":{"rendered":"Intel PIN, Cheatz, Hax, And Detection Part 2"},"content":{"rendered":"<p>Hi!<\/p>\n<p>Today I&#8217;m going to go over more on intel&#8217;s PIN, more on cheats, and less on detection since I already covered that.  I feel like I&#8217;ve spent way too much time on this and it&#8217;s a huge turn off against my productivity. And of course, I commit to a talk and it HAS to be done. Blech. At least I can now work on the talk more. <\/p>\n<p>As I&#8217;ve gone over before, PIN allows us to dynamically instrument programs without source code. The documentation is nice, but leaves some things to be desired. For example &#8211; how in the hell do you stop on a specific memory address rather than some API name? <\/p>\n<p>Cheats and PIN. <\/p>\n<p>Traditionally a cheat will make use of (WriteProcessMemory) or something similiar to &#8216;peek&#8217; and &#8216;poke&#8217; memory addresses and with values of our choice. Once we&#8217;ve established the right memory addresses and values of course, we write our 3rd party &#8216;helper&#8217; app (trainer) that attaches to a game and does our peek \/ poke. Here is what a typical cheat looks like in C# for windows. C# because not everything has to be C\/C++, and managed code is awesome for those kick ass easy guis:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Collections.Generic<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Linq<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Text<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Threading.Tasks<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Diagnostics<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">using<\/span> <span style=\"color: #000000\">System.Runtime.InteropServices<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">namespace<\/span> <span style=\"color: #000000\">Trainer<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n    <span style=\"color: #204a87; font-weight: bold\">class<\/span> <span style=\"color: #000000\">Program<\/span>\r\n    <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;kernel32.dll&quot;)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">internal<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">OpenProcess<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">dwDesiredAccess<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">bInheritHandle<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">dwProcessId<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;kernel32.dll&quot;, SetLastError = true)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">WriteProcessMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">hProcess<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">lpBaseAddress<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">lpBuffer<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">nSize<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">out<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">lpNumberOfBytesWritten<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;kernel32.dll&quot;)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">ReadProcessMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">hProcess<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">lpBaseAddress<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">lpBuffer<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">UInt32<\/span> <span style=\"color: #000000\">nSize<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">ref<\/span> <span style=\"color: #000000\">UInt32<\/span> <span style=\"color: #000000\">lpNumberOfBytesRead<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n<span style=\"color: #c4a000\">        <\/span>\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;kernel32.dll&quot;)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">CloseHandle<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">hHandle<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;User32.dll&quot;)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">private<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #204a87; font-weight: bold\">short<\/span> <span style=\"color: #000000\">GetAsyncKeyState<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">System<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">Int32<\/span> <span style=\"color: #000000\">vKey<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n<span style=\"color: #c4a000\">        [DllImport(&quot;kernel32.dll&quot;)]<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">VirtualProtectEx<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">hProcess<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">lpAddress<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">UIntPtr<\/span> <span style=\"color: #000000\">dwSize<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">uint<\/span> <span style=\"color: #000000\">flNewProtect<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">out<\/span> <span style=\"color: #204a87; font-weight: bold\">uint<\/span> <span style=\"color: #000000\">lpflOldProtect<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">ProcID<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">Handle<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">class<\/span> <span style=\"color: #000000\">OutIgnore<\/span><span style=\"color: #000000; font-weight: bold\">&lt;<\/span><span style=\"color: #000000\">T<\/span><span style=\"color: #000000; font-weight: bold\">&gt;<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n<span style=\"color: #c4a000\">            [ThreadStatic]<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #000000\">T<\/span> <span style=\"color: #000000\">Ignored<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">WriteMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">data<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #000000\">WriteProcessMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Handle<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">data<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">data<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">Length<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">out<\/span> <span style=\"color: #000000\">OutIgnore<\/span><span style=\"color: #000000; font-weight: bold\">&lt;<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">&gt;.<\/span><span style=\"color: #000000\">Ignored<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">Read<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">length<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">ret<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #204a87; font-weight: bold\">new<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #000000\">length<\/span><span style=\"color: #000000; font-weight: bold\">];<\/span>\r\n            <span style=\"color: #000000\">ReadProcessMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Handle<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ret<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">UInt32<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">ret<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">Length<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">ref<\/span> <span style=\"color: #000000\">OutIgnore<\/span><span style=\"color: #000000; font-weight: bold\">&lt;<\/span><span style=\"color: #204a87; font-weight: bold\">uint<\/span><span style=\"color: #000000; font-weight: bold\">&gt;.<\/span><span style=\"color: #000000\">Ignored<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">ret<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">BitConverter<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">ToInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Read<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Address<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">),<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span> <span style=\"color: #000000\">ByteReadOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">Read<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">1<\/span><span style=\"color: #000000; font-weight: bold\">)[<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">];<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">float<\/span> <span style=\"color: #000000\">FloatReadOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">naddr<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">Read<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">sizeof<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">float<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">BitConverter<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">ToSingle<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">naddr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">IntReadOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">naddr<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">Read<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">sizeof<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">BitConverter<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">ToInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">naddr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">public<\/span> <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">WriteOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IntPtr<\/span> <span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">res<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">len<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #000000\">WriteMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">address<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">res<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t\t\r\n        <span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">Main<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">string<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000\">args<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n            <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">WriteLine<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Started. W8 process....&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n            <span style=\"color: #204a87; font-weight: bold\">while<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ProcID<\/span> <span style=\"color: #000000; font-weight: bold\">==<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n            <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n                <span style=\"color: #204a87; font-weight: bold\">foreach<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Process<\/span> <span style=\"color: #000000\">id<\/span> <span style=\"color: #204a87; font-weight: bold\">in<\/span> <span style=\"color: #000000\">Process<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">GetProcessesByName<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;AoK HD&quot;<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span>\r\n                <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n                    <span style=\"color: #000000\">ProcID<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">id<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">Id<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n                <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n            <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n            <span style=\"color: #000000\">Handle<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">OpenProcess<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">x001F0FFF<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">false<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ProcID<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n            \r\n            <span style=\"color: #204a87; font-weight: bold\">bool<\/span> <span style=\"color: #000000\">makeitrain<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #204a87; font-weight: bold\">false<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t\t\t\r\n            <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">WriteLine<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Injected&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\t\t\r\n            <span style=\"color: #204a87; font-weight: bold\">while<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">true<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n            <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n                <span style=\"color: #204a87; font-weight: bold\">string<\/span> <span style=\"color: #000000\">cmd<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">ReadLine<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\r\n                <span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">cmd<\/span> <span style=\"color: #000000; font-weight: bold\">==<\/span> <span style=\"color: #4e9a06\">&quot;exit&quot;<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n                    <span style=\"color: #204a87; font-weight: bold\">return<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n                <span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">cmd<\/span> <span style=\"color: #000000; font-weight: bold\">==<\/span> <span style=\"color: #4e9a06\">&quot;makeitrain&quot;<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n                <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n                    <span style=\"color: #000000\">makeitrain<\/span> <span style=\"color: #000000; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(!<\/span><span style=\"color: #000000\">makeitrain<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n                    <span style=\"color: #000000\">WriteMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">xA444A4<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(!<\/span><span style=\"color: #000000\">makeitrain<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #000000; font-weight: bold\">?<\/span> <span style=\"color: #204a87; font-weight: bold\">new<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">x0<\/span> <span style=\"color: #000000; font-weight: bold\">}<\/span> <span style=\"color: #000000; font-weight: bold\">:<\/span> <span style=\"color: #204a87; font-weight: bold\">new<\/span> <span style=\"color: #204a87; font-weight: bold\">byte<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">x1<\/span> <span style=\"color: #000000; font-weight: bold\">});<\/span>\r\n                <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n                <span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">cmd<\/span> <span style=\"color: #000000; font-weight: bold\">==<\/span> <span style=\"color: #4e9a06\">&quot;get&quot;<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n                <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n                    <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">WriteLine<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;State: &quot;<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">CPed<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">GetByteOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">x46F<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n                    <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">WriteLine<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Animation State: &quot;<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">CPed<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">GetByteOffset<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">x4DF<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n                    <span style=\"color: #000000\">Console<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">WriteLine<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Gold: &quot;<\/span> <span style=\"color: #000000; font-weight: bold\">+<\/span> <span style=\"color: #000000\">ReadInt32<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">IntPtr<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000\">xB7CE50<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">4<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n                    \r\n                <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n            <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n        <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n    <span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Your classic trainer in action &#8211; Find a memory address, poke it with a new value. Easy right? Can you guess the game? Hint: <i>GetProcessesByName(&#8220;AoK HD&#8221;)<\/i>. That&#8217;s right, age of empires. <\/p>\n<p>It&#8217;s pretty simple in action &#8211; writes itself directly to the address space of the process after opening a handle to it. This will work for a lot of games too. The problem is, many games no check for this sort of thing. We have to be more creative. But how?<\/p>\n<p>Well, we could try DLL Injection. For those unfamiliar with DLL Injection, we basically write a dll, place code in the &#8216;DLL_PROCESS_ATTACH&#8217; region, then when our dll is injected, our code is run in the address space of the game \/ process. This is a bonus because it makes the program think it belongs or something. <\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #8f5902; font-style: italic\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;tlhelp32.h&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;Psapi.h&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;stdio.h&gt; <\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">static<\/span> <span style=\"color: #204a87; font-weight: bold\">const<\/span> <span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">ammo<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #0000cf; font-weight: bold\">0x0EB9B878<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000\">HANDLE<\/span> <span style=\"color: #000000\">dllhandle<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n<span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">ThreadProc<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">LPVOID<\/span> <span style=\"color: #000000\">lpdwThreadParam<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\r\n<span style=\"color: #000000\">MODULEINFO<\/span> <span style=\"color: #000000\">GetModuleInfo<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span> <span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">szModule<\/span> <span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">MODULEINFO<\/span> <span style=\"color: #000000\">modinfo<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span><span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">};<\/span>\r\n\t<span style=\"color: #000000\">HMODULE<\/span> <span style=\"color: #000000\">hModule<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">GetModuleHandle<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">szModule<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">hModule<\/span> <span style=\"color: #ce5c00; font-weight: bold\">==<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> \r\n\t\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">modinfo<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">GetModuleInformation<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">GetCurrentProcess<\/span><span style=\"color: #000000; font-weight: bold\">(),<\/span> <span style=\"color: #000000\">hModule<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">modinfo<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">sizeof<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">MODULEINFO<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">modinfo<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">WriteToMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DWORD<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">addressToWrite<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">char<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">valueToWrite<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">byteNum<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">unsigned<\/span> <span style=\"color: #204a87; font-weight: bold\">long<\/span> <span style=\"color: #000000\">OldProtection<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">VirtualProtect<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">LPVOID<\/span><span style=\"color: #000000; font-weight: bold\">)(<\/span><span style=\"color: #000000\">addressToWrite<\/span><span style=\"color: #000000; font-weight: bold\">),<\/span> <span style=\"color: #000000\">byteNum<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">PAGE_EXECUTE_READWRITE<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">OldProtection<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">memcpy<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">LPVOID<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">addressToWrite<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">valueToWrite<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">byteNum<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">VirtualProtect<\/span><span style=\"color: #000000; font-weight: bold\">((<\/span><span style=\"color: #000000\">LPVOID<\/span><span style=\"color: #000000; font-weight: bold\">)(<\/span><span style=\"color: #000000\">addressToWrite<\/span><span style=\"color: #000000; font-weight: bold\">),<\/span> <span style=\"color: #000000\">byteNum<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">OldProtection<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">ChangeMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">baseadress<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">value<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">offset1<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">offset2<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">BOOL<\/span> <span style=\"color: #000000\">msg<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #8f5902; font-style: italic\">\/\/DWORD d, ds;<\/span>\r\n\t<span style=\"color: #000000\">DWORD<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">adress<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DWORD<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000; font-weight: bold\">)((<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DWORD<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000; font-weight: bold\">)(<\/span><span style=\"color: #000000\">baseadress<\/span> <span style=\"color: #ce5c00; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset1<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span> <span style=\"color: #ce5c00; font-weight: bold\">+<\/span> <span style=\"color: #000000\">offset2<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\t\r\n \r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">msg<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">szTest<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #0000cf; font-weight: bold\">10<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span> <span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t\t<span style=\"color: #000000\">sprintf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">szTest<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;The final adress is : %X&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">adress<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\t<span style=\"color: #000000\">MessageBox<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">szTest<\/span> <span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">MB_OK<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n \r\n\t<span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span><span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">adress<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">value<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">BOOL<\/span> <span style=\"color: #000000\">WINAPI<\/span> <span style=\"color: #000000\">DllMain<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">(<\/span>\r\n    <span style=\"color: #000000\">HANDLE<\/span>   <span style=\"color: #000000\">hinstDLL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span>\r\n    <span style=\"color: #000000\">DWORD<\/span>       <span style=\"color: #000000\">fdwReason<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span>\r\n    <span style=\"color: #000000\">LPVOID<\/span>      <span style=\"color: #000000\">lpvReserved<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n    <span style=\"color: #204a87; font-weight: bold\">if<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">fdwReason<\/span><span style=\"color: #ce5c00; font-weight: bold\">==<\/span><span style=\"color: #000000\">DLL_PROCESS_ATTACH<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n    <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n \t\t<span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">threadId<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t\t<span style=\"color: #000000\">dllhandle<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">hinstDLL<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n        <span style=\"color: #000000\">CreateThread<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">LPTHREAD_START_ROUTINE<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">ThreadProc<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">threadId<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n   \r\n    <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #0000cf; font-weight: bold\">1<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">ThreadProc<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">LPVOID<\/span> <span style=\"color: #000000\">lpdwThreadParam<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">MODULEINFO<\/span> <span style=\"color: #000000\">mInfo<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">GetModuleInfo<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;fearxp.exe&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">base<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DWORD<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">mInfo<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">lpBaseOfDll<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">ChangeMemory<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">base<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #0000cf; font-weight: bold\">1234<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">ammo<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">ammo<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">FALSE<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n   \r\n    <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>The idea is pretty simple &#8211; poke a memory address after creating a new thread. The thread runs in the context of the running program. I know what you&#8217;re thinking &#8211; &#8220;Joe, isn&#8217;t this good enough for my cheats? surely the game won&#8217;t detect this? right?&#8221; and you&#8217;d be half right. Some game devs are clever assholes. We need to be more clever than them to inject code into their products and not have them caught. We can always use hooking and detours to do our injection. <\/p>\n<p>Recall, I&#8217;ve <a href=\"https:\/\/www.gironsec.com\/blog\/2016\/03\/detours-trampolines-and-code-caves\/\" target=\"_blank\">talked about detours before<\/a>. Yeah those, we can use those for game hacking. Given we have our function pointer address \/ memory address or function name, we can &#8216;detour&#8217; to our code to peek \/ poke whatever address \/ value combo we want. Though I used detouring for more&#8230;evil purposes, however the concepts carry over.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #8f5902; font-style: italic\">#include &quot;stdafx.h&quot;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;detours.h&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#pragma comment(lib, &quot;detours.lib&quot;)<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">typedef<\/span> <span style=\"color: #000000\">void<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">WINAPI<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">pFunc<\/span><span style=\"color: #000000; font-weight: bold\">)(<\/span><span style=\"color: #204a87; font-weight: bold\">void<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">WINAPI<\/span> <span style=\"color: #000000\">MyFunc<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">void<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n<span style=\"color: #000000\">pFunc<\/span> <span style=\"color: #000000\">FuncToDetour<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">pFunc<\/span><span style=\"color: #000000; font-weight: bold\">)(<\/span><span style=\"color: #0000cf; font-weight: bold\">0x4BD11B<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span> <span style=\"color: #8f5902; font-style: italic\">\/\/ Set it at address to detour in<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">WINAPI<\/span> <span style=\"color: #000000\">MyFunc<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">void<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">MessageBox<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">L&quot;NONO&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">L&quot;TEST&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">MB_OK<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">unsigned<\/span> <span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">buf<\/span><span style=\"color: #000000; font-weight: bold\">[]<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xfc\\xe8\\x82\\x00\\x00\\x00\\x60\\x89\\xe5\\x31\\xc0\\x64\\x8b\\x50\\x30&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x8b\\x52\\x0c\\x8b\\x52\\x14\\x8b\\x72\\x28\\x0f\\xb7\\x4a\\x26\\x31\\xff&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xac\\x3c\\x61\\x7c\\x02\\x2c\\x20\\xc1\\xcf\\x0d\\x01\\xc7\\xe2\\xf2\\x52&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x57\\x8b\\x52\\x10\\x8b\\x4a\\x3c\\x8b\\x4c\\x11\\x78\\xe3\\x48\\x01\\xd1&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x51\\x8b\\x59\\x20\\x01\\xd3\\x8b\\x49\\x18\\xe3\\x3a\\x49\\x8b\\x34\\x8b&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x01\\xd6\\x31\\xff\\xac\\xc1\\xcf\\x0d\\x01\\xc7\\x38\\xe0\\x75\\xf6\\x03&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x7d\\xf8\\x3b\\x7d\\x24\\x75\\xe4\\x58\\x8b\\x58\\x24\\x01\\xd3\\x66\\x8b&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x0c\\x4b\\x8b\\x58\\x1c\\x01\\xd3\\x8b\\x04\\x8b\\x01\\xd0\\x89\\x44\\x24&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x24\\x5b\\x5b\\x61\\x59\\x5a\\x51\\xff\\xe0\\x5f\\x5f\\x5a\\x8b\\x12\\xeb&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x8d\\x5d\\x68\\x33\\x32\\x00\\x00\\x68\\x77\\x73\\x32\\x5f\\x54\\x68\\x4c&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x77\\x26\\x07\\xff\\xd5\\xb8\\x90\\x01\\x00\\x00\\x29\\xc4\\x54\\x50\\x68&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x29\\x80\\x6b\\x00\\xff\\xd5\\x6a\\x08\\x59\\x50\\xe2\\xfd\\x40\\x50\\x40&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x50\\x68\\xea\\x0f\\xdf\\xe0\\xff\\xd5\\x97\\x68\\x02\\x00\\x1f\\x90\\x89&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xe6\\x6a\\x10\\x56\\x57\\x68\\xc2\\xdb\\x37\\x67\\xff\\xd5\\x57\\x68\\xb7&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xe9\\x38\\xff\\xff\\xd5\\x57\\x68\\x74\\xec\\x3b\\xe1\\xff\\xd5\\x57\\x97&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x68\\x75\\x6e\\x4d\\x61\\xff\\xd5\\x68\\x63\\x6d\\x64\\x00\\x89\\xe3\\x57&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x57\\x57\\x31\\xf6\\x6a\\x12\\x59\\x56\\xe2\\xfd\\x66\\xc7\\x44\\x24\\x3c&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x01\\x01\\x8d\\x44\\x24\\x10\\xc6\\x00\\x44\\x54\\x50\\x56\\x56\\x56\\x46&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x56\\x4e\\x56\\x56\\x53\\x56\\x68\\x79\\xcc\\x3f\\x86\\xff\\xd5\\x89\\xe0&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\x4e\\x56\\x46\\xff\\x30\\x68\\x08\\x87\\x1d\\x60\\xff\\xd5\\xbb\\xaa\\xc5&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xe2\\x5d\\x68\\xa6\\x95\\xbd\\x9d\\xff\\xd5\\x3c\\x06\\x7c\\x0a\\x80\\xfb&quot;<\/span>\r\n\t\t<span style=\"color: #4e9a06\">&quot;\\xe0\\x75\\x05\\xbb\\x47\\x13\\x72\\x6f\\x6a\\x00\\x53\\xff\\xd5&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">extern<\/span> <span style=\"color: #4e9a06\">&quot;C&quot;<\/span> <span style=\"color: #204a87; font-weight: bold\">__declspec<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">dllexport<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">DoNothingAlready<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">void<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">ayylmao<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #0000cf; font-weight: bold\">20345<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">_asm<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #000000\">xor<\/span> <span style=\"color: #000000\">eax<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">eax<\/span>\r\n\t\t\t<span style=\"color: #000000\">xor<\/span> <span style=\"color: #000000\">ecx<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ecx<\/span>\r\n\t\t\t<span style=\"color: #000000\">mov<\/span> <span style=\"color: #000000\">eax<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ayylmao<\/span>\r\n\t\t\t<span style=\"color: #000000\">mov<\/span> <span style=\"color: #000000\">ecx<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span>\r\n\t\t<span style=\"color: #f57900\">testd:<\/span>\r\n\t\t<span style=\"color: #000000\">fnop<\/span>\r\n\t\t\t<span style=\"color: #000000\">inc<\/span> <span style=\"color: #000000\">ecx<\/span>\r\n\t\t\t<span style=\"color: #000000\">cmp<\/span> <span style=\"color: #000000\">eax<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ecx<\/span>\r\n\t\t\t<span style=\"color: #000000\">jnz<\/span> <span style=\"color: #000000\">testd<\/span>\r\n\t\t\t<span style=\"color: #000000\">pop<\/span> <span style=\"color: #000000\">ebx<\/span>\r\n\t\t\t<span style=\"color: #000000\">nop<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">BOOL<\/span> <span style=\"color: #000000\">WINAPI<\/span> <span style=\"color: #000000\">DllMain<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">HINSTANCE<\/span> <span style=\"color: #000000\">hinst<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">DWORD<\/span> <span style=\"color: #000000\">dwReason<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">LPVOID<\/span> <span style=\"color: #000000\">reserved<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">DetourIsHelperProcess<\/span><span style=\"color: #000000; font-weight: bold\">())<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">TRUE<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">dwReason<\/span> <span style=\"color: #ce5c00; font-weight: bold\">==<\/span> <span style=\"color: #000000\">DLL_PROCESS_ATTACH<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #000000\">DetourRestoreAfterWith<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t\t<span style=\"color: #000000\">DetourTransactionBegin<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t\t<span style=\"color: #000000\">DetourUpdateThread<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">GetCurrentThread<\/span><span style=\"color: #000000; font-weight: bold\">());<\/span>\r\n\t\t<span style=\"color: #000000\">DetourAttach<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">PVOID<\/span><span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">FuncToDetour<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">MyFunc<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\t<span style=\"color: #000000\">DetourTransactionCommit<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">else<\/span> <span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">dwReason<\/span> <span style=\"color: #ce5c00; font-weight: bold\">==<\/span> <span style=\"color: #000000\">DLL_PROCESS_DETACH<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #000000\">DetourTransactionBegin<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t\t<span style=\"color: #000000\">DetourUpdateThread<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">GetCurrentThread<\/span><span style=\"color: #000000; font-weight: bold\">());<\/span>\r\n\t\t<span style=\"color: #000000\">DetourDetach<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">PVOID<\/span><span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">FuncToDetour<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">MyFunc<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\t<span style=\"color: #000000\">DetourTransactionCommit<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">TRUE<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>This is certainly something that would be hard to detect right? So what else is there to inject code in? Enter binary instrumentation and PIN. A mad man could possibly conceive a way to inject code and hack games using a framework that&#8217;s been used for all kinds of things from unpacking malware, to finding bugs and memory leaks. It seems game hacking with PIN is just inevitable. <\/p>\n<p>But joe you might say, &#8220;how the hell do you stop on a particular memory address in PIN? I don&#8217;t see a way to specify a pointer like with detours, only function names!&#8221; and you&#8217;d be right. But keep in mind, with PIN we have full control of the context \/ registers. It&#8217;s a simple matter of halting on a particular instruction pointer address. Easy peasy. Observe:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #8f5902; font-style: italic\">\/*<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">alexander hanel - thanks alex, much love. <\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">log IP of all instructions that do not reside in a module. <\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">*\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;stdio.h&gt; <\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;iostream&gt; <\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &quot;pin.H&quot; <\/span>\r\n<span style=\"color: #000000\">namespace<\/span> <span style=\"color: #000000\">WINDOWS<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;Windows.h&gt;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;string&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;algorithm&gt;<\/span>\r\n\r\n\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/ bool started = FALSE;<\/span>\r\n<span style=\"color: #000000\">ADDRINT<\/span> <span style=\"color: #000000\">addr<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000\">vector<\/span><span style=\"color: #ce5c00; font-weight: bold\">&lt;<\/span><span style=\"color: #000000\">string<\/span><span style=\"color: #ce5c00; font-weight: bold\">&gt;<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">othercompare<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #0000cf; font-weight: bold\">32<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span>  <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;0x000000014000112A&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/__int64 curIP = 0;<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">FILE<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">trace<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/ This function is called before every instruction is executed<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/ and prints the IP<\/span>\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">printip<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">ip<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #0000cf; font-weight: bold\">32<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">sprintf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;0x%p&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">ip<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">strcmp<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">othercompare<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #8f5902; font-style: italic\">\/\/ we do our hax here<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;******************************************\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;******************************************\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;****************Found it!!!***************\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;******************************************\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;******************************************\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\t<span style=\"color: #000000\">fprintf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">trace<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;Instruction Pointer: 0x%p\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ip<\/span> <span style=\"color: #000000; font-weight: bold\">);<\/span> \r\n\t\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/ Pin calls this function every time a new instruction is encountered<\/span>\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Instruction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INS<\/span> <span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">IMG<\/span> <span style=\"color: #000000\">img<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">IMG_FindByAddress<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INS_Address<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n\t<span style=\"color: #000000\">string<\/span> <span style=\"color: #000000\">path<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_Valid<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">img<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #ce5c00; font-weight: bold\">?<\/span> <span style=\"color: #000000\">IMG_Name<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">img<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #ce5c00; font-weight: bold\">:<\/span> <span style=\"color: #4e9a06\">&quot;InvalidImg&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">auto<\/span> <span style=\"color: #000000\">it<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">std<\/span><span style=\"color: #ce5c00; font-weight: bold\">::<\/span><span style=\"color: #000000\">find<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">begin<\/span><span style=\"color: #000000; font-weight: bold\">(),<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #000000; font-weight: bold\">(),<\/span> <span style=\"color: #000000\">path<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">it<\/span> <span style=\"color: #ce5c00; font-weight: bold\">!=<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #000000; font-weight: bold\">())<\/span>\r\n\t\t<span style=\"color: #204a87; font-weight: bold\">return<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">else<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #000000\">fprintf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">trace<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;Instruction: %s\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">INS_Disassemble<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span> <span style=\"color: #8f5902; font-style: italic\">\/\/ also print the decoded instructions<\/span>\r\n\t\t<span style=\"color: #000000\">INS_InsertCall<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IPOINT_BEFORE<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">AFUNPTR<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">printip<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IARG_INST_PTR<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IARG_END<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Image<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG<\/span> <span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span> \r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_IsMainExecutable<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span>\r\n\t\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Main module loading...\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">else<\/span>\r\n\t\t<span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">push_back<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_Name<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">).<\/span><span style=\"color: #000000\">c_str<\/span><span style=\"color: #000000; font-weight: bold\">());<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Fini<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INT32<\/span> <span style=\"color: #000000\">code<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">{<\/span> \r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;The following modules were loaded: \\r\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">ii<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">for<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ii<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span> <span style=\"color: #000000\">ii<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&lt;<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">size<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span> <span style=\"color: #000000\">ii<\/span><span style=\"color: #ce5c00; font-weight: bold\">++<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\t<span style=\"color: #000000\">cout<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&lt;&lt;<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #000000\">ii<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&lt;&lt;<\/span> <span style=\"color: #000000\">endl<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span> \r\n\r\n<span style=\"color: #000000\">INT32<\/span> <span style=\"color: #000000\">Usage<\/span><span style=\"color: #000000; font-weight: bold\">()<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">{<\/span> \r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #ce5c00; font-weight: bold\">-<\/span><span style=\"color: #0000cf; font-weight: bold\">1<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">main<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">argc<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">argv<\/span><span style=\"color: #000000; font-weight: bold\">[])<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">{<\/span> \r\n\t<span style=\"color: #000000\">trace<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">fopen<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;itrace.out&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;w&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">PIN_Init<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">argc<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">argv<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span> \r\n\t\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">Usage<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t<span style=\"color: #000000\">IMG_AddInstrumentFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Image<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">INS_AddInstrumentFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Instruction<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span> \r\n\t<span style=\"color: #000000\">PIN_AddFiniFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Fini<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span> \r\n\t<span style=\"color: #000000\">PIN_StartProgram<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span> \r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Where do we obtain the instruction pointer address? That&#8217;s up to you(me), and the disassembler. Let&#8217;s go over an example.<\/p>\n<p>Here we have a simple C program I wrote (in 64 bit no less) that uses the MessageBox API. <\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #8f5902; font-style: italic\">#include &lt;windows.h&gt;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">main<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">void<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">msg1<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;this will work&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">msg2<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;this too&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #000000\">MessageBox<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87\">NULL<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">msg1<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">msg2<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">MB_YESNOCANCEL<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Here we run the fucker:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_prof.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_prof.png\" alt=\"\" width=\"720\" height=\"367\" class=\"alignnone size-full wp-image-1456\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_prof.png 720w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_prof-300x153.png 300w\" sizes=\"(max-width: 720px) 100vw, 720px\" \/><\/a><\/p>\n<p>And here&#8217;s what it looks like in IDA:<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_ida.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_ida.png\" alt=\"\" width=\"845\" height=\"466\" class=\"alignnone size-full wp-image-1455\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_ida.png 845w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_ida-300x165.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/simple_ida-768x424.png 768w\" sizes=\"(max-width: 845px) 100vw, 845px\" \/><\/a><\/p>\n<p>We would want to instrument at the address <i>0x0000000140001000<\/i>. To modify the call to MessageBox, we need to alter the contents of the registers. The register &#8216;r9d&#8217; contains the value 3 or <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms645505(v=vs.85).aspx\" target=\"_blank\">MB_YESNOCANCEL<\/a>. With what? How about something simple like changing the MessageBox &#8216;uType&#8217; to &#8216;MB_OK&#8217; or 0. This means we would need to modify the &#8216;r9d&#8217; register at the address <i>0x0000000140001004<\/i> or just before the call to MessageBox. This means we have to halt on this address and modify the CONTEXT structure. For those who don&#8217;t know, the CONTEXT structure is a windows term for the state of registers. PIN has my back when it comes to modifying this structure. <\/p>\n<p>This code we use to modify the context structure. using some creative C++ hax, I&#8217;m able to stop on a certain address and call modify at the right time:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; 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: #8f5902; font-style: italic\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &lt;fstream&gt;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">#include &quot;pin.H&quot;<\/span>\r\n\r\n<span style=\"color: #000000\">vector<\/span><span style=\"color: #ce5c00; font-weight: bold\">&lt;<\/span><span style=\"color: #000000\">string<\/span><span style=\"color: #ce5c00; font-weight: bold\">&gt;<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/\/REG testedRegister = REG_INVALID();<\/span>\r\n\r\n<span style=\"color: #000000\">BOOL<\/span> <span style=\"color: #000000\">hitit<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #204a87\">false<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000\">REG<\/span> <span style=\"color: #000000\">testedRegister<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">REG_R9D<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">stop_on_me<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #0000cf; font-weight: bold\">32<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span>  <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;0000000140001004&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">[<\/span><span style=\"color: #0000cf; font-weight: bold\">32<\/span><span style=\"color: #000000; font-weight: bold\">]<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #4e9a06\">&quot;&quot;<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">show_instruction_pointer<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ADDRINT<\/span> <span style=\"color: #000000\">rip<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">ADDRINT<\/span> <span style=\"color: #000000\">r9<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> \r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">sprintf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #4e9a06\">&quot;%p&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">Addrint2VoidStar<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">rip<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">memcmp<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">stop_on_me<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #204a87; font-weight: bold\">sizeof<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">joincompare<\/span><span style=\"color: #000000; font-weight: bold\">)))<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Instruction pointer is %p\\r\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span><span style=\"color: #000000\">Addrint2VoidStar<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">rip<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n\t<span style=\"color: #8f5902; font-style: italic\">\/\/printf(&quot;Value of R9 is %p\\r\\n&quot;,Addrint2VoidStar(r9));<\/span>\r\n\t<span style=\"color: #000000\">hitit<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #204a87\">true<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">void<\/span> <span style=\"color: #000000\">afterfunc_call<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">CONTEXT<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">ctxt<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">hitit<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Setting the value of R9 to 0&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000\">PIN_SetContextRegval<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ctxt<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">testedRegister<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0x0000000000000000<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n    \r\n<span style=\"color: #8f5902; font-style: italic\">\/\/ Pin calls this function every time a new instruction is encountered<\/span>\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Instruction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INS<\/span> <span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t<span style=\"color: #000000\">IMG<\/span> <span style=\"color: #000000\">img<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">IMG_FindByAddress<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INS_Address<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">));<\/span>\r\n\t<span style=\"color: #000000\">string<\/span> <span style=\"color: #000000\">path<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_Valid<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">img<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #ce5c00; font-weight: bold\">?<\/span> <span style=\"color: #000000\">IMG_Name<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">img<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span> <span style=\"color: #ce5c00; font-weight: bold\">:<\/span> <span style=\"color: #4e9a06\">&quot;InvalidImg&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">auto<\/span> <span style=\"color: #000000\">it<\/span> <span style=\"color: #ce5c00; font-weight: bold\">=<\/span> <span style=\"color: #000000\">std<\/span><span style=\"color: #ce5c00; font-weight: bold\">::<\/span><span style=\"color: #000000\">find<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">begin<\/span><span style=\"color: #000000; font-weight: bold\">(),<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #000000; font-weight: bold\">(),<\/span> <span style=\"color: #000000\">path<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">it<\/span> <span style=\"color: #ce5c00; font-weight: bold\">!=<\/span> <span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #000000; font-weight: bold\">())<\/span>\r\n\t\t<span style=\"color: #204a87; font-weight: bold\">return<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">else<\/span>\r\n\t<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n\t\r\n\t <span style=\"color: #000000\">REGSET<\/span> <span style=\"color: #000000\">regsin<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n     <span style=\"color: #000000\">REGSET<\/span> <span style=\"color: #000000\">regsout<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n     <span style=\"color: #000000\">REGSET_Clear<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">regsin<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n     <span style=\"color: #000000\">REGSET_Clear<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">regsout<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n     <span style=\"color: #000000\">REGSET_Insert<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">regsout<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">testedRegister<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t \r\n\t <span style=\"color: #8f5902; font-style: italic\">\/\/ insert call to check for instruction pointer<\/span>\r\n    <span style=\"color: #000000\">INS_InsertCall<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IPOINT_BEFORE<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">AFUNPTR<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span><span style=\"color: #000000\">show_instruction_pointer<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IARG_REG_VALUE<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">REG_INST_PTR<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IARG_END<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t <span style=\"color: #8f5902; font-style: italic\">\/\/ insert call to modify register value<\/span>\r\n\t<span style=\"color: #000000\">INS_InsertCall<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">ins<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IPOINT_BEFORE<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">AFUNPTR<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">afterfunc_call<\/span><span style=\"color: #000000; font-weight: bold\">),<\/span><span style=\"color: #000000\">IARG_PARTIAL_CONTEXT<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">regsin<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&amp;<\/span><span style=\"color: #000000\">regsout<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">IARG_END<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t\r\n\t<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Image<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG<\/span> <span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span> \r\n\t<span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_IsMainExecutable<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span>\r\n\t\t<span style=\"color: #000000\">printf<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #4e9a06\">&quot;Main module loading...\\n&quot;<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n\t<span style=\"color: #204a87; font-weight: bold\">else<\/span>\r\n\t\t<span style=\"color: #000000\">modules<\/span><span style=\"color: #000000; font-weight: bold\">.<\/span><span style=\"color: #000000\">push_back<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">IMG_Name<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Img<\/span><span style=\"color: #000000; font-weight: bold\">).<\/span><span style=\"color: #000000\">c_str<\/span><span style=\"color: #000000; font-weight: bold\">());<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #000000\">VOID<\/span> <span style=\"color: #000000\">Fini<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">INT32<\/span> <span style=\"color: #000000\">code<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">VOID<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span><span style=\"color: #000000\">v<\/span><span style=\"color: #000000; font-weight: bold\">)<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n    \r\n  \r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #8f5902; font-style: italic\">\/* ===================================================================== *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/* Print Help Message                                                    *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/* ===================================================================== *\/<\/span>\r\n\r\n<span style=\"color: #000000\">INT32<\/span> <span style=\"color: #000000\">Usage<\/span><span style=\"color: #000000; font-weight: bold\">()<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n    <span style=\"color: #000000\">cerr<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&lt;&lt;<\/span> <span style=\"color: #4e9a06\">&quot;Just more code hax by joe&quot;<\/span> <span style=\"color: #ce5c00; font-weight: bold\">&lt;&lt;<\/span> <span style=\"color: #000000\">endl<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n    \r\n    <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #ce5c00; font-weight: bold\">-<\/span><span style=\"color: #0000cf; font-weight: bold\">1<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n\r\n<span style=\"color: #8f5902; font-style: italic\">\/* ===================================================================== *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/* Main                                                                  *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/* ===================================================================== *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/*   argc, argv are the entire command line: pin -t &lt;toolname&gt; -- ...    *\/<\/span>\r\n<span style=\"color: #8f5902; font-style: italic\">\/* ===================================================================== *\/<\/span>\r\n\r\n<span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">main<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #204a87; font-weight: bold\">int<\/span> <span style=\"color: #000000\">argc<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #204a87; font-weight: bold\">char<\/span> <span style=\"color: #ce5c00; font-weight: bold\">*<\/span> <span style=\"color: #000000\">argv<\/span><span style=\"color: #000000; font-weight: bold\">[])<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">{<\/span>\r\n    <span style=\"color: #8f5902; font-style: italic\">\/\/ Initialize pin<\/span>\r\n    <span style=\"color: #204a87; font-weight: bold\">if<\/span> <span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">PIN_Init<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">argc<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #000000\">argv<\/span><span style=\"color: #000000; font-weight: bold\">))<\/span> <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #000000\">Usage<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n\t<span style=\"color: #000000\">IMG_AddInstrumentFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Image<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span> <span style=\"color: #8f5902; font-style: italic\">\/\/ remove other modules, except main module, let that run<\/span>\r\n    <span style=\"color: #000000\">INS_AddInstrumentFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Instruction<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n    <span style=\"color: #000000\">PIN_AddFiniFunction<\/span><span style=\"color: #000000; font-weight: bold\">(<\/span><span style=\"color: #000000\">Fini<\/span><span style=\"color: #000000; font-weight: bold\">,<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">);<\/span>\r\n    <span style=\"color: #000000\">PIN_StartProgram<\/span><span style=\"color: #000000; font-weight: bold\">();<\/span>\r\n    \r\n    <span style=\"color: #204a87; font-weight: bold\">return<\/span> <span style=\"color: #0000cf; font-weight: bold\">0<\/span><span style=\"color: #000000; font-weight: bold\">;<\/span>\r\n<span style=\"color: #000000; font-weight: bold\">}<\/span>\r\n<\/pre>\n<\/div>\n<p>Does it work? Hell yeah!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/biach.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/biach.png\" alt=\"\" width=\"862\" height=\"674\" class=\"alignnone size-full wp-image-1457\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/biach.png 862w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/biach-300x235.png 300w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/biach-768x601.png 768w\" sizes=\"(max-width: 862px) 100vw, 862px\" \/><\/a><\/p>\n<p>So how the hell would you cheat like this? Well I&#8217;ve already demonstrated register manipulation, memory manipulation, so the rest should be trivial. <\/p>\n<p>I&#8217;m sorry it took so long to go through this framework and make it into something useful. I&#8217;m not done with PIN just yet, I plan to release something awesome using it soon. Stay tuned!<br \/>\n<a href=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/1500181336625.jpg\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/1500181336625.jpg\" alt=\"\" width=\"432\" height=\"789\" class=\"alignnone size-full wp-image-1458\" srcset=\"https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/1500181336625.jpg 432w, https:\/\/www.gironsec.com\/blog\/wp-content\/uploads\/2017\/07\/1500181336625-164x300.jpg 164w\" sizes=\"(max-width: 432px) 100vw, 432px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hi! Today I&#8217;m going to go over more on intel&#8217;s PIN, more on cheats, and less on detection since I already covered that. I feel like I&#8217;ve spent way too much time on this and it&#8217;s a huge turn off against my productivity. And of course, I commit to a talk and it HAS to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,6,7],"tags":[113],"_links":{"self":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1449"}],"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=1449"}],"version-history":[{"count":5,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1449\/revisions"}],"predecessor-version":[{"id":1460,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/posts\/1449\/revisions\/1460"}],"wp:attachment":[{"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/media?parent=1449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/categories?post=1449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gironsec.com\/blog\/wp-json\/wp\/v2\/tags?post=1449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}