AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Does the user (NOT the program) have admin privileges?

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
daorc



Joined: 18 Oct 2006
Posts: 169

PostPosted: Wed Mar 03, 2010 4:05 pm    Post subject: Does the user (NOT the program) have admin privileges? Reply with quote

Hi everyone,

Just to let you know, I'm aware of the A_IsAdmin variable.

Does anyone know if there's a way of finding out if the current USER is an administrator? If the following script is run by an admin user in windows vista or 7, the A_IsAdmin value is "0" because the script doesn't have admin privileges.

Code:
msgbox "%A_IsAdmin%"


I'm NOT looking for a way to elevate the privileges of a script ... I'd like to know how to find out if the current user is an administrator.

Any ideas...?

Thank you in advance!

Daorc
Back to top
View user's profile Send private message
Carcophan



Joined: 24 Dec 2008
Posts: 1308
Location: :noitacoL

PostPosted: Wed Mar 03, 2010 5:14 pm    Post subject: Reply with quote

Do you know who your admins are? Or is this aimed at an open audience you have no control over?

I personally use 'a_username' and an If statement to verify they are/are not an admin.

I know it isn't exactly your request, but its the only thing I could think of.
Back to top
View user's profile Send private message
da_boogie_man
Guest





PostPosted: Wed Mar 03, 2010 8:18 pm    Post subject: Re: Does the user (NOT the program) have admin privileges? Reply with quote

daorc wrote:

Just to let you know, I'm aware of the A_IsAdmin variable.

Does anyone know if there's a way of finding out if the current USER is an administrator? If the following script is run by an admin user in windows vista or 7, the A_IsAdmin value is "0" because the script doesn't have admin privileges.


HELP FILE wrote:

If the current user has admin rights, this variable contains 1. Otherwise, it contains 0. Under Windows 95/98/Me, this variable always contains 1.


DBM
Back to top
daorc



Joined: 18 Oct 2006
Posts: 169

PostPosted: Thu Mar 04, 2010 9:21 am    Post subject: Reply with quote

Thank you both for your replies.

@Carcophan
Quote:
Do you know who your admins are? Or is this aimed at an open audience you have no control over?

Thanks for the suggestion, but I'm afraid it is aimed at an open audience, so that solution won't work for me.

@da_boogie_man
Quote:
If the current user has admin rights, this variable contains 1

The help file is misleading when working with vista or 7 (with UAC). In these OSs, "0" is always returned if the script is running without administrative privileges. Perhaps this wording in the help file should be changed...?

I'm really surprised there isn't just a registry key somewhere in HKCU to read. Ah well ... keep any suggestions coming ....

Thanks,

Daorc
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Mar 04, 2010 4:19 pm    Post subject: Re: Does the user (NOT the program) have admin privileges? Reply with quote

This probably needs converted to AutoHotkey (& maybe added as a built-in {A_IsAdminGroup})...
...including the raw code, in case that disappears...

Non-AutoHotkey-ified C code wrote:
Code:
#include <windows.h>
#include <stdio.h>

int main() {
   HANDLE hToken;
   TOKEN_ELEVATION_TYPE elevationType;
   DWORD dwSize;

   OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
   GetTokenInformation(hToken, TokenElevationType, &elevationType, sizeof(elevationType), &dwSize);

   switch (elevationType) {
      case TokenElevationTypeDefault:
         wprintf(TEXT("\nTokenElevationTypeDefault - User is not using a split token.\n"));
         break;
      case TokenElevationTypeFull:
         wprintf(TEXT("\nTokenElevationTypeFull - User has a split token, and the process is running elevated.\n"));
         break;
      case TokenElevationTypeLimited:
         wprintf(TEXT("\nTokenElevationTypeLimited - User has a split token, but the process is not running elevated.\n"));
         break;
   }

   if (hToken) {
      CloseHandle(hToken);
   }
}

