代码如下:
class ImpersonateWin32Sec(object):
def __init__(self, domain, username, password):
self.username = username
self.password = password
self.domain = domain
self.handle = None
def __enter__(self):
self.handle = win32security.LogonUser(self.username, self.domain, self.password, win32con.LOGON32_LOGON_INTERACTIVE, win32con.LOGON32_PROVIDER_DEFAULT)
win32security.ImpersonateLoggedOnUser(self.handle)
def __exit__(self, *args):
win32security.RevertToSelf()
self.handle.Close()
with ImpersonateWin32Sec("domain", "altuser", "password"):
prc = subprocess.Popen(cmdLine, cwd = "C:\\Temp", stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE, env = env, shell = shell, creationflags = CREATE_NO_WINDOW)
stdOut, stdErr = prc.communicate(timeout = 60)
我试图以不同的用户身份运行程序。它似乎只能部分工作,因为有些命令失败了。以下3个命令表明,环境的某些部分仍然适用于原始用户:
^{pr2}$我的代码有什么遗漏/错误?用户找到了一个替代gedoni的主令牌,而不是为某个gedoni创建的信息?这会是问题吗?如果是,如何获得主令牌?一个代码示例将不胜感激。在
你好,马丁
我终于知道怎么做了。这是相当复杂的,我不得不合并几个例子中的代码(其中一些是用C编写的)。以下示例在作为网络服务或系统用户执行时有效。不管是在用户会话还是会话0中执行。在
代码如下:
相关问题 更多 >
编程相关推荐