Python Windows服务:调用SetTokenInformation时拒绝访问

Python Windows服务:调用SetTokenInformation时拒绝访问,python,session,windows-services,access-denied,Python,Session,Windows Services,Access Denied,我用Python编写了Windows服务。我需要做的是在指定的用户会话中从此服务启动一些应用程序。服务正在会话0中运行,因此我使用的算法是下一个: 从当前服务获取令牌,该服务具有(实际上应该具有)完整的系统权限 复制它 将我想要的会话ID分配给复制的令牌 使用“CreateProcessAsUser”函数使用复制的令牌运行应用程序 我有同样的东西写在C++窗口服务上,它工作得很好。至于Python,我在尝试调用“SetTokenInformation”函数时捕捉到“拒绝访问”。也许,有人有一个想

我用Python编写了Windows服务。我需要做的是在指定的用户会话中从此服务启动一些应用程序。服务正在会话0中运行,因此我使用的算法是下一个:

  • 从当前服务获取令牌,该服务具有(实际上应该具有)完整的系统权限

  • 复制它

  • 将我想要的会话ID分配给复制的令牌
  • 使用“CreateProcessAsUser”函数使用复制的令牌运行应用程序
  • <>我有同样的东西写在C++窗口服务上,它工作得很好。至于Python,我在尝试调用“SetTokenInformation”函数时捕捉到“拒绝访问”。也许,有人有一个想法,为什么会发生这种情况,或者也许可以通过windows服务中的ID在某些用户会话中共享另一种启动进程的方法

    下面是一些代码:

    class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "TestService"
    _svc_display_name_ = "Test Service"
    
    def __init__(self,args):
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
    
    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
    
    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()
    
    def get_next_msg(self):
        address = ('localhost', 6000)
        listener = Listener(address, authkey='secret password')
        conn = listener.accept()
        msg = conn.recv()
        listener.close()
        return msg
    
    def process_msg(self):
        token = win32security.OpenProcessToken(win32process.GetCurrentProcess(), 
                     win32security.TOKEN_ALL_ACCESS)
    
        duplicated = win32security.DuplicateToken(token, 2)
    
        curr_proc_id = win32process.GetCurrentProcessId()
        curr_session_id = win32ts.ProcessIdToSessionId(curr_proc_id)
    
        # access denied! error code: 5
        win32security.SetTokenInformation(duplicated, win32security.TokenSessionId, curr_session_id)
    
    def main(self):
        while True:
            msg = self.get_next_msg()
            self.process_msg()
    
    
    if __name__ == '__main__':
        win32serviceutil.HandleCommandLine(AppServerSvc)
    

    在windows 10上,以管理员身份运行,使用activepython 2.7 32位最新版本+pywin32最新版本,您的代码可以完美运行


    我建议尝试使用ActivePython并更新pywin32,然后关闭杀毒软件并重试代码。

    解决方案是使用DuplicateTokenEx而不是DuplicateToken:

        duplicated = win32security.DuplicateTokenEx(token, 
                                                    impersonation_lvl, 
                                                    win32security.TOKEN_ALL_ACCESS, 
                                                    win32security.TokenPrimary)
    

    然后SetTokenInformation工作正常,不会出现拒绝访问错误。

    非常感谢您花时间回答这个问题并尝试这个示例。在调用SetTokenInformation后,您是否成功运行了上面的代码并且没有得到任何错误?