def bing(host, output): pattern = re.compile("<li class=\"b_algo\"(.+?)</li>") http = urllib3.PoolManager() try: response = http.request('GET', 'https://www.bing.com/search?q=ip%%3a%s' % host.get_ip(), decode_content=True).data.decode('utf-8') bing_results = re.findall(pattern, response) with open(output, 'a') as out: for item in bing_results: full_resource = re.sub( '\"', '', re.findall('(http(s)?://[^\s]+)', item)[0][0]) out.write(full_resource + '\n') host_name = re.sub('/(.)*', '', re.sub('(http(s)?://)', '', full_resource)) if (host_name == '') or (host_name in host.get_name()): pass else: host.set_name(host_name + '\n') except: logger.error(f"Error connecting with Bing.com") finally: http.clear()
def __get_ssh_key(self): """ Fetch locally stored SSH key. """ try: self.ssh_key = RSAKey.from_private_key_file(self.ssh_key_filepath) logger.info(f'Found SSH key at self {self.ssh_key_filepath}') except SSHException as error: logger.error(error) return self.ssh_key
def upload_single_file(self, file, remote_path): """Upload a single file to a remote directory.""" if self.client is None: self.client = self.__connect() try: self.scp.put(file, recursive=True, remote_path=remote_path) except SCPException as error: logger.error(error) raise error finally: logger.info(f'Uploaded {file} to {remote_path}')
def upload_ssh_key(bot): try: os.system( f'ssh-copy-id -i {bot.ssh_key_filepath} {bot.user}@{bot.host}>/dev/null 2>&1' ) os.system( f'ssh-copy-id -i {bot.ssh_key_filepath}.pub {bot.user}@{bot.host}>/dev/null 2>&1' ) logger.info(f'{bot.ssh_key_filepath} uploaded to {bot.host}') except FileNotFoundError as error: logger.error(error)
def execute_commands(self, commands): """ Execute multiple commands in succession. :param commands: List of unix commands as strings. """ if self.client is None: self.client = self.__connect() for cmd in commands: stdin, stdout, stderr = self.client.exec_command(cmd) stdout.channel.recv_exit_status() response = stdout.readlines() errors = stderr.readlines() for line in response: logger.info(f'INPUT: {cmd} | OUTPUT: {line.strip()}') for line in errors: logger.error(f'INPUT: {cmd} | OUTPUT: {line.strip()}')
def queryAPI(host): try: r2 = requests.get("https://api.hackertarget.com/reverseiplookup/?q=" + host.get_ip()).text if (r2.find("No DNS A records found") == -1) and (r2.find("API count exceeded") == -1 and r2.find("error") == -1): for host_name in r2.split('\n'): if (host_name == "") or (host_name in host.get_name()): pass else: host.set_name(host_name + '\n') except: logger.error(f"Error connecting with HackerTarget.com API") finally: sleep(0.5)
def __connect(self): """ Open connection to remote host. """ try: self.client = SSHClient() self.client.load_system_host_keys() self.client.set_missing_host_key_policy(AutoAddPolicy()) self.client.connect(self.host, username=self.user, key_filename=self.ssh_key_filepath, look_for_keys=True, timeout=5000) self.scp = SCPClient(self.client.get_transport()) except AuthenticationException as error: logger.info( 'Authentication failed: did you remember to create an SSH key?' ) logger.error(error) raise error finally: return self.client