Pwntools Tutorial - Tubes

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

Pwntools

목록 보기
1/8
post-thumbnail

Tubes


  • Basic I/O
    • Receiving Data
    • Sending data
    • Manipulating Integers
  • Processes and Basic Features
  • Interactive Sessions
  • Networking
  • Secure Shell
  • Serial Ports


Basic I/O

데이터 받기 / Receiving Data

  • recv(n) - n byte만큼 데이터 받음
  • recvline() - newline 전까지 데이터 받음
  • recvuntil(delim) - delimiter가(”: “ 등) 발견될 때까지 데이터 받음
  • recvregex(pattern) - 정규식 (pattern) 이 만족될 때까지 데이터 받음
  • recvrepeat(timeout) - timeout이 될 때까지 데이터 받음 (시간 기반)
  • clean() - 버퍼 초기화

데이터 보내기 / Sending Data

  • send(data) - 데이터 보냄
  • sendline(data) - 데이터 + newline 을 보냄

Integer 조작 / Manipulating Integers

  • pack(int) - word-size 의 packed integer를 보냄
  • unpack() - 데이터를 받고 word-size integer로 unpack함

Processes and Basic Features

Processes

프로세스 오브젝트를 생성하기 위해서는 process를 사용한다.

from pwn import *

io = process('sh')
io.sendline('echo Hello, world')
io.recvline()
# 'Hello, world\n'
from pwn import *

io = process(['sh', '-c', 'echo $MYENV'], env={'MYENV': 'MYVAL'}) #command-line 인자, 환경 세팅 등
# 세부 내용은 full reference 참조

io.recvline()
# 'MYVAL\n'
from pwn import *

io = process(['sh', '-c', 'echo A; sleep 1; echo B; sleep 1; echo C; sleep 1; echo DDD'])

io.recv()
# 'A\n'

io.recvn(4)
# 'B\nC\n'

hex(io.unpack())
# 0xa444444

Interactive Sessions

프로세스 오브젝트 등에서 쉘을 얻기 위해서는 object.interactive를 사용한다.

from pwn import *

# Let's pretend we're uber 1337 and landed a shell.
io = process('sh')

# <exploit goes here>

io.interactive()

Networking

원격/네트워크 연결을 맺기 위해서는 remote, listen을 사용한다.

  • remote() : 연결을 요청한 뒤 object를 반환한다.
  • listen() : 연결을 기다린 뒤 object를 반환한다.

example of remote :

from pwn import *

io = remote('google.com', 80)
io.send('GET /\r\n\r\n')
io.recvline()
# 'HTTP/1.0 200 OK\r\n'
from pwn import *

dns  = remote('8.8.8.8', 53, typ='udp')
tcp6 = remote('google.com', 80, fam='ipv6')

example of listen :

from pwn import *

client = listen(8080).wait_for_connection()

Secure Shell

SSH 연결을 위해서는 ssh를 사용한다.

from pwn import *

session = ssh('bandit0', 'bandit.labs.overthewire.org', 2220, password='bandit0')

io = session.process('/bin/sh', env={"PS1":""})
io.sendline('echo Hello, world!')
io.recvline()
# 'Hello, world!\n'

이 외 더 많은 것들은 SSH tutorial에서 다룸 ( 포트포워딩, 파일 업로드 등 )

Serial Ports

시리얼 포트 또한 serialtube를 통해 사용할 수 있다.

from pwn import *

io = serialtube('/dev/ttyUSB0', baudrate=115200)

Pwntools - official tutorial
Full Reference

0개의 댓글