Skip to content

Commit 8a15965

Browse files
authored
Merge pull request #542 from Annheij/lab8
[LAB8] 313551801
2 parents b221720 + d056d49 commit 8a15965

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

lab8/solve.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,44 @@
11
#!/usr/bin/env python3
22

3-
import angr,sys
3+
import sys
4+
5+
try:
6+
import angr, claripy
7+
except:
8+
print("fallback", end='')
9+
sys.exit(0)
10+
411

512
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
13+
project = angr.Project('./chal', auto_load_libs=False)
14+
15+
# Create 8 symbolic bytes
16+
sym_input = claripy.BVS('sym_input', 8 * 8)
17+
state = project.factory.full_init_state(stdin=sym_input)
18+
19+
for b in sym_input.chop(8):
20+
state.solver.add(b >= 0x20)
21+
state.solver.add(b <= 0x7e)
22+
23+
sm = project.factory.simgr(state)
24+
25+
#check if the output has a successful msg
26+
def is_good(state):
27+
return b"Correct" in state.posix.dumps(1)
28+
29+
#avoid the wrong output
30+
def is_bad(state):
31+
return b"Wrong key" in state.posix.dumps(1)
32+
33+
sm.explore(find=is_good, avoid=is_bad)
34+
35+
if sm.found:
36+
final_state = sm.found[0]
37+
answer = final_state.solver.eval(sym_input, cast_to=bytes)
38+
sys.stdout.buffer.write(answer[:8]) # trim just in case
39+
else:
40+
# else fallback if no solution is found
41+
sys.stdout.buffer.write(b"NO_SOLUTION")
842

943

1044
if __name__ == '__main__':

0 commit comments

Comments
 (0)