# 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 middle block where the key phrase is verified.
addr_start = 0x401604
# The address where the hook will be established.
# It is right before the string comparison function.
# Dumps it's arguments and finds out the reference string.
addr_hook = 0x40160E
addr_avoid = 0x401635
addr_target = 0x40162B
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)
We define and add the hook to the function.
# Takes the state as initialiser.
class HookClass(angr.SimProcedure):
def run(self):
# Dumps the address of the argument to "strings_not_equal" function from the register.
addr = self.state.regs.esi
# Dereferences the address of the string and prints it.
print(self.state.mem[addr].string.concrete)
return 0
# Adds the hook to the execution flow.
project.hook(addr_hook, HookClass())
Create a simulation manager with this blank state that would help us manage the symbolic execution.
simgr = project.factory.simulation_manager(state)
Run the simulation manager so as to walk over various states.
simgr.explore(find=addr_target, avoid=addr_avoid)
There's the string we need to append to the input we provide to the input for phase_4.
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 actual verification part of the function.
addr_start = 0x40126C
# The address of the return block of phase_7.
addr_target = 0x401282
# 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.
# A BitVectorSymbol is declared.
user_input = claripy.BVS('user_input', 32)
Add the constraint as observed at the end of the first block.
state.solver.add(user_input<1001)
Store the symbolic value in the register and set others as required to prepare it for further execution of the binary.
state.regs.rax = user_input
state.regs.rbx = state.regs.rax
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(user_input, cast_to=int)
And that's the string we have to give as input to pass the secret phase.