def test_compile(self):
     """
     Tests that the wrapper can output mib files.
     """
     try:
         input_dir = tempfile.mkdtemp()
         output_dir = tempfile.mkdtemp()
         shutil.copy('conpot/tests/data/VOGON-POEM-MIB.mib', input_dir)
         find_mibs([input_dir])
         compile_mib('VOGON-POEM-MIB', output_dir)
         self.assertIn('VOGON-POEM-MIB.py', os.listdir(output_dir))
     finally:
         shutil.rmtree(input_dir)
         shutil.rmtree(output_dir)
Exemple #2
0
    def __init__(self, host, port, template, log_queue, mibpaths, rawmibs_dirs):
        """
        :param host:        hostname or ip address on which to server the snmp service (string).
        :param port:        listen port (integer).
        :param template:    path to conpot xml configuration file (string).
        :param log_queue:   shared log queue (list).
        :param mibpaths:    collection of paths to search for COMPILED mib files (iterable collection of strings).
        :param rawmibs_dir: directory to search for raw mib files, these files will get compiled by conpot (string).
        """
        self.host = host
        self.port = port

        dyn_rsp = DynamicResponder()

        dom = etree.parse(template)
        mibs = dom.xpath('//conpot_template/snmp/mibs/*')

        # only enable snmp server if we have configuration items
        if mibs:
            try:
                tmp_mib_dir = tempfile.mkdtemp()
                mibpaths.append(tmp_mib_dir)
                available_mibs = find_mibs(rawmibs_dirs)
                self.cmd_responder = CommandResponder(self.host, self.port, log_queue, mibpaths, dyn_rsp)

                # parse global snmp configuration
                snmp_config = dom.xpath('//conpot_template/snmp/config/*')
                if snmp_config:

                    for entity in snmp_config:

                        # TARPIT: individual response delays
                        if entity.attrib['name'].lower() == 'tarpit':

                            if entity.attrib['command'].lower() == 'get':
                                self.cmd_responder.resp_app_get.tarpit = self.config_sanitize_tarpit(entity.text)
                            elif entity.attrib['command'].lower() == 'set':
                                self.cmd_responder.resp_app_set.tarpit = self.config_sanitize_tarpit(entity.text)
                            elif entity.attrib['command'].lower() == 'next':
                                self.cmd_responder.resp_app_next.tarpit = self.config_sanitize_tarpit(entity.text)
                            elif entity.attrib['command'].lower() == 'bulk':
                                self.cmd_responder.resp_app_bulk.tarpit = self.config_sanitize_tarpit(entity.text)

                # parse mibs and oid tables
                for mib in mibs:
                    mib_name = mib.attrib['name']
                    # compile the mib file if it is found and not already loaded.
                    if mib_name in available_mibs and not self.cmd_responder.has_mib(mib_name):
                        compile_mib(mib_name, tmp_mib_dir)
                    for symbol in mib:
                        symbol_name = symbol.attrib['name']

                        # retrieve instance from template
                        if 'instance' in symbol.attrib:
                            # convert instance to (int-)tuple
                            symbol_instance = symbol.attrib['instance'].split('.')
                            symbol_instance = tuple(map(int, symbol_instance))
                        else:
                            # use default instance (0)
                            symbol_instance = (0,)

                        # retrieve value from template
                        value = symbol.xpath('./value/text()')[0]

                        # retrieve engine from template
                        if len(symbol.xpath('./engine')) > 0:
                            engine_type = symbol.find('./engine').attrib['type']
                            engine_aux = symbol.findtext('./engine')
                        else:
                            # disable dynamic responses (static)
                            engine_type = 'static'
                            engine_aux = ''

                            # register this MIB instance to the command responder
                        self.cmd_responder.register(mib_name, symbol_name, symbol_instance, value, engine_type, engine_aux)
            finally:
                #cleanup compiled mib files
                shutil.rmtree(tmp_mib_dir)
        else:
            self.cmd_responder = None
