Advertisement
Guest User

Untitled

a guest
May 13th, 2021
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #define BEGIN_PRIVILEGES(name, n) static const union { TOKEN_PRIVILEGES name;\
  2. struct { ULONG PrivilegeCount; LUID_AND_ATTRIBUTES Privileges[n];} label(_) = { n, {
  3.  
  4. #define LAA(se) {{se}, SE_PRIVILEGE_ENABLED }
  5. #define LAA_D(se) {{se} }
  6.  
  7. #define END_PRIVILEGES }};};
  8.  
  9. BEGIN_PRIVILEGES(tp_backup_restore, 2)
  10.     LAA(SE_BACKUP_PRIVILEGE),
  11.     LAA(SE_RESTORE_PRIVILEGE),
  12. END_PRIVILEGES
  13.  
  14. UNICODE_STRING Empty {};
  15.  
  16. const SECURITY_QUALITY_OF_SERVICE sqos = {
  17.     sizeof (sqos), SecurityImpersonation, SECURITY_DYNAMIC_TRACKING, FALSE
  18. };
  19.  
  20. const OBJECT_ATTRIBUTES soa = { sizeof(soa), 0, 0, 0, 0, const_cast<SECURITY_QUALITY_OF_SERVICE*>(&sqos) };
  21.  
  22. NTSTATUS GetToken(PVOID buf, const TOKEN_PRIVILEGES* RequiredSet)
  23. {
  24.     NTSTATUS status;
  25.  
  26.     union {
  27.         PVOID pv;
  28.         PBYTE pb;
  29.         PSYSTEM_PROCESS_INFORMATION pspi;
  30.     };
  31.  
  32.     pv = buf;
  33.     ULONG NextEntryOffset = 0;
  34.  
  35.     do
  36.     {
  37.         pb += NextEntryOffset;
  38.  
  39.         HANDLE hProcess, hToken, hNewToken;
  40.  
  41.         CLIENT_ID ClientId = { pspi->UniqueProcessId };
  42.  
  43.         if (ClientId.UniqueProcess)
  44.         {
  45.             if (0 <= NtOpenProcess(&hProcess, PROCESS_QUERY_LIMITED_INFORMATION,
  46.                 const_cast<POBJECT_ATTRIBUTES>(&soa), &ClientId))
  47.             {
  48.                 status = NtOpenProcessToken(hProcess, TOKEN_DUPLICATE, &hToken);
  49.  
  50.                 NtClose(hProcess);
  51.  
  52.                 if (0 <= status)
  53.                 {
  54.                     status = NtDuplicateToken(hToken, TOKEN_ADJUST_PRIVILEGES|TOKEN_IMPERSONATE,
  55.                         const_cast<POBJECT_ATTRIBUTES>(&soa), FALSE, TokenImpersonation, &hNewToken);
  56.  
  57.                     NtClose(hToken);
  58.  
  59.                     if (0 <= status)
  60.                     {
  61.                         status = NtAdjustPrivilegesToken(hNewToken, FALSE, const_cast<PTOKEN_PRIVILEGES>(RequiredSet), 0, 0, 0);
  62.  
  63.                         if (STATUS_SUCCESS == status)  
  64.                         {
  65.                             status = NtSetInformationThread(NtCurrentThread(), ThreadImpersonationToken, &hNewToken, sizeof(hNewToken));
  66.                         }
  67.  
  68.                         NtClose(hNewToken);
  69.  
  70.                         if (STATUS_SUCCESS == status)
  71.                         {
  72.                             return STATUS_SUCCESS;
  73.                         }
  74.                     }
  75.                 }
  76.             }
  77.         }
  78.  
  79.     } while (NextEntryOffset = pspi->NextEntryOffset);
  80.  
  81.     return STATUS_UNSUCCESSFUL;
  82. }
  83.  
  84. NTSTATUS ImpersonateToken(const TOKEN_PRIVILEGES* RequiredSet)
  85. {
  86.     NTSTATUS status;
  87.     ULONG cb = 0x40000;
  88.  
  89.     do
  90.     {
  91.         status = STATUS_INSUFFICIENT_RESOURCES;
  92.  
  93.         if (PBYTE buf = new BYTE[cb += PAGE_SIZE])
  94.         {
  95.             if (0 <= (status = NtQuerySystemInformation(SystemProcessInformation, buf, cb, &cb)))
  96.             {
  97.                 status = GetToken(buf, RequiredSet);
  98.  
  99.                 if (status == STATUS_INFO_LENGTH_MISMATCH)
  100.                 {
  101.                     status = STATUS_UNSUCCESSFUL;
  102.                 }
  103.             }
  104.  
  105.             delete [] buf;
  106.         }
  107.  
  108.     } while(status == STATUS_INFO_LENGTH_MISMATCH);
  109.  
  110.     return status;
  111. }
  112.  
  113. ImpersonateToken(&tp_backup_restore);
  114. //////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement