diving in to spooler: discovering lpe and rce

46
Diving in to spooler: Discovering LPE and RCE Vulnerabilities in Windows Printer. Dr. Zhiniang Peng of Sangfor Force XueFeng Li of Sangfor Force Research Team Lewis Lee of Sangfor Force Research Team

Upload: others

Post on 29-Apr-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Diving in to spooler: Discovering LPE and RCE

Diving in to spooler: Discovering LPE and RCE Vulnerabilities in Windows Printer.

Dr. Zhiniang Peng of Sangfor ForceXueFeng Li of Sangfor Force Research TeamLewis Lee of Sangfor Force Research Team

Page 2: Diving in to spooler: Discovering LPE and RCE

C:\> whoarewe

- Zhiniang Peng

Dr. Zhiniang Peng (@edwardzpeng) is the Principal Security Researcher at Sangfor Force. His current research areas include applied cryptography, software security and threat hunting. He has more than 10 years of experience in both offensive and defensive security and published many research in both academia and industry.

- Xuefeng Li

Xuefeng Li (@lxf02942370) is an intern at intern at Force Research Team and a student at South China University of Technology. He has been engaged in Windows vulnerability hunting and exploitation for almost one year and ranked #10 on the MSRC Most Valuable Security Researcher list in 2020.

- Lewis Lee

Lewis Lee (@LewisLee53) is an intern at Force Research Team and a student at South China University of Technology.

Page 3: Diving in to spooler: Discovering LPE and RCE

1. Introduction to Printer Spooler

2. Vulnerability Analysis : Medium2System

2.2 Mitigation Bypass - CVE-2020-1337

2.3 Mitigation Bypass - CVE-2020-17014

3. Vulnerability Analysis : Remote Code Execution

3.1 Memory Corruption - CVE-2021-24088, CVE-2021-24077,CVE-2021-1722

Agenda

2.1 First meet with spooler - CVE-2020-1048

2.5 EvilCopyFileEvent - Configuring Printer is Dangerous

2.4 Mitigation Bypass - CVE-2020-17001

4. Conclusion3.2 PrintNightmare

Page 4: Diving in to spooler: Discovering LPE and RCE

01

Introduction to Printer Spooler

Page 5: Diving in to spooler: Discovering LPE and RCE

Introduction to Printer Spooler

Add, remove and configure printer

Spool high-level function calls into printer jobs

Receive and schedule printer jobs for printing

Application Spoolsv.exeRPC

Localspl.dllMain Component

Page 6: Diving in to spooler: Discovering LPE and RCE

Related Research

Evil Printer: How To Hack Windows Machines With Printing Protocol - Zhipeng Huoand Chuanda Ding

A Decade After Stuxnet's Printer Vulnerability: Printing is Still the Stairway to Heaven - PELEG HADAR and TOMER BAR

Page 7: Diving in to spooler: Discovering LPE and RCE

02

Vulnerability Analysis : Medium2System

Page 8: Diving in to spooler: Discovering LPE and RCE

First meet with spooler - CVE-2020-1048

Quick review of PrintDemon(CVE-2020-1048 - found by Yarden Shafir & Alex Ionescu)

Add-PrinterDriver -Name "Generic / Text Only"Step1: Add a Printer Driver

Add-PrinterPort –Name "C:\windows\system32\1.txt"

Add-Printer -Name "PrintDemon" -DriverName "Generic / Text Only" -PortName "c:\windows\system32\1.txt"

"Hello, World!" | Out-Printer -Name "PrintDemon"

Step2: Add a new printer port

Step3: Add a new printer

Step4: Add a printer job to printer queue

Step5: Restart spooler or restart your computer

Step6: Resume the printer job. "Hello, World!"` should be written into `C:\windows\system32\1.txt

Page 9: Diving in to spooler: Discovering LPE and RCE

Root Cause Analysis

Client Side

Sever Side

1. Add printer driver2. Add printer port3. Add new printer4. Add a printer job5. Resume your printer job

LcmStartDocPort

Impersonate Client

Save as Shadow Printer Job

