
- Packing and Unpacking Integers
- File I/O
- Hashing and Encoding
- Base64
- Hashes
- URL Encoding
- Hex Encoding
- Bit Manipluation and Hex Dumping
- Hex Dumping
- Pattern Generation
pack, unpack을 통해 패킹과 언패킹을 수행할 수 있다. 여기서, pack()과 unpack()은 context의 설정에 기반해 동작한다. (endian, bits, sign 등)
pack(1)
# '\x01\x00\x00\x00'
pack(-1)
# '\xff\xff\xff\xff'
pack(2**32 - 1)
# '\xff\xff\xff\xff'
pack(1, endian='big')
# '\x00\x00\x00\x01'
p16(1)
# '\x01\x00'
hex(unpack('AAAA'))
# '0x41414141'
hex(u16('AA'))
# '0x4141'
간단히 한개의 함수를 호출하는 것만으로 파일 입출력을 사용할 수 있다.
from pwn import *
write('filename', 'data')
read('filename')
# 'data'
read('filename', 1)
# 'd'
Base64 En/Decoding 을 위해서는 b64e, b64d를 사용한다. (함수의 반환 형식은 str임)
from pwn import *
'hello' == b64d(b64e('hello'))
md5sumhex, sha1sumhex 등의 해쉬 함수를 사용할 수 있다. ( 자세한 내용은 레퍼런스 참조 )
from pwn import *
md5sumhex('hello') == '5d41402abc4b2a76b9719d911017c592'
write('file', 'hello')
md5filehex('file') == '5d41402abc4b2a76b9719d911017c592'
sha1sumhex('hello') == 'aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d'
urlencode, urldecode를 통해 URL En/decoding 이 가능하다.
from pwn import *
urlencode("Hello, World!") == '%48%65%6c%6c%6f%2c%20%57%6f%72%6c%64%21'
urldecode('%48%65%6c%6c%6f%2c%20%57%6f%72%6c%64%21') == "Hello, World!"
enhex, unhex를 통해 ascii 문자열 - 16진수 간 변환이 가능하다.
from pwn import *
enhex('hello')
# '68656c6c6f'
unhex('776f726c64')
# 'world'
bits, unbits를 통해 값 - 이진수 (각 자리에 따른 배열로 구성) 간 변환을 할 수 있다.
from pwn import *
bits(0b1000001) == bits('A')
# [0, 0, 0, 1, 0, 1, 0, 1]
unbits([0,1,0,1,0,1,0,1])
# 'U'Pattern Generation
패턴 생성은 수학 계산 없이 오프셋을 찾을 수 있는 매우 쉬운 방법이다.
간단한 BOF 가 있다고 가정해보자. 그리고 우리는 패턴을 생성하고, 대상 에플리케이션에 전달할 것이다.
io = process(...)
io.send(cyclic(512))
Core dump 에서, 우리는 크래시가 0x61616178에서 발생한 것을 볼 수 있을 것이다.
단순히 그 숫자를 패턴과 비교함으로써, 우리는 crash frame의 분석 없이 오프셋을 얻을 수 있다.
cyclic_find(0x61616178)
# 92