|
1 | 1 | #!/usr/bin/env python3 |
2 | | - |
3 | | -import angr,sys |
| 2 | +import angr |
| 3 | +import claripy |
| 4 | +from angr import options as angr_options |
| 5 | +import logging |
| 6 | +logging.getLogger("angr").setLevel(logging.ERROR) |
4 | 7 |
|
5 | 8 | def main(): |
6 | | - secret_key = b"" |
7 | | - sys.stdout.buffer.write(secret_key) |
| 9 | + project = angr.Project('./chal', auto_load_libs=False) |
| 10 | + |
| 11 | + # Create symbolic bitvectors: 8 characters, each 8 bits |
| 12 | + input_len = 8 |
| 13 | + input_chars = [claripy.BVS(f'char{i}', 8) for i in range(input_len)] |
| 14 | + input_concat = claripy.Concat(*input_chars) |
| 15 | + |
| 16 | + # Append null terminator to simulate fgets() |
| 17 | + input_with_null = claripy.Concat(input_concat, claripy.BVV(0, 8)) |
| 18 | + |
| 19 | + # Create the initial state and feed symbolic input to stdin |
| 20 | + state = project.factory.full_init_state( |
| 21 | + args=["./chal"], |
| 22 | + stdin=input_with_null |
| 23 | + ) |
| 24 | + state.options.add(angr_options.ZERO_FILL_UNCONSTRAINED_MEMORY) |
| 25 | + state.options.add(angr_options.ZERO_FILL_UNCONSTRAINED_REGISTERS) |
| 26 | + |
| 27 | + # Constrain input characters to be printable ASCII |
| 28 | + for c in input_chars: |
| 29 | + state.solver.add(c >= 0x20) # Avoid non-printable characters |
| 30 | + state.solver.add(c <= 0x7e) |
| 31 | + |
| 32 | + # Explore paths to reach puts("Correct!") and avoid puts("Wrong key!") |
| 33 | + simgr = project.factory.simgr(state) |
| 34 | + |
| 35 | + def is_successful(s): |
| 36 | + return b"Correct!" in s.posix.dumps(1) |
| 37 | + |
| 38 | + def is_failed(s): |
| 39 | + return b"Wrong key!" in s.posix.dumps(1) |
| 40 | + |
| 41 | + simgr.explore(find=is_successful, avoid=is_failed) |
8 | 42 |
|
| 43 | + if simgr.found: |
| 44 | + found = simgr.found[0] |
| 45 | + result = found.solver.eval(input_concat, cast_to=bytes) |
| 46 | + print(result.decode()) |
| 47 | + else: |
| 48 | + print("No solution found.") |
9 | 49 |
|
10 | 50 | if __name__ == '__main__': |
11 | 51 | main() |
0 commit comments