winapi.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "winapi.hpp"
  4. #include "cbasetypes.h"
  5. // Taken from https://support.microsoft.com/de-de/help/118626/how-to-determine-whether-a-thread-is-running-in-user-context-of-local
  6. bool IsCurrentUserLocalAdministrator(void){
  7. #ifdef WIN32
  8. BOOL fReturn = FALSE;
  9. DWORD dwStatus;
  10. DWORD dwAccessMask;
  11. DWORD dwAccessDesired;
  12. DWORD dwACLSize;
  13. DWORD dwStructureSize = sizeof(PRIVILEGE_SET);
  14. PACL pACL = NULL;
  15. PSID psidAdmin = NULL;
  16. HANDLE hToken = NULL;
  17. HANDLE hImpersonationToken = NULL;
  18. PRIVILEGE_SET ps;
  19. GENERIC_MAPPING GenericMapping;
  20. PSECURITY_DESCRIPTOR psdAdmin = NULL;
  21. SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
  22. /*
  23. Determine if the current thread is running as a user that is a member
  24. of the local admins group.
  25. To do this, create a security descriptor that has a DACL which has an ACE
  26. that allows only local aministrators access.
  27. Then, call AccessCheck with the current thread's token and the security
  28. descriptor. It will say whether the user could access an object if it
  29. had that security descriptor.
  30. Note: you do not need to actually create the object. Just checking access
  31. against the security descriptor alone will be sufficient.
  32. */
  33. const DWORD ACCESS_READ = 1;
  34. const DWORD ACCESS_WRITE = 2;
  35. __try
  36. {
  37. /*
  38. AccessCheck() requires an impersonation token. We first get a
  39. primary token and then create a duplicate impersonation token. The
  40. impersonation token is not actually assigned to the thread, but is
  41. used in the call to AccessCheck. Thus, this function itself never
  42. impersonates, but does use the identity of the thread. If the
  43. thread was impersonating already, this function uses that impersonation
  44. context.
  45. */
  46. if (!OpenThreadToken(GetCurrentThread(), TOKEN_DUPLICATE | TOKEN_QUERY,
  47. TRUE, &hToken))
  48. {
  49. if (GetLastError() != ERROR_NO_TOKEN)
  50. __leave;
  51. if (!OpenProcessToken(GetCurrentProcess(),
  52. TOKEN_DUPLICATE | TOKEN_QUERY, &hToken))
  53. __leave;
  54. }
  55. if (!DuplicateToken(hToken, SecurityImpersonation,
  56. &hImpersonationToken))
  57. __leave;
  58. /*
  59. Create the binary representation of the well-known SID that
  60. represents the local administrators group. Then create the
  61. security
  62. descriptor and DACL with an ACE that allows only local admins
  63. access.
  64. After that, perform the access check. This will determine whether
  65. the current user is a local admin.
  66. */
  67. if (!AllocateAndInitializeSid(&SystemSidAuthority, 2,
  68. SECURITY_BUILTIN_DOMAIN_RID,
  69. DOMAIN_ALIAS_RID_ADMINS,
  70. 0, 0, 0, 0, 0, 0, &psidAdmin))
  71. __leave;
  72. psdAdmin = LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
  73. if (psdAdmin == NULL)
  74. __leave;
  75. if (!InitializeSecurityDescriptor(psdAdmin,
  76. SECURITY_DESCRIPTOR_REVISION))
  77. __leave;
  78. // Compute size needed for the ACL.
  79. dwACLSize = sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) +
  80. GetLengthSid(psidAdmin) - sizeof(DWORD);
  81. pACL = (PACL)LocalAlloc(LPTR, dwACLSize);
  82. if (pACL == NULL)
  83. __leave;
  84. if (!InitializeAcl(pACL, dwACLSize, ACL_REVISION2))
  85. __leave;
  86. dwAccessMask = ACCESS_READ | ACCESS_WRITE;
  87. if (!AddAccessAllowedAce(pACL, ACL_REVISION2, dwAccessMask,
  88. psidAdmin))
  89. __leave;
  90. if (!SetSecurityDescriptorDacl(psdAdmin, TRUE, pACL, FALSE))
  91. __leave;
  92. /*
  93. AccessCheck validates a security descriptor somewhat;
  94. set the group and owner so that enough of the security descriptor is
  95. filled out to make AccessCheck happy.
  96. */
  97. SetSecurityDescriptorGroup(psdAdmin, psidAdmin, FALSE);
  98. SetSecurityDescriptorOwner(psdAdmin, psidAdmin, FALSE);
  99. if (!IsValidSecurityDescriptor(psdAdmin))
  100. __leave;
  101. dwAccessDesired = ACCESS_READ;
  102. /*
  103. Initialize GenericMapping structure even though you
  104. do not use generic rights.
  105. */
  106. GenericMapping.GenericRead = ACCESS_READ;
  107. GenericMapping.GenericWrite = ACCESS_WRITE;
  108. GenericMapping.GenericExecute = 0;
  109. GenericMapping.GenericAll = ACCESS_READ | ACCESS_WRITE;
  110. if (!AccessCheck(psdAdmin, hImpersonationToken, dwAccessDesired,
  111. &GenericMapping, &ps, &dwStructureSize, &dwStatus,
  112. &fReturn))
  113. {
  114. fReturn = FALSE;
  115. __leave;
  116. }
  117. }
  118. __finally
  119. {
  120. // Clean up.
  121. if (pACL) LocalFree(pACL);
  122. if (psdAdmin) LocalFree(psdAdmin);
  123. if (psidAdmin) FreeSid(psidAdmin);
  124. if (hImpersonationToken) CloseHandle(hImpersonationToken);
  125. if (hToken) CloseHandle(hToken);
  126. }
  127. return fReturn;
  128. #else
  129. return false;
  130. #endif
  131. }