A signal handler overflows
Zozo walkthrough: a signal handler overflow to a return-to-libc shell
Zozo gives remote shell access to a limited account and one SGID binary. The binary's SIGINT handler decodes a name field into a fixed buffer with no bounds check, which opens the door to a classic return-to-libc.
- 01
Inspect
SSH access gives a restricted shell with two files: a root-owned flag.txt and an SGID binary named signal. The binary installs a SIGINT handler, starts five threads and waits for a name.
- 02
Hypothesize
The handler decodes the name as hex pairs into a fixed 32-byte stack buffer with no length check, which is a classic stack overflow once SIGINT is delivered.
- 03
Test
Protections show no stack canary, a non-executable stack and no PIE, which rules out stack shellcode but makes a return-to-libc chain straightforward once libc addresses are known.
- 04
Solve
Overflow the return address with the address of system in libc followed by the address of a /bin/sh string, encoded as hex pairs in the name, then send SIGINT.
Zozo was a 100-point pwn task in WhiteHat Contest 11. The challenge gave SSH access to a remote machine under a restricted account, rather than a binary to download directly, and the first part of the walkthrough is really about getting the target file off the box at all.
Getting into the account
The provided credentials logged into a shell with almost nothing available: no access outside the home directory, and critically no access to /dev, which meant tools relying on /dev/null for a handshake, scp among them, failed outright. Two files sat in the home directory. flag.txt was owned by root and readable only by a group called pwnreader. A second file, signal, was executable by the logged-in account, owned by pwnreader, and had the SGID bit set, which is the standard shape of a “read a protected file through a vulnerable setgid binary” task.
Copying the binary out used a plain redirection over the SSH connection rather than any file-transfer protocol:
ssh -p1094 -l pwnguest 118.70.80.143 dd if=/home/ubuntu/signal >/tmp/signal
file identifies the result as a 32-bit ELF, dynamically linked, built for Linux 2.6.24, not stripped. Not stripped means function names survive, which saves time in the disassembler later.
A detour that was not the intended path
Separately from the binary, the host itself was running Linux kernel 3.13.0-32-generic, which carries a known local privilege escalation issue (tracked as CVE-2014-4014). A public exploit for it gives a root shell directly, with no interaction with the signal binary at all. During the event the server was taken down abruptly, which strongly suggested that at least one team used this route and ended the contest for everyone else rather than reporting it. It is worth flagging explicitly: this path has nothing to do with the binary’s own bug and says nothing about how the task was meant to be solved. The rest of this walkthrough covers the intended route.
What signal does when it starts
Traced with strace, the binary installs a SIGINT handler, prints Enter your name:, spawns five threads that each call pthread_join on the same handle, and then waits. Sending an interrupt (Ctrl-C, or SIGINT sent to the process) triggers the handler, which prints a message, sleeps for three seconds, prints Goodbye, and then processes whatever name was entered: it decodes the string two characters at a time as a hexadecimal byte, writes each decoded byte with putchar, and finally copies the accumulated output into another buffer with strcpy before printing it with printf as a format string (not as a literal string argument).
A decompiled version of the handler makes the bug structure explicit. The decoding loop writes into a fixed local array named src here, sized to hold at most a handful of bytes comfortably, but the loop runs for strlen(name) * 2 iterations with no bound tied to the array’s actual size. Because name is attacker-controlled and read earlier with no length limit either, this loop alone is a stack buffer overflow. Two more issues sit right next to it: the final printf is called on a buffer that was built from user-controlled bytes rather than a fixed format string, which is a format string vulnerability, and the strcpy that fills that buffer is itself unbounded.
Of the three, the hex-decoding overflow is the most direct one to use, because it gives byte-for-byte control over what ends up on the stack, including over the saved return address, as long as the written bytes do not include a null terminator too early.
Checking what protections apply
checksec-style output for the binary reports no stack canary, FORTIFY disabled, a non-executable stack, no PIE, and partial RELRO. Non-executable stack rules out writing raw shellcode into the overflowed buffer and jumping to it. No PIE means the binary’s own addresses are fixed across runs, though that matters less here since the overwrite target is a libc function, not binary code. The absence of a canary means the overflow can proceed in a straight line to the saved return address without first needing to guess or leak a canary value.
A scan of the .rel.plt section with readelf -r lists the imported functions: printf, signal, sleep, strcpy, malloc, puts, strerror, strlen, putchar, strncpy, pthread_join, scanf, pthread_create, strtol. Nothing in that list executes a shell directly (no system, no execve), which rules out a plain PLT redirect and confirms that a return-to-libc chain, pivoting into the loaded C library itself rather than the binary’s own imports, is the way to a shell.
Building the overflow
The decoding loop reads the name two hex characters at a time, so the overflow payload itself must be written entirely as pairs of hex digits, which the handler converts back to raw bytes before they land on the stack. Filling the buffer up to the return address took a run of filler bytes, 34 bytes in the working payload (encoded as hex, so 68 characters), followed by a null terminator byte that stops the decoding loop cleanly, and then the fabricated return sequence:
ABCDEFGHIJABCDEFGHIJABCDEFGHIJABCD[NULL]
The letters here stand in for the filler; what follows them in the real payload are the address of system in the loaded libc and, immediately after it, the address of a /bin/sh string that already exists inside libc’s own data. Because this is a 32-bit x86 target, addresses are four bytes written in little-endian order, so each address appears reversed byte by byte when typed out as hex pairs.
Both addresses came from a live debugging session against a locally run copy of the binary with the same libc:
gdb-peda$ p/x &system
gdb-peda$ find /bin/sh
This step depends on having the exact libc and libpthread the target server used; the task environment having gone offline partway through the event, this part of the walkthrough records the method rather than a specific address, since any address captured here would not match a different system’s libc layout. In practice, matching the dynamic linker’s reported library versions (visible via .dynamic entries or ldd on the binary) to a local copy of the same distribution is the way to get addresses that transfer.
Putting it together
The full input is one line: the filler bytes, a null terminator to end the hex-decoding loop early, the address of system, a placeholder return address for after the call (since nothing needs to run afterward, any readable address works), and the address of /bin/sh. Sent as the “name” and followed by a SIGINT signal to the running process, the handler decodes it onto the stack, returns not to its caller but into system("/bin/sh"), and the SGID process hands over a shell running as the pwnreader group, which can read flag.txt.
What transfers to other challenges
The pattern here, a signal handler or any rarely-reviewed callback that decodes user input into a fixed-size stack buffer, shows up often enough to be worth checking for specifically when a binary installs custom signal handling. readelf -r or objdump -R for a quick inventory of available functions before assuming shellcode is necessary saves time, since a return-to-libc or return-oriented chain is often simpler once a non-executable stack is confirmed. The underlying question, “what writer puts a 0 or 1 into the channel the rest of the program trusts,” echoes the fork-and-pipe structure in RE1 from the same contest. For spotting a shellcode or ROP-heavy region by its byte patterns before disassembling, see binary data visualization.
Questions readers ask
Why did some teams get a root shell instead of solving the intended task?
The server ran an outdated kernel with a known local privilege escalation vulnerability (CVE-2014-4014), unrelated to the signal binary. That bug gave root directly and was not the path the task designed, and the server was taken offline during the event once it was used that way.
Why use a signal handler instead of a normal function for the overflow?
Delivering SIGINT during the blocking read lets the handler run with its own stack frame while the main thread is paused, and the handler is where the vulnerable hex-decoding loop lives. Triggering it is simply a matter of sending an interrupt (Ctrl-C over an interactive session, or a signal to the process) after providing the name.
Why was scp not usable to copy the binary off the server?
The restricted account had no access to /dev, and scp depends on being able to open /dev/null for its protocol handshake, so the transfer failed. Reading the file's bytes through a plain command over the same SSH session, redirected to a local file, worked instead.
What makes this a return-to-libc attack rather than shellcode injection?
The stack is marked non-executable, so injected machine code cannot run from it. A return-to-libc attack instead overwrites the return address and stack arguments so execution continues inside existing library code, in this case the libc system function, which needs no new executable memory.
Why does the exploit need the address of the string /bin/sh in addition to system?
system expects one argument, a pointer to the command line to run. Building the fake call frame means placing the address of system where the return address normally goes, followed by a return address for after the call and then the address of a /bin/sh string already present in the loaded libc, which supplies that argument.
New to capture the flag? Formats, teams and a first-month plan in one place.
Read the CTF guide