1. Add printer driver2. Add printer port3. Add new printer4. Add a printer job5. Restart spooler 6. Resume your printer job

LcmStartDocPort

Impersonate Self(SYSTEM)

Save as Shadow Printer Jobs

Initialization ProcessShadowJobs

Normal steps:

Vulnerable steps:

Page 10: Diving in to spooler: Discovering LPE and RCE

Client Side

s

Sever Side

The Patch of CVE-2020-1048

Add-PrinterPort -Name "C:\windows\system32\1.txt"

Impersonate Client

PortIsValid(“C:\windows\system32\1.txt”)

Return Fail

Page 11: Diving in to spooler: Discovering LPE and RCE

\Device\HarddiskVolume6 <====> \??\D:

Symlink \??\C: --> \??\D:

Case 1 : Using Device Symbolic Links

Mitigation Bypass – CVE-2020-1337

Page 12: Diving in to spooler: Discovering LPE and RCE

Device Symbolic Links only works for current user

Open \??\C:\Windows\1.txt ============> \??\D:\Windows\1.txtReparse

Open \??\C:\Windows\1.txt ============> \??\C:\Windows\1.txt

Running as SYSTEM (Impersonate Client)

Running as SYSTEM

Not Reparse

Symlink \??\C: --> \??\D:

Page 13: Diving in to spooler: Discovering LPE and RCE

Win Time-Of-Use-Time-Of-Check

Client Side

Medium user writable!

Sever Side

Impersonate Client

PortIsValid(“C:\windows\system32\1.txt”)

CreateFileW(“D:\windows\system32\1.txt”)

Device Symlink \??\C:\ ------> \??\D:\

Add-PrinterPort -Name "C:\windows\system32\1.txt"

Return Success

Page 14: Diving in to spooler: Discovering LPE and RCE

Case 2 : Using Junction Attack

Client Side

Sever Side

Junction C:\1 ----> C:\Windows\System32

Add-PrinterPort -Name "C:\1\1.txt"

New-Item -Type Junction -Path C:\1 -Value C:\Windows\System32

Impersonate Client

PortIsValid(“C:\1\1.txt”)

Return Success

1. Add new printer2. Add a printer job3. Restart your computer4. Resume your printer job

LcmStartDocPort(“C:\1\1.txt”)

Impersonate Self

CreateFileW(“C:\Windows\System32\1.txt”)

Page 15: Diving in to spooler: Discovering LPE and RCE

Patch of CVE-2020-1337

Client Side

Sever Side

Impersonate Client

PortIsValid(“C:\windows\system32\1.txt”)

CreateFileW(“D:\windows\system32\1.txt”)

Device Symlink \??\C:\ ------> \??\D:\

Add-PrinterPort -Name "C:\windows\system32\1.txt"

IsPortALink(handle,“C:\windows\system32\1.txt”)

Path Redirection attack detected!!!

Patch 1: Adding path redirection check when checking

Page 16: Diving in to spooler: Discovering LPE and RCE

Patch of CVE-2020-1337

Client Side

Sever Side

Junction C:\1 ----> C:\Windows\System32

Add-PrinterPort -Name "C:\1\1.txt"

New-Item -Type Junction -Path C:\1 -Value C:\Windows\System32

Impersonate Client

PortIsValid(“C:\1\1.txt”)

Return Success

1. Add new printer2. Add a printer job3. Restart your computer4. Resume your printer job

LcmStartDocPort(“C:\1\1.txt”)

Impersonate Self

CreateFileW(“C:\Windows\System32\1.txt”)

Patch 2: Adding path redirection check when using

IsPortALink(handle, “C:\1\1.txt”)

Page 17: Diving in to spooler: Discovering LPE and RCE

Client Side

Sever Side

Add-PrinterPort -Name "C:\1\1.txt"

New-Item -Type Junction -Path C:\1 -Value C:\Windows\System32

1. Add new printer2. Add a printer job3. Restart your computer4. Resume your printer job

LcmStartDocPort(“C:\1\1.txt”)

