gpt4 book ai didi

python - 在 Python 中使用 WIN32 API CreateProcessAsUser

转载 作者:太空宇宙 更新时间:2023-11-04 10:37:34 24 4
gpt4 key购买 nike

我一直在努力寻找一个很好的例子来说明如何在 Python 中使用 CreateProcessAsUser() WIN32 API 以及 LogonUser() API,但没有成功。

如有任何帮助,我们将不胜感激。

最佳答案

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

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

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

要了解这些特权如何映射到 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)

希望这对您有所帮助。

关于python - 在 Python 中使用 WIN32 API CreateProcessAsUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22615365/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com