pwntools各使用模块简介

pwntools

pwntools 是一款专门用于CTF Exploit的python库,能够很方便的进行本地与远程利用的切换,并且里面包含多个模块,使利用变得简单。
可以在github上直接搜索pwntools 进行安装。

基本模块

asm : 汇编与反汇编,支持x86/x64/arm/mips/powerpc等基本上所有的主流平台
dynelf : 用于远程符号泄漏,需要提供leak方法
elf : 对elf文件进行操作,可以获取elf文件中的PLT条目和GOT条目信息
gdb : 配合gdb进行调试,设置断点之后便能够在运行过程中直接调用GDB断下,类似于设置为即使调试JIT
memleak : 用于内存泄漏
shellcraft : shellcode的生成器

asm 汇编模块

这个模块提供一些基本的汇编与反汇编操作,一般可以用linux的objdump以及IDA Pro能够完成基本的需求。

dynelf 模块

DynELF是leak信息的神器。前提条件是要提供一个输入地址,输出此地址最少1byte数的函数。官网给出的说明是:Given a function which can leak data at an arbitrary address, any symbol in any loaded library can be resolved.

# Assume a process or remote connection
p = process('./pwnme')

# Declare a function that takes a single address, and
# leaks at least one byte at that address.
def leak(address):
    data = p.read(address, 4)
    log.debug("%#x => %s" % (address, (data or '').encode('hex')))
    return data

# For the sake of this example, let's say that we
# have any of these pointers.  One is a pointer into
# the target binary, the other two are pointers into libc
main   = 0xfeedf4ce
libc   = 0xdeadb000
system = 0xdeadbeef

# With our leaker, and a pointer into our target binary,
# we can resolve the address of anything.
#
# We do not actually need to have a copy of the target
# binary for this to work.
d = DynELF(leak, main)
assert d.lookup(None,     'libc') == libc
assert d.lookup('system', 'libc') == system

# However, if we *do* have a copy of the target binary,
# we can speed up some of the steps.
d = DynELF(leak, main, elf=ELF('./pwnme'))
assert d.lookup(None,     'libc') == libc
assert d.lookup('system', 'libc') == system

# Alternately, we can resolve symbols inside another library,
# given a pointer into it.
d = DynELF(leak, libc + 0x1234)
assert d.lookup('system')      == system

 

elf 模块

这是一个静态模块,即静态加载ELF文件,然后通过相关接口获取一些信息,常用接口有:
got 获取指定函数的GOT条目
plt 获取指定函数的PLT条目
address 获取ELF的基址
symbols 获取函数的实际地址(待确定)

gdb 模块

该模块用于调用gdb调试
在python文件中直接设置断点,当运行到该位置之后就会断下

import pwnlib
from pwn import *
p = process('./c')
pwnlib.gdb.attach(p)

rop 模块

用于自动产生ROP链 还不支持X64?

elf = ELF('ropasaurusrex')
rop = ROP(elf)
rop.call('read', (0, elf.bss(0x80)))
rop.dump() ## 展示当前的ROP chain
### 搜索指定指令 rop.search(move=0, regs=None, order='size')
'''
move(int),栈指针调整的字节数
regs(list),搜索的寄存器list
order(str),多个gadgets的排序方式,可选值=['size', 'regs']
'''
rop.r13_r14_r15_rbp == rop.search(regs=['r13','r14','r15','rbp'], order = 'regs')

rop.call, 两个参数,第一个是需要call的函数或者一个地址,第二个是函数参数,为list,只有一个参数需要在后面加上一个’,’使其变为list
也可以使用ROPgadget进行gadget搜索

shellcraft 模块

shellcraft模块是shellcode的模块,包含一些生成shellcode的函数。

其中的子模块声明架构,比如shellcraft.arm 是ARM架构的,shellcraft.amd64是AMD64架构,shellcraft.i386是Intel 80386架构的,以及有一个shellcraft.common是所有架构通用的。

有的时候我们需要在写exp的时候用到简单的shellcode,pwntools提供了对简单的shellcode的支持。 
首先,常用的,也是最简单的shellcode,即调用/bin/sh可以通过shellcraft得到:

注意,由于各个平台,特别是32位和64位的shellcode不一样,所以最好先设置context。

print(shellcraft.sh()) # 打印出shellcode
print(asm(shellcraft.sh())) # 打印出汇编后的shellcod

asm可以对汇编代码进行汇编,不过pwntools目前的asm实现还有一些缺陷,比如不能支持相对跳转等等,只可以进行简单的汇编操作。如果需要更复杂一些的汇编功能,可以使用keystone-engine项目,这里就不再赘述了。

asm也是架构相关,所以一定要先设置context,避免一些意想不到的错误。

 

posted @ 2017-09-12 21:16  S_s_s  阅读(13772)  评论(0编辑  收藏  举报