<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Buffer-Overflow on Monish Kumar&#39;s Blog</title>
        <link>https://itsmonish.pages.dev/tags/buffer-overflow/</link>
        <description>Recent content in Buffer-Overflow on Monish Kumar&#39;s Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <lastBuildDate>Sat, 26 Apr 2025 17:51:33 +0530</lastBuildDate><atom:link href="https://itsmonish.pages.dev/tags/buffer-overflow/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>Binary Exploitation - Baby Buffer Overflow 32-bit</title>
        <link>https://itsmonish.pages.dev/blog/huntress-ctf-2024/binary-exploitation-baby-buffer-overflow/</link>
        <pubDate>Sat, 26 Apr 2025 17:51:33 +0530</pubDate>
        
        <guid>https://itsmonish.pages.dev/blog/huntress-ctf-2024/binary-exploitation-baby-buffer-overflow/</guid>
        <description>&lt;h1 id=&#34;baby-buffer-overflow---32bit&#34;&gt;Baby Buffer Overflow - 32bit
&lt;/h1&gt;&lt;h2 id=&#34;challenge-statement&#34;&gt;Challenge Statement
&lt;/h2&gt;&lt;p&gt;Author: @aenygma&lt;/p&gt;
&lt;p&gt;Can you command this program to where it cannot go?&lt;/p&gt;
&lt;p&gt;To get the flag, you must somehow take control of its excecution.&lt;/p&gt;
&lt;p&gt;Is it even possible?&lt;/p&gt;
&lt;p&gt;Attachments: &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/baby-buffer-overflow/babybufov&#34; &gt;babybufov&lt;/a&gt;, &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/baby-buffer-overflow/babybufov.c&#34; &gt;babybufov.c&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This challenge was accompanied with a per-user instance&lt;/p&gt;
&lt;h2 id=&#34;solution&#34;&gt;Solution
&lt;/h2&gt;&lt;p&gt;Opening the C source file given in the attachment section, we can see that it is a fairly straight forward program. There is the main function which requests some data using &lt;code&gt;gets&lt;/code&gt;. If you have spent some time with C programming you would&amp;rsquo;ve known that this is a vulnerable function. The problem with this function is that it takes input with no limit on it&amp;rsquo;s length. Consequently, it writes data to addresses beyond the space reserved or allocated for the string causing a, that&amp;rsquo;s right, a &lt;strong&gt;buffer overflow&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now due to some issues with the linker I couldn&amp;rsquo;t execute the binary provided directly. Luckily the source code had a comment to indicate how the program is compiled. So I used:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;gcc -fno-pie -no-pie -Wno-implicit-function-declaration -fno-stack-protector -m32 babybufov.c -o babybufov
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Notice that the compilation parameters turn off the stack protection, which makes write to stack possible. So obviously we are supposed to redirect execution flow to the uncalled &lt;code&gt;target&lt;/code&gt; function in the code.&lt;/p&gt;
&lt;p&gt;Here is a awesome &lt;a class=&#34;link&#34; href=&#34;https://www.youtube.com/watch?v=8QzOC8HfOqU&amp;amp;list=PLhixgUqwRTjxglIswKp9mpkfPNfHkzyeN&amp;amp;index=15&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;video&lt;/a&gt; from liveoverflow that depicts this in a more illustratively. I suggest you watch the entire playlist if you are new to this sort of things.&lt;/p&gt;
&lt;p&gt;So in a gist, function calls push return address on the stack so that when the function ends it can return the control to where it came from. The same stack also houses local variables such as the one that collects the string from the &lt;code&gt;gets&lt;/code&gt; function. Now the idea is that we put enough characters in the string so that it overflows to the return address value and overwrites it.&lt;/p&gt;
&lt;p&gt;A little knowledge of the GNU Debugger (gdb) is advisable for doing the below steps.&lt;/p&gt;
&lt;p&gt;First we need to know how many characters it takes to overflow to the return address. Usually this is the string (buffer) size, but it can be more due to other variables. So I put in a recognizable string like &lt;code&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/code&gt; as input to observe the return address.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/1.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;overflow reached&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Of course it seg-faulted because there is no valid address as &lt;code&gt;0x46454443&lt;/code&gt;. But if you notice this is not a random address. 46 is the letter F in hexadecimal. So the address translates to &lt;code&gt;FEDC&lt;/code&gt;. The reason why it is in reverse is because of the endianess. So now we now the address can be overwritten from the second &lt;code&gt;C&lt;/code&gt; in the input.&lt;/p&gt;
&lt;p&gt;Now we need to know the address of the function we need to redirect to. For this we can disassemble the target function to get the address of the first instruction in the address.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/2.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;target function disassembly&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;So the address to redirect the control is &lt;code&gt;0x080491a6&lt;/code&gt;. We can&amp;rsquo;t manually type in the return address is because it will be taken as string and stored differently in the stack. So I put together an python script &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/baby-buffer-overflow/exploitgen.py&#34; &gt;exploitgen.py&lt;/a&gt; that generates an exploit input that can overwrite the return address.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; struct
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;buf &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;abcdefghijklmnopqrstuvwxyzab&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;addr &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; struct&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pack(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;I&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0x080491a6&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;exp &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; buf &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; addr
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;with&lt;/span&gt; open(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;exploit&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;wb&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; f:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(exp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;encode())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;The above code has the buffer required to fill the input buffer and adds the address at the end of it to overwrite the return address. The &lt;a class=&#34;link&#34; href=&#34;https://docs.python.org/3/library/struct.html&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;struct&lt;/a&gt; module offers packing and unpacking data between python and C data types with specific sizes. Since an address is 4 bytes long, I have used unsigned int (the &lt;code&gt;I&lt;/code&gt;) as the target type. The newline is to mimic the press of enter button to finish input.&lt;/p&gt;
&lt;p&gt;Once the exploit is generated, we can redirect it to the program directly like:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;./babybufov_custom &amp;lt; exploit
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/3.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;exploit successful&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Yup, we got the jackpot message in the target function. So we are successful in redirecting the execution. One might ask, is that it? Visiting again the source code for target we can see there is a shell that gets invoked. Since we give no more input it simply exits. So I added a little more to the &lt;a class=&#34;link&#34; href=&#34;https://itsmonish.pages.dev/others/huntressctf-2024/baby-buffer-overflow/exploitgen.py&#34; &gt;exploitgen.py&lt;/a&gt;.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;import&lt;/span&gt; struct
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;buf &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; &lt;span style=&#34;color:#e6db74&#34;&gt;b&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;abcdefghijklmnopqrstuvwxyzab&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;addr &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; struct&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;pack(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;I&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#ae81ff&#34;&gt;0x080491a6&lt;/span&gt;)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;exp &lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt; buf &lt;span style=&#34;color:#f92672&#34;&gt;+&lt;/span&gt; addr
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#66d9ef&#34;&gt;with&lt;/span&gt; open(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;exploit&amp;#34;&lt;/span&gt;, &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;wb&amp;#34;&lt;/span&gt;) &lt;span style=&#34;color:#66d9ef&#34;&gt;as&lt;/span&gt; f:
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(exp)
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;encode())
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    f&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;write(&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;ls&lt;/span&gt;&lt;span style=&#34;color:#ae81ff&#34;&gt;\n&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;&lt;/span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;.&lt;/span&gt;encode())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now it lists the files in the directory it is on as shown below:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/4.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;testing ls with exploit&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;And now we have code execution as well. Now it&amp;rsquo;s time to test this on the real thing. The challenge came with a per-user instance. Spinning it up gave a netcat command to the program directly. So I redirected my exploit to the netcat program:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/5.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;failure&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;It didn&amp;rsquo;t work. I tried a couple more times to meet with the same result. But &lt;strong&gt;it worked on my machine&lt;/strong&gt;. For those who already figured out what went wrong, well done. It took me quite a while, longer than I would like to admit. You see the problem is that I recompiled the code in my system, which made the addresses different. So I was redirecting the code to a wrong address.&lt;/p&gt;
&lt;p&gt;Since we have the original binary, it will should the same addresses as the one over netcat. So I used objdump to get the address of the target function.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/6.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;objdump target&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;Armed with the actual address I need, I put that in my exploit program, generated an exploit and redirected it the netcat program.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/7.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;success on remote&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;So it appears there is a file &lt;code&gt;flag&lt;/code&gt; in the same directory as the program. So I added a &lt;code&gt;cat flag&lt;/code&gt; to my exploit and repeated the same.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://itsmonish.pages.dev/images/huntressctf-2024/baby-overflow-32bit/8.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;got the flag&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;So there it was, the flag. This is quite simple if you know your basics well (and don&amp;rsquo;t do silly mistakes like me).&lt;/p&gt;
</description>
        </item>
        
    </channel>
</rss>
