def do_run(self): pipe_handle = None while not self.exit_loop: if not pipe_handle: try: pipe_handle = self.CreatePipeHandle() except Exception as e: log("CreatePipeHandle()", exc_info=True) log.error("Error: failed to create named pipe") log.error(" at path '%s'", self.pipe_name) log.error(" %s", e) return log("CreatePipeHandle()=%#x", pipe_handle) if c_long(pipe_handle).value==INVALID_HANDLE_VALUE: log.error("Error: invalid handle for named pipe '%s'", self.pipe_name) e = GetLastError() log.error(" '%s' (%i)", FormatMessageSystem(e).rstrip("\n\r."), e) return event = CreateEventA(None, True, False, None) overlapped = OVERLAPPED() overlapped.hEvent = event overlapped.Internal = None overlapped.InternalHigh = None overlapped.union.Pointer = None r = ConnectNamedPipe(pipe_handle, overlapped) log("ConnectNamedPipe()=%s", r) if self.exit_loop: break if r==0: e = GetLastError() log("GetLastError()=%s (%i)", FormatMessageSystem(e).rstrip("\n\r"), e) if e==ERROR_PIPE_CONNECTED: pass elif e==ERROR_IO_PENDING: while not self.exit_loop: r = WaitForSingleObject(event, INFINITE) log("WaitForSingleObject(..)=%s", WAIT_STR.get(r, r)) if r==WAIT_TIMEOUT: continue if r==0: break log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" %s", WAIT_STR.get(r, r)) CloseHandle(pipe_handle) pipe_handle = None break else: log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" error %s", e) CloseHandle(pipe_handle) pipe_handle = None if self.exit_loop: break #from now on, the pipe_handle will be managed elsewhere: if pipe_handle: self.new_connection_cb("named-pipe", self, pipe_handle) pipe_handle = None if pipe_handle: self.close_handle(pipe_handle)
def do_run(self): while not self.exit_loop: pipe_handle = None try: pipe_handle = self.CreatePipeHandle() except Exception as e: log("CreatePipeHandle()", exc_info=True) log.error("Error: failed to create named pipe") log.error(" at path '%s'", self.pipe_name) log.error(" %s", e) return log("CreatePipeHandle()=%s", pipe_handle) if pipe_handle == INVALID_HANDLE_VALUE: log.error("Error: invalid handle for named pipe '%s'", self.pipe_name) return event = CreateEventA(None, True, False, None) overlapped = OVERLAPPED() overlapped.hEvent = event overlapped.Internal = None overlapped.InternalHigh = None overlapped.union.Pointer = None r = ConnectNamedPipe(pipe_handle, overlapped) log("ConnectNamedPipe()=%s", r) if not r and not self.exit_loop: r = WaitForSingleObject(event, INFINITE) log("WaitForSingleObject(..)=%s", WAIT_STR.get(r, r)) if r: log("do_run() error on WaitForSingleObject", exc_info=True) log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) CloseHandle(pipe_handle) continue if self.exit_loop: CloseHandle(pipe_handle) break if r == 0 and False: e = GetLastError() if e == ERROR_PIPE_CONNECTED: pass else: log.error("Error: cannot connect to named pipe '%s'", self.pipe_name) log.error(" error %s", e) CloseHandle(pipe_handle) continue #from now on, the pipe_handle will be managed elsewhere: self.new_connection_cb(self, pipe_handle)
def close(self): log("%s.close()", self) ph = self.pipe_handle if not ph: return self.pipe_handle = None def _close_err(fn, e): l = log.error try: code = e[0] except (IndexError, TypeError): #python3? code = 0 if code == ERROR_PIPE_NOT_CONNECTED: l = log.debug l("Error: %s(%s) %i: %s", fn, ph, code, e) try: FlushFileBuffers(ph) except Exception as e: _close_err("FlushFileBuffers", e) try: DisconnectNamedPipe(ph) except Exception as e: _close_err("DisconnectNamedPipe", e) try: CloseHandle(ph) except Exception as e: _close_err("CloseHandle", e)
def run(self): log("%s.run()", self) try: self.do_run() except Exception: log.error("Error: named pipe '%s'", self.pipe_name, exc_info=True) tp = self.token_process if tp: self.token_process = None CloseHandle(tp) self.tokens = [] self.security_attributes = None self.security_descriptor = None
def check(self, password): token = HANDLE() domain = '' #os.environ.get('COMPUTERNAME') status = LogonUser(self.username, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, byref(token)) error = GetLastError() if status: CloseHandle(token) return True log.error("Error: win32 authentication failed:") log.error(" %s", FormatError(error)) return False
def check(self, password): token = HANDLE() domain = '' #os.environ.get('COMPUTERNAME') password_str = bytestostr(password) if LOG_CREDENTIALS: log("LogonUser(%s, %s, %s, CLEARTEXT, DEFAULT, %#x)", self.username, domain, password_str, addressof(token)) status = LogonUser(self.username, domain, password_str, LOGON32_LOGON_NETWORK_CLEARTEXT, LOGON32_PROVIDER_DEFAULT, byref(token)) log("LogonUser(..)=%#x", status) if status: CloseHandle(token) return True log.error("Error: win32 authentication failed:") log.error(" %s", FormatError(GetLastError())) return False
def get_owner_info(owner, our_window): if not owner: return "unknown" if owner==our_window: return "our window (hwnd=%#x)" % our_window pid = DWORD(0) GetWindowThreadProcessId(owner, byref(pid)) if not pid: return "unknown (hwnd=%#x)" % owner #log("get_owner_info(%#x) pid=%s", owner, pid.value) proc_handle = OpenProcess(PROCESS_QUERY_INFORMATION, False, pid) if not proc_handle: return "pid %i (hwnd=%#x)" % (pid.value, owner) try: size = DWORD(256) process_name = create_string_buffer(size.value+1) if not QueryFullProcessImageNameA(proc_handle, 0, process_name, byref(size)): return "pid %i" % pid.value return "'%s' with pid %s (hwnd=%#x)" % (bytestostr(process_name.value), pid.value, owner) finally: CloseHandle(proc_handle)
def close_handle(self, pipe_handle): try: log("CloseHandle(%#x)", pipe_handle) CloseHandle(pipe_handle) except Exception: log("CloseHandle(%#x)", pipe_handle, exc_info=True)