|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
3 |
| -import angr,sys |
| 3 | +import angr,sys,claripy |
| 4 | + |
| 5 | +def is_successful(state): |
| 6 | + stdout = state.posix.dumps(sys.stdout.fileno()) |
| 7 | + print(stdout) |
| 8 | + if b"CTF" in stdout: |
| 9 | + return True |
| 10 | + return False |
| 11 | + |
| 12 | +def should_abort(state): |
| 13 | + stdout = state.posix.dumps(sys.stdout.fileno()) |
| 14 | + if b"Wrong" in stdout: |
| 15 | + return True |
| 16 | + return False |
| 17 | + |
4 | 18 |
|
5 | 19 | def main():
|
6 |
| - secret_key = b"" |
| 20 | + proj = angr.Project("chal", auto_load_libs=False) |
| 21 | + |
| 22 | + |
| 23 | + # add_options = { |
| 24 | + # angr.options.SYMBOL_FILL_UNCONSTRAINED_MEMORY, |
| 25 | + # angr.options.SYMBOL_FILL_UNCONSTRAINED_REGISTERS |
| 26 | + # } |
| 27 | + # ) |
| 28 | + # inp_ ch = [claripy.BVS('inp_%d', % i, 8) for i in range(9)] |
| 29 | + # inp = claripy.Concat(*inp_ch + [charipy.BVV(b'\n')]) |
| 30 | + # for k in inp_ch: |
| 31 | + # state.solver.add(i >= 32) |
| 32 | + # state.solver.add(i <= 126) |
| 33 | + |
| 34 | + inp = [claripy.BVS(f'c{i}', 8) for i in range(8)] |
| 35 | + sym_input = claripy.Concat(*inp + [claripy.BVV(b'\n')]) |
| 36 | + |
| 37 | + state = proj.factory.entry_state( |
| 38 | + stdin=sym_input, |
| 39 | + add_options = { |
| 40 | + angr.options.SYMBOL_FILL_UNCONSTRAINED_MEMORY, |
| 41 | + angr.options.SYMBOL_FILL_UNCONSTRAINED_REGISTERS |
| 42 | + } |
| 43 | + ) |
| 44 | + |
| 45 | + for i in inp: |
| 46 | + state.solver.add(i >= 32) |
| 47 | + state.solver.add(i <= 126) |
| 48 | + |
| 49 | + |
| 50 | + simgr = proj.factory.simgr() |
| 51 | + simgr.explore(find=is_successful, avoid=should_abort) |
| 52 | + |
| 53 | + secret_key = simgr.found[0].posix.dumps(0) |
| 54 | + |
7 | 55 | sys.stdout.buffer.write(secret_key)
|
8 |
| - |
| 56 | + |
9 | 57 |
|
10 | 58 | if __name__ == '__main__':
|
11 | 59 | main()
|
0 commit comments