针对angr提供的练习题,现在进行求解07_angr_symbolic_file,需要使用angr求解出正确密码。但是需要提供排除地址以减少路径求解时间。
具体代码如下所示
import angr
import claripy
import sys
def main(argv):
path_to_binary = argv[1]
project = angr.Project(path_to_binary)
start_address = 0x80488ea
initial_state = project.factory.blank_state(addr=start_address)
filename = 'OJKSQYDP.txt' # :string
symbolic_file_size_bytes = 64
password = claripy.BVS('password', symbolic_file_size_bytes * 8)
password_file = angr.storage.SimFile(filename, content=password, size=symbolic_file_size_bytes)
initial_state.fs.insert(filename,password_file)
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]
solution = solution_state.solver.eval(password,cast_to=bytes)
print(solution.decode('utf-8'))
else:
raise Exception('Could not find the solution')
if __name__ == '__main__':
main(sys.argv)
把第一个读取的内容存入OJKSQYDP.txt,因此入口地址为其后指令,即0x80488ea。文件大小为64,对应指令push offset a64s。
因为需要将输入内容存储在OJKSQYDP.txt,再读取并存储于buffer中。此外需要模拟文件系统,使用angr.storage.SimFile(),里面的内容为自定义的符号变量password。
下面验证实验结果
执行刚刚写好的程序,保存为scaffold07.py,并将其与07_angr_symbolic_file放于同一文件夹中,具体如下图所示。
再执行07_angr_symbolic_file,然后需要我们输入angr刚刚求解出的密码,结果为Good Job。
至此,求解07_angr_symbolic_file已全部完成。