Pwn with File结构体(二)
前言
本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274
最新版的 libc 中会对 vtable 检查,所以之前的攻击方式,告一段落。下面介绍一种,通过修改 _IO_FILE 实现任意地址读和任意地址写的方式。
正文
_IO_FILE 通过这些指针,来读写数据。

如果我们修改了它们,然后通过一些文件读写函数时,我们就能实现 任意地址读写。
任意地址读

代码示例
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
FILE *fp;
char *msg = "hello_file";
char *buf = malloc(100);
read(0, buf, 100);
fp = fopen("key.txt", "rw");
// 设置 flag 绕过 check
fp->_flags &= ~8;
fp->_flags |= 0x800;
// _IO_write_base write数据的起始地址, _IO_write_ptr write数据的终止地址
fp->_IO_write_base = msg;
fp->_IO_write_ptr = msg + 6;
//绕过检查
fp->_IO_read_end = fp->_IO_write_base;
// write 的目的 文件描述符, 1 --> 标准输出
fp->_fileno = 1;
fwrite(buf, 1, 100, fp);
return 0;
}

任意地址写

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
FILE *fp;
char msg[100];
char *buf = malloc(100);
fp = fopen("key.txt", "rw");
// 设置 flag 绕过 check
fp->_flags &= ~4;
// _IO_buf_base buffer 的起始地址, _IO_buf_end buffer 的终止地址
// fread 先把数据读入 [_IO_buf_base, _IO_buf_end] 形成的 buffer
// 然后复制到目的 buffer
fp->_IO_buf_base = msg;
fp->_IO_buf_end = msg + 100;
// 设置 文件描述符, 0---> stdin, 从标准输入读数据
fp->_fileno = 0;
fread(buf, 1, 6, fp);
puts(msg);
puts(buf);
return 0;
}

利用 stdin / stdout 任意地址写/ 读
puts, scanf 等一批系统函数默认使用的 stdin , stdout ,stderr 等结构体进行操作,通过修改这些结构体的内容,可以更方便的实现任意地址读,任意地址写。
stdin 也是 _IO_FILE 结构体
#include <stdio.h>
#include <stdlib.h>
int global_val = 0xaabbccdd;
int main(int argc, char * argv[])
{
FILE *fp;
int var;
fp = stdin;
fp->_flags &= ~4;
fp->_IO_buf_base = stdout;
fp->_IO_buf_end = stdout + 100;
scanf("%d",&var);
printf("0x%x\n", global_val);
return 0;
}
运行之

成功修改 stdout 结构体
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
FILE *fp;
char *msg = "hello_stdout";
char *buf = malloc(100);
fp = stdout;
// 设置 flag 绕过 check
fp->_flags &= ~8;
fp->_flags |= 0x800;
// _IO_write_base write数据的起始地址, _IO_write_ptr write数据的终止地址
fp->_IO_write_base = msg;
fp->_IO_write_ptr = msg + 12;
//绕过检查
fp->_IO_read_end = fp->_IO_write_base;
// write 的目的 文件描述符, 1 --> 标准输出
fp->_fileno = 1;
puts("<----->this is append on msg ");
return 0;
}

成功读到了, msg 的内容。
参考:
https://www.slideshare.net/AngelBoy1/play-with-file-structure-yet-another-binary-exploit-technique
Pwn with File结构体(二)的更多相关文章
- Pwn with File结构体(一)
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 利用 FILE 结构体进行攻击,在现在的 ctf 比赛中也经常出现 ...
- Pwn with File结构体(四)
前言 前面几篇文章说道,glibc 2.24 对 vtable 做了检测,导致我们不能通过伪造 vtable 来执行代码.今天逛 twitter 时看到了一篇通过绕过 对vtable 的检测 来执行代 ...
- Pwn with File结构体(三)
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 前面介绍了几种 File 结构体的攻击方式,其中包括修改 vtab ...
- Pwn with File结构体之利用 vtable 进行 ROP
前言 本文以 0x00 CTF 2017 的 babyheap 为例介绍下通过修改 vtable 进行 rop 的操作 (:-_- 漏洞分析 首先查看一下程序开启的安全措施 18:07 haclh@u ...
- Linux_Struct file()结构体
struct file结构体定义在/linux/include/linux/fs.h(Linux 2.6.11内核)中,其原型是:struct file { /* * f ...
- Linux--struct file结构体
struct file(file结构体): struct file结构体定义在include/linux/fs.h中定义.文件结构体代表一个打开的文件,系统中的每个打开的文件在内核空间都有一个关联的 ...
- 2018.5.2 file结构体
f_flags,File Status Flag f_pos,表示当前读写位置 f_count,表示引用计数(Reference Count): dup.fork等系统调用会导致多个文件描述符指向同一 ...
- fd与FILE结构体
文件描述符 fd 概念:文件描述符在形式上是一个非负整数.实际上,它是一个索引值,指向内核为每一个进程所维护的该进程打开文件的记录表.当程序打开一个现有文件或者创建一个新文件时,内核向进程返回一个文件 ...
- file结构体中private_data指针的疑惑
转:http://www.360doc.com/content/12/0506/19/1299815_209093142.shtml hi all and barry, 最近在学习字符设备驱动,不太明 ...
随机推荐
- POJ 1018
#include<iostream> #define MAXN 105 #define inf 10000000 #include<vector> #include<io ...
- ConcurrentHashMap的使用注意事项
有人说:虽然ConcurrentHashMap是线程安全的,但是在如下的代码中: ConcurrentHashMap<String,String> map; String getStrin ...
- js04
接着看一些js的基础,这里主要说一下js的对象. 1.对象: js中的所有事物都可以看作是对象:字符串.数值.数组.函数... 内建对象:String Date Array ...
- ruby:TypeError: 对象不支持此属性或方法(<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolink)
我整理了一下 目前有三种方法,我是windows 7 64位, 答案中很多人是win8也成功了. 方法1:找到当前项目下 app/javascripts/applications.js 把里面的// ...
- Chapter 3 Phenomenon——16
"Your X-rays look good," he said. "Does your head hurt? Edward said you hit it pretty ...
- Chapter 3 Phenomenon——9
"You were over there," I suddenly remembered, and his chuckle stopped short. “你之前不在这里”我突然记 ...
- BFS和DFS详解以及java实现
前言 图在算法世界中的重要地位是不言而喻的,曾经看到一篇Google的工程师写的一篇<Get that job at Google!>文章中说到面试官问的问题中几乎有一半的问题都可以用图的 ...
- SC Create 创建一个Windows系统服务 转
转自:http://www.360doc.com/content/13/0428/09/7555793_281451268.shtml sc create Serv-U binpath= &q ...
- JavaScript里的循环方法总结
JavaScript诞生已经有20多年了,我们一直使用的用来循环一个数组的方法是这样的: for (var index = 0; index < myArray.length; index++) ...
- Python---战机小游戏,学习pygame
import pygame # 导入游戏包 pygame.init() # 导入并初始化所有pygame模块,使用其他模块之前必须先调用init()方法 print('下面是游戏代码:') # 绘制矩 ...