Skip to content

Instantly share code, notes, and snippets.

@icchy
Last active March 30, 2023 15:04
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save icchy/1b702fc56ec37844f711 to your computer and use it in GitHub Desktop.
Save icchy/1b702fc56ec37844f711 to your computer and use it in GitHub Desktop.
pwntools使い方 まとめ

pwntoolsの便利そうな機能まとめ

公式リファレンス: pwntools

install

最新版を降らせる
pip install "git+https://github.com/Gallopsled/pwntools#egg=pwntools"

template

from pwn import *

context(os='linux', arch='i386')
context.log_level = 'debug' # output verbose log

HOST = "target"
POST = 1337
conn = None

if len(sys.argv) > 1 and sys.argv[2] == 'r':
    conn = remote(HOST, PORT)
else:
    conn = process('./pwnme')

# preparing for exploitation

log.info('Pwning')

tube

# remote
conn = remote(HOST, PORT)
# local
conn = process('./pwnme')
  • conn.send(str)
  • conn.sendline(str)
  • conn.recvuntil(delim)
  • conn.recvregrex(re_expr)
  • conn.read(bytes)
  • conn.interact()
  • など

constants

context(os='linux', arch='i386')

int(constants.SYS_exit) # 1
str(constatns.SYS_exit) # "SYS_exit"

大体の定数が登録されている

  • SYS_exit
  • STDIN_FILENO
  • O_RDONLY
  • AF_INET
  • SIGKILL
  • など

pack, unpack

p32(0xdeadbeef) # '\xef\xbe\xad\xde'
p64(0xdeadbeef) # '\xef\xbe\xad\xde\x00\x00\x00\x00'
hex(u32('\xef\xbe\xad\xde')) # '0xdeadbeef'
hex(u64('/bin/sh\x00')) # '0x68732f6e69622f'

context(arch='i386')
pack(0xdeadbeef) # "\xef\xbe\xad\xde"

context(arch='amd64')
pack(0xdeadbeef) # '\xef\xbe\xad\xde\x00\x00\x00\x00'
  • pack, unpack - context.archを元に決定される
  • p32, p64, u32, u64, etc... - 手動

shellcraft

シェルコード生成ツール asmと組み合わせて使う

  • shellcraft.sh()

しか使ったことない

asm, disasm

disasmあまり使わない(問題に依る)

leak = 0xdeadbeef
shellcode = asm("""
mov eax, SYS_write
mov ebx, STDOUT_FILENO
mov ecx, {leak}
mov edx, 0x100
int 0x80
""".format(**locals()))

# 部分的にアーキテクチャを変える
shellcode = asm("""
mov rax, SYS_write
mov rdi, STDOUT_FILENO
mov rsi, {leak}
mov rdx, 0x100
syscall
""".format(**locals()), arch='amd64')

ELF

ELFからシンボル情報などを取得する

elf = ELF('./libc.so.6')

libc_system = elf.symbols['system']
libc_binsh = next(elf.search("/bin/sh"))

# get section address
addr_bss = elf.bss()
addr_dynsym = elf.get_section_by_name('.dynsym').header['sh_addr']

ROP

rop chainを作るのに便利そうだったけど使いにくかった

gdb

EntryPoint = elf.symbols['main'] or elf.entrypoint
conn = gdb.debug(['./pwnme'], execute="b *{0}\nc".format(hex(EntryPoint)))

gdbserverと連携してバイナリを起動,デバッグできる
util.misc.run_in_new_terminalによってgdbが起動される(tmuxに対応)
戻り値はremote, processと同じtubeクラス

Other

  • xor - stringとstring同士のxorが出来便利
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment