简述
Pwntools 分为两个模块,一个是pwn,简单地from pwn import *
即可将所有子模块和常用系统库导入;
另一个是pwnlib,可以根据需要导入子模块,而不是像上面这个这样all in…(正常人谁不all in还一个个挑呢hhh)
常用的子模块
pwnlib.tubes
通常用于与目标文件或者目标服务器进行交互
主要函数在pwnlib.tubes.tube
中实现,子模块只负责某个管道特殊的地方。四种管道及其子模块如下:
1 2 3 4 5 6 7 8 9 10 11 12 13
| interactive() recv(numb=1096,timeout=default) recvn(numb,timeout=default) recvall() recvline(keepends=True) recvrepeat(timeout=default) recvuntil(delims,timeout=default) send(data) sendafter(delim,data,timeout=default) sendline(data) sendlineafter close()
|
pwnlib.context
用于设置运行时变量,如目标系统、目标体系结构、端序、日志等
1 2 3 4 5 6 7 8 9 10 11 12 13
| >>> context.clear() >>> context.os = 'linux' >>> context.arch = 'arm' >>> context.bits = 32 >>> context.endian = 'little' >>> vars(context) {'os': 'linux', 'bits': 32, 'arch': 'arm', 'endian': 'little'} >>> context.update(os = 'linux', arch = 'amd64', bits = 64) >>> context.log_level = 'debug' >>> context.log_file = '/tmp/pwnlog.txt' >>> vars(context) {'log_level': 10, 'bits': 64, 'endian': 'little', 'arch': 'amd64', 'log_file': <open file '/tmp/pwnlog.txt', mode 'a' at 0x7f6024e8f9c0>, 'os': 'linux'}
|
pwnlib.elf
用于操作ELF文件,包括符号查找、虚拟内存、文件偏移、以及修改和保存二进制文件等功能。
1 2 3 4 5 6 7 8 9 10 11
| asm(address,assembly) bss(offset) checksec() disable_nx() disasm(address,n_bytes) offset_to_vaddr(offset) vaddr_to_offset(address) read(address,count) write(address,data) section(name) debug()
|
pwnlib.asm
用于汇编和反汇编代码(确保已安装对应体系结构的binutils),体系结构、端序、字长等信息可以作为asm()的参数,但最好通过pwnlib.context来设置。
1 2 3 4 5 6 7 8 9 10 11 12 13
| >>> from pwn import * >>> asm('nop') '\x90' >>> context.clear(arch='amd64') >>> bin_sh=asm(shellcraft.amd64.linux.sh()) >>> filename = make_elf(bin_sh,extract=False) >>> p = process(filename) [x] Starting local process '/tmp/pwn-asm-CxI3u5/step3-elf' [+] Starting local process '/tmp/pwn-asm-CxI3u5/step3-elf': pid 4018 >>> p.sendline('echo hello') >>> p.recv() 'hello\n'
|
pwnlib.shellcraft
使用这个模块可以生成各种体系结构(aarch64、amd64、arm、i386、mips、thumb等)的shellcode代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| >>> print shellcraft.arm.linux.sh() /* execve(path='/bin///sh', argv=['sh'], envp=0) */ /* push '/bin///sh\x00AA' */ movw r7, movt r7, push {r7} movw r7, movt r7, push {r7} movw r7, movt r7, push {r7} mov r0, sp /* push argument array ['sh\x00'] */ /* push 'sh\x00\x00' */ movw r7, push {r7} eor r12, r12 /* 0 ( push {r12} /* null terminate */ mov r1, add r1, sp mov r12, r1 push {r12} /* 'sh\x00' */ mov r1, sp eor r2, r2 /* 0 ( /* call execve() */ mov r7, svc 0
|
pwnlib.util
util其实是模块的集合,包含了一些小工具。这里介绍俩packing和cyclic。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| >>> p32(0xdeadbeef) '\xef\xbe\xad\xde' >>> p64(0xdeadbeef).encode('hex') 'efbeadde00000000' >>> p32(0xdeadbeef,endian='big',sign='unsigned') '\xde\xad\xbe\xef'
>>> u32('1234') 875770417 >>> u32('1234',endian='big',sign='signed') 825373492 >>> u32('\xef\xbe\xad\xde') 3735928559
>>> cyclic(20) 'aaaabaaacaaadaaaeaaa' >>> cyclic(30) 'aaaabaaacaaadaaaeaaafaaagaaaha'
>>> cyclic_find(0x61616162) 4 >>> cyclic_find(0x61616261) 3 >>> cyclic_find(0x61616661) 19
|
未完待续..
参考资料
《CTF竞赛权威指南(PWN篇)》
Comments