Skip to content

Commit aaa271c

Browse files
authored
Merge pull request #543 from BrianGodd/lab8
[LAB8] 111550083
2 parents c01fa11 + dfa7bfe commit aaa271c

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

lab8/solve.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
#!/usr/bin/env python3
22

33
import angr,sys
4+
import claripy
45

56
def main():
6-
secret_key = b""
7-
sys.stdout.buffer.write(secret_key)
7+
8+
#新建專案且不自動載入函式庫
9+
proj = angr.Project('./chal', auto_load_libs=False)
10+
11+
#建立8-bit輸入
12+
sym_chars = [claripy.BVS(f'byte_{i}', 8) for i in range(8)]
13+
sym_input = claripy.Concat(*sym_chars)
14+
15+
#初始化執行狀態並模擬stdin輸入
16+
state = proj.factory.full_init_state(
17+
stdin = angr.SimFileStream(name='stdin', content=sym_input, has_end=True)
18+
)
19+
20+
#建立模擬器並開始搜尋個別狀態
21+
simgr = proj.factory.simgr(state)
22+
simgr.explore(
23+
find = lambda s:b"Correct!" in s.posix.dumps(1)
24+
)
25+
26+
#找到則輸出結果,否則輸出 "No solution found!"
27+
if simgr.found:
28+
found = simgr.found[0]
29+
secret_key = found.solver.eval(sym_input, cast_to=bytes)
30+
sys.stdout.buffer.write(secret_key)
31+
else:
32+
print("No solution found!")
33+
sys.exit(1)
834

935

1036
if __name__ == '__main__':

0 commit comments

Comments
 (0)