1

我需要以用户身份创建进程。但是在创建过程中会出现一些错误。错误是:1314 CreateProcessAsUser A required privilege is not held by the client.

import win32api
import win32security
import win32process
import pywintypes


def attempt_to_logon():
    username = "abcdef"
    password = "123456"
    try:
        hUser = win32security.LogonUser(username, None,
                                    password, win32security.LOGON32_LOGON_INTERACTIVE,
                                    win32security.LOGON32_PROVIDER_DEFAULT)
    except win32security.error:
        print "unable to logon"
        return None
    return hUser


def run_as_user(hUser):
    startup = win32process.STARTUPINFO()
    startup.dwFlags = win32process.STARTF_USESHOWWINDOW
    startup.wShowWindow = win32con.SW_SHOW
    startup.lpDesktop = 'winsta0\default'

    try:
        result = win32process.CreateProcessAsUser(hUser,
                                              None,  # appName
                                              "c:\\windows\\notepad.exe",  # commandLine
                                              None,  # process attrs
                                              None,  # thread attrs
                                              0,  # inherit handles
                                              0,  # create flags
                                              None,  # new environment dict
                                              None,  # current directory
                                              startup)  # startup info

    except pywintypes.error, (errcode, method, msg):
        print errcode, method, msg

def AdjustPriv(priv, enable=1):
    flags = win32security.TOKEN_ADJUST_PRIVILEGES | win32security.TOKEN_QUERY
    htoken = win32security.OpenProcessToken(
    win32api.GetCurrentProcess(), flags)
    id = win32security.LookupPrivilegeValue(None, priv)
    if enable:
        newPriv = [(id, win32security.SE_PRIVILEGE_ENABLED)]
    else:
        newPriv = [(id, 0)]
    win32security.AdjustTokenPrivileges(htoken, 0, newPriv)

if __name__ == "__main__":
    AdjustPriv(win32security.SE_TCB_NAME)
    AdjustPriv(win32security.SE_ASSIGNPRIMARYTOKEN_NAME)
    AdjustPriv(win32security.SE_INCREASE_QUOTA_NAME)
    hUser = attempt_to_logon()
    run_as_user(hUser)

run_as_user当我尝试创建进程时,错误出现在函数中。我能够阅读,我必须设置SE_TCB_NAMESE_ASSIGNPRIMARYTOKEN_NAME以及E_INCREASE_QUOTA_NAME使用 CreateProcessAsUser。所以我不知道问题出在哪里。

4

0 回答 0