Пример #1
0
    def from_dict(d):
        try:
            from nmap_scan.Scripts.ScriptParser import parse

            return parse(Script.dict_to_xml(d, False))
        except NmapXMLParserException:
            raise NmapDictParserException()
Пример #2
0
    def __parse_xml(self):

        logging.info('Parsing Port')
        attr = self.__xml.attrib
        self.__protocol = attr['protocol']
        self.__port = int(attr['portid'])
        logging.debug('Port: "{port}"'.format(port=self.__port))
        logging.debug('Protocol: "{protocol}"'.format(protocol=self.__protocol))
        logging.debug('Owner: "{owner}"'.format(owner=self.__owner))
        self.__state = State(self.__xml.find('state'), False)
        self.__service = Service(self.__xml.find('service'), False)

        owner = self.__xml.find('owner')
        if None != owner:
            self.__owner = owner.attrib['name']

        for script_xml in self.__xml.findall('script'):
            script = parse(script_xml, False)
            existing_script = self.__scripts.get(script.get_id(), None)
            if None == existing_script:
                self.__scripts[script.get_id()] = script
            elif isinstance(existing_script, list):
                self.__scripts[script.get_id()].append(script)
            else:
                self.__scripts[script.get_id()] = [existing_script, script]
Пример #3
0
    def test_has_script_three_times(self, filepath, expected):
        xml = self.create_xml(filepath)
        e = self.create_instance(xml)
        sxml = self.create_xml(expected)
        script = parse(sxml)

        assert 1 == len(e.get_host_scripts())
        assert e.has_hostscript(script.get_id())
        assert 3 == len(e.get_hostscript(script.get_id()))
        assert 3 == len(e.get_host_scripts()[script.get_id()])
        for hs in e.get_hostscript(script.get_id()):
            assert hs.equals(script)
Пример #4
0
 def test_post_script(self, filepath, expected):
     xml = self.create_xml(filepath)
     e = self.create_instance(xml)
     expected_objects = []
     for ex in expected:
         expected_objects.append(parse(self.create_xml(ex)))
     hosts = e.get_post_scripts()
     assert len(expected) == len(hosts)
     for ex in expected_objects:
         assert e.has_pre_script(ex.get_id())
         assert ex.equals(e.get_post_script(ex.get_id()))
         exist = False
         for c in e.get_post_scripts():
             if ex.equals(e.get_post_scripts()[c]):
                 exist = True
                 break
         assert exist
Пример #5
0
 def create_instance(self, xml):
     return parse(xml)
Пример #6
0
    def __parse_xml(self):
        self.validate(self.__xml)
        nmaprun = self.get_xml().attrib
        self.__scanner = nmaprun['scanner']
        self.__scanner_args = nmaprun.get('args', None)
        self.__start = int(
            nmaprun['start']) if None != nmaprun.get('start', None) else None
        self.__startstr = nmaprun.get('startstr', None)
        self.__version = nmaprun['version']
        self.__xmloutputversion = nmaprun.get('xmloutputversion', None)
        self.__profile_name = nmaprun.get('profile_name', None)

        for host_xml in self.get_xml().findall('host'):
            self.__hosts.append(Host(host_xml, False))

        verbose_xml = self.get_xml().find('verbose')
        if None != verbose_xml:
            self.__verbose_level = int(
                verbose_xml.attrib['level']
            ) if None != verbose_xml.attrib.get('level') else None

        debugging_xml = self.get_xml().find('debugging')
        if None != debugging_xml:
            self.__debugging_level = int(
                debugging_xml.attrib['level']
            ) if None != debugging_xml.attrib.get('level') else None

        run_stats_xml = self.get_xml().find('runstats')
        if None != run_stats_xml:
            self.__run_stats = RunStats(run_stats_xml, False)

        for scaninfo_xml in self.get_xml().findall('scaninfo'):
            self.__scaninfos.append(ScanInfo(scaninfo_xml, False))

        for target_xml in self.__xml.findall('target'):
            self.__targets.append(Target(target_xml, False))

        for output_xml in self.__xml.findall('output'):
            self.__outputs.append(Output(output_xml, False))

        for task_progress_xml in self.__xml.findall('taskprogress'):
            self.__task_progresses.append(
                TaskProgress(task_progress_xml, False))

        for task_begin_xml in self.__xml.findall('taskbegin'):
            self.__task_begins.append(TaskBegin(task_begin_xml, False))

        for task_end_xml in self.__xml.findall('taskend'):
            self.__task_ends.append(TaskEnd(task_end_xml, False))

        for hosthint_xml in self.__xml.findall('hosthint'):
            self.__host_hints.append(HostHint(hosthint_xml, False))

        for prescript_xml in self.__xml.findall('prescript'):
            for script_xml in prescript_xml.findall('script'):
                script = parse(script_xml, False)
                existing_script = self.__pre_scripts.get(script.get_id(), None)
                if None == existing_script:
                    self.__pre_scripts[script.get_id()] = script
                elif isinstance(existing_script, list):
                    self.__pre_scripts[script.get_id()].append(script)
                else:
                    self.__pre_scripts[script.get_id()] = [
                        existing_script, script
                    ]

        for postscript_xml in self.__xml.findall('postscript'):
            for script_xml in postscript_xml.findall('script'):
                script = parse(script_xml, False)
                existing_script = self.__post_scripts.get(
                    script.get_id(), None)
                if None == existing_script:
                    self.__post_scripts[script.get_id()] = script
                elif isinstance(existing_script, list):
                    self.__post_scripts[script.get_id()].append(script)
                else:
                    self.__post_scripts[script.get_id()] = [
                        existing_script, script
                    ]
