11#!/usr/bin/env python3
2-
3- import angr ,sys
2+ import logging
3+ logging .getLogger ('angr' ).setLevel (logging .ERROR )
4+ import angr
5+ import claripy
6+ import sys
47
58def main ():
6- secret_key = b""
7- sys .stdout .buffer .write (secret_key )
8-
9+ # Load the binary
10+ proj = angr .Project ('./chal' , auto_load_libs = False )
11+
12+ # Create symbolic input (8 bytes)
13+ input_chars = [claripy .BVS (f'char_{ i } ' , 8 ) for i in range (8 )]
14+
15+ # Create initial state with symbolic input on stdin
16+ state = proj .factory .entry_state (stdin = claripy .Concat (* input_chars ))
17+
18+ # Optionally constrain input to printable ASCII (32-126)
19+ for c in input_chars :
20+ state .solver .add (c >= 32 )
21+ state .solver .add (c <= 126 )
22+
23+ # Create simulation manager
24+ simgr = proj .factory .simulation_manager (state )
25+
26+ # Explore to find the path that prints the flag
27+ # Find the address of puts("Correct! ...") or successful exit
28+ def is_successful (state ):
29+ stdout_content = state .posix .dumps (1 ) # Check stdout
30+ return b"Correct!" in stdout_content
31+
32+ def is_failed (state ):
33+ stdout_content = state .posix .dumps (1 )
34+ return b"Wrong key!" in stdout_content
35+
36+ simgr .explore (find = is_successful , avoid = is_failed )
37+
38+ # Check if a successful state was found
39+ if simgr .found :
40+ found_state = simgr .found [0 ]
41+ # Extract concrete values for the input
42+ secret_key = b""
43+ for c in input_chars :
44+ val = found_state .solver .eval (c )
45+ secret_key += bytes ([val ])
46+
47+ # Output the secret key to stdout
48+ sys .stdout .buffer .write (secret_key )
49+ else :
50+ print ("No solution found!" )
51+ sys .exit (1 )
952
1053if __name__ == '__main__' :
11- main ()
54+ main ()
0 commit comments