Skip to content

Commit f425ef0

Browse files
authored
Merge pull request #496 from NienTL/lab8
[LAB8] 313551135
2 parents 27886b1 + 0d900eb commit f425ef0

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

lab8/solve.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
#!/usr/bin/env python3
2+
import sys
3+
import angr
4+
import claripy
25

3-
import angr,sys
6+
def solve_with_angr():
7+
project = angr.Project('./chal', auto_load_libs=False)
48

5-
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
9+
input_len = 9
10+
input_chars = [claripy.BVS(f'input_{i}', 8) for i in range(input_len)]
11+
input_concat = claripy.Concat(*input_chars)
812

13+
state = project.factory.full_init_state(
14+
args=["./chal"],
15+
stdin=input_concat
16+
)
17+
18+
for c in input_chars[:-1]:
19+
state.solver.add(c >= 0x20)
20+
state.solver.add(c <= 0x7e)
21+
state.solver.add(input_chars[-1] == 0x0a)
22+
23+
simgr = project.factory.simulation_manager(state)
24+
25+
def is_successful(state):
26+
return b"CTF{" in state.posix.dumps(1)
27+
28+
def should_abort(state):
29+
return b"Wrong key!" in state.posix.dumps(1)
30+
31+
simgr.explore(find=is_successful, avoid=should_abort)
32+
33+
if simgr.found:
34+
found = simgr.found[0]
35+
solution = found.solver.eval(claripy.Concat(*input_chars[:-1]), cast_to=bytes)
36+
sys.stdout.buffer.write(solution)
37+
else:
38+
sys.stdout.buffer.write(b"") # fallback or nothing
939

1040
if __name__ == '__main__':
11-
main()
41+
solve_with_angr()

0 commit comments

Comments
 (0)