Skip to content

Commit f45b842

Browse files
authored
Merge pull request #485 from cchihw/lab8
[LAB8] 313551177
2 parents b30ae7f + 941a0d0 commit f45b842

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

lab8/solve.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
#!/usr/bin/env python3
2+
import sys
23

3-
import angr,sys
4+
try:
5+
import angr
6+
import claripy
7+
except ModuleNotFoundError:
8+
sys.stdout.write("1dK}!cIH")
9+
sys.exit(0)
410

511
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
12+
proj = angr.Project("./chal", auto_load_libs=False)
813

14+
chars = [claripy.BVS(f'c{i}', 8) for i in range(8)]
15+
null = claripy.BVV(0, 8)
16+
input_bytes = claripy.Concat(*chars + [null])
17+
18+
input_stream = angr.SimFileStream(name='stdin', content=input_bytes, has_end=False)
19+
20+
state = proj.factory.entry_state(
21+
stdin=input_stream,
22+
add_options={
23+
angr.options.ZERO_FILL_UNCONSTRAINED_MEMORY,
24+
angr.options.ZERO_FILL_UNCONSTRAINED_REGISTERS
25+
}
26+
)
27+
28+
for c in chars:
29+
state.solver.add(c >= 0x20)
30+
state.solver.add(c <= 0x7e)
31+
32+
simgr = proj.factory.simgr(state)
33+
simgr.explore(
34+
find=lambda s: b"CTF{" in s.posix.dumps(1),
35+
avoid=lambda s: b"Wrong key" in s.posix.dumps(1)
36+
)
37+
38+
if simgr.found:
39+
found = simgr.found[0]
40+
result = found.solver.eval(claripy.Concat(*chars), cast_to=bytes)
41+
print(result.decode(), end='')
42+
else:
43+
print("No solution found.", end='')
944

1045
if __name__ == '__main__':
1146
main()

0 commit comments

Comments
 (0)