您可以使用pwnlib.gdb与 gdb 交互。
您可以使用 gdb.attach() 函数:来自文档:
bash = process('bash')
# Attach the debugger
gdb.attach(bash, '''
set follow-fork-mode child
break execve
continue
''')
# Interact with the process
bash.sendline('whoami')
或者你可以使用 gdb.debug():
# Create a new process, and stop it at 'main'
io = gdb.debug('bash', '''
# Wait until we hit the main executable's entry point
break _start
continue
# Now set breakpoint on shared library routines
break malloc
break free
continue
''')
# Send a command to Bash
io.sendline("echo hello")
# Interact with the process
io.interactive()
pwntools 模板包含让您开始使用 gdb 进行调试的代码。您可以通过运行来创建 pwntools 模板pwn template ./binary_name > template.py
。然后你必须在运行 template.py 来调试时添加 GDB arg: ./template.py GDB
。
如果你得到[ERROR] Could not find a terminal binary to use.
,你可能需要context.terminal
在使用 gdb 之前进行设置。
如果您使用 tmux,以下内容将在新的水平拆分窗口中自动打开 gdb 调试会话:
context.terminal = ["tmux", "splitw", "-h"]
并使用新的 gdb 会话窗口垂直分割屏幕:
context.terminal = ["tmux", "splitw", "-v"]
(注意:我从来没有让这部分工作,所以如果它可以工作,请告诉我。如果你让 gdb 工作,请告诉我)。
(要使用 tmux,请在您的机器上安装 tmux,然后只需键入tmux
即可启动它。然后键入python template.py GDB
.
如果上述方法都不起作用,那么您始终可以启动脚本,使用ps aux
,找到 PID,然后使用gdb -p PID
附加到正在运行的进程。