Pwntools Tutorial - Debugging

모씨김(mossikim)·2025년 8월 13일

Pwntools

목록 보기
6/8
post-thumbnail

Debugging


  • Prerequisites
  • Launching a Process Under GDB
  • Attaching to a Running Process
    • Local Processes
    • Forking Servers
  • Debugging Foreign Architectures
  • Troubleshooting (서술 안함, 오피셜 튜토리얼에 나와있음)
    • Behind the Scenes
    • Specifying a Teminal Window
    • Environment Variables
    • Unable to Attach to Processes
    • argv0 and argc==0


Prerequisites

우선 gdb와 gdbserver가 설치되어있어야 한다. which gdb, which gdbserver를 통해서 확인할 수 있다.

설치 명령어 :
$ sudo apt-get install gdb gdbserver

Launching a Process Under GDB

GDB에서 프로세스를 시작하기 위해서는, gdb.debug를 사용하면 된다.

>>> io = gdb.debug("/bin/bash", gdbscript='continue')
>>> io.sendline('echo hello')
>>> io.recvline()
# b'hello\n'
>>> io.interactive()

Attaching to a Running Process

가끔 디버거에서 대상 프로그램을 실행시키지 않고, 프로그램을 특정 exploit 상태에서 attach 하고 싶을 수 있다.

Local Processes

타겟을 실행가능한 상태로 작용하기 위해서 process() 튜브를 열어야 한다. 단순히 gdb.attach() 를 사용함으로써 gdb debugger에 attach 할 수 있다.

>>> io = process('/bin/sh')
>>> gdb.attach(io, gdbscript='continue')

Forking Servers

가끔 바이너리에는 포크 서버가 있을 때가 있다. 그리고 그 연결된 프로세스를 디버깅해야 할 때가 있다.

서버가 현재 환경에서 작동한다면, 또한 쉽게 진행할 수 있다.

server 예시 :

>>> socat = process(['socat', 'TCP-LISTEN:4141,reuseaddr,fork', 'EXEC:/bin/bash -i'])

local 예시 :

>>> io = remote('localhost', 4141)
[x] Opening connection to localhost on port 4141
[x] Opening connection to localhost on port 4141: Trying 127.0.0.1
[+] Opening connection to localhost on port 4141: Done
>>> io.sendline('echo hello')
>>> io.recvline()
b'hello\n'
>>> io.lport, io.rport

이제 gdb attach하면 된다. (예시) :

>>> gdb.attach(io)

Debugging Foreign Architecture

다른 인텔 기반의 아키텍처에서 (arm이나 powerpc 같은) 디버깅하는 것은 어렵지 않다.

>>> context.arch = 'arm'
>>> elf = ELF.from_assembly(shellcraft.echo("Hello, world!\n") + shellcraft.exit())
>>> process(elf.path).recvall()
b'Hello, world!\n'

process() 대신에 gdb.debug()를 쓰면 된다.

>>> gdb.debug(elf.path).recvall()
b'Hello, world!\n'

Debugging Foreign Architectures - Tips and Limitations,
Trouble Shooting은 공식 tutorial, reference에 나와있다. (서술하기 귀찮음)


Pwntools - official tutorial
Full Reference

0개의 댓글