#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[5][2] = [ _
["Everyone", $SE_GROUP_MANDATORY], _
["Users", $SE_GROUP_MANDATORY], _
["Administrators", $SE_GROUP_MANDATORY], _
["SYSTEM", $SE_GROUP_MANDATORY], _
["Restricted", $SE_GROUP_MANDATORY] _
]
Local $aPrivileges[10][2] = [ _
["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