Skip to content

Commit 51b2895

Browse files
author
mamie1031
committed
feat: modify solve.py to pass the lab8
1 parent 973359e commit 51b2895

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

lab8/solve.py

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,47 @@
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 target file
9+
proj = angr.Project('./chal', auto_load_libs=False)
810

11+
# Create 8 bytes symbolic input
12+
input_size = 8
13+
symbolic_input = claripy.BVS('input', input_size * 8)
14+
15+
# Create initial state, simulate standard input
16+
state = proj.factory.entry_state(
17+
stdin=angr.storage.file.SimFileStream(name='stdin', content=symbolic_input, has_end=False),
18+
add_options={
19+
angr.options.ZERO_FILL_UNCONSTRAINED_MEMORY,
20+
angr.options.ZERO_FILL_UNCONSTRAINED_REGISTERS
21+
}
22+
)
23+
24+
# The input is ASCII
25+
for i in range(input_size):
26+
byte = symbolic_input.get_byte(i)
27+
state.solver.add(byte >= 32, byte <= 126)
28+
29+
simgr = proj.factory.simulation_manager(state)
30+
31+
# Using objdump to find target addr
32+
find_addr = 0x401307 # puts("Correct!...") 的地址
33+
avoid_addr = 0x4013b3 # puts("Wrong key!") 的地址
34+
35+
simgr.explore(find=find_addr, avoid=avoid_addr)
36+
37+
# Find correct path
38+
if simgr.found:
39+
found_state = simgr.found[0]
40+
secret_key = found_state.solver.eval(symbolic_input, cast_to=bytes)
41+
sys.stdout.buffer.write(secret_key)
42+
else:
43+
print("No solution found!", file=sys.stderr)
44+
sys.exit(1)
945

1046
if __name__ == '__main__':
11-
main()
47+
main()

0 commit comments

Comments
 (0)