如何以Administrator账户执行外部程序?附代码
本帖最后由 qq4045728 于 2015-5-22 17:24 编辑如何以Administrator账户执行外部程序?#RequireAdmin是不可行的
因为程序是作为服务启动的,权限账户是SYSTEM,想通过Administrator 执行外部一个程序,求方法
下面的代码可以在任务管理器看到程序已经是以Administrator 运行,但是还是最高权限,是不是代码有误?
#include <RunAsEx.au3>
Example1()
; 创建一个用户名为guest,但实际权限却是SYSTEM的注册表进程(regedit.exe)。
Func Example1()
Local $hImpersonationToken = RxImpersonateSystemContext()
If $hImpersonationToken = 0 Then
ConsoleWrite(StringFormat("Error impersonating system context=%d:%d\n", @error, @extended))
Exit 1
EndIf
If Not RxAdjustTokenPriv($hImpersonationToken, $SE_CREATE_TOKEN_PRIVILEGE, 2) Then
ConsoleWrite(StringFormat("error enabling CreateToken privilege=%d\n", @error))
EndIf
If Not RxAdjustTokenPriv($hImpersonationToken, $SE_ASSIGN_PRIMARY_TOKEN_PRIVILEGE, 2) Then
ConsoleWrite(StringFormat("error enabling AssignPrimaryToken privilege=%d\n", @error))
EndIf
If Not RxAdjustTokenPriv($hImpersonationToken, $SE_TCB_PRIVILEGE, 2) Then
ConsoleWrite(StringFormat("error enabling Tcb privilege=%d\n", @error))
EndIf
Local $aSids = [ _
["Everyone", $SE_GROUP_MANDATORY], _
["Users", $SE_GROUP_MANDATORY], _
["Administrators", $SE_GROUP_MANDATORY], _
["SYSTEM", $SE_GROUP_MANDATORY], _
["Restricted", $SE_GROUP_MANDATORY] _
]
Local $aPrivileges = [ _
["SeChangeNotifyPrivilege", 2], _
["SeImpersonatePrivilege", 2], _
["SeShutdownPrivilege", 0], _
["SeTakeOwnershipPrivilege", 0], _
["SeRestorePrivilege", 0], _
["SeBackupPrivilege", 0], _
["SeDebugPrivilege", 0], _
["SeTcbPrivilege", 3], _
["SeCreateTokenPrivilege", 0], _
["SeAssignPrimaryTokenPrivilege", 3] _
]
; 进程所属用户的SID。
Local $pTrusteeSid = RxCreateWellKnownSid($WinAccountAdministratorSid)
ConsoleWrite(StringFormat("Trustee(Administrator)=0x%016x\n", $pTrusteeSid))
Local $hToken = RxCreateToken($pTrusteeSid, $aSids, 0, $aPrivileges, $RAE_SESSION_CURRENT)
ConsoleWrite(StringFormat("Token=0x%08x, Error=0x%08x\n", $hToken, @error))
; 创建进程。
Local $tProcessInfo = RunAsEx($hToken, "regedit.exe")
ConsoleWrite(StringFormat("RunAsEx, error=%d, process=%d\n", @error, DllStructGetData($tProcessInfo, 3)))
; 等待进程运行结束。
DllCall("Kernel32.dll", "long", "WaitForSingleObject", "handle", DllStructGetData($tProcessInfo, "hProcess"), "long", -1)
RxCloseHandle(DllStructGetData($tProcessInfo, "hProcess"))
RxCloseHandle(DllStructGetData($tProcessInfo, "hThread"))
RxCloseHandle($hToken)
RxBufferFree($pTrusteeSid)
RxCloseHandle($hImpersonationToken)
RxRevertToSelf()
ConsoleWrite(@CRLF)
EndFunc ;==>Example1
页:
[1]