Skip to content
KitploitKITPLOIT
ToolsBlog
Submit
ToolsBlog
Submit

Hacking, PenTest, and Cybersecurity Tools for Your Security Arsenal!

Kitploit is a directory of hacking, cybersecurity, and pentesting tools. Discover the latest project updates to find vulnerabilities, analyze systems, automate testing, and strengthen your security.

··Feeds·Contact·Privacy·© 2026 Kitploit

Tool Directory

Categories

View all categories
Loading categories
CVE-2021-3156-Baron-Samedit — A simple Docker lab and Exploit setup for CVE-2021-3156 - "Baron Samedit". | Kitploit
Tools/GitHubGitHub/theleopard65/cve-2021-3156-baron-samedit
Privilege EscalationContainer SecurityVulnerability AnalysisExploitationShellcodeLearning & EducationBinary ExploitationLabs & Practice

Most Popular

View all →

Discover the most used tools by our community.

Explore all tools

Browse our collection of tools

View all tools →
Share
GitHub
theleopard65/cve-2021-3156-baron-samedit

CVE-2021-3156-Baron-Samedit

A simple Docker lab and Exploit setup for CVE-2021-3156 - "Baron Samedit".

View Repository
136 months agoNot yet reviewed

CVE-2021-3156 - "Baron Samedit" Lab & Exploit

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.

Repository Contents

  • 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 shell
  • Makefile - Compiles the exploit and the malicious library

Getting Started

Prerequisites

  • Docker (or Docker Engine)
  • Basic familiarity with C language
  • Basic familarity with Make
  • Knowledge of command‑line tools

Building and Running the Lab

  1. Clone this repository:
root@kitploit:~
git clone https://github.com/Theleopard65/CVE-2021-3156-Baron-Samedit.git
cd CVE-2021-3156-Baron-Samedit
  1. Build the Docker image:
root@kitploit:~
docker build -t cve-2021-3156-lab .
  1. Run the container:
    • You will be logged in as user poc inside the container.
    • The working directory is /home/exploit, which already contains source files.
root@kitploit:~
docker run -it --rm --name baron_samedit_lab cve-2021-3156-lab

Compiling and Running the Exploit

  • Inside the container, the exploit is already compiled by the Docker build process. To run it manually:
root@kitploit:~
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.

Manual Compilation (if needed)

  • To recompile:
root@kitploit:~
make clean && make

This creates:

  • exploit - the main exploit binary
  • libnss_x/x.so.2 - the malicious shared library

How the Exploit Works

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:

  1. Allocates a large buffer (buf) to trigger the overflow.
  2. Uses LC_MESSAGES, LC_TELEPHONE, and LC_MEASUREMENT environment variables to shape the heap layout.
  3. Overflows into files service_user struct, replacing its name with a path to our malicious lib (libnss_x/x.so.2).
  4. The malicious library's constructor (in shellcode.c) runs setuid(0), setgid(0), and spawns a root shell.

Cleaning Up

To remove the Docker image:

root@kitploit:~
docker rmi cve-2021-3156-lab

References

  • Original advisory by Qualys
  • Blasty’s Proof of Concept
  • NIST NVD Entry for the CVE

License

This project is provided for educational purposes only. Use at your own risk.


Download Tool