Exemple #3
0
    def __init__(self, host, port, template, log_queue, mibpaths,
                 rawmibs_dirs):
        """
        :param host:        hostname or ip address on which to server the snmp service (string).
        :param port:        listen port (integer).
        :param template:    path to conpot xml configuration file (string).
        :param log_queue:   shared log queue (list).
        :param mibpaths:    collection of paths to search for COMPILED mib files (iterable collection of strings).
        :param rawmibs_dir: directory to search for raw mib files, these files will get compiled by conpot (string).
        """
        self.host = host
        self.port = port

        dyn_rsp = DynamicResponder()

        dom = etree.parse(template)
        mibs = dom.xpath('//conpot_template/snmp/mibs/*')

        # only enable snmp server if we have configuration items
        if mibs:
            try:
                tmp_mib_dir = tempfile.mkdtemp()
                mibpaths.append(tmp_mib_dir)
                available_mibs = find_mibs(rawmibs_dirs)
                self.cmd_responder = CommandResponder(self.host, self.port,
                                                      log_queue, mibpaths,
                                                      dyn_rsp)

                # parse global snmp configuration
                snmp_config = dom.xpath('//conpot_template/snmp/config/*')
                if snmp_config:

                    for entity in snmp_config:

                        # TARPIT: individual response delays
                        if entity.attrib['name'].lower() == 'tarpit':

                            if entity.attrib['command'].lower() == 'get':
                                self.cmd_responder.resp_app_get.tarpit = self.config_sanitize_tarpit(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'set':
                                self.cmd_responder.resp_app_set.tarpit = self.config_sanitize_tarpit(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'next':
                                self.cmd_responder.resp_app_next.tarpit = self.config_sanitize_tarpit(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'bulk':
                                self.cmd_responder.resp_app_bulk.tarpit = self.config_sanitize_tarpit(
                                    entity.text)

                        # EVASION: response thresholds
                        if entity.attrib['name'].lower() == 'evasion':

                            if entity.attrib['command'].lower() == 'get':
                                self.cmd_responder.resp_app_get.threshold = self.config_sanitize_threshold(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'set':
                                self.cmd_responder.resp_app_set.threshold = self.config_sanitize_threshold(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'next':
                                self.cmd_responder.resp_app_next.threshold = self.config_sanitize_threshold(
                                    entity.text)
                            elif entity.attrib['command'].lower() == 'bulk':
                                self.cmd_responder.resp_app_bulk.threshold = self.config_sanitize_threshold(
                                    entity.text)

                # parse mibs and oid tables
                for mib in mibs:
                    mib_name = mib.attrib['name']
                    # compile the mib file if it is found and not already loaded.
                    if mib_name in available_mibs and not self.cmd_responder.has_mib(
                            mib_name):
                        compile_mib(mib_name, tmp_mib_dir)
                    for symbol in mib:
                        symbol_name = symbol.attrib['name']

                        # retrieve instance from template
                        if 'instance' in symbol.attrib:
                            # convert instance to (int-)tuple
                            symbol_instance = symbol.attrib['instance'].split(
                                '.')
                            symbol_instance = tuple(map(int, symbol_instance))
                        else:
                            # use default instance (0)
                            symbol_instance = (0, )

                        # retrieve value from template
                        value = symbol.xpath('./value/text()')[0]

                        # retrieve engine from template
                        if len(symbol.xpath('./engine')) > 0:
                            engine_type = symbol.find(
                                './engine').attrib['type']
                            engine_aux = symbol.findtext('./engine')
                        else:
                            # disable dynamic responses (static)
                            engine_type = 'static'
                            engine_aux = ''

                            # register this MIB instance to the command responder
                        self.cmd_responder.register(mib_name, symbol_name,
                                                    symbol_instance, value,
                                                    engine_type, engine_aux)
            finally:
                #cleanup compiled mib files
                shutil.rmtree(tmp_mib_dir)
        else:
            self.cmd_responder = None