Advertisement
Guest User

GetCsrssPid3

a guest
Jan 22nd, 2017
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #define echo(x) x
  2. #define label(x) echo(x)__LINE__
  3. #define RTL_CONSTANT_STRINGW(s) { sizeof( s ) - sizeof( (s)[0] ), sizeof( s ),(PWSTR)(s) }
  4. #define STATIC_UNICODE_STRING(name, str) static const WCHAR label(__)[] = L##str; static const UNICODE_STRING name = RTL_CONSTANT_STRINGW(label(__))
  5.  
  6. NTSTATUS GetCsrssPid(HANDLE* pUniqueProcessId)
  7. {
  8.     NTSTATUS status;
  9.     ULONG cb = 0x10000;
  10.     do
  11.     {
  12.         status = STATUS_INSUFFICIENT_RESOURCES;
  13.  
  14.         if (PVOID buf = ExAllocatePool(PagedPool, cb))
  15.         {
  16.             if (0 <= (status = NtQuerySystemInformation(SystemProcessInformation, buf, cb, &cb)))
  17.             {
  18.                 status = STATUS_NOT_FOUND;
  19.  
  20.                 union {
  21.                     PSYSTEM_PROCESS_INFORMATION pspi;
  22.                     PVOID pv;
  23.                     PUCHAR pb;
  24.                 };
  25.  
  26.                 pv = buf;
  27.  
  28.                 ULONG NextEntryOffset = 0;
  29.                 do
  30.                 {
  31.                     pb += NextEntryOffset;
  32.  
  33.                     STATIC_UNICODE_STRING(csrss, "csrss.exe");
  34.  
  35.                     if (pspi->UniqueProcessId)
  36.                         DbgPrint("%p %wZ\n", pspi->UniqueProcessId, &pspi->ImageName);
  37.  
  38.                     if (RtlEqualUnicodeString(&csrss, &pspi->ImageName, TRUE))
  39.                     {
  40.                         ///////// This part never is executed ///////
  41.                        
  42.                         *pUniqueProcessId = pspi->UniqueProcessId;
  43.                          DbgPrint("%p\n", *pUniqueProcessId);
  44.                         status = STATUS_SUCCESS;
  45.                         break;
  46.                        
  47.                        ////////////////////////////////////////////
  48.                     }
  49.  
  50.                 } while (NextEntryOffset = pspi->NextEntryOffset);
  51.             }
  52.             ExFreePool(buf);
  53.         }
  54.  
  55.     } while (status == STATUS_INFO_LENGTH_MISMATCH);
  56.  
  57.     return status;
  58. }
  59.  
  60. // Usage:
  61.  
  62. HANDLE hCsrssPid = (HANDLE)0;
  63. GetCsrssPid(&hCsrssPid);
  64. DbgPrint("%x\n", hCsrssPid);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement