针对angr提供的17道练习题,现在进行求解11_angr_sim_scanf,它也是关于hook输入的问题,需要使用angr求解出正确密码。但是需要提供排除地址以减少路径求解时间。 具体代码如下所示
import angr
import claripy
import sys
def main(argv):
path_to_binary = argv[1]
project = angr.Project(path_to_binary)
initial_state = project.factory.entry_state()
class ReplacementScanf(angr.SimProcedure):
def run(self, format_string, scanf0_address, scanf1_address):
scanf0 = claripy.BVS('scanf0', 32)
scanf1 = claripy.BVS('scanf1', 32)
self.state.memory.store(scanf0_address, scanf0, endness=project.arch.memory_endness)
self.state.memory.store(scanf1_address, scanf1, endness=project.arch.memory_endness)
self.state.globals['solution0'] = scanf0
self.state.globals['solution1'] = scanf1
scanf_symbol = '__isoc99_scanf'
project.hook_symbol(scanf_symbol, ReplacementScanf())
simulation = project.factory.simgr(initial_state)
def is_successful(state):
stdout_output = state.posix.dumps(sys.stdout.fileno())
return b'Good Job.' in stdout_output
def should_abort(state):
stdout_output = state.posix.dumps(sys.stdout.fileno())
return b'Try again.' in stdout_output
simulation.explore(find=is_successful, avoid=should_abort)
if simulation.found:
solution_state = simulation.found[0]
stored_solutions0 = solution_state.globals['solution0']
stored_solutions1 = solution_state.globals['solution1']
solution0 = solution_state.solver.eval(stored_solutions0)
solution1 = solution_state.solver.eval(stored_solutions1)
print('solution are: {0},{1} '.format(solution0,solution1))
else:
raise Exception('Could not find the solution')
if __name__ == '__main__':
main(sys.argv)
下面验证实验结果 执行刚刚写好的程序,保存为scaffold11.py,并将其与11_angr_sim_scanf放于同一文件夹中,具体如下图所示。 再执行11_angr_sim_scanf,然后需要我们输入angr刚刚求解出的密码,结果为Good Job。 至此,求解11_angr_sim_scanf已全部完成。
|