This repository provides a self-contained, educational environment to understand and exploit CVE-2021-3156, a heap-based buffer overflow in sudo that allows any unprivileged user to gain root privileges on vulnerable systems (sudo versions prior to 1.9.5p2).
This material is for authorized security training and research only.
Unauthorized/Unethical use against systems you do not own is illegal.
Dockerfile- Builds an Ubuntu 20.04 container with sudo 1.8.31 (vulnerable)exploit.c- Main exploit payload (adapted for Ubuntu 20.04)shellcode.c- Shared library constructor that spawns a root shellMakefile- Compiles the exploit and the malicious library
- Docker (or Docker Engine)
- Basic familiarity with C language
- Basic familarity with Make
- Knowledge of command‑line tools
- Clone this repository:
git clone https://github.com/Theleopard65/CVE-2021-3156-Baron-Samedit.git
cd CVE-2021-3156-Baron-Samedit- Build the Docker image:
docker build -t cve-2021-3156-lab .- Run the container:
- You will be logged in as user
pocinside the container. - The working directory is
/home/exploit, which already contains source files.
- You will be logged in as user
docker run -it --rm --name baron_samedit_lab cve-2021-3156-lab- Inside the container, the exploit is already compiled by the Docker build process. To run it manually:
cd /home/exploit
./exploitIf successful, you should see a root shell prompt (#). The exploit overwrites the service_user struct in sudo's heap and forces it to load the malicious library libnss_x/x.so.2, whose constructor executes the shellcode.
- To recompile:
make clean && makeThis creates:
exploit- the main exploit binarylibnss_x/x.so.2- the malicious shared library
CVE-2021-3156 is a heap‑based overflow in sudoedit's command‑line argument handling. By supplying a specially crafted buffer and environment variables, an attacker can overwrite a service_user struct on the heap. This struct contains a function pointer that, when hijacked, can be made to point to a malicious library loaded via the NSS (Name Service Switch) mechanism. The library's constructor then executes arbitrary code with root privileges.
This exploit:
- Allocates a large buffer (
buf) to trigger the overflow. - Uses
LC_MESSAGES,LC_TELEPHONE, andLC_MEASUREMENTenvironment variables to shape the heap layout. - Overflows into
filesservice_user struct, replacing its name with a path to our malicious lib (libnss_x/x.so.2). - The malicious library's constructor (in
shellcode.c) runssetuid(0),setgid(0), and spawns a root shell.
To remove the Docker image:
docker rmi cve-2021-3156-labThis project is provided for educational purposes only. Use at your own risk.