Impersonate Self

CreateFileW(“C:\Windows\System32\1.txt”)

Fix a bug but bring an arbitrary file deletion bug

IsPortALink(handle, “C:\1\1.txt”)

If Return Fail

DeleteFileW(“C:\1\1.txt”)

DeleteFileW(“C:\Windows\System32\1.txt”)

Reparse

Mitigation Bypass - CVE-2020-17014

Page 18: Diving in to spooler: Discovering LPE and RCE

Bypass IsPortALink – found by James Forshaw

Mitigation Bypass - CVE-2020-17001

😠 MS uses GetFinalPathNameByHandle to prevent path redirection attack

Bypass IsPortALink – found by James Forshaw

Page 19: Diving in to spooler: Discovering LPE and RCE

Symlink \??\C:\1\1.txt ----> \??\C:\Windows\System32\1.txt

GetFinalPathNameByHandle

\??\C:\1\1.txt \??\C:\Windows\System32\1.txt

file handleCreateFileW

Hardlink \??\C:\1\1.txt ----> \??\C:\Windows\System32\1.txt

GetFinalPathNameByHandle

\??\C:\1\1.txt \??\C:\1\.txt

file handleCreateFileW

😠 Unfortunately, MS has released a mitigation for hardlink almost two years ago.

Behavior of GetFinalPathNameByHandle

Page 20: Diving in to spooler: Discovering LPE and RCE

Behavior of GetFinalPathNameByHandle while handling UNC PATH

Administrative Shares (Admin$, IPC$, C$) in Windows 10

⚫ C$ - Default Driver Share

⚫ IPC$ - Remote IPC (used in named pipe)

⚫ Admin$ - Remote admin (point to %SystemRoot% Directory)

Administrative Shares are used for remote access and can also be accessed locally

Page 21: Diving in to spooler: Discovering LPE and RCE

Behavior of GetFinalPathNameByHandle while handling UNC PATH

Symlink \??\C:\test\1.txt ---> \??\C:\Windows\System32\1.txt

GetFinalPathNameByHandle

\??\UNC\localhost\$C\test\1.txt

file handleCreateFileW

\??\UNC\localhost\$C\test\1.txt

UNC PATH

😀 Bypass GetFinalPathNameByHandle!!!

UNC PATH

Page 22: Diving in to spooler: Discovering LPE and RCE

Finally fixed the root cause – Impersonate Self(SYSTEM)

Page 23: Diving in to spooler: Discovering LPE and RCE

EvilCopyFileEvent - Configuring Printer is Dangerous

Printer Creator – Medium User

Medium user can manage printer

Add-Printer -Name "PrintDemon" -DriverName "Generic / Text Only" -PortName “C:\1\1.txt"

Limited user can create a printer and configure this printer

Page 24: Diving in to spooler: Discovering LPE and RCE

Localspl.dll! SplSetPrinterDataEx

Configure Your Printer

Page 25: Diving in to spooler: Discovering LPE and RCE

Printer attributes

Handler

Set different printer attributes to trigger different handler calls

Localspl.dll!SplSetPrinterDataEx

Page 26: Diving in to spooler: Discovering LPE and RCE

Localspl.dll! SplCopyFileEvent

Load library?

Page 27: Diving in to spooler: Discovering LPE and RCE

szFilePath must be a canonical path!

Page 28: Diving in to spooler: Discovering LPE and RCE

Legal szFilePath for IsModuleFilePathAllowed

Case 2 : szFilePath is under the the root directory of C:\windows\system32

Case 1 : szFilePath is under the C:\Windows\System32\spool\drivers

✓ C:\windows\system32\XXX.DLL

✗ C:\windows\system32\XXX\XXX.DLL

✓ C:\Windows\System32\spool\drivers\XXX.DLL

✓ C:\Windows\System32\spool\drivers\XXX\XXX.DLL

X cannot be ‘/’ or ‘\’

Page 29: Diving in to spooler: Discovering LPE and RCE

✓ C:\Windows\System32\spool\drivers\XXX.DLL

