<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>Stack Smasher</title>
		<link>http://www.allegro.cc/forums/view/617807</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Tue, 16 Apr 2019 11:39:09 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve been a little bored waiting for my laptop power adapter to arrive so I can use my laptop again, and so I&#39;ve been doing a little experimenting.</p><p>I managed to execute a function I never called. It&#39;s the little victories that make me happy. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>Anyway, here&#39;s some (/dangerous/) source code. Run at your own risk. It prints an error message inside the function that was never called. Obviously, I managed to overwrite the return address of the stack frame with the address of my function, so when it returns, arbitrary code is executed.</p><p>This is really nothing special, seeing as how it&#39;s my own code and I can make it do anything I want. I won&#39;t go into attack vectors, as that&#39;s not the point.</p><p>Initial attempts to &#39;fix&#39; what I broke...<br /><span class="remote-thumbnail"><span class="json">{"name":"611989","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/9\/197c512c6993d729cf3925d2303cd03c.png","w":1356,"h":1057,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/1\/9\/197c512c6993d729cf3925d2303cd03c"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/1/9/197c512c6993d729cf3925d2303cd03c-240.jpg" alt="611989" width="240" height="187" /></span></p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span>
<span class="number">  2</span>
<span class="number">  3</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="p">#warning RUN @ YOUR OWN RISK NEWBIE</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="p">#include &lt;stdio.h&gt;</span>
<span class="number">  8</span><span class="p">#include &lt;stdlib.h&gt;</span>
<span class="number">  9</span>
<span class="number"> 10</span>
<span class="number"> 11</span>
<span class="number"> 12</span><span class="k1">typedef</span> <span class="k1">void</span> <span class="k2">(</span><span class="k3">*</span>VOIDFUNC<span class="k2">)</span><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span>
<span class="number"> 14</span><span class="k1">typedef</span> VOIDFUNC<span class="k3">*</span> VOIDFUNCPTR<span class="k2">;</span>
<span class="number"> 15</span>
<span class="number"> 16</span><span class="k1">static</span> VOIDFUNC old_data <span class="k3">=</span> <span class="k2">(</span>VOIDFUNC<span class="k2">)</span><span class="n">0</span><span class="k2">;</span>
<span class="number"> 17</span><span class="k1">static</span> VOIDFUNCPTR return_address <span class="k3">=</span> <span class="k2">(</span>VOIDFUNCPTR<span class="k2">)</span><span class="n">0</span><span class="k2">;</span>
<span class="number"> 18</span><span class="k1">static</span> VOIDFUNCPTR stack_address <span class="k3">=</span> <span class="k2">(</span>VOIDFUNCPTR<span class="k2">)</span><span class="n">0</span><span class="k2">;</span>
<span class="number"> 19</span>
<span class="number"> 20</span>
<span class="number"> 21</span>
<span class="number"> 22</span><span class="k1">void</span> put_it_back<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 23</span>   <span class="k1">if</span> <span class="k2">(</span>return_address<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 24</span>      <span class="c">/// Put back the previous value</span>
<span class="number"> 25</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stderr , <span class="s">"Returning data {%p} to %p\n"</span> , old_data , return_address<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 26</span>      <span class="k3">*</span>return_address <span class="k3">=</span> old_data<span class="k2">;</span>
<span class="number"> 27</span>   <span class="k2">}</span>
<span class="number"> 28</span><span class="k2">}</span>
<span class="number"> 29</span>
<span class="number"> 30</span>
<span class="number"> 31</span>
<span class="number"> 32</span><span class="k1">void</span> i_never_called_this_code<span class="k2">(</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 33</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stderr , <span class="s">"\n\nArbitrary code execution... oops!\n\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 34</span>   <span class="c">/// Attempt to repair the damage, so it goes back to the 'CORRECT' frame</span>
<span class="number"> 35</span>   put_it_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 36</span>   stack_address <span class="k3">=</span> return_address<span class="k2">;</span>
<span class="number"> 37</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stderr , <span class="s">"***Stack Address is %p ***\n"</span> , stack_address<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 38</span>
<span class="number"> 39</span>   <span class="c">/// We better abort before shit gets crazy</span>
<span class="number"> 40</span><span class="c">//   abort();/// haha just kidding</span>
<span class="number"> 41</span>   <span class="c">/// No really</span>
<span class="number"> 42</span><span class="c">//   abort();/// you really want to know what happens, don't you?</span>
<span class="number"> 43</span>   <span class="c">/// Ahem</span>
<span class="number"> 44</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_37.html" target="_blank">abort</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 45</span><span class="k2">}</span>
<span class="number"> 46</span>
<span class="number"> 47</span>
<span class="number"> 48</span>
<span class="number"> 49</span><span class="k1">void</span> break_the_stack<span class="k2">(</span><span class="k1">char</span><span class="k3">*</span> stackptr<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 50</span>
<span class="number"> 51</span>   <span class="c">/// Store the old data (ie, return address we're SUPPOSED to be going to</span>
<span class="number"> 52</span>   old_data <span class="k3">=</span> <span class="k3">*</span><span class="k2">(</span>VOIDFUNCPTR<span class="k2">)</span><span class="k2">(</span>stackptr<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 53</span>   return_address <span class="k3">=</span> <span class="k2">(</span>VOIDFUNCPTR<span class="k2">)</span>stackptr<span class="k2">;</span>
<span class="number"> 54</span>
<span class="number"> 55</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stderr , <span class="s">"Reading data {%p} at %p\n"</span> , old_data , return_address<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 56</span>   
<span class="number"> 57</span>   <span class="c">/// Smash the stackptr, </span>
<span class="number"> 58</span>   <span class="c">/// When we find the return address stored on the stack, we replace it with our function address</span>
<span class="number"> 59</span>   <span class="c">/// that's where it will go next</span>
<span class="number"> 60</span>   <span class="c">/// Basicallly, we're calling our code without calling it</span>
<span class="number"> 61</span>   <span class="c">/// Once that's done, 411 UR B4535 B310NG 2 US</span>
<span class="number"> 62</span>   <span class="k3">*</span><span class="k2">(</span>VOIDFUNCPTR<span class="k2">)</span><span class="k2">(</span>stackptr<span class="k2">)</span> <span class="k3">=</span> i_never_called_this_code<span class="k2">;</span>
<span class="number"> 63</span><span class="k2">}</span>
<span class="number"> 64</span>
<span class="number"> 65</span>
<span class="number"> 66</span>
<span class="number"> 67</span><span class="k1">int</span> main<span class="k2">(</span><span class="k1">int</span> argc , <span class="k1">char</span><span class="k3">*</span><span class="k3">*</span> argv<span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 68</span>   
<span class="number"> 69</span>   <span class="k1">char</span><span class="k3">*</span> stack <span class="k3">=</span> <span class="k2">(</span><span class="k1">char</span><span class="k3">*</span><span class="k2">)</span><span class="k3">&amp;</span>stack<span class="k2">;</span>
<span class="number"> 70</span>   
<span class="number"> 71</span>   <span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>argc<span class="k2">;</span>
<span class="number"> 72</span>   <span class="k2">(</span><span class="k1">void</span><span class="k2">)</span>argv<span class="k2">;</span>
<span class="number"> 73</span>   
<span class="number"> 74</span>   <span class="k1">while</span> <span class="k2">(</span><span class="n">1</span><span class="k2">)</span> <span class="k2">{</span>
<span class="number"> 75</span>      stack <span class="k3">-</span><span class="k3">=</span> <span class="k1">sizeof</span><span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">int</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 76</span>      <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stdout , <span class="s">"0x%lx\n"</span> , <span class="k2">(</span><span class="k1">unsigned</span> <span class="k1">long</span> <span class="k1">int</span><span class="k2">)</span>stack<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 77</span>      break_the_stack<span class="k2">(</span>stack<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 78</span>      put_it_back<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 79</span>      <span class="k1">if</span> <span class="k2">(</span>stack_address<span class="k2">)</span> <span class="k2">{</span><span class="c">/// == (VOIDFUNCPTR)stack) {</span>
<span class="number"> 80</span>         <span class="k1">break</span><span class="k2">;</span>
<span class="number"> 81</span>      <span class="k2">}</span>
<span class="number"> 82</span>      <span class="k1">else</span> <span class="k2">{</span>
<span class="number"> 83</span>         <span class="c">/// Cleanliness is next to godliness</span>
<span class="number"> 84</span><span class="c">///         *return_address = old_data;</span>
<span class="number"> 85</span>      <span class="k2">}</span>
<span class="number"> 86</span>   <span class="k2">}</span>
<span class="number"> 87</span>   
<span class="number"> 88</span>   <a href="http://www.delorie.com/djgpp/doc/libc/libc_345.html" target="_blank">fprintf</a><span class="k2">(</span>stderr , <span class="s">"What just happened?\n"</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 89</span>   
<span class="number"> 90</span>   <span class="k1">return</span> <span class="n">0</span><span class="k2">;</span>
<span class="number"> 91</span><span class="k2">}</span>
</div></div><p>

So you can see, it gets the address of an object on the stack, and then decrements the address, overwriting memory until the return address is overwritten with the address of the function I want to call.</p><p>NOTE : This will destroy your stack, and may? destroy your computer. Maybe you should run it in a VM, through something safe like GDB.</p><p>I can only test on Windows XP at the moment. This is with MinGW GCC 8.1.</p><p>So far I&#39;ve managed to get it to run arbitrary code, but I can&#39;t seem to put humpty dumpty back together right yet.</p><p><span class="remote-thumbnail"><span class="json">{"name":"611990","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/6793e6aa09dd2ff2b5e982912205ada2.png","w":1356,"h":1057,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/7\/6793e6aa09dd2ff2b5e982912205ada2"}</span><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/7/6793e6aa09dd2ff2b5e982912205ada2-240.jpg" alt="611990" width="240" height="187" /></span></p><p>For now, it halts with an <span class="source-code"><a href="http://www.delorie.com/djgpp/doc/libc/libc_37.html" target="_blank">abort</a><span class="k2">(</span><span class="k2">)</span><span class="k2">;</span></span> so it doesn&#39;t turn into Skynet.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 12 Apr 2019 11:24:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Looks cool! </p><p>Does anyone remember Progranisms? Now that <i>was</i> dangerous!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Peter Hull)</author>
		<pubDate>Fri, 12 Apr 2019 16:24:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title"><a href="http://www.allegro.cc/forums/thread/617807/1041990#target">Peter Hull</a> said:</div><div class="quote"><p>
Progranisms
</p></div></div><p>
Oh sweet youth
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Chris Katko)</author>
		<pubDate>Fri, 12 Apr 2019 18:24:24 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>The first screenshot reminds me of those old cartoons (edit - Family Circus) where they run around the yard and it leaves a trail :</p><p><img src="http://www.allegro.cc//djungxnpq2nug.cloudfront.net/image/cache/6/2/6212e70b3c1c3a8dfee3bb5f0e73dc43.jpg" alt="611991" width="191" height="144" /></p><p>Those addresses are totally random. It&#39;s just playing hopscotch through memory. <img src="http://www.allegro.cc/forums/smileys/shocked.gif" alt=":o" /></p><p>Can anyone give me a guide to the addresses on the stack?</p><p> if you look at the second screenshot you can see some addresses near 0 x 0 0 2 3 f f 0 0 through 0 x 0 0 2 4 0 0 0 0. That&#39;s 256 Bytes of memory. That&#39;s the address where the stack variables live. The addresses near 0x0401.... are the addresses of functions like main and break the stack. Static variables are near there too. In between the stack variables are the frame pointers.</p><p>I can&#39;t quite &#39;fix&#39; the damage I caused though... when I put back the address of where it&#39;s supposed to return to it is actually trying to &#39;run&#39; the stack memory. <img src="http://www.allegro.cc/forums/smileys/lipsrsealed.gif" alt=":-X" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Edgar Reynaldo)</author>
		<pubDate>Fri, 12 Apr 2019 21:59:40 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m posting now to help me remember tomorrow. I used to be heavily invested in this sort of thing. I think I may have posted my detouring library on here at some point (takes function address, decompiles function header, relocates/recompiles it somewhere else, inserts jump to your function where header was; so all function calls get redirected then return to the original function). Anyway, I want to look at what you did when I&#39;m awake. Too exhausted to look now.</p><p>[[EDIT]]<br />Okay, back awake. </p><p>So, what it comes down to, is the call and ret instructions in ASM. The call instruction is functionally equivalent to
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// RIP is the 64 bit pointer to the currently executing instruction. </span>
<span class="c">// Y is the size of a push instruction plus the size of the jmp function. </span>
<span class="c">// I say Y because in x86/64 land, these instructions come out to </span>
<span class="c">// different machine code width different numbers of bytes depending on</span>
<span class="c">// the arguments. For instance, a jmp instruction can be anywhere between 2</span>
<span class="c">// and 14 bytes long</span>
push rip<span class="k3">+</span>Y <span class="c">// Push the return address (directly after the jmp) onto the stack</span>
jmp SomeFunction <span class="c">// Move the instruction pointer to the function we want to call</span>
</pre></div></div><p>

Meanwhile, the ret instruction is functionally equivalent to
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Pull the top value off the stack and put it in the instruction</span>
<span class="c">// pointer, effectively jumping there and removing it from the</span>
<span class="c">// stack simultaneously</span>
pop rip
</pre></div></div><p>

So, obviously, as soon as you&#39;re in your function, the top stack entry is the return address, right? Wrong. <br />Just like in C/C++, in ASM there is the concept of stack scope, which is managed by RBP and RSP. RBP points to the current bottom of the stack, and RSP to the current head. This means calling push is functionally equivalent to
</p><div class="source-code snippet"><div class="inner"><pre>sub RSP, <span class="k1">sizeof</span><span class="k2">(</span>X<span class="k2">)</span> <span class="c">// Move the head of the stack to make room (stack grows down for some reason lost to time)</span>
mov <span class="k2">[</span>RSP<span class="k2">]</span>, X <span class="c">// Store X at the current address of RSP, the stack head</span>
</pre></div></div><p>
And calling pop is the reverse. <br />What about RBP then? I said there was scope, and RBP is where it happens. The standard entry sequence to a function (the stuff that gets called before your variables are initialized) is
</p><div class="source-code snippet"><div class="inner"><pre>push RBP <span class="c">// Store the old base of the stack on the stack</span>
mov RBP, RSP <span class="c">// Set the new base as the current top of the stack</span>
sub RSP, X <span class="c">// Allocate X off the stack, where X is the sizeof all local variables</span>
</pre></div></div><p>

This means (because, again, the stack grows down for some reason) inside a function, you can use [RBP - X] to access local variables and [RBP + X + 8] to access the parent scope&#39;s variables (+8 to skip the return address). Writing into [RBP + X] would be bad, since that means you&#39;re modifying outside your scope. Of course, there&#39;s no safety here, so nothing is stopping you.</p><p>What it comes down to, is a stack frame for the following would be something like...
</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="k1">void</span> foo<span class="k2">(</span><span class="k2">)</span>
<span class="number">  2</span><span class="k2">{</span>
<span class="number">  3</span>    <span class="k1">int</span> x<span class="k2">;</span>
<span class="number">  4</span>    <span class="c">// stack frame for right here below</span>
<span class="number">  5</span><span class="k2">}</span>
<span class="number">  6</span>
<span class="number">  7</span><span class="k1">void</span> bar<span class="k2">(</span><span class="k2">)</span>
<span class="number">  8</span><span class="k2">{</span>
<span class="number">  9</span>    <span class="k1">int</span> a, b<span class="k2">;</span>
<span class="number"> 10</span>    <span class="c">// ...</span>
<span class="number"> 11</span>    foo<span class="k2">(</span>a <span class="k3">+</span> b<span class="k2">)</span>
<span class="number"> 12</span>foo_done:
<span class="number"> 13</span><span class="k2">}</span>
<span class="number"> 14</span>
<span class="number"> 15</span><span class="k1">int</span> main<span class="k2">(</span><span class="k2">)</span>
<span class="number"> 16</span><span class="k2">{</span>
<span class="number"> 17</span>    bar<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 18</span>bar_done:
<span class="number"> 19</span><span class="k2">}</span>
</div></div><p>
</p><div class="source-code snippet"><div class="inner"><pre><span class="c">// Note: This is assuming x64 with a __fastcall calling convention,</span>
<span class="c">// meaning the first two parameters are passed in registers, rather</span>
<span class="c">// than on the stack</span>
<span class="c">// Address Value</span>
   <span class="n">0x100</span>   <span class="n">0x100</span>    <span class="c">// Absolute base of the stack</span>
   <span class="n">0x108</span>   bar_done <span class="c">// Return address after bar is called. The next instruction</span>
   <span class="n">0x110</span>   <span class="n">0x100</span>    <span class="c">// The base of the stack for main</span>
   <span class="n">0x118</span>   a        <span class="c">// bar::a</span>
   <span class="n">0x120</span>   b        <span class="c">// bar::b</span>
   <span class="n">0x128</span>   foo_done <span class="c">// The return address after foo is called.</span>
   <span class="n">0x130</span>   <span class="n">0x110</span>    <span class="c">// The stack base pointer for bar</span>
   <span class="n">0x138</span>   x        <span class="c">// foo::x</span>
</pre></div></div><p>

That&#39;s all generally true anyway. In reality, optimizing compilers will sometimes give you non-standard stack frames, particularly on leaf functions (functions that don&#39;t call anything). Furthermore, a nice debugging compiler will probably insert byte patterns in the callstack in and around function calls which they can check for on return to make sure you didn&#39;t smash the stack.</p><p>If you want to know more, such as how to properly hot patch/detour a function, how to inject code into a foreign process, how to evade process intrusion detection facilities ... ... all for academic purposes only, let me know.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (relpatseht)</author>
		<pubDate>Sat, 13 Apr 2019 11:06:06 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>If you want to see dangerous, I just posted another thread which has a video which shows that there is a RISC processor hidden on your X86 CPU that can bypass all security.  The guy logged onto LINUX and gave himself root access when he was just a normal user using it.  </p><p>He gives out the source code and programs he wrote so you may be interested in seeing what you can do with it.  Mind blowing stuff anyhow.  See the thread called &quot;GOD MODE ENABLED...&quot; etc.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Neil Roy)</author>
		<pubDate>Tue, 16 Apr 2019 11:39:09 +0000</pubDate>
	</item>
</rss>
