11#!/usr/bin/env python3
2-
3- import angr
4- import claripy
52import sys
63
74def main ():
8- # Load target file
9- proj = angr .Project ('./chal' , auto_load_libs = False )
10-
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 )
5+ # Iterate over x4 and x6 to find a valid solution
6+ for x4 in range (32 , 127 ): # ASCII range
7+ x5 = 3 * x4 # Equation 3: x4 * 3 = x5
8+ if x5 < 32 or x5 > 126 : # Check if x5 is in ASCII range
9+ continue
10+ for x6 in range (32 , 127 ):
11+ x7 = x6 - 1 # Equation 4: x6 - x7 = 1
12+ if x7 < 32 or x7 > 126 : # Check x7
13+ continue
14+ if (x5 ^ x6 ) != 42 : # Equation 6: x5 XOR x6 = 0x2A (42)
15+ continue
16+ # Equations 2 and 5: x2 + x3 = 200, x1 + x2 - x3 = 50
17+ # Solve: x1 + (200 - x3) - x3 = 50 → x1 - 2*x3 = -150 → x3 = (x1 + 150)/2
18+ for x1 in range (32 , 127 ):
19+ if (x1 + 150 ) % 2 != 0 : # x3 must be an integer
20+ continue
21+ x3 = (x1 + 150 ) // 2
22+ x2 = 200 - x3 # Equation 2: x2 + x3 = 200
23+ if x2 < 32 or x2 > 126 or x3 < 32 or x3 > 126 : # Check x2, x3
24+ continue
25+ # Equation 5: x1 + x2 - x3 = 50
26+ if x1 + x2 - x3 != 50 :
27+ continue
28+ # Equation 1: x0 XOR x1 = 0x55 (85)
29+ x0 = x1 ^ 85
30+ if x0 < 32 or x0 > 126 : # Check x0
31+ continue
32+ # Found a valid solution
33+ secret_key = bytes ([x0 , x1 , x2 , x3 , x4 , x5 , x6 , x7 ])
34+ sys .stdout .buffer .write (secret_key )
35+ return
36+ print ("No solution found!" , file = sys .stderr )
37+ sys .exit (1 )
4538
4639if __name__ == '__main__' :
4740 main ()
0 commit comments