Here's the best AutoHotkey version I can come up with, however it was written on XP SP1 & still gives me A_LastError=87 meaning ERROR_INVALID_PARAMETER...cuz the TokenElevationType needs Win Vista or higher to work...but I tested the rest of the script asking for TokenType & that seems to work...

Code:
Gosub, WindowsConstants

Debug=1
Debug_Success=1
;//TokenInformationClass:=TokenType
TokenInformationClass:=TokenElevationType

;//sizeof_hToken:=VarSetCapacity(hToken, 128, 0)
;//sizeof_elevationType:=VarSetCapacity(elevationType, 128, 0)
;//sizeof_ReturnLength:=VarSetCapacity(ReturnLength, 128, 0)

DllCall("SetLastError", "UInt", 0)
ret:=DllCall("Advapi32.dll\OpenProcessToken", "UInt", DllCall("GetCurrentProcess"), "UInt", TOKEN_QUERY, "UIntP", hToken)
;//NumGet_hToken:=NumGet(hToken)
msgbox.="`nret(" ret ") el(" errorlevel ") le(" A_LastError ")`t`tOpenProcessToken`t`thToken(" hToken ") NumGet_hToken(" NumGet_hToken ")"
DllCall("SetLastError", "UInt", 0)
DllCall("Advapi32.dll\GetTokenInformation"
   , "UInt", hToken               ;//__in       HANDLE TokenHandle,
   , "UInt", TokenInformationClass      ;//__in       TOKEN_INFORMATION_CLASS TokenInformationClass,
   , "Int", 0                     ;//__out_opt  LPVOID TokenInformation,
   , "Int", 0                     ;//__in       DWORD TokenInformationLength,
   , "UIntP", ReturnLength)         ;//__out      PDWORD ReturnLength
msgbox.="`nret(" ret ") el(" errorlevel ") le(" A_LastError ")`t`tGetTokenInformation1`tReturnLength(" ReturnLength ") NumGet(" NumGet(ReturnLength) ")"
sizeof_elevationType:=VarSetCapacity(elevationType, ReturnLength, 0)
DllCall("SetLastError", "UInt", 0)
DllCall("Advapi32.dll\GetTokenInformation"
   , "UInt", hToken               ;//__in       HANDLE TokenHandle,
   , "UInt", TokenInformationClass      ;//__in       TOKEN_INFORMATION_CLASS TokenInformationClass,
   , "UIntP", elevationType         ;//__out_opt  LPVOID TokenInformation,
   , "Int", sizeof_elevationType      ;//__in       DWORD TokenInformationLength,
   , "UIntP", ReturnLength)         ;//__out      PDWORD ReturnLength
;//NumGet_ReturnLength:=NumGet(ReturnLength)
;//NumGet_elevationType:=NumGet(elevationType)
msgbox.="`nret(" ret ") el(" errorlevel ") le(" A_LastError ")`t`tGetTokenInformation2`televationType(" elevationType ")"
;//msgbox, 64, , % "elevationType(" elevationType ")"
;//msgbox, 64, , % "NumGet_elevationType(" NumGet_elevationType ")"
;//GetTokenInformation(hToken, TokenElevationType, &elevationType, sizeof(elevationType), &dwSize)

if ((Debug && elevationType="") || (Debug_Success && elevationType!="")) {
   debuginfo=
   (LTrim
      `n`n*** DEBUG ***
      TokenInformationClass(%TokenInformationClass%)

      elevationType(%elevationType%) sizeof_elevationType(%sizeof_elevationType%)
      ReturnLength(%ReturnLength%) NumGet_ReturnLength(%NumGet_ReturnLength%)
      %msgbox%
      *** /DEBUG ***
   )
}

