Pwntools Tutorial - Logging

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

Pwntools

목록 보기
8/8
post-thumbnail

Logging


  • Functions
  • Command Line
  • Context
  • Tubes
  • Scoped


Functions

로깅 함수는 “from pwn import *” 로 import 했을 때 노출된다. 이는 다음과 같은 루틴들을 노출한다.

  • error

  • warn

  • info

  • debug

    예시 :

>>> warn('Warning!')
[!] Warning!
>>> info('Info!')
[*] Info!
>>> debug('Debug!')

기본 로그 레벨이 info 이기에 마지막 줄은 기본적으로 표시되지 않는다. ( debug()를 말하는 것 같음 )

print 대신 이러한 함수를 사용함으로써 더욱 정확한 양의 디버깅 정보를 볼 수 있다.


Command Line

가장 많은 로그 정보를 출력하는 가장 쉬운 방법은 script를 DEBUG 라는 magic argument를 넘겨주는 것이다.

$ python exploit.py DEBUG

이것은 정확한 전송/수신 바이트의 크기, 그리고 pwntools 내부에서 일어나는 일들을 확인하는데 유용하다.


Context

대상의 아키텍처를 세팅한 것과 동일한 방식으로 또한 context.log_level을 통해서 로깅 레벨을 세팅할 수 있다.

>>> context.log_level = 'debug'

log_console

기본적으로, 모든 로그들은 stdout으로 출력된다. 만약 이를 stderr 와 같은 다른 파일?로 변경하고 싶다면, context.log_console 에서 설정해야 한다.

>>> context.log_console = sys.stderr

log_file

특정한 파일로 로그를 출력하려면, context.log_file 에서 설정해야 한다.

>>> context.log_file = './log.txt'

Tubes

각각의 튜브는 개별적으로 로깅 레벨을 관리한다. 튜브가 생성될 때, 간단히 level=’’ 을 오브젝트에 넘겨줌으로써 설정할 수 있다.

>>> io = process('sh', level='debug')
[x] Starting local process '/usr/bin/sh' argv=[b'sh']
[+] Starting local process '/usr/bin/sh' argv=[b'sh'] : pid 34475
>>> io.sendline('echo hello')
[DEBUG] Sent 0xb bytes:
    b'echo hello\n'
>>> io.recvline()
[DEBUG] Received 0x6 bytes:
    b'hello\n'
b'hello\n'

이것은 모든 튜브에서 (process, remote 등) , 그리고 튜브와 유사한 것들 (gdb.attach, gdb.debug 등) 에서도 또한 작동한다.

>>> asm('nop', log_level='debug')
[DEBUG] cpp -C -nostdinc -undef -P -I/home/user/pwntools/pwnlib/data/includes /dev/stdin
[DEBUG] Assembling
    .section .shellcode,"awx"
    .global _start
    .global __start
    _start:
    __start:
    .intel_syntax noprefix
    nop
[DEBUG] /usr/bin/x86_64-linux-gnu-as -32 -o /tmp/user/pwn-asm-0yy12n6i/step2 /tmp/user/pwn-asm-0yy12n6i/step1
[DEBUG] /usr/bin/x86_64-linux-gnu-objcopy -j .shellcode -Obinary /tmp/user/pwn-asm-0yy12n6i/step3 /tmp/user/pwn-asm-0yy12n6i/step4
b'\x90'

Scoped

exploit script의 모든 로그들을 활성화하려면, 수동적으로 context.log_level을 사용하거나 scoped helper를 사용하면 된다.

io = process(...)
with context.local(log_level='debug'):
	# Things inside the 'with' block are logged verbosely
	io.recvall()

Pwntools - official tutorial
Full Reference

0개의 댓글