Skip to content

Commit acb9b9f

Browse files
NienTzuNienTzu
authored andcommitted
submit lab8 4th
1 parent dcc727f commit acb9b9f

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

lab8/solve.py

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
#!/usr/bin/env python3
2+
23
import sys
4+
import angr
5+
import claripy
6+
7+
def solve_with_angr():
8+
project = angr.Project('./chal', auto_load_libs=False)
39

4-
try:
5-
import angr
6-
import claripy
7-
import logging
8-
logging.getLogger("angr").setLevel(logging.ERROR)
10+
input_len = 8
11+
input_chars = [claripy.BVS(f'input_{i}', 8) for i in range(input_len)]
12+
input_concat = claripy.Concat(*input_chars)
913

10-
def solve_with_angr():
11-
project = angr.Project("./chal", auto_load_libs=False)
12-
input_len = 9
13-
input_chars = [claripy.BVS(f"input_{i}", 8) for i in range(input_len)]
14-
input_concat = claripy.Concat(*input_chars)
1514

16-
state = project.factory.full_init_state(
17-
args=["./chal"],
18-
stdin=input_concat
19-
)
15+
state = project.factory.full_init_state(
16+
args=["./chal"],
17+
stdin=input_concat
18+
)
2019

21-
for c in input_chars[:-1]:
22-
state.solver.add(c >= 0x20)
23-
state.solver.add(c <= 0x7e)
24-
state.solver.add(input_chars[-1] == 0x0a)
20+
for c in input_chars:
21+
state.solver.add(c >= 0x20)
22+
state.solver.add(c <= 0x7e)
2523

26-
simgr = project.factory.simulation_manager(state)
2724

28-
def is_successful(state):
29-
return b"CTF{" in state.posix.dumps(1)
25+
simgr = project.factory.simulation_manager(state)
3026

31-
def should_abort(state):
32-
return b"Wrong key!" in state.posix.dumps(1)
27+
def is_successful(state):
28+
return b"CTF{" in state.posix.dumps(1)
3329

34-
simgr.explore(find=is_successful, avoid=should_abort)
30+
def should_abort(state):
31+
return b"Wrong key!" in state.posix.dumps(1)
3532

36-
if simgr.found:
37-
found = simgr.found[0]
38-
solution = found.solver.eval(claripy.Concat(*input_chars[:-1]), cast_to=bytes)
39-
print("Solution:", solution)
40-
return solution
41-
else:
42-
return b"Q`U4DD0/"
33+
simgr.explore(find=is_successful, avoid=should_abort)
4334

44-
def main():
45-
sys.stdout.buffer.write(solve_with_angr())
35+
if simgr.found:
36+
found = simgr.found[0]
37+
solution = found.solver.eval(input_concat, cast_to=bytes)
38+
# print("Solution: ", solution)
39+
return solution
40+
else:
41+
# print("No solution!")
42+
return b""
4643

47-
except ImportError:
48-
def main():
49-
sys.stdout.buffer.write(b"Q`U4DD0/")
44+
def main():
45+
sys.stdout.buffer.write(solve_with_angr())
5046

51-
if __name__ == "__main__":
52-
main()
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)