Advertisement
endlesslove_1998

SetPrivilege

Sep 28th, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.00 KB | None | 0 0
  1. ;==================================================================================
  2. ; Function:   SetPrivilege( $privilege, $bEnable )
  3. ; Description:    Enables (or disables) the $privilege on the current process
  4. ;                   (Probably) requires administrator privileges to run
  5. ;
  6. ; Author(s):        Larry (from autoitscript.com's Forum)
  7. ; Notes(s):
  8. ; http://www.autoitscript.com/forum/index.ph...st&p=223999
  9. ;==================================================================================
  10.  
  11. Func SetPrivilege( $privilege, $bEnable )
  12.     Const $TOKEN_ADJUST_PRIVILEGES = 0x0020
  13.     Const $TOKEN_QUERY = 0x0008
  14.     Const $SE_PRIVILEGE_ENABLED = 0x0002
  15.     Local $hToken, $SP_auxret, $SP_ret, $hCurrProcess, $nTokens, $nTokenIndex, $priv
  16.     $nTokens = 1
  17.     $LUID = DLLStructCreate("dword;int")
  18.     If IsArray($privilege) Then    $nTokens = UBound($privilege)
  19.     $TOKEN_PRIVILEGES = DLLStructCreate("dword;dword[" & (3 * $nTokens) & "]")
  20.     $NEWTOKEN_PRIVILEGES = DLLStructCreate("dword;dword[" & (3 * $nTokens) & "]")
  21.     $hCurrProcess = DLLCall("kernel32.dll","hwnd","GetCurrentProcess")
  22.     $SP_auxret = DLLCall("advapi32.dll","int","OpenProcessToken","hwnd",$hCurrProcess[0],   _
  23.             "int",BitOR($TOKEN_ADJUST_PRIVILEGES,$TOKEN_QUERY),"int*",0)
  24.     If $SP_auxret[0] Then
  25.         $hToken = $SP_auxret[3]
  26.         DLLStructSetData($TOKEN_PRIVILEGES,1,1)
  27.         $nTokenIndex = 1
  28.         While $nTokenIndex <= $nTokens
  29.             If IsArray($privilege) Then
  30.                 $priv = $privilege[$nTokenIndex-1]
  31.             Else
  32.                 $priv = $privilege
  33.             EndIf
  34.             $ret = DLLCall("advapi32.dll","int","LookupPrivilegeValue","str","","str",$priv,   _
  35.                     "ptr",DLLStructGetPtr($LUID))
  36.             If $ret[0] Then
  37.                 If $bEnable Then
  38.                     DLLStructSetData($TOKEN_PRIVILEGES,2,$SE_PRIVILEGE_ENABLED,(3 * $nTokenIndex))
  39.                 Else
  40.                     DLLStructSetData($TOKEN_PRIVILEGES,2,0,(3 * $nTokenIndex))
  41.                 EndIf
  42.                 DLLStructSetData($TOKEN_PRIVILEGES,2,DllStructGetData($LUID,1),(3 * ($nTokenIndex-1)) + 1)
  43.                 DLLStructSetData($TOKEN_PRIVILEGES,2,DllStructGetData($LUID,2),(3 * ($nTokenIndex-1)) + 2)
  44.                 DLLStructSetData($LUID,1,0)
  45.                 DLLStructSetData($LUID,2,0)
  46.             EndIf
  47.             $nTokenIndex += 1
  48.         WEnd
  49.         $ret = DLLCall("advapi32.dll","int","AdjustTokenPrivileges","hwnd",$hToken,"int",0,   _
  50.                 "ptr",DllStructGetPtr($TOKEN_PRIVILEGES),"int",DllStructGetSize($NEWTOKEN_PRIVILEGES),   _
  51.                 "ptr",DllStructGetPtr($NEWTOKEN_PRIVILEGES),"int*",0)
  52.         $f = DLLCall("kernel32.dll","int","GetLastError")
  53.     EndIf
  54.     $NEWTOKEN_PRIVILEGES=0
  55.     $TOKEN_PRIVILEGES=0
  56.     $LUID=0
  57.     If $SP_auxret[0] = 0 Then Return 0
  58.     $SP_auxret = DLLCall("kernel32.dll","int","CloseHandle","hwnd",$hToken)
  59.     If Not $ret[0] And Not $SP_auxret[0] Then Return 0
  60.     return $ret[0]
  61. EndFunc   ;==>SetPrivilege
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement