Skip to content

Commit c0b3408

Browse files
committed
feat: implement binary analysis for lab8
1 parent cdae9d4 commit c0b3408

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

lab8/solve.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,48 @@
11
#!/usr/bin/env python3
22

3-
import angr,sys
3+
import sys
4+
5+
import angr
6+
import claripy
7+
8+
PROJECT_PATH = "./chal"
9+
10+
11+
def answer(key):
12+
sys.stdout.buffer.write(key)
13+
14+
15+
def success(state):
16+
return b"Correct" in state.posix.dumps(1)
17+
18+
19+
def failure(state):
20+
return b"Wrong" in state.posix.dumps(1)
21+
422

523
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
24+
project = angr.Project(PROJECT_PATH, auto_load_libs=False)
25+
26+
input = claripy.BVS("input", 64) # 8 bytes
27+
28+
state = project.factory.full_init_state(stdin=input)
29+
30+
for byte in input.chop(8):
31+
# answer should be printable ascii, 0x20 ~ 0x7E
32+
state.solver.add(byte >= 0x20)
33+
state.solver.add(byte <= 0x7E)
34+
35+
simgr = project.factory.simgr(state)
36+
37+
simgr.explore(find=success, avoid=failure)
38+
39+
if simgr.found:
40+
found = simgr.found[0]
41+
solution = found.solver.eval(input, cast_to=bytes)
42+
answer(solution)
43+
else:
44+
raise Exception("AnswerNotFoundError")
845

946

10-
if __name__ == '__main__':
47+
if __name__ == "__main__":
1148
main()

0 commit comments

Comments
 (0)