AutoHotkey Community

It is currently May 27th, 2012, 4:35 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: March 3rd, 2010, 5:05 pm 
Offline

Joined: October 18th, 2006, 8:07 pm
Posts: 169
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 3rd, 2010, 6:14 pm 
Offline

Joined: December 24th, 2008, 3:25 am
Posts: 1401
Location: :noitacoL
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.


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 3rd, 2010, 9:18 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 10:21 am 
Offline

Joined: October 18th, 2006, 8:07 pm
Posts: 169
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 4th, 2010, 5:19 pm 
This probably needs converted to AutoHotkey (& maybe added as a built-in {A_IsAdminGroup})...
...including the raw code, in case that disappears...

[quote="Non-AutoHotkey-ified C code"][/quote]
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...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: March 4th, 2010, 5:26 pm 
Offline

Joined: October 18th, 2006, 8:07 pm
Posts: 169
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 :)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: March 4th, 2010, 5:38 pm 
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)...


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Mickers, rbrtryn and 67 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group