proxytokernel
proxytokernel
Streaming vulnerabilities
from the Windows Kernel
Angelboy
HEXACON2024 | 2024.10.05
1
Who am I
• Angelboy (@scwuaptx)
• Senior Security of DEVCORE
• MSRC 2024 MVR Top 100
• Speaker at
• CODE BLUE, HITCON, HITB GSEC
• Master of Pwn of Pwn2Own Toronto 2022
2
Looking at historical vulnerabilities is
indispensable
3
Pwn2Own Vancouver 2024
4
In-the-wild
• Win32k
• GDI (Graphics Device Interface) and UI functions
• Windows drawing, font management …
• Complexity of Code
• It has been a popular target for attackers over the past decade.
5
In-the-wild
• CLFS
• Common Log File System
• Handles log-based transaction processing
• Complexity of Code
• It has been a popular target for attackers over the past six years.
6
In-the-wild
• MSKSSRV
• Microsoft Kernel Streaming Service
• Handles synchronization of multimedia streams
• Very small
7
In-the-wild
• MSKSSRV
• Microsoft Kernel Streaming Service
• Handles synchronization of multimedia streams
• Very small
• Last year it became a very popular target, with 2 ITW exploits in just a
few month.
8
In-the-wild
• Win32k
• CLFS
• MSKSSRV
• …
9
Let's take a look at MSKSSRV
10
MSKSSRV
11
MSKSSRV
12
MSKSSRV
13
MSKSSRV
14
But is that the end of it ?
15
Actually …
16
MSKSSRV
ksthunk.sys
ks.sys
portcls.sys
mspclock.sys
HdAudio.sys
17
18
19
20
CVE-2024-38054
CVE-2024-30084
CVE-2024-35250
CVE-2024-30090
CVE-2024-38057
21
Brief overview of Kernel Streaming
22
23
What is Kernel Streaming ?
24
What is kernel streaming ?
25
How to interact with Device?
26
Enumerate Device
27
Enumerate KS Device
\\?\hdaudio#subfunc_01&ven_8086&dev_2812&nid_0001&subsys
_00000000&rev_1000#6&2f1f346a&0&0002&0000001d#{6994ad
04-93ef-11d0-a3cc-00a0c9223196}\ehdmiouttopo
28
Enumerate KS Device
• KsOpenDefaultDevice
• Opens a handle to the first device that is listed in the specified Plug and
Play (PnP) category
29
KS Object
30
KS Object
• After we open these Devices, Kernel Streaming will establish some Kernel
Streaming related instance
• KS Filter
• KS Pin
• …
• Encapsulate hardware function
31
KS Filter
Filter
Filter Factory
https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/audio-filters
32
KS Pin
Source Pin
Filter
Filter Factory
Sink Pin
https://learn.microsoft.com/en-us/windows-hardware/drivers/audio/audio-filters
33
KS Property
34
KS Property
35
Kernel Streaming Architecture
36
Kernel Streaming Architecture
Application
User Mode
Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
37
Kernel Streaming Architecture
Application
User Mode
Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
38
ksthunk
KS Filter
39
Kernel Streaming Architecture
Application
User Mode
Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
40
ks.sys
41
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
42
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
Convert 32 bit request to 64 bit request …
or pass it down directly
HdAudio usbvideo
KS Filter
43
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
44
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KsPropertyHandler
KS Filter
45
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KsPropertyHandler
Look for the property set, item and
the handler
KS Filter
46
The work flow of set pin state
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KsPropertyHandler
KS Filter
portcls!PinPropertyDeviceState
47
From attacker's view
48
From attacker's view
49
From attacker's view
50
From attacker's view
51
We found some trivial vulnerabilities in few
days …
52
Vulnerabilities
• Portcls.sys
• CVE-2024-38055 (OOB)
• CVE-2024-38056
• Ksthunk
• CVE-2024-38054 (OOB)
• CVE-2024-38057
53
We found some interesting things
54
Is really safe ?
55
Is really safe ?
UserMode(1)
56
The Overlooked Bug Class
57
PreviousMode
• A field in the thread object that indicates whether the parameters for a
System Service Call originated in user mode or kernel mode.
Application
User Mode
Kernel Mode
NtCreateFile kthread->PreviousMode =
UserMode
ZwCreateFile
Device Driver Device Driver
kthread->PreviousMode =
KernelMode
58
IRP RequestorMode
• IRP->RequestorMode
• the execution mode of the original requester of the operation
• A copy of the PreviousMode value from the thread object
59
IRP RequestorMode
60
But there are some issues in some cases …
61
A logical bug class
PreviousMode == UserMode
https://googleprojectzero.blogspot.com/2019/03/windows-kernel-logic-bug-class-access.html
62
A logical bug class
https://googleprojectzero.blogspot.com/2019/03/windows-kernel-logic-bug-class-access.html
63
A logical bug class
https://googleprojectzero.blogspot.com/2019/03/windows-kernel-logic-bug-class-access.html
64
A logical bug class
https://googleprojectzero.blogspot.com/2019/03/windows-kernel-logic-bug-class-access.html
65
It focuses on Zw* system service call
66
Are there other potential causes
for this bug class?
67
Are there other potential causes
for this bug class?
68
The Bug Pattern
• IoBuildDeviceIoControlRequest
69
The Bug Pattern
• IoBuildDeviceIoControlRequest
70
The Bug Pattern
• IoBuildDeviceIoControlRequest
Application
User Mode
IoBuildDeviceIoControlRequest
71
The Bug Pattern
• IoBuildDeviceIoControlRequest
Application
User Mode
RequestorMode
Kernel Mode
Nt*
= KernelMode
IofCallDriver
Device Driver Device Driver
IoBuildDeviceIoControlRequest
72
After quick review of this bug pattern in KS
73
74
But …
75
KernelMode
CKsPin::GetState
76
CKsPin::GetState
SerializePropertySet
KernelMode
77
CKsPin::GetState
UnserializePropertySet
KernelMode
SerializePropertySet
78
Look for the bug pattern in KS
1. KsSynchronousIoControlDevice
2. Controllable
• InputBuffer
• OutputBuffer
3. IOCTL relies on RequestorMode for security checks
79
Look for the bug pattern in KS
1. KsSynchronousIoControlDevice
2. Controllable
• InputBuffer
• OutputBuffer
80
Look for the bug pattern in KS
1. KsSynchronousIoControlDevice
2. Controllable
• InputBuffer
• OutputBuffer
3. IOCTL relies on RequestorMode for security checks
81
The Vulnerability & Exploitation
82
83
Unserialize the property set
• KSPROPERTY_TYPE_UNSERIALIZESET
• Interaction with multiple properties with a single call
Property Set
Property 1
Kernel
Application Property 2 Streming
84
UnserializePropertySet
85
UnserializePropertySet
86
UnserializePropertySet
87
UnserializePropertySet
KernelMode
88
UnserializePropertySet
User Control
89
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
90
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
RequestorMode
I/O Manager = UserMode
portcls ks ks.sys
Convert 32-bit to 64-bit …
HdAudio usbvideo
KS Filter
91
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
92
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KsPropertyHandler
KS Filter
93
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KsPropertyHandler
94
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter KsSynchronousIoControlDevice
95
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
RequestorMode
I/O Manager = KernelMode
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter KsSynchronousIoControlDevice
96
We can do arbitrary IOCTL_KS_PROPERTY with
KernelMode now
97
We need to find a target to EoP
98
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
KsSynchronousIoControlDevice
99
ksthunk!DispatchIoctl
100
ksthunk!DispatchIoctl
101
ksthunk!DispatchIoctl
102
ksthunk!DispatchIoctl
103
ksthunk!DispatchIoctl
104
UnserializePropertySet
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
KsSynchronousIoControlDevice
105
ksthunk!DispatchIoctl
106
We have an arbitrary call with one argument now
107
Exploitation
108
Mitigation on Win11
• kCFG
• kASLR
• SMEP
• …
109
Mitigation on Win11
• kCFG
• kASLR
• NtQuerySystemInformation
• SMEP
• Reuse Kernel Code
• …
110
Bypass kCFG
111
Bypass kCFG
112
Bypass kCFG
113
Two hours later …
114
Bypass kCFG
115
Bypass kCFG
• RtlSetAllBits
• The RtlSetAllBits routine sets all bits in a given bitmap variable.
116
We can set all bits in arbitrary memory
117
Abuse token privilege
Token
_RTL_BITMAP
SizeOfBitmap
Privileges
Buffer
118
Abuse token privilege
119
The Last Step
120
121
It's like a Proxy to Kernel !
122
However …
123
124
KS Device in Hyper-V
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
KsSynchronousIoControlDevice
125
KS Device in Hyper-V
Application
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
mskssrv …
ksthunk.sys No DrmAudioStream
property set
ks.sys
…
KS Filter
KsSynchronousIoControlDevice
126
127
IOCTL_KS_PROPERTY
• Neither I/O
• Using user input buffer directly
• Inputbuffer = Parameters.DeviceIoControl.Type3InputBuffer
• Outputbuffer = Irp->UserBuffer
128
KspPropertyHandler
129
KspPropertyHandler
130
Let's take a look at UnserializePropertySet again
131
UnserializePropertySet
132
UnserializePropertySet
133
UnserializePropertySet User Input Buffer
Application KSPROPSETID_Service
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
mskssrv …
ksthunk.sys
ks.sys
…
KS Filter
134
UnserializePropertySet User Input Buffer
Application KSPROPSETID_Service
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager SystemBuffer
mskssrv … KSPROPSETID_Service
ksthunk.sys
ks.sys
…
KsPropertyHandler
135
UnserializePropertySet User Input Buffer Trigger Race Condition
Application DrmAudioStream
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager
mskssrv …
ksthunk.sys
ks.sys
…
call UnserializePropertySet
KS Filter
136
UnserializePropertySet User Input Buffer
Application DrmAudioStream
User Mode
IOCTL_KS_PROPERTY Kernel Mode
I/O Manager New Input Buffer
mskssrv … DrmAudioStream
ksthunk.sys
ks.sys
…
call UnserializePropertySet
KS Filter
137
UnserializePropertySet User Input Buffer
Application DrmAudioStream
User Mode
IOCTL_KS_PROPERTY Kernel Mode
RequestorMode
I/O Manager = KernelMode New Input Buffer
mskssrv … DrmAudioStream
ksthunk.sys
ks.sys
…
KS Filter
KsSynchronousIoControlDevice
138
139
140
Is that the end of it ?
141
142
KS Event
143
KS Event
• Event sets are groups of related events for which a listener can request
notification.
• Client can register event for
• Device State Change
• Time interval
• ...
144
KS Event
145
kstunk!ThunkEnableEventIrp
146
ThunkEnableEventIrp
User input
147
ThunkEnableEventIrp
148
ThunkEnableEventIrp
KernelMode
149
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
I/O Manager
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
150
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
RequestorMode
I/O Manager = UserMode
portcls ks ks.sys
ThunkEnableEventIrp
…
HdAudio usbvideo
KS Filter
151
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
I/O Manager
RequestorMode
= UserMode mskssrv drmk mspclock …
portcls ks ks.sys
ThunkEnableEventIrp
…
Convert 32-bit to 64-bit
HdAudio usbvideo
KS Filter
152
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
I/O Manager
RequestorMode
= UserMode mskssrv drmk mspclock …
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
KsSynchronousIoControlDevice
153
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
RequestorMode
I/O Manager = KernelMode
portcls ks ks.sys
…
HdAudio usbvideo
KS Filter
KsSynchronousIoControlDevice
154
We can do arbitrary IOCTL_KS_ENABLE_EVENT
with KernelMode now
155
We need to find a target to EoP
156
But we didn't find a suitable target in ksthunk
157
We decide to pass it down to look for target
158
ThunkEnableEventIrp
Application
User Mode
IOCTL_KS_ENABLE_EVENT Kernel Mode
RequestorMode
I/O Manager = KernelMode
portcls ks ks.sys
…
HdAudio usbvideo
KspEnableEvent
KS Filter
159
We found some interesting …
160
KspEnableEvent
161
KS Event
162
We can provide arbitrary kernel object to it !
163
But …
164
ThunkEnableEventIrp
165
ThunkEnableEventIrp
166
Fortunately,
there are double fetch everywhere.
167
ThunkEnableEventIrp
Race window
168
If we trigger the event, it would call
KsGenerateEvent
169
KsGenerateEvent
170
KsGenerateEvent
171
KsIncrementCountedWorker
172
We have arbitrary increment primitive now
173
Arbitrary increment primitive to EoP
174
It seems trivial, but ...
175
Arbitrary increment primitive to EoP
176
Arbitrary increment primitive to EoP
• IoRing
• Need to overwrite IoRing->RegBuffersCount and IoRing->RegBuffers
• Good Candidate
• Only need to trigger the bug twice
177
KsIncrementCountedWorker
178
179
Let's find a new way !
180
Arbitrary increment primitive to EoP
181
Why does having SeDebugPrivilege allow you to
open high-privilege process?
182
PsOpenProcess
183
PsOpenProcess
184
PsOpenProcess
185
Make abusing token privilege great again
Application
User Mode
Kernel Mode
NtOpenProcess
PsOpenProcess
SeSinglePrivilegeCheck Nt
SeSinglePrivilegeCheck SeDebugPrivilege
0x14
Token
Privileges
Eprocess->Token
186
Make abusing token privilege great again
Application
User Mode
Kernel Mode
NtOpenProcess
PsOpenProcess
Nt
SeSinglePrivilegeCheck SeDebugPrivilege
0x14
Token
Privileges
Eprocess->Token
187
One more interesting …
188
nt!SeDebugPrivilege
Writable !!!
189
Make abusing token privilege great again !
190
Make abusing token privilege great again
191
Make abusing token privilege great again
192
nt!SeChangeNotifyPrivilege
193
How about changing the value of
nt!SeDebugPrivilege from 0x14 to 0x17 ?
194
Make abusing token privilege great again
Application
User Mode
Kernel Mode
NtOpenProcess
PsOpenProcess
SeSinglePrivilegeCheck Nt
SeSinglePrivilegeCheck SeDebugPrivilege
0x17
Token
Privileges
Eprocess->Token
195
Make abusing token privilege great again
Application
User Mode
Kernel Mode
NtOpenProcess
PsOpenProcess
Nt
SeSinglePrivilegeCheck SeDebugPrivilege
0x17
Token
Privileges
Eprocess->Token
196
Make abusing token privilege great again
Application
User Mode
Kernel Mode
NtOpenProcess
PsOpenProcess
Nt
SeSinglePrivilegeCheck SeDebugPrivilege
0x17
Token
Privileges
Eprocess->Token
197
Make abusing token privilege great again
Nt
SeDebugPrivilege
0x17
V1 == 0x14
199
Make abusing token privilege great again
200
201
Proxying to Kernel again !
202
203
The Next
204
The Next
205
The Next
206
Takeaways
207
Is that the end of it ?
208
CVE-2024-38125
CVE-2024-38055 CVE-2024-38056
CVE-2024-38191
CVE-2024-38054 CVE-2024-38144
CVE-2024-30084
CVE-2024-38052
CVE-2024-35250
CVE-2024-38057 CVE-2024-30090
209
To Be Continued …
210
Thanks!
scwuaptx
[email protected]
211