GDB를 이용한 원격 디버깅(GDB Remote Debugging)

오늘은 간단하게 gdb를 이용한 remote debugging에 대한 이야기를 할까 합니다.

gdbserver

gdb는 패키지 내 gdbserver를 내장하고 있습니다. 이 서버는 gdb를 원격상태로 두어 사용자가 네트워크를 통해 디버깅할 수 있도록 하는 기능입니다. 리눅스 쉘에서 “gdbserver” 명령을 주게되면 Usage가 나타나며, 옵션을 통해 원격 서버로 사용 가능합니다.

Usage: gdbserver [OPTIONS] COMM PROG [ARGS ...]
gdbserver [OPTIONS] --attach COMM PID
gdbserver [OPTIONS] --multi COMM

COMM may either be a tty device (for serial debugging), or
HOST:PORT to listen for a TCP connection.

Options:
  --debug               Enable general debugging output.
  --debug-format=opt1[,opt2,...]
                        Specify extra content in debugging output.
                          Options:
                            all
                            none
                            timestamp
  --remote-debug        Enable remote protocol debugging output.
  --version             Display version information and exit.
  --wrapper WRAPPER --  Run WRAPPER to start new programs.
  --once                Exit after the first connection has closed.

간단하게 인자값에 대해 설명하면 host ip, 포트, 디버깅할 파일 순으로 입력합니다.

gdbserver 127.0.0.1(host ip/디버깅할 host):8844(port) db_exploit(execution file)

명령으로 주면

gdbserver 127.0.0.1:8844 db_exploit

이렇게 들어가지요. gdbserver는 localhost(127.0.0.1)에 8844 포트를 open 하고 대기 후 접근 시 db_exploit이라는 파일에 디버깅을 걸어 원격지에 있는 사용자가 디버깅할 수 있도록 도와줍니다.

gdb client 를 이용하여 gdbserver 접근하기

일단 별도의 파일을 열지 않고 gdb를 실행하여 디버거로 들어갑니다.

gdb -q
(gdb) 

여기서 원격 접속을 위해 사용할 target 명령에 대해 잠깐 help 를 보고가면..

(gdb) help target
Connect to a target machine or process.
The first argument is the type or protocol of the target machine.
Remaining arguments are interpreted by the target protocol.  For more
information on the arguments for a particular protocol, type
`help target ' followed by the protocol name.

List of target subcommands:

target core -- Use a core file as a target
target ctf -- Use a CTF directory as a target
target exec -- Use an executable file as a target
target extended-remote -- Use a remote computer via a serial line
target native -- Native process (started by the "run" command)
target record -- Log program while executing and replay execution from log
target record-btrace -- Collect control-flow trace and provide the execution history
target record-core -- Log program while executing and replay execution from log
target record-full -- Log program while executing and replay execution from log
target remote -- Use a remote computer via a serial line
target tfile -- Use a trace file as a target

Type "help target" followed by target subcommand name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.

이렇게 나오는데요, 여기서 “target remote – Use a remote computer via a serial line” 이 부분을 이용하여 원격 디버깅을 진행하도록 하겠습니다.

target remote 127.0.0.1(아까host에서 설정한 ip):8844(설정한Port) db_exploit(디버깅할 파일)

이 인자값으로 명령을 주면 gdbserver로 접속이 가능합니다.

(gdb) target remote 127.0.0.1:8844 db_exploit
Remote debugging using 127.0.0.1:8844 db_exploit
warning: Could not load vsyscall page because no executable was specified
try using the "file" command first.
0x00007ffff7dd9cd0 in ?? ()
(gdb) set disassembly intel

원격지 사용자가 gdb를 통해 접속하면 서버에서는 아래와 같이 pid를 생성하고 원격지에 디버깅이 시작되지요.

gdbserver 127.0.0.1:8844 db_exploit 
Process db_exploit created; pid = 9260
Listening on port 8844
Remote debugging from host 127.0.0.1