Exemple #1
0
 async def do_scan(self, module: Script,target: Union[dict]) -> Iterable[dict]:
     records = []
     func_name = self.pm.func_name
     parameter = self.pm.parameter
     flag = -1
     try:
         poc = module.POC()
         poc.initialize(target['host'], target['port'], target['url'], parameter)
         func = getattr(poc, func_name)
         logger.debug(
             "Running %s:%s for %s:%s" % (module.__name__, func_name, poc.target_host, poc.target_port))
         async with async_timeout.timeout(timeout=int(conf['basic']['timeout'])):
             await func()
             flag = poc.flag
             if poc.url != None:
                 target['url'] = poc.url
     except AttributeError as e:
         if 'has no attribute \'POC\'' in get_safe_ex_string(e):
             logger.error('Invalid POC script, Please check the script: %s' % module.__name__, )
         elif '\'POC\' object has no attribute' in get_safe_ex_string(e):
             logger.error('%s, Please check it in the script: %s' % (e, module.__name__))
         elif 'Function is not exist.' in get_safe_ex_string(e):
             logger.error(
                 'Function is not exist, Please check \'%s\' in the script: %s' % (
                     func_name, module.__name__,))
         else:
             self.errmsg = traceback.format_exc()
             logger.error(self.errmsg)
             logger.error("%s %s:%s for %s:%d" % (e, module.__name__, func_name, target['host'], target['port']))
         self._error_task_count += 1
     except KeyError as e:
         logger.error("Missing parameters: %s, please load parameters by -p. For example. -p %s=value" % (
             e, str(e).replace('\'', '')))
         self._error_task_count += 1
     except (ConnectionResetError, ConnectionAbortedError, TimeoutError):
         flag = poc.flag
     except (CancelledError, ConnectionRefusedError, OSError):
         if target['status'] != None:
             target['status'] -= 1
         else:
             target['status'] = -1
     except Exception:
         self._error_task_count += 1
         errmsg = traceback.format_exc()
         logger.error("Error for " + target['host'] + ":" + str(target['port']) + "\r\n"+ errmsg)
     finally:
         if conf.VERBOSE or flag >= 0:
             if poc.flag >= 0:
                 self._find_task_count += 1
                 if module.__name__ == 'script.info.port_scan':
                     target['status'] = 5
                     if  len(poc.res) == 0 :
                         poc.res = [{"info": None , "key": "port scan"}]
                     for res in poc.res:
                         target['service'] = res['info']
                         await self.vul_targets.put(target)
                 else:
                     target['status'] = 3
             data = {
                 "id": target['id'],
                 "flag": poc.flag,
                 'module_name': module.__name__,
                 'func_name': func_name,
                 "name": poc.name,
                 'target_host': poc.target_host,
                 'target_port': poc.target_port,
                 'url': poc.url,
                 'base_url': poc.base_url,
                 'level': poc.level,
                 'type': poc.type,
                 "req": poc.req,
                 "res": poc.res,
                 "other": poc.other,
             }
             self.hashdb.insert(data)
             self.hashdb.flush()
             print_dic(data)
             records.append(data)
         logger.debug("Ending  %s:%s for %s:%s" % (module.__name__, func_name, poc.target_host, poc.target_port))
     return records