CVE-2021-3156 - Exploit修改

倾旋
倾旋
技术分享|2021-2-9|最后更新: 2023-6-22|
type
status
date
slug
summary
tags
category
icon
password
URL

0x01 为什么要修改?

本人不擅长二进制,但是看了一下网上公开的Exploit,都需要输入一次密码才能够利用这个漏洞,还是不满足于一些实战场景,如果获得不到交互式Shell,那么用原有的Exploit就不能利用了。

0x02 Linux 管道符

在Linux中有多种办法可以在Shell中使用管道符,跳过交互输入,如修改一个用户的密码:
该命令只适用于旧版,不建议在命令行中传递明文密码
于是我查看了sudoedit的帮助参数:
notion image
设置-S参数,可以直接通过管道符传递密码,那么也就是说,给Exploit增加这么一个参数就能在提权的时候不需要输入密码了,从而跳过交互,但前提还是需要用C语言模拟这个管道传递字符。

0x03 Exploit分析

本文修改的提权Exploit 溢出点主要是在环境变量中,通过调用execve触发。
notion image
在21行传入了argv,可以将这个数组添加一个元素,也就是等同于添加一个命令行参数:
紧接着,需要思考如何传入密码了。
经过测试,即使密码错误的情况下,也能够提权成功。
经过查阅资料,关于execve的特点如下:
execve创建的进程将会重新初始化堆栈、堆和(初始化和未初始化的)数据段。
All process attributes are preserved during an execve(), except the following:
  • The dispositions of any signals that are being caught are reset to the default (signal(7)).
  • Any alternate signal stack is not preserved (sigaltstack(2)).
  • Memory mappings are not preserved (mmap(2)).
  • Attached System V shared memory segments are detached (shmat(2)).
  • POSIX shared memory regions are unmapped (shm_open(3)).
  • Open POSIX message queue descriptors are closed(mq_overview(7)).
  • Any open POSIX named semaphores are closed (sem_overview(7)).
  • POSIX timers are not preserved (timer_create(2)).
  • Any open directory streams are closed (opendir(3)).
  • Memory locks are not preserved (mlock(2), mlockall(2)).
  • Exit handlers are not preserved (atexit(3), on_exit(3)).
  • The floating-point environment is reset to the default (see fenv(3)).
  • ….
这就意味着,在调用execve之前向stdin写入数据,创建后的进程也是读不到的。
于是我写了一个小例子来测验我的解决办法:
notion image
测试结果正确:2!
stdin的值为:test\r\n1111,wc -l 命令读取是以\n为每一项分割的,因此返回的是2。
这个方案可以直接拿到Exploit中,将stdin覆盖,向管道写入密码\n命令,然后把命令传入即可。
为什么这里是密码\n命令呢?
是因为第一个\n是为了结束密码的传递,然后开始堆溢出,溢出完成后,后续的命令刚好会传入到Shellcode启动的/bin/sh中。
notion image
修改后的Exploit:

0x03 演示效果

notion image
©2021-2024 倾旋. All rights reserved.