
A simple Docker lab and Exploit setup for CVE-2021-3156 - "Baron Samedit".
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 librarygit clone https://github.com/Theleopard65/CVE-2021-3156-Baron-Samedit.git
cd CVE-2021-3156-Baron-Samedit
docker build -t cve-2021-3156-lab .
poc inside the container./home/exploit, which already contains source files.docker run -it --rm --name baron_samedit_lab cve-2021-3156-lab
cd /home/exploit
./exploit
If 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.
make clean && make
This creates:
exploit - the main exploit binarylibnss_x/x.so.2 - the malicious shared libraryCVE-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:
buf) to trigger the overflow.LC_MESSAGES, LC_TELEPHONE, and LC_MEASUREMENT environment variables to shape the heap layout.files service_user struct, replacing its name with a path to our malicious lib (libnss_x/x.so.2).shellcode.c) runs setuid(0), setgid(0), and spawns a root shell.To remove the Docker image:
docker rmi cve-2021-3156-lab
This project is provided for educational purposes only. Use at your own risk.