KCTF{_n_o_w_o_r_d_s_c_h_a_r}Title: pwn2
Problem: "I don't like words but I love chars."
Connection: nc 66.228.49.41 1337
Upon connecting to the server, we are presented with a prompt:
== KSA Jail ==
>
Inputting standard Python commands like print(1), import os, or just variable names results in errors like error: name not allowed or error: unknown function. This indicates a restricted Python environment (jail) where specific AST (Abstract Syntax Tree) nodes are blocked.
I wrote a script to enumerate allowed inputs using pwntools.
'a', '' were echoed back. Lists, Dicts, Tuples, and Attributes were blocked (error: node not allowed).print, eval, exec etc. were unknown.L() -> Returned 28 (Integer). Likely the length of the flag.Q() -> error: Oracle.Q() missing 2 required positional arguments: 'i' and 'x'.S() -> error: Oracle.S() missing 1 required positional argument: 'guess'.28.S('test') returned 'Nope.'.Q(0, 'A') gave an error expecting integers.Q(0, 0) returned -1.Q(0, 75) (ASCII for 'K') returned 0.Q(0, 76) (ASCII for 'L') returned 1.Q(0, 74) (ASCII for 'J') returned -1.From this behavior, we deduced that Q(i, x) compares the character at index i of the flag with the integer x.
0 if flag[i] == chr(x).1 if flag[i] < chr(x) (guess x is greater than target).-1 if flag[i] > chr(x) (guess x is smaller than target).Since we have an Oracle that tells us if our guess is too high or too low, we can perform a binary search for each character of the flag. This reduces the complexity from 256 (or ~95 printable) requests per character to just log2(95) ≈ 7 requests per character.
L().i from 0 to length-1:low=32 (printable space start) to high=126.low <= high:mid = (low + high) // 2Q(i, mid)Result == 0: Found character chr(mid). Break.Result == 1: Target is smaller, set high = mid - 1.Result == -1: Target is larger, set low = mid + 1.Running the exploit yielded the flag:KCTF{_n_o_w_o_r_d_s_c_h_a_r}
exploit.py: The python script used to perform the binary search.