Skip to content

Commit 4dab518

Browse files
authored
Merge pull request #563 from Ramez96/lab8
[LAB8] 313553801
2 parents b519d62 + 41b38ce commit 4dab518

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

lab8/solve.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
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+
# Define the input length and create symbolic variables
9+
input_len = 8
10+
chars = [claripy.BVS(f'char_{i}', 8) for i in range(input_len)]
11+
sym_input = claripy.Concat(*chars)
812

13+
# Create initial program state with symbolic stdin
14+
project = angr.Project("./chal", auto_load_libs=False)
15+
state = project.factory.full_init_state(
16+
stdin=angr.SimFileStream(name='stdin', content=sym_input, has_end=True)
17+
)
18+
19+
# Constrain characters to be printable ASCII (optional but good practice)
20+
for c in chars:
21+
state.solver.add(c >= 0x20)
22+
state.solver.add(c <= 0x7e)
23+
24+
# Setup simulation manager
25+
simgr = project.factory.simgr(state)
26+
27+
# Explore only until we see the success message
28+
simgr.explore(find=lambda s: b"Correct!" in s.posix.dumps(1),
29+
avoid=lambda s: b"Wrong key!" in s.posix.dumps(1))
30+
31+
# Extract solution
32+
if simgr.found:
33+
found = simgr.found[0]
34+
result = found.solver.eval(sym_input, cast_to=bytes)
35+
sys.stdout.buffer.write(result + b"\n")
36+
else:
37+
print("No solution found", file=sys.stderr)
38+
sys.exit(1)
939

1040
if __name__ == '__main__':
1141
main()

0 commit comments

Comments
 (0)