def search(data_cb, close_cb, term, roots=('HKU', 'HKLM', 'HKCC'), key=True, name=True, value=True, regex=False, ignorecase=False, first=False, equals=False): data_cb = nowait(data_cb) close_cb = nowait(close_cb) completed = threading.Event() worker = threading.Thread(name='Reg:Search', target=_search, args=(completed, data_cb, close_cb, term, roots, key, name, value, regex, ignorecase, first, equals)) worker.start() def interrupt(): completed.set() worker.join() return interrupt
def run(on_data, on_close, iface=None, bpf=None, timeout=None, count=0): sniffer = SniffSession(nowait(on_data), nowait(on_close), iface, bpf, timeout, count) sniffer.start() return sniffer.nice_name, sniffer.stop
def flush_loop(queue, encoding): try: stdout_write = sys.stdout.write stdout_flush = sys.stdout.flush if type(sys.stdout) is not file: stdout_write = nowait(sys.stdout.write) stdout_flush = nowait(sys.stdout.flush) while True: buf = b"" while True: try: buf += queue.get_nowait() except Empty: break if buf: if encoding: try: buf = buf.decode(encoding) except Exception: pass stdout_write(buf) stdout_flush() time.sleep(0.05) except: print(traceback.format_exc()) raise
def execute(self, close_cb, read_cb=None): if read_cb: read_cb = nowait(read_cb) if close_cb: close_cb = nowait(close_cb) t = threading.Thread(target=self._execute, args=(read_cb, close_cb)) t.daemon = True t.start()
def attach(self, read_cb, close_cb): if self.active: self.read_cb = nowait(read_cb) self.close_cb = nowait(close_cb) if self._buffer: for item in self._buffer: self.read_cb(item) else: close_cb()
def get_shell(self, on_read, on_exit): if self.no_stdin: raise ValueError('You need to specify no_stdin=False to use shell') if self.no_stdor: raise ValueError('You need to specify no_stdor=False to use shell') reader = threading.Thread( target=self.stdor_loop, args=( nowait(on_read), nowait(on_exit))) reader.daemon = True reader.start() return self.stdin
def execute(self, on_exit, on_read): if self.run(): on_exit = nowait(on_exit) on_read = nowait(on_read) self.on_exit = on_exit loop = threading.Thread( target=self.stdor_loop, args=(on_exit, on_read)) loop.daemon = True loop.start() return True else: return False
def execute(self, complete_cb, write_cb=True): ''' Execute process ''' if complete_cb: self.complete_cb = nowait(complete_cb) if write_cb and write_cb is not True: self.write_cb = nowait(write_cb) self.terminate = True try: hProcess, pStdin, pStdout = mexec(self.cmdline, self.raw_pe, write_cb is not None, self.hidden, self.dupHandle) except Exception, e: self.write_cb('[!] memexec failed: {}\n'.format(e)) return False
def search(host, port, user, domain, password, ntlm, data_cb, close_cb, term, roots=('HKU', 'HKLM', 'HKCC'), key=True, name=True, value=True, regex=False, ignorecase=False, first=False, equals=False, timeout=30): conninfo = ConnectionInfo(host, port, user, domain, password, ntlm, timeout=timeout) data_cb = nowait(data_cb) close_cb = nowait(close_cb) completed = threading.Event() worker = threading.Thread(name='Reg:Search', target=_search, args=(conninfo, completed, data_cb, close_cb, term, roots, key, name, value, regex, ignorecase, first, equals)) worker.start() def interrupt(): completed.set() worker.join() return interrupt
def run_cb(self, on_data, on_completed, on_error=None): if not self.terminate: self.terminate = threading.Event() on_completed = nowait(on_completed) search = threading.Thread(target=self._run_thread, args=(on_data, on_completed, on_error)) search.daemon = False search.start()
def ssh_interactive(term, w, h, wp, hp, host, port, user, passwords, private_keys, program, data_cb, close_cb, timeout): private_keys = obtain(private_keys) passwords = obtain(passwords) data_cb = nowait(data_cb) close_cb = nowait(close_cb) ssh_passwords, key_passwords = passwords try: ssh = SSH(host, port, user, ssh_passwords, key_passwords, private_keys, interactive=True, timeout=timeout) if not ssh.connected: raise ValueError( 'No valid credentials found to connect to {}:{} user={}'. format(ssh.host, ssh.port, ssh.user or 'any')) except gaierror as e: raise ValueError('Unable to connect to {}:{} - {}'.format( host, port, e.strerror)) except NoValidConnectionsError: raise ValueError('Unable to connect to {}:{}'.format(host, port)) attach, writer, resizer, closer = ssh.shell(term, w, h, wp, hp, program, data_cb, close_cb) def ssh_close(): closer() ssh.close() return attach, writer, resizer, ssh_close
def ssh_keyscan(hosts, port, key_type, data_cb, close_cb, timeout): data_cb = nowait(data_cb) default_port = '22' if port: default_port += ',' + str(port) def _on_open_port(args): host, port, socket = args try: ssh_conn = Transport(socket) if key_type is not None: new_preferred_keys = [key_type] new_preferred_keys.extend(ssh_conn._preferred_keys) ssh_conn._preferred_keys = tuple(new_preferred_keys) try: ssh_conn.start_client() key = ssh_conn.get_remote_server_key() key_md5 = md5(str(key)).hexdigest() fingerprint = ':'.join( a + b for a, b in zip(key_md5[::2], key_md5[1::2])) data_cb(host, port, True, (key.get_name(), fingerprint, b64encode(str(key)))) finally: ssh_conn.close() except (socket_error, NoValidConnectionsError): data_cb(host, port, None, None) except Exception as e: data_cb(host, port, False, 'Exception: {}: {}'.format(type(e), str(e))) finally: socket.close() closer = scanthread_parse(hosts, default_port, close_cb, on_open_port=_on_open_port, pass_socket=True) return closer.set
def __init__(self, ref): self.create_connection = nowait(ref['create_connection']) self.connect = nowait(ref['connect']) self.on_connected = nowait(ref['on_connected']) self.on_data = nowait(ref['on_data']) self.on_disconnect = nowait(ref['on_disconnect'])
def _ssh_cmd(ssh_cmd, thread_name, arg, hosts, port, user, passwords, private_keys, data_cb, close_cb, timeout): hosts = obtain(hosts) private_keys = obtain(private_keys) default_passwords = obtain(passwords) default_user = user default_port = port data_cb = nowait(data_cb) close_cb = nowait(close_cb) current_connection = [None] closed = Event() def ssh_exec_thread(): for host, port, user, passwords, key_passwords in iter_hosts( hosts, default_passwords, default_port, default_user): if closed.is_set(): break ssh = None try: ssh = SSH(host, port, user, passwords, key_passwords, private_keys, timeout=timeout) if not ssh.connected: data_cb((0, True, ssh.host, ssh.port, ssh.user)) continue current_connection[0] = ssh except (socket_error, NoValidConnectionsError): if ssh: data_cb((0, False, ssh.host, ssh.port, ssh.user)) else: data_cb((0, False, host, port, user)) continue except Exception as e: data_cb( (3, 'Exception: {}: {}\n{}'.format(type(e), str(e), format_exc(limit=20)))) continue conninfo = [4] conninfo.extend(ssh.success_args) data_cb(tuple(conninfo)) def remote_data(data): data_cb((1, data)) if closed.is_set(): break try: if ssh_cmd == SSH.check_output: def remote_exit(status): data_cb((2, status)) ssh_cmd(ssh, arg, on_stdout=remote_data, on_stderr=remote_data, on_exit=remote_exit) else: if ssh_cmd == SSH.upload_file: def remote_exit(status, stdout, stderr): if stdout: data_cb((1, stdout)) if stderr: data_cb((3, stderr)) data_cb((2, status)) src, dst, touch, perm, chown, run, delete = arg ssh_cmd(ssh, *arg, completed_cb=remote_exit) elif ssh_cmd in (SSH.download_file, SSH.download_tar): def remote_exit(status, stderr): if stderr: data_cb((3, stderr)) data_cb((2, status)) ssh_cmd(ssh, arg, remote_data, completed_cb=remote_exit) finally: ssh.close() def ssh_exec_thread_wrap(): try: ssh_exec_thread() finally: try: closed.set() except: pass try: close_cb() except: pass thread = Thread(name=thread_name, target=ssh_exec_thread_wrap) thread.daemon = True thread.start() return closed.set
def size(self, filepath, callback, nonblock=False): filepath = decodepath(filepath) if nonblock: callback = nowait(callback) self._submit_command(self._size, self._expand(filepath), callback)
def transfer(self, filepath, callback, nonblock=False): filepath = decodepath(filepath) if nonblock: callback = nowait(callback) self._submit_command(self._pack_any, self._expand(filepath), callback)
def create_video_streamer(callback, quality): streamer = VideoStreamer(nowait(callback), quality) streamer.start() return streamer