if (elevationType=TokenElevationTypeDefault) {
   msgbox, 64, , STANDARD USER`n`nTokenElevationTypeDefault - User is not using a split token.%debuginfo%
} else if (elevationType=TokenElevationTypeFull) {
   msgbox, 64, , ADMIN USER WITH ADMIN PERMS`n`nTokenElevationTypeFull - User has a split token, and the process is running elevated.%debuginfo%
} else if (elevationType=TokenElevationTypeLimited) {
   msgbox, 64, , ADMIN USER WITHOUT ADMIN PERMS`n`nTokenElevationTypeLimited - User has a split token, but the process is not running elevated.%debuginfo%
} else {
   msgbox, 16, , UNKNOWN`n`nCouldn't retrieve TokenElevationType - Minimum OS needed for that info is WinVista...%debuginfo%
}

if (hToken) {
   ;//DllCall("SetLastError", "UInt", 0)
   DllCall("CloseHandle", "UInt", hToken)
   ;//msgbox, 64, , % "`nret(" ret ") el(" errorlevel ") le(" A_LastError ")`t`tCloseHandle"
}
return

WindowsConstants:
TOKEN_QUERY:=0x0008
/*
typedef enum  {
  TokenElevationTypeDefault   = 1,
  TokenElevationTypeFull,
  TokenElevationTypeLimited
} TOKEN_ELEVATION_TYPE , *PTOKEN_ELEVATION_TYPE;
*/
TokenElevationTypeDefault:=1
TokenElevationTypeFull:=2
TokenElevationTypeLimited:=3
/*typedef enum _TOKEN_INFORMATION_CLASS {
  TokenUser                    = 1,
  TokenGroups,
  TokenPrivileges,
  TokenOwner,
  TokenPrimaryGroup,
  TokenDefaultDacl,
  TokenSource,
  TokenType,
  TokenImpersonationLevel,
  TokenStatistics,
  TokenRestrictedSids,
  TokenSessionId,
  TokenGroupsAndPrivileges,
  TokenSessionReference,
  TokenSandBoxInert,
  TokenAuditPolicy,
  TokenOrigin,
  TokenElevationType,
  TokenLinkedToken,
  TokenElevation,
  TokenHasRestrictions,
  TokenAccessInformation,
  TokenVirtualizationAllowed,
  TokenVirtualizationEnabled,
  TokenIntegrityLevel,
  TokenUIAccess,
  TokenMandatoryPolicy,
  TokenLogonSid,
  MaxTokenInfoClass
} TOKEN_INFORMATION_CLASS, *PTOKEN_INFORMATION_CLASS;
*/
TokenType:=8
TokenElevationType:=18
return

...I would like to see a reliable version for XP tho...
Back to top
daorc



Joined: 18 Oct 2006
Posts: 169

PostPosted: Thu Mar 04, 2010 4:26 pm    Post subject: Reply with quote

wow! That's great. Thank you! And thanks for the link to the blog entry as well. It's going to take me some time to look through, but it seems to do exactly what I was looking for. I'm sure this will be useful for many other people as well.

Thanks again!

Daorc Smile
Back to top
View user's profile Send private message
Guest






PostPosted: Thu Mar 04, 2010 4:38 pm    Post subject: Re: Does the user (NOT the program) have admin privileges? Reply with quote

Could a mod apply this minor patch to the above code?...(& no need to add a mod note to that post if you do)...

Code:
--- isadmingroup_posted.ahk   Thu Mar 04 11:19:19 2010
+++ isadmingroup.ahk   Thu Mar 04 11:38:19 2010
@@ -1,7 +1,7 @@
 Gosub, WindowsConstants
 
 Debug=1
-Debug_Success=1
+;//Debug_Success=1
 ;//TokenInformationClass:=TokenType
 TokenInformationClass:=TokenElevationType
 
@@ -36,7 +36,7 @@
 ;//msgbox, 64, , % "NumGet_elevationType(" NumGet_elevationType ")"
 ;//GetTokenInformation(hToken, TokenElevationType, &elevationType, sizeof(elevationType), &dwSize)
 
-if ((Debug && elevationType="") || (Debug_Success && elevationType!="")) {
+if ((Debug && !elevationType) || (Debug_Success && elevationType)) {
    debuginfo=
    (LTrim
       `n`n*** DEBUG ***

...(& delete this reply if you want to)...
Back to top
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group