Skip to content

Commit

Permalink
Merge 3edc0ea into ff7e786
Browse files Browse the repository at this point in the history
  • Loading branch information
srenfo committed Jun 3, 2020
2 parents ff7e786 + 3edc0ea commit 4526c21
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 309 deletions.
149 changes: 0 additions & 149 deletions conpot/protocols/snmp/build_pysnmp_mib_wrapper.py

This file was deleted.

2 changes: 1 addition & 1 deletion conpot/protocols/snmp/command_responder.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def register(self, mibname, symbolname, instance, value, profile_map_name):
logger.debug('Registered: OID %s Instance %s ASN.1 (%s @ %s) value %s dynrsp.', s.name, instance, s.label, mibname, value)

else:
logger.debug('Skipped: OID for symbol %s not found in MIB %s', symbolname, mibname)
logger.warning('Skipped: OID for symbol %s not found in MIB %s', symbolname, mibname)

def _get_mibSymbol(self, mibname, symbolname):
modules = self.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.mibSymbols
Expand Down
66 changes: 66 additions & 0 deletions conpot/protocols/snmp/mib_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Copyright (C) 2013 Johnny Vestergaard <jkv@unixcluster.dk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import logging
from typing import List

from pysmi.codegen.pysnmp import PySnmpCodeGen, baseMibs
from pysmi.compiler import MibCompiler, statusCompiled, statusUntouched
from pysmi.parser.smi import SmiV2Parser
from pysmi.reader.httpclient import HttpReader
from pysmi.reader.localfile import FileReader
from pysmi.searcher.pyfile import PyFileSearcher
from pysmi.searcher.pypackage import PyPackageSearcher
from pysmi.searcher.stub import StubSearcher
from pysmi.writer.pyfile import PyFileWriter

logger = logging.getLogger(__name__)


def compile_mib(mib_name: str, mib_sources: List[str], output_dir: str) -> bool:
"""
Compile raw MIB to .py file, fetching dependencies as necessary.
:param mib_name: name of the MIB to compile
:param mib_sources: list of paths to search for raw MIBs
:param output_dir: path to the output directory
:return: True if we successfully compile the .mib to a .py
"""

logger.debug("Compiling MIB: %s", mib_name)

# create a mib compiler with output dir
mib_compiler = MibCompiler(SmiV2Parser(), PySnmpCodeGen(), PyFileWriter(output_dir))

# add sources from where we fetch dependencies
for source in mib_sources:
mib_compiler.addSources(FileReader(source))

mib_compiler.addSources(HttpReader("mibs.snmplabs.com", 80, "/asn1/@mib@"))

# add searchers
mib_compiler.addSearchers(PyFileSearcher(output_dir))
mib_compiler.addSearchers(PyPackageSearcher("pysnmp.mibs"))
mib_compiler.addSearchers(StubSearcher(*baseMibs))

# compile, there should be a MIBFILE.py generated under output_dir
results = mib_compiler.compile(mib_name)

if results[mib_name] == statusCompiled or results[mib_name] == statusUntouched:
return True

return False
83 changes: 38 additions & 45 deletions conpot/protocols/snmp/snmp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import logging
import tempfile
import shutil
import os

from lxml import etree

import conpot.core as conpot_core
from conpot.core.protocol_wrapper import conpot_protocol
from conpot.protocols.snmp.command_responder import CommandResponder
from conpot.protocols.snmp.build_pysnmp_mib_wrapper import find_mibs, compile_mib
import conpot.core as conpot_core

from conpot.protocols.snmp.mib_compiler import compile_mib

logger = logging.getLogger()

Expand Down Expand Up @@ -81,46 +79,41 @@ def xml_general_config(self, dom):
elif entity.attrib['command'].lower() == 'bulk':
self.cmd_responder.resp_app_bulk.threshold = self.config_sanitize_threshold(entity.text)

def xml_mib_config(self, dom, mibpaths, rawmibs_dirs):
try:
mibs = dom.xpath('//snmp/mibs/*')
tmp_mib_dir = tempfile.mkdtemp()
mibpaths.append(tmp_mib_dir)
available_mibs = find_mibs(rawmibs_dirs)

databus = conpot_core.get_databus()
# 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 databus
value = databus.get_value(symbol.xpath('./value/text()')[0])
profile_map_name = symbol.xpath('./value/text()')[0]

# register this MIB instance to the command responder
self.cmd_responder.register(mib_name,
symbol_name,
symbol_instance,
value,
profile_map_name)
finally:
# cleanup compiled mib files
shutil.rmtree(tmp_mib_dir)
def xml_mib_config(self, dom, compiled_dirs, rawmibs_dirs):
mibs = dom.xpath('//snmp/mibs/*')

databus = conpot_core.get_databus()
# parse mibs and oid tables
for mib in mibs:
mib_name = mib.attrib['name']

if not self.cmd_responder.has_mib(mib_name):
if not compile_mib(mib_name, rawmibs_dirs, compiled_dirs[0]):
logger.warning('Skipped: Failed to compile MIB %s', mib_name)
continue

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 databus
value = databus.get_value(symbol.xpath('./value/text()')[0])
profile_map_name = symbol.xpath('./value/text()')[0]

# register this MIB instance to the command responder
self.cmd_responder.register(mib_name,
symbol_name,
symbol_instance,
value,
profile_map_name)

def config_sanitize_tarpit(self, value):

Expand Down
2 changes: 1 addition & 1 deletion conpot/templates/IEC104/template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<value type="value">100000000</value>
</key>
<key name="ifPhysAddress">
<value type="value">"\x00\x0e\x8c\x29\xc5\x1a"</value>
<value type="value">"0x000e8c29c51a"</value>
</key>
<key name="ifAdminStatus">
<value type="value">1</value>
Expand Down

0 comments on commit 4526c21

Please sign in to comment.