def get_ports_options(config_file): if not ConfigParser.scan_options: try: cfg = ConfigParser.parser(config_file) except Exception, err: raise FlashLightExceptions( "Error when parsing {0}: {1}".format( config_file, str(err))) try: tcp_ports = "-sS -p T:{0}".format(cfg["tcp_ports"]) except: tcp_ports = None try: udp_ports = "U:{0} -sU".format(cfg["udp_ports"]) except: udp_ports = None if tcp_ports and udp_ports: ConfigParser.scan_options = "{0},{1}".format( tcp_ports, udp_ports) elif tcp_ports: ConfigParser.scan_options = tcp_ports elif udp_ports: ConfigParser.scan_options = "-p {0}".format(udp_ports)
def __init__(self, config_file, output_dir, ip_file_to_scan, nmap_optimize, scan_type): self._ip_file_to_scan = ip_file_to_scan try: self._scan_options = ConfigParser.get_scripts_options(config_file) except Exception, err: raise FlashLightExceptions(str(err))
def get_scripts_options(config_file): script_options = None try: ports_options = ConfigParser.get_ports_options(config_file) except Exception, err: raise FlashLightExceptions(str(err))
def parser(config_file): if not ConfigParser.result: try: with open(config_file, 'r') as stream: cfg = yaml.load(stream) except IOError: raise FlashLightExceptions( "{0} cannot be opened !!!".format(config_file)) except Exception, err: raise FlashLightExceptions(str(err)) for section in cfg: ConfigParser.result[section] = ','.join([ value.strip() for value in ''.join( [value for value in cfg[section]]).split(',') ])
def get_screen_ports(config_file): cfg = ConfigParser.parser if ConfigParser.result else ConfigParser.parser( config_file) try: return cfg["screen_ports"] except KeyError: return ConfigParser.default_ports except: raise FlashLightExceptions(str(err))
def _run(self, result_file, logger): self.__ip_file_to_scan.seek(0) gnmap_file = "{0}.gnmap".format(self.__output_file) cmd = "{0} -oA {1}".format(self._proc_cmd, self.__output_file) logger._logging("START: Nmap Ping Scan") logger._logging("CMD - Ping Scan: {0}".format(cmd)) proc = subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE,).communicate() try: with open(gnmap_file, "r") as fd: result_file.write("\n".join([ re.search(self.__host_up, line).groups()[0] for line in fd if re.search(self.__host_up, line) ])) except Exception, err: raise FlashLightExceptions(str(err))
try: ports_options = ConfigParser.get_ports_options(config_file) except Exception, err: raise FlashLightExceptions(str(err)) if ConfigParser.result["scripts"]: try: script_options = "--script=default,{0}".format( ConfigParser.result["scripts"]) except: script_options = "--script=default" else: try: cfg = ConfigParser.parser(config_file) except Exception, err: raise FlashLightExceptions(str(err)) try: script_options = "--script=default,{0}".format(cfg["scripts"]) except: script_options = "--script=default" return "{0} {1}".format(ports_options, script_options) @staticmethod def get_screen_ports(config_file): cfg = ConfigParser.parser if ConfigParser.result else ConfigParser.parser( config_file) try: