Exemple #1
0
 def test_hosts_skipped(self, filepath, expected):
     xml = self.create_xml(filepath)
     e = self.create_instance(xml)
     expected_objects = []
     for ex in expected:
         expected_objects.append(Host(self.create_xml(ex)))
     hosts = e.get_hosts_skipped()
     assert len(expected) == len(hosts)
     assert len(expected) == len(e.get_hosts_skipped_with_port([22]))
     assert hosts == e.get_hosts_skipped()
     for ex in expected_objects:
         exist = False
         for c in e.get_hosts_skipped():
             if ex.equals(c):
                 exist = True
                 break
         assert exist
Exemple #2
0
    def test_hosts(self, filepath, expected):
        xml = self.create_xml(filepath)
        e = self.create_instance(xml)
        expected_objects = []
        for ex in expected:
            expected_objects.append(Host(self.create_xml(ex)))

        assert len(expected) == len(e.get_hosts())
        assert len(expected) == len(e.get_hosts_with_port([22]))
        assert len(expected) == len(e.get_host_with_port(22))
        assert len(expected) == len(e.get_hosts_with_script('unknownId'))
        for ex in expected_objects:
            exist = False
            for c in e.get_hosts():
                if ex.equals(c):
                    exist = True
                    break
            assert exist
Exemple #3
0
    def dict_to_xml(d, validate_xml=True):
        xml = etree.Element('nmaprun')

        if None != d.get('scanner', None):
            xml.attrib['scanner'] = d.get('scanner', None)
        if None != d.get('version', None):
            xml.attrib['version'] = d.get('version', None)
        if None != d.get('xmloutputversion', None):
            xml.attrib['xmloutputversion'] = d.get('xmloutputversion', None)
        if None != d.get('profile_name', None):
            xml.attrib['profile_name'] = d.get('profile_name', None)
        if None != d.get('args', None):
            xml.attrib['args'] = d.get('args', None)
        if None != d.get('start', None):
            xml.attrib['start'] = str(d.get('start', None))
        if None != d.get('startstr', None):
            xml.attrib['startstr'] = d.get('startstr', None)

        if None != d.get('scaninfo', None):
            for status_dict in d['scaninfo']:
                xml.append(ScanInfo.dict_to_xml(status_dict, validate_xml))

        if None != d.get('verbose', None):
            verbose_xml = etree.Element('verbose')
            verbose_xml.attrib['level'] = str(d['verbose'])
            xml.append(verbose_xml)

        if None != d.get('debugging', None):
            debugging_xml = etree.Element('debugging')
            debugging_xml.attrib['level'] = str(d['debugging'])
            xml.append(debugging_xml)

        if None != d.get('targets', None):
            for e_dict in d['targets']:
                xml.append(Target.dict_to_xml(e_dict, validate_xml))
        if None != d.get('taskbegin', None):
            for e_dict in d['taskbegin']:
                xml.append(TaskBegin.dict_to_xml(e_dict, validate_xml))
        if None != d.get('taskprogress', None):
            for e_dict in d['taskprogress']:
                xml.append(TaskProgress.dict_to_xml(e_dict, validate_xml))
        if None != d.get('taskend', None):
            for e_dict in d['taskend']:
                xml.append(TaskEnd.dict_to_xml(e_dict, validate_xml))

        if None != d.get('hosthints', None):
            for e_dict in d['hosthints']:
                xml.append(HostHint.dict_to_xml(e_dict, validate_xml))

        if None != d.get('prescripts', None):
            for script_dict in d['prescripts']:
                script_xml = etree.Element('prescript')
                script_xml.append(Script.dict_to_xml(script_dict,
                                                     validate_xml))
                xml.append(script_xml)
        if None != d.get('postscripts', None):
            for script_dict in d['postscripts']:
                script_xml = etree.Element('postscript')
                script_xml.append(Script.dict_to_xml(script_dict,
                                                     validate_xml))
                xml.append(script_xml)

        if None != d.get('hosts', None):
            for e_dict in d['hosts']:
                xml.append(Host.dict_to_xml(e_dict, validate_xml))
        if None != d.get('outputs', None):
            for e_dict in d['outputs']:
                xml.append(Output.dict_to_xml(e_dict, validate_xml))

        if None != d.get('runstats', None):
            xml.append(RunStats.dict_to_xml(d['runstats'], validate_xml))

        if validate_xml:
            try:
                validate(xml)
            except NmapXMLParserException:
                raise NmapDictParserException()

        return xml
Exemple #4
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
                    ]
Exemple #5
0
 def create_instance(self, xml):
     return Host(xml)