Пример #7
0
    def __parse_xml(self):

        logging.info('Parsing Host')
        attr = self.__xml.attrib
        self.__start_time = int(
            attr['starttime']) if None != attr.get('starttime', None) else None
        self.__end_time = int(
            attr['endtime']) if None != attr.get('endtime', None) else None
        self.__comment = attr.get('comment', None)

        logging.debug('Start time: "{time}"'.format(time=self.__start_time))
        logging.debug('End time: "{time}"'.format(time=self.__end_time))
        logging.debug('Comment: "{comment}"'.format(comment=self.__comment))
        for smurf_xml in self.__xml.findall('smurf'):
            logging.debug(
                'Smurf: "{smurf}"'.format(smurf=smurf_xml.attrib['responses']))
            self.__smurfs.append(smurf_xml.attrib['responses'])
        for distance_xml in self.__xml.findall('distance'):
            logging.debug('Distance: "{distance}"'.format(
                distance=distance_xml.attrib['value']))
            self.__distances.append(int(distance_xml.attrib['value']))

        self.__status = Status(self.__xml.find('status'))

        for addresses_xml in self.__xml.findall('address'):
            self.__addresses.append(HostAddress(addresses_xml, False))

        hostnames_xml = self.__xml.find('hostnames')
        if hostnames_xml != None:
            for hostname_xml in hostnames_xml.findall('hostname'):
                self.__hostnames.append(HostName(hostname_xml, False))

        ports_xml = self.__xml.find('ports')
        if ports_xml != None:
            for port_xml in ports_xml.findall('port'):
                self.__ports.append(Port(port_xml, False))
            for extraports_xml in ports_xml.findall('extraports'):
                self.__extraports.append(ExtraPort(extraports_xml, False))

        for hostscript_xml in self.__xml.findall('hostscript'):
            for script_xml in hostscript_xml.findall('script'):
                script = parse(script_xml, False)
                existing_script = self.__hostscripts.get(script.get_id(), None)
                if None == existing_script:
                    self.__hostscripts[script.get_id()] = script
                elif isinstance(existing_script, list):
                    self.__hostscripts[script.get_id()].append(script)
                else:
                    self.__hostscripts[script.get_id()] = [
                        existing_script, script
                    ]

        for time_xml in self.__xml.findall('times'):
            self.__times.append(Time(time_xml, False))
        for trace_xml in self.__xml.findall('trace'):
            self.__traces.append(Trace(trace_xml, False))
        for uptime_xml in self.__xml.findall('uptime'):
            self.__uptimes.append(Uptime(uptime_xml, False))
        for ipidsequence_xml in self.__xml.findall('ipidsequence'):
            self.__ipidsequences.append(IPIDSequence(ipidsequence_xml, False))
        for tcpsequence_xml in self.__xml.findall('tcpsequence', False):
            self.__tcpsequences.append(TCPSequence(tcpsequence_xml))
        for tcptssequence_xml in self.__xml.findall('tcptssequence'):
            self.__tcptssequences.append(
                TCPTSSequence(tcptssequence_xml, False))
        for os_xml in self.__xml.findall('os'):
            self.__os.append(OS(os_xml, False))