|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
3 | | -import angr,sys |
| 3 | +import angr |
| 4 | +import claripy |
| 5 | +import sys |
4 | 6 |
|
5 | 7 | def main(): |
6 | | - secret_key = b"" |
7 | | - sys.stdout.buffer.write(secret_key) |
| 8 | + project = angr.Project('./chal', auto_load_libs=False) |
8 | 9 |
|
| 10 | + buffer_len = 16 |
| 11 | + input_len = 8 |
| 12 | + |
| 13 | + chars = [claripy.BVS(f'char{i}', 8) for i in range(input_len)] |
| 14 | + null_term = claripy.BVV(0, 8) |
| 15 | + padding = [claripy.BVV(0, 8) for _ in range(buffer_len - input_len - 1)] |
| 16 | + full_input = claripy.Concat(*chars, null_term, *padding) |
| 17 | + |
| 18 | + # Create state with symbolic stdin |
| 19 | + state = project.factory.full_init_state( |
| 20 | + args=["./chal"], |
| 21 | + stdin=angr.SimFileStream(name='stdin', content=full_input, has_end=False), |
| 22 | + add_options={angr.options.ZERO_FILL_UNCONSTRAINED_MEMORY} # avoids unknown stack/memory |
| 23 | + ) |
| 24 | + |
| 25 | + for c in chars: |
| 26 | + state.solver.add(c >= 0x20) # printable ASCII |
| 27 | + state.solver.add(c <= 0x7e) |
| 28 | + |
| 29 | + simgr = project.factory.simgr(state) |
| 30 | + |
| 31 | + def is_successful(state): |
| 32 | + return b"Correct!" in state.posix.dumps(1) |
| 33 | + |
| 34 | + def should_abort(state): |
| 35 | + return b"Wrong key!" in state.posix.dumps(1) |
| 36 | + |
| 37 | + simgr.explore(find=is_successful, avoid=should_abort) |
| 38 | + |
| 39 | + if simgr.found: |
| 40 | + found = simgr.found[0] |
| 41 | + result = found.solver.eval(claripy.Concat(*chars), cast_to=bytes) |
| 42 | + sys.stdout.buffer.write(result + b"\n") |
| 43 | + else: |
| 44 | + print("No solution found.") |
9 | 45 |
|
10 | 46 | if __name__ == '__main__': |
11 | 47 | main() |
0 commit comments