Skip to content

Commit 41b1abe

Browse files
authored
Merge pull request #501 from MinKuanIsHere/lab8
[LAB8] 313552009
2 parents 8257583 + 848708c commit 41b1abe

1 file changed

Lines changed: 58 additions & 5 deletions

File tree

lab8/solve.py

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

3-
import angr,sys
3+
import angr
4+
import claripy
5+
import logging
6+
logging.getLogger('angr').setLevel(logging.ERROR)
7+
import sys
8+
import os
49

510
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
8-
11+
# Check if ./chal exists
12+
if not os.path.isfile('./chal'):
13+
print("Error: './chal' binary not found. Run 'make' to compile it.", file=sys.stderr)
14+
sys.exit(1)
15+
16+
# Load the binary
17+
try:
18+
proj = angr.Project('./chal', auto_load_libs=False)
19+
except Exception as e:
20+
print(f"Error loading binary: {e}", file=sys.stderr)
21+
sys.exit(1)
22+
23+
# Create symbolic input (8 bytes)
24+
input_chars = [claripy.BVS(f'char_{i}', 8) for i in range(8)]
25+
26+
# Create initial state with symbolic input on stdin
27+
state = proj.factory.entry_state(stdin=claripy.Concat(*input_chars))
28+
29+
# Constrain input to printable ASCII (32-126)
30+
for c in input_chars:
31+
state.solver.add(c >= 32)
32+
state.solver.add(c <= 126)
33+
34+
# Create simulation manager
35+
simgr = proj.factory.simulation_manager(state)
36+
37+
# Explore to find the path that prints the flag
38+
def is_successful(state):
39+
stdout_content = state.posix.dumps(1) # Check stdout
40+
return b"Correct!" in stdout_content
41+
42+
def is_failed(state):
43+
stdout_content = state.posix.dumps(1)
44+
return b"Wrong key!" in stdout_content
45+
46+
simgr.explore(find=is_successful, avoid=is_failed)
47+
48+
# Check if a successful state was found
49+
if simgr.found:
50+
found_state = simgr.found[0]
51+
# Extract concrete values for the input
52+
secret_key = b""
53+
for c in input_chars:
54+
val = found_state.solver.eval(c)
55+
secret_key += bytes([val])
56+
57+
# Output the secret key to stdout
58+
sys.stdout.buffer.write(secret_key)
59+
else:
60+
print("No solution found!", file=sys.stderr)
61+
sys.exit(1)
962

1063
if __name__ == '__main__':
11-
main()
64+
main()

0 commit comments

Comments
 (0)