Skip to content

Commit 28690c1

Browse files
committed
complete lab8
1 parent 6494ab9 commit 28690c1

1 file changed

Lines changed: 43 additions & 4 deletions

File tree

lab8/solve.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,50 @@
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+
# Load the binary
9+
project = angr.Project("./chal", auto_load_libs=False)
810

11+
# Declare 8 symbolic bytes as input
12+
key_len = 8
13+
key = [claripy.BVS(f'key{i}', 8) for i in range(key_len)]
14+
15+
# Concatenate to form a single bitvector
16+
input_bytes = claripy.Concat(*key)
17+
18+
# Create symbolic state at program entry
19+
state = project.factory.full_init_state(
20+
args=["./chal"],
21+
stdin=input_bytes
22+
)
23+
24+
# Constrain input to be printable (optional but practical)
25+
for k in key:
26+
state.solver.add(k >= 0x20) # space
27+
state.solver.add(k <= 0x7e) # ~
28+
29+
# Set up simulation
30+
simgr = project.factory.simgr(state)
31+
32+
# Define success/failure conditions
33+
def is_successful(state):
34+
return b"Correct! The flag is:" in state.posix.dumps(1)
35+
36+
def should_abort(state):
37+
return b"Wrong key!" in state.posix.dumps(1)
38+
39+
# Explore until success
40+
simgr.explore(find=is_successful, avoid=should_abort)
41+
42+
if simgr.found:
43+
found_state = simgr.found[0]
44+
solution = found_state.solver.eval(input_bytes, cast_to=bytes)
45+
sys.stdout.buffer.write(solution)
46+
else:
47+
print("No solution found.")
948

1049
if __name__ == '__main__':
11-
main()
50+
main()

0 commit comments

Comments
 (0)