# Imports
import angr
import claripy
We push the input values to the stack and then pop them right before the function exits. Since no value is pushed onto the stack during the execution of the function, we can safely manipulate the stack in the way we planned.
Load the binary.
project = angr.Project('bomb64')
Define the address for the start of the execution path.
# The address where the symbolic execution shall begin. It is the beginning of the func4 function.
addr_start = 0x400FCE
# The address of the return of func4.
addr_target = 0x401007
# The address of the first instruction of the explode_bomb function, which is to be avoided.
addr_bomb = 0x40143A
Define a blank state for the simulation, a state with most of it's data uninitialized. Pass the address where the state is initialized, along with the user input to be given via standard input.
state = project.factory.blank_state(addr=addr_start)
Move a symbolic value into the relevant register.
num_1 = claripy.BVS("num_1", 32)
Setup the state of the registers as they were right before the function call.
state.regs.edx = 0xE
state.regs.esi = 0
state.regs.edi = num_1
Create a simulation manager with this blank state that would help us manage the symbolic execution.
simgr = project.factory.simulation_manager(state)
We call the explore method of the simulation manager, tasked with finding an execution path that reaches the target address and avoids the address which explodes the bomb.
simgr.explore(find=addr_target, avoid=addr_bomb, enable_veritesting=True)
We dereference the execution path "found" by the simulation manager and dump the stack.
found = simgr.found[0]
found.solver.eval(num_1)
This is the first number. Since we already know the second number is 0, the user input that passes the fourth phase is 7 0
Let's progressively add constraints to reach different possible end values. Add the constraint for the new value to be smaller than the one in previous iteration.
In the form: state.solver.add(num_1 < x), where x is the integer value discovered in the previous iteration.
Let's put it in a loop to get the all the values.
x = 10
while x > 0:
# Load the binary.
project = angr.Project('bomb64')
# Init the addresses.
addr_start = 0x400FCE
addr_target = 0x401007
addr_bomb = 0x40143A
# Init a blank state.
state = project.factory.blank_state(addr=addr_start)
# Define the symbolic value.
num_1 = claripy.BVS("num_1", 32)
# Setup the registers.
state.regs.edx = 0xE
state.regs.esi = 0
state.regs.edi = num_1
# Add the constraint.
state.solver.add(num_1<x)
# Init the simulation manager.
simgr = project.factory.simulation_manager(state)
# Let Angr explore.
simgr.explore(find=addr_target, avoid=addr_bomb, enable_veritesting=True)
# Get the answer from the found state.
found = simgr.found[0]
x = found.solver.eval(num_1)
print(x, "0")
And there we go, more possible end values.