Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;==================================================================================
- ; Function: SetPrivilege( $privilege, $bEnable )
- ; Description: Enables (or disables) the $privilege on the current process
- ; (Probably) requires administrator privileges to run
- ;
- ; Author(s): Larry (from autoitscript.com's Forum)
- ; Notes(s):
- ; http://www.autoitscript.com/forum/index.ph...st&p=223999
- ;==================================================================================
- Func SetPrivilege( $privilege, $bEnable )
- Const $TOKEN_ADJUST_PRIVILEGES = 0x0020
- Const $TOKEN_QUERY = 0x0008
- Const $SE_PRIVILEGE_ENABLED = 0x0002
- Local $hToken, $SP_auxret, $SP_ret, $hCurrProcess, $nTokens, $nTokenIndex, $priv
- $nTokens = 1
- $LUID = DLLStructCreate("dword;int")
- If IsArray($privilege) Then $nTokens = UBound($privilege)
- $TOKEN_PRIVILEGES = DLLStructCreate("dword;dword[" & (3 * $nTokens) & "]")
- $NEWTOKEN_PRIVILEGES = DLLStructCreate("dword;dword[" & (3 * $nTokens) & "]")
- $hCurrProcess = DLLCall("kernel32.dll","hwnd","GetCurrentProcess")
- $SP_auxret = DLLCall("advapi32.dll","int","OpenProcessToken","hwnd",$hCurrProcess[0], _
- "int",BitOR($TOKEN_ADJUST_PRIVILEGES,$TOKEN_QUERY),"int*",0)
- If $SP_auxret[0] Then
- $hToken = $SP_auxret[3]
- DLLStructSetData($TOKEN_PRIVILEGES,1,1)
- $nTokenIndex = 1
- While $nTokenIndex <= $nTokens
- If IsArray($privilege) Then
- $priv = $privilege[$nTokenIndex-1]
- Else
- $priv = $privilege
- EndIf
- $ret = DLLCall("advapi32.dll","int","LookupPrivilegeValue","str","","str",$priv, _
- "ptr",DLLStructGetPtr($LUID))
- If $ret[0] Then
- If $bEnable Then
- DLLStructSetData($TOKEN_PRIVILEGES,2,$SE_PRIVILEGE_ENABLED,(3 * $nTokenIndex))
- Else
- DLLStructSetData($TOKEN_PRIVILEGES,2,0,(3 * $nTokenIndex))
- EndIf
- DLLStructSetData($TOKEN_PRIVILEGES,2,DllStructGetData($LUID,1),(3 * ($nTokenIndex-1)) + 1)
- DLLStructSetData($TOKEN_PRIVILEGES,2,DllStructGetData($LUID,2),(3 * ($nTokenIndex-1)) + 2)
- DLLStructSetData($LUID,1,0)
- DLLStructSetData($LUID,2,0)
- EndIf
- $nTokenIndex += 1
- WEnd
- $ret = DLLCall("advapi32.dll","int","AdjustTokenPrivileges","hwnd",$hToken,"int",0, _
- "ptr",DllStructGetPtr($TOKEN_PRIVILEGES),"int",DllStructGetSize($NEWTOKEN_PRIVILEGES), _
- "ptr",DllStructGetPtr($NEWTOKEN_PRIVILEGES),"int*",0)
- $f = DLLCall("kernel32.dll","int","GetLastError")
- EndIf
- $NEWTOKEN_PRIVILEGES=0
- $TOKEN_PRIVILEGES=0
- $LUID=0
- If $SP_auxret[0] = 0 Then Return 0
- $SP_auxret = DLLCall("kernel32.dll","int","CloseHandle","hwnd",$hToken)
- If Not $ret[0] And Not $SP_auxret[0] Then Return 0
- return $ret[0]
- EndFunc ;==>SetPrivilege
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement