def getDesktopWindow(): console_id = win32ts.WTSGetActiveConsoleSessionId() if console_id == 0xffffffff: # User not logged in right now? logging.info("No console user") return None hwnd = None # Get processes running on this console svr = win32ts.WTSOpenServer(".") ps_list = win32ts.WTSEnumerateProcesses(svr, 1, 0) for ps in ps_list: logging.info("PS " + str(ps)) win32ts.WTSCloseServer(svr) # sessions = win32ts.WTSEnumerateSessions(None, 1, 0) # for session in win32ts.WTSEnumerateSessions(win32ts.WTS_CURRENT_SERVER_HANDLE, 1, 0): # print "SessionId: %s" % session['SessionId'] # print "\tWinStationName: %s" % session['WinStationName'] # print "\tState: %s" % session['State'] # print # if session["WinStationName"] == "Console": # cs = session #if cs is not None: # # Get process list for this session return hwnd
def runScreenShotApp2_old(self): console_id = win32ts.WTSGetActiveConsoleSessionId() if console_id == 0xffffffff: # User not logged in right now? logging.info("No console user") return None dc = None logging.info("Got console: " + str(console_id)) # Get processes running on this console svr = win32ts.WTSOpenServer(".") user_token = win32ts.WTSQueryUserToken(console_id) logging.info("User Token " + str(user_token)) # hwnd = win32gui.GetDC(win32con.HWND_DESKTOP) # win32gui.GetDesktopWindow() # dc = ctypes.windll.user32.GetDC(win32con.HWND_DESKTOP) # logging.info("DC before impersonation " + str(dc)) # win32gui.ReleaseDC(win32con.HWND_DESKTOP, dc) # Switch to the user win32security.ImpersonateLoggedOnUser(user_token) logging.info("Impersonating " + win32api.GetUserName()) app_path = os.path.dirname( os.path.dirname( os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) cmd = os.path.join(app_path, "sshot\\dist\\sshot.exe") logging.info("Running sshot app " + cmd) logging.info(os.system(cmd)) # hwnd = ctypes.windll.user32.GetDC(win32con.HWND_DESKTOP) # logging.info("HWND after impersonation " + str(hwnd)) # ps_list = win32ts.WTSEnumerateProcesses(svr, 1, 0) # for ps in ps_list: # logging.info("PS " + str(ps)) win32ts.WTSCloseServer(svr) # Revert back to normal user win32security.RevertToSelf() user_token.close() return
def close(self): win32ts.WTSCloseServer(self.handle)
def runScreenShotApp(self): global DISABLE_SSHOT if DISABLE_SSHOT is True: return # Get the session id for the console session_id = win32ts.WTSGetActiveConsoleSessionId() if session_id == 0xffffffff: # User not logged in right now? logging.info("No console user") return None # logging.info("Got Console: " + str(session_id)) # Login to the terminal service to get the user token for the console id svr = win32ts.WTSOpenServer(".") user_token = win32ts.WTSQueryUserToken(session_id) # logging.info("User Token " + str(user_token)) # Copy the token user_token_copy = win32security.DuplicateTokenEx( user_token, win32security.SecurityImpersonation, win32security.TOKEN_ALL_ACCESS, win32security.TokenPrimary) # Put this token in the logged in session win32security.SetTokenInformation(user_token_copy, win32security.TokenSessionId, session_id) # Switch to the user # win32security.ImpersonateLoggedOnUser(user_token) # logging.info("Impersonating " + win32api.GetUserName()) # Run the screen shot app # app_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))) # cmd = os.path.join(app_path, "sshot\\dist\\sshot.exe") cmd = os.path.join(ROOT_FOLDER, "ope_laptop_binaries\\sshot\\sshot.exe" ) # "c:\\programdata\\ope\\bin\\sshot.exe" # cmd = "cmd.exe" logging.info("Running sshot app " + cmd) # Use win create process function si = win32process.STARTUPINFO() si.dwFlags = win32process.STARTF_USESHOWWINDOW si.wShowWindow = win32con.SW_NORMAL # si.lpDesktop = "WinSta0\Default" si.lpDesktop = "" # Setup envinroment for the user environment = win32profile.CreateEnvironmentBlock(user_token, False) try: ( hProcess, hThread, dwProcessId, dwThreadId ) = win32process.CreateProcessAsUser( user_token_copy, None, # AppName (really command line, blank if cmd line supplied) "\"" + cmd + "\"", # Command Line (blank if app supplied) None, # Process Attributes None, # Thread Attributes 0, # Inherits Handles win32con. NORMAL_PRIORITY_CLASS, # or win32con.CREATE_NEW_CONSOLE, environment, # Environment os.path.dirname(cmd), # Curr directory si) # Startup info # logging.info("Process Started: " + str(dwProcessId)) # logging.info(hProcess) except Exception as e: logging.info("Error launching process: " + str(e)) # logging.info(os.system(cmd)) # Return us to normal security # win32security.RevertToSelf() # Cleanup win32ts.WTSCloseServer(svr) user_token.close() user_token_copy.close() return
def CloseServer(server_h): return Ts.WTSCloseServer(server_h)