我是第一次学习安格尔的学生。
我在看这个网址里的密码。
import angr
import claripy
FLAG_LEN = 15
STDIN_FD = 0
base_addr = 0x100000 # To match addresses to Ghidra
proj = angr.Project("./a.out", main_opts={'base_addr': base_addr})
flag_chars = [claripy.BVS('flag_%d' % i, 8) for i in range(FLAG_LEN)]
flag = claripy.Concat( *flag_chars + [claripy.BVV(b'\n')]) # Add \n for scanf() to accept the input
state = proj.factory.full_init_state(
args=['./a.out'],
add_options=angr.options.unicorn,
stdin=flag,
)
# Add constraints that all characters are printable
for k in flag_chars:
state.solver.add(k >= ord('!'))
state.solver.add(k <= ord('~'))
simgr = proj.factory.simulation_manager(state)
find_addr = 0x101124 # SUCCESS
avoid_addr = 0x10110d # FAILURE
simgr.explore(find=find_addr, avoid=avoid_addr)
if (len(simgr.found) > 0):
for found in simgr.found:
print(found.posix.dumps(STDIN_FD))https://github.com/google/google-ctf/tree/master/2020/quals/reversing-beginner/attachments
这是googlectf初学者的答案。
但是,上述代码不起作用。它不能给我答案。
我想知道为什么代码不起作用。
当我执行此代码时,输出为空。
我在Ubuntu20.04中用python3在wsl2中运行代码
谢谢。
发布于 2022-06-05 11:58:29
我相信这个脚本没有打印任何东西,因为angr找不到解决方案,然后退出。您可以通过在脚本中附加以下内容来证明这一点:
else:
raise Exception('Could not find the solution')如果异常引发,则找不到有效的解决方案。
从为什么它不能工作的角度来看,这段代码看起来像来自几个不同来源的复制和粘贴,所以它相当复杂。
例如,将标志符号传递给stdin的方式并不理想。默认情况下,stdin是一个SimPackets,所以最好保持这种状态。
下面的脚本解决了这个问题,我已经对它进行了评论,以帮助您理解。您将注意到,由于上述原因,将stdin=angr.SimPackets(name='stdin', content=[(flag, 15)])更改为stdin=flag将导致脚本失败。
import angr
import claripy
base = 0x400000 # Default angr base
project = angr.Project("./a.out")
flag = claripy.BVS("flag", 15 * 8) # length is expected in bits here
initial_state = project.factory.full_init_state(
stdin=angr.SimPackets(name='stdin', content=[(flag, 15)]), # provide symbol and length (in bytes)
add_options ={
angr.options.SYMBOL_FILL_UNCONSTRAINED_MEMORY,
angr.options.SYMBOL_FILL_UNCONSTRAINED_REGISTERS
}
)
# constrain flag to common alphanumeric / punctuation characters
[initial_state.solver.add(byte >= 0x20, byte <= 0x7f) for byte in flag.chop(8)]
sim = project.factory.simgr(initial_state)
sim.explore(
find=lambda s: b"SUCCESS" in s.posix.dumps(1), # search for a state with this result
avoid=lambda s: b"FAILURE" in s.posix.dumps(1) # states that meet this constraint will be added to the avoid stash
)
if sim.found:
solution_state = sim.found[0]
print(f"[+] Success! Solution is: {solution_state.posix.dumps(0)}") # dump whatever was sent to stdin to reach this state
else:
raise Exception('Could not find the solution') # Tell us if angr failed to find a solution state一些琐事-实际上有多个“解决方案”的程序会接受,我想CTF标志服务器只接受一个。
❯ echo -ne 'CTF{\x00\xe0MD\x17\xd1\x93\x1b\x00n)' | ./a.out
Flag: SUCCESShttps://stackoverflow.com/questions/69439863
复制相似问题