Skip to content

Commit d436d59

Browse files
committed
feat(lab8): add symbolic execution script using angr to solve input constraints
1 parent 4f3572f commit d436d59

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

lab8/solve.py

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

3-
import angr,sys
3+
import angr
4+
import claripy
5+
import sys
46

57
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
8+
project = angr.Project('./chal', auto_load_libs=False)
89

10+
buffer_len = 16
11+
input_len = 8
12+
13+
chars = [claripy.BVS(f'char{i}', 8) for i in range(input_len)]
14+
null_term = claripy.BVV(0, 8)
15+
padding = [claripy.BVV(0, 8) for _ in range(buffer_len - input_len - 1)]
16+
full_input = claripy.Concat(*chars, null_term, *padding)
17+
18+
# Create state with symbolic stdin
19+
state = project.factory.full_init_state(
20+
args=["./chal"],
21+
stdin=angr.SimFileStream(name='stdin', content=full_input, has_end=False),
22+
add_options={angr.options.ZERO_FILL_UNCONSTRAINED_MEMORY} # avoids unknown stack/memory
23+
)
24+
25+
for c in chars:
26+
state.solver.add(c >= 0x20) # printable ASCII
27+
state.solver.add(c <= 0x7e)
28+
29+
simgr = project.factory.simgr(state)
30+
31+
def is_successful(state):
32+
return b"Correct!" in state.posix.dumps(1)
33+
34+
def should_abort(state):
35+
return b"Wrong key!" in state.posix.dumps(1)
36+
37+
simgr.explore(find=is_successful, avoid=should_abort)
38+
39+
if simgr.found:
40+
found = simgr.found[0]
41+
result = found.solver.eval(claripy.Concat(*chars), cast_to=bytes)
42+
sys.stdout.buffer.write(result + b"\n")
43+
else:
44+
print("No solution found.")
945

1046
if __name__ == '__main__':
1147
main()

0 commit comments

Comments
 (0)