def test_create_process_with_logonw(self): _advapi32.CreateProcessWithLogonW( username=None, domain=None, password=None, command_line='%s -c "import sys; print(sys.executable)"' % sys.executable)
def run_as(self, command): # Importing these only on windows, as they won't exist on linux. import win32api import win32event import win32process from winsys import _advapi32 exit_code = -1 process_handle = None thread_handle = None try: # Open process as that user # https://github.com/tjguk/winsys/blob/master/winsys/_advapi32.py proc_info = _advapi32.CreateProcessWithLogonW( username=self.username, domain=".", password=self.password, command_line=command) process_handle = proc_info.hProcess thread_handle = proc_info.hThread logger.debug("Waiting for process to finish. Timeout: {}ms".format( WAIT_TIMEOUT_IN_MILLISECONDS)) # https://social.msdn.microsoft.com/Forums/vstudio/en-US/b6d6a7ae-71e9-4edb-ac8f # -408d2a41750d/what-events-on-a-process-handle-signal-satisify-waitforsingleobject # ?forum=vcgeneral # Ignoring return code, as we'll use `GetExitCode` to determine the state of the # process later. _ = win32event.WaitForSingleObject( # Waits until the specified object is signaled, or time-out. process_handle, # Ping process handle WAIT_TIMEOUT_IN_MILLISECONDS, # Timeout in milliseconds ) exit_code = win32process.GetExitCodeProcess(process_handle) finally: try: if process_handle is not None: win32api.CloseHandle(process_handle) if thread_handle is not None: win32api.CloseHandle(thread_handle) except Exception as err: logger.error("Close handle error: " + str(err)) return exit_code
def runas(self, command_line, password=core.UNSET, load_profile=False): """Run a command logged on as this user :param command_line: command line to run, quoted as necessary :param password: password; if not supplied, standard Windows prompt :param with_profile: if True, HKEY_CURRENT_USER is loaded [False] """ #~ if not password: #~ password = dialogs.get_password(self.name, self.domain) logon_flags = 0 if load_profile: logon_flags |= _advapi32.LOGON_FLAGS.WITH_PROFILE process_info = _advapi32.CreateProcessWithLogonW( username=self.name, domain=self.domain, password=password, command_line=command_line, logon_flags=logon_flags)