✓ C:\Windows\System32\spool\drivers\XXX\XXX.DLLCASE 1 :

⚫ Both them are not medium user writable.

CASE 2 :

⚫ C:\windows\system32\ are not medium user writable.

⚫ C:\windows\system32\Tasks\XXX are writable by medium user, but it’s illegal

here.

✓ C:\windows\system32\XXX.DLL

✗ C:\windows\system32\XXX\XXX.DLL

☹ Seems Not Exploitable?

Page 30: Diving in to spooler: Discovering LPE and RCE

NTFS Alternate Data Streams (ADS)

⚫ ADS is file attribute only found on the NTFS file system⚫ ADS allows user to create sub streams for a file or a directory by using

separator “:” ⚫ ADS is widely abused to write hidden data for malware files

For file – C:\1\AAA:BBB

AAA is the file or directory nameBBB is the ADS on AAA

Page 31: Diving in to spooler: Discovering LPE and RCE

C:\Windows\System32\Tasks:exp.dll must meet the CASE 2 - C:\windows\system32\XXX.DLL

😀

Medium user can write ADS of C:\Windows\System32\Tasks

Page 32: Diving in to spooler: Discovering LPE and RCE

Get SYSTEM Privilege with Twice Printer API Call!

Page 33: Diving in to spooler: Discovering LPE and RCE

03

Vulnerability Analysis : Remote Code Execution

Page 34: Diving in to spooler: Discovering LPE and RCE

Memory Corruption - CVE-2021-24088, CVE-2021-24077, CVE-2021-1722

Retrieves print jobs in a specified printer.

User supplied buffer and size

Page 35: Diving in to spooler: Discovering LPE and RCE

Root Cause

Page 36: Diving in to spooler: Discovering LPE and RCE

pJob

Jobs

Data

String pointer

Copy

String

Data

String pointer

Data

String

Page 37: Diving in to spooler: Discovering LPE and RCE

pJob

Job0

CopyString0

String1

Start

“AAAA”

“BBBB”“AAAA”

“BBBB”End

String pointer

String pointer

Data

Data

Data

String pointer

String pointer

Data

Data

Data

Page 38: Diving in to spooler: Discovering LPE and RCE

pJob

Job1

Copy

Start

“CCCC”

“DDDD”“AAAA”

“BBBB”

End

“CCCC”

“DDDD”

String0

String1

String pointer

String pointer

Data

Data

Data

String0

String1

String pointer

String pointer

Data

Data

Data

String0

String1

String pointer

String pointer

Data

Data

Data

Page 39: Diving in to spooler: Discovering LPE and RCE

pJob

Job2

Copy

Start

“EEEE”

“FFFF”“AAAA”

“BBBB”

End

“CCCC”

“DDDD”

Underflow

String0

String1

String pointer

String pointer

Data

Data

Data

String0

String1

String pointer

String pointer

Data

Data

Data

String0

String1

String pointer

String pointer

Data

Data

Data“EEEE”

“FFFF”

Page 40: Diving in to spooler: Discovering LPE and RCE

pJob

Job

CopyDocName

String

Start

“AAAA”

“BBBB”

End

Control the job size

Data

Data

Data

Page 41: Diving in to spooler: Discovering LPE and RCE
Page 42: Diving in to spooler: Discovering LPE and RCE

pJob

Job

DocName

Start

“AAAA…”

End

Copy“AAAA…”

Controllable size and content

Page 43: Diving in to spooler: Discovering LPE and RCE

PrintNightmare

“PrintNightmare”

LPE → RCE

LPE

Page 44: Diving in to spooler: Discovering LPE and RCE

Disable print spooler:

Stop-Service -Name Spooler –ForceSet-Service -Name Spooler -StartupType Disabled

Page 45: Diving in to spooler: Discovering LPE and RCE

Conclusion

• Spooler is still a good attack surface through years of vulnerabilities disclosure.

• Disabled your spooler, if you don’t need it.

Page 46: Diving in to spooler: Discovering LPE and RCE

Q & A

Thanks for listening!

Any Questions?