在 Python 中使用 WIN32 API CreateProcessAsUser

wil*_*oup 2 python winapi ctypes process

我一直试图找到一个很好的例子,说明如何在 Python 中使用 CreateProcessAsUser() WIN32 API 和 LogonUser() API,但无济于事。

对此的任何帮助将不胜感激。

Eug*_* C. 5

首先,您应该知道 Windows API 的 Python 扩展与 Windows API 紧密映射。在此用例中,以下链接应该对您非常有用:

如果您将这些文档与 pywin 文档一起学习,您将学到很多东西。

正在编写,请注意,为了使用 CreateProcessAsUser(),您必须拥有权限SE_INCREASE_QUOTA_NAME,可能还有SE_ASSIGNPRIMARYTOKEN_NAME。这些可以通过 secpol.msc > 用户权限分配在您的本地工作站(假设您是管理员)上分配。

要了解这些权限如何映射到 secpol.msc 中显示的权限,请使用以下链接:

现在进入代码:

# First create a token. We're pretending this user actually exists on your local computer or Active Directory domain.
user = "ltorvalds"
pword = "IAMLINUXMAN"
domain = "." # means current domain
logontype = win32con.LOGON32_LOGON_INTERACTIVE
provider = win32con.LOGON32_PROVIDER_WINNT50
token = win32security.LogonUser(user, domain, pword , logontype, provider)

# Now let's create the STARTUPINFO structure. Read the link above for more info on what these can do.
startup = win32process.STARTUPINFO()

# Finally, create a cmd.exe process using the "ltorvalds" token.
appname = "c:\\windows\\system32\\cmd.exe"
priority = win32con.NORMAL_PRIORITY_CLASS
win32process.CreateProcessAsUser(token, appname, None, None, None, True, priority, None, None, startup)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。