def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the conpot directory self.dir_name = os.path.dirname(conpot.__file__) self.kamstrup_management_server = KamstrupServer( self.dir_name + "/templates/kamstrup_382/kamstrup_meter/kamstrup_meter.xml", None, None, ) self.server_greenlet = gevent.spawn( self.kamstrup_management_server.start, "127.0.0.1", 0 ) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize(self.dir_name + "/templates/kamstrup_382/template.xml") gevent.sleep(1) self.request_parser = request_parser.KamstrupRequestParser() self.command_responder = CommandResponder( self.dir_name + "/templates/kamstrup_382/kamstrup_meter/kamstrup_meter.xml" )
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # make paths platform-independent template = reduce(os.path.join, 'conpot/templates/default/template.xml'.split('/')) modbus_template = reduce( os.path.join, 'conpot/templates/default/modbus/modbus.xml'.split('/')) self.databus = conpot_core.get_databus() self.databus.initialize(template) args = namedtuple('FakeArgs', 'mibpaths raw_mib') self.modbus = modbus_server.ModbusServer(modbus_template, 'none', args, timeout=2) self.modbus_server = StreamServer(('127.0.0.1', 0), self.modbus.handle) self.modbus_server.start() # We have to use different slave IDs under different modes. In tcp mode, # only 255 and 0 make sense. However, modbus_tcp.TcpMaster explicitly # ignores slave ID 0. Therefore we can only use 255 in tcp mode. self.target_slave_id = 1 if self.modbus.mode == 'serial' else 255
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/kamstrup_382/template.xml') self.request_parser = request_parser.KamstrupRequestParser() self.command_responder = CommandResponder('conpot/templates/kamstrup_382/kamstrup_meter/kamstrup_meter.xml')
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/kamstrup_382.xml') self.request_parser = request_parser.KamstrupRequestParser() self.command_responder = CommandResponder('conpot/templates/kamstrup_382.xml')
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default.xml') modbus = modbus_server.ModbusServer('conpot/templates/default.xml', timeout=2) self.modbus_server = StreamServer(('127.0.0.1', 0), modbus.handle) self.modbus_server.start()
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default/template.xml') args = namedtuple('FakeArgs', 'mibpaths raw_mib') modbus = modbus_server.ModbusServer('conpot/templates/default/modbus/modbus.xml', 'none', args, timeout=2) self.modbus_server = StreamServer(('127.0.0.1', 0), modbus.handle) self.modbus_server.start()
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize("conpot/templates/default/template.xml") args = namedtuple("FakeArgs", "") bacnet = bacnet_server.BacnetServer("conpot/templates/default/bacnet/bacnet.xml", "none", args) self.bacnet_server = DatagramServer(("127.0.0.1", 47808), bacnet.handle) gevent.spawn(self.bacnet_server.start) gevent.sleep(1)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default/template.xml') args = namedtuple('FakeArgs', '') self.bacnet_server = bacnet_server.BacnetServer('conpot/templates/default/bacnet/bacnet.xml', 'none', args) self.server_greenlet = gevent.spawn(self.bacnet_server.start, '0.0.0.0', 0) gevent.sleep(1) assert self.bacnet_server.server.server_port
def setUp(self): self.template_path = 'conpot/templates/ipmi/ipmi/ipmi.xml' # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/ipmi/template.xml') args = namedtuple('FakeArgs', 'port') args.port = 0 self.ipmi_server = IpmiServer(self.template_path, 'conpot/templates/ipmi/', args) self.greenlet = gevent.spawn(self.ipmi_server.start, '127.0.0.1', 0)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory dir_name = os.path.dirname(conpot.__file__) args = namedtuple('FakeArgs', 'port') args.port = 0 conpot_core.get_databus().initialize(dir_name + '/templates/default/template.xml') self.ipmi_server = IpmiServer(dir_name + '/templates/default/ipmi/ipmi.xml', dir_name + '/templates/default/', args) self.greenlet = gevent.spawn(self.ipmi_server.start, '127.0.0.1', 0) gevent.sleep(1)
def setUp(self): self.template_path = 'conpot/templates/ipmi/ipmi/ipmi.xml' # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/ipmi/template.xml') self.ipmi_server = IpmiServer( self.template_path, 'conpot/templates/ipmi/', {} ) self.greenlet = gevent.spawn(self.ipmi_server.start, '127.0.0.1', 0)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() args = namedtuple('FakeArgs', '') self.http_server = web_server.HTTPServer('conpot/templates/default/http/http.xml', 'conpot/templates/default/', args) self.http_worker = gevent.spawn(self.http_server.start, '127.0.0.1', 0) gevent.sleep(0.5) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default/template.xml')
def setUp(self): conpot_core.get_sessionManager().purge_sessions() self.modbus, self.greenlet = spawn_test_server( modbus_server.ModbusServer, "default", "modbus") self.databus = conpot_core.get_databus() self.host = self.modbus.server.server_host self.port = self.modbus.server.server_port # We have to use different slave IDs under different modes. In tcp mode, # only 255 and 0 make sense. However, modbus_tcp.TcpMaster explicitly # ignores slave ID 0. Therefore we can only use 255 in tcp mode. self.target_slave_id = 1 if self.modbus.mode == "serial" else 255
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory self.dir_name = os.path.dirname(conpot.__file__) args = namedtuple('FakeArgs', '') self.guardian_ast_server = GuardianASTServer(self.dir_name + '/templates/guardian_ast/guardian_ast/guardian_ast.xml', self.dir_name + '/templates/guardian_ast', args) self.server_greenlet = gevent.spawn(self.guardian_ast_server.start, '127.0.0.1', 0) # initialize the databus self.guardian_ast_server.databus.initialize(self.dir_name + '/templates/guardian_ast/template.xml') gevent.sleep(1)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory dir_name = os.path.dirname(conpot.__file__) args = namedtuple('FakeArgs', 'port') args.port = 0 conpot_core.get_databus().initialize(dir_name + '/templates/default/template.xml') self.ipmi_server = IpmiServer( dir_name + '/templates/default/ipmi/ipmi.xml', dir_name + '/templates/default/', args) self.greenlet = gevent.spawn(self.ipmi_server.start, '127.0.0.1', 0) gevent.sleep(1)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default/template.xml') args = namedtuple('FakeArgs', 'mibpaths raw_mib') modbus = modbus_server.ModbusServer( 'conpot/templates/default/modbus/modbus.xml', 'none', args, timeout=2) self.modbus_server = StreamServer(('127.0.0.1', 0), modbus.handle) self.modbus_server.start()
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.dct_server = DCTServer() gevent.spawn(self.dct_server.start) gevent.sleep(0.5) # self.databus = conpot_core.get_databus() # self.databus.initialize('conpot/templates/default/template.xml') args = namedtuple('FakeArgs', '') self.dct = DCT('conpot/templates/default/dct/dct.xml', 'none', args) gevent.spawn(self.dct.start, '0.0.0.0', 9002) gevent.sleep(0.5)
def test_modbus_logging(self): """ Objective: Test if modbus generates log messages as expected. Expected output is a dictionary with the following structure: {'timestamp': datetime.datetime(2013, 4, 23, 18, 47, 38, 532960), 'remote': ('127.0.0.1', 60991), 'data_type': 'modbus', 'id': '01bd90d6-76f4-43cb-874f-5c8f254367f5', 'data': {'function_code': 1, 'slave_id': 1, 'request': '0100010080', 'response': '0110ffffffffffffffffffffffffffffffff'}} """ self.databus.set_value('memoryModbusSlave1BlockA', [1 for b in range(0,128)]) master = modbus_tcp.TcpMaster(host='127.0.0.1', port=self.modbus_server.server_port) master.set_timeout(1.0) #issue request to modbus server master.execute(slave=1, function_code=cst.READ_COILS, starting_address=1, quantity_of_x=128) #extract the generated logentry log_queue = conpot_core.get_sessionManager().log_queue log_item = log_queue.get(True, 2) self.assertIsInstance(log_item['timestamp'], datetime) self.assertTrue('data' in log_item) # we expect session_id to be 36 characters long (32 x char, 4 x dashes) self.assertTrue(len(str(log_item['id'])), log_item) self.assertEqual('127.0.0.1', log_item['remote'][0]) self.assertEquals('modbus', log_item['data_type']) #testing the actual modbus data expected_payload = {'function_code': 1, 'slave_id': 1,'request': '000100000006010100010080', 'response': '0110ffffffffffffffffffffffffffffffff'} self.assertDictEqual(expected_payload, log_item['data'])
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.http_server = web_server.HTTPServer('127.0.0.1', 0, 'conpot/templates/default.xml', 'conpot/templates/www/default/',) # get the assigned ephemeral port for http self.http_port = self.http_server.server_port self.http_worker = gevent.spawn(self.http_server.start) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize('conpot/templates/default.xml')
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the conpot directory self.dir_name = os.path.dirname(conpot.__file__) self.kamstrup_management_server = KamstrupManagementServer( self.dir_name + '/templates/kamstrup_382/kamstrup_management/kamstrup_management.xml', None, None ) self.server_greenlet = gevent.spawn(self.kamstrup_management_server.start, '127.0.0.1', 0) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize(self.dir_name + '/templates/kamstrup_382/template.xml') gevent.sleep(1)
def test_modbus_logging(self): """ Objective: Test if modbus generates log messages as expected. Expected output is a dictionary with the following structure: {'timestamp': datetime.datetime(2013, 4, 23, 18, 47, 38, 532960), 'remote': ('127.0.0.1', 60991), 'data_type': 'modbus', 'id': '01bd90d6-76f4-43cb-874f-5c8f254367f5', 'data': {'function_code': 1, 'slave_id': 1, 'request': '0100010080', 'response': '0110ffffffffffffffffffffffffffffffff'}} """ self.databus.set_value( "memoryModbusSlave%dBlockA" % self.target_slave_id, [1 for b in range(0, 128)], ) master = modbus_tcp.TcpMaster( host="127.0.0.1", port=self.modbus_server.server_port ) master.set_timeout(1.0) # issue request to modbus server master.execute( slave=self.target_slave_id, function_code=cst.READ_COILS, starting_address=1, quantity_of_x=128, ) # extract the generated log entries log_queue = conpot_core.get_sessionManager().log_queue conn_log_item = log_queue.get(True, 2) conn_expected_payload = {"type": "NEW_CONNECTION"} self.assertDictEqual(conn_expected_payload, conn_log_item["data"]) modbus_log_item = log_queue.get(True, 2) self.assertIsInstance(modbus_log_item["timestamp"], datetime) self.assertTrue("data" in modbus_log_item) # we expect session_id to be 36 characters long (32 x char, 4 x dashes) self.assertTrue(len(str(modbus_log_item["id"])), modbus_log_item) self.assertEqual("127.0.0.1", modbus_log_item["remote"][0]) self.assertEqual("modbus", modbus_log_item["data_type"]) req = ( "000100000006%s0100010080" % ("01" if self.target_slave_id == 1 else "ff") ).encode() # testing the actual modbus data modbus_expected_payload = { "function_code": 1, "slave_id": self.target_slave_id, "request": req, "response": b"0110ffffffffffffffffffffffffffffffff", } self.assertDictEqual(modbus_expected_payload, modbus_log_item["data"])
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory self.dir_name = os.path.dirname(conpot.__file__) args = namedtuple("FakeArgs", "") self.bacnet_server = bacnet_server.BacnetServer( self.dir_name + "/templates/default/bacnet/bacnet.xml", "none", args) self.server_greenlet = gevent.spawn(self.bacnet_server.start, "127.0.0.1", 0) gevent.sleep(1) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize(self.dir_name + "/templates/default/template.xml")
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory self.dir_name = os.path.dirname(conpot.__file__) args = namedtuple('FakeArgs', '') self.guardian_ast_server = GuardianASTServer( self.dir_name + '/templates/guardian_ast/guardian_ast/guardian_ast.xml', self.dir_name + '/templates/guardian_ast', args) self.server_greenlet = gevent.spawn(self.guardian_ast_server.start, '127.0.0.1', 0) # initialize the databus self.guardian_ast_server.databus.initialize( self.dir_name + '/templates/guardian_ast/template.xml') gevent.sleep(1)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() self.dir_name = os.path.dirname(conpot.__file__) args = namedtuple("FakeArgs", "") self.http_server = web_server.HTTPServer( self.dir_name + "/templates/default/http/http.xml", self.dir_name + "/templates/default/", args, ) self.http_worker = gevent.spawn(self.http_server.start, "127.0.0.1", 0) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize(self.dir_name + "/templates/default/template.xml") gevent.sleep(1)
def test_failing_connection_connection_lost_event(self, mock_timeout): """ Objective: Test if correct exception is executed when a socket.error with EPIPE occurs """ mock_timeout.side_effect = OSError(32, "Socket Error") conpot_core.get_sessionManager().purge_sessions() s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("127.0.0.1", 2404)) time.sleep(0.1) log_queue = conpot_core.get_sessionManager().log_queue con_new_event = log_queue.get() con_lost_event = log_queue.get(timeout=1) self.assertEqual("NEW_CONNECTION", con_new_event["data"]["type"]) self.assertEqual("CONNECTION_LOST", con_lost_event["data"]["type"]) s.close()
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the conpot directory self.dir_name = os.path.dirname(conpot.__file__) self.kamstrup_management_server = KamstrupManagementServer( self.dir_name + '/templates/kamstrup_382/kamstrup_management/kamstrup_management.xml', None, None) self.server_greenlet = gevent.spawn( self.kamstrup_management_server.start, '127.0.0.1', 0) # initialize the databus self.databus = conpot_core.get_databus() self.databus.initialize(self.dir_name + '/templates/kamstrup_382/template.xml') gevent.sleep(1)
def setUp(self): # clean up before we start... conpot_core.get_sessionManager().purge_sessions() # get the current directory self.dir_name = os.path.dirname(conpot.__file__) # make paths platform-independent template = self.dir_name + "/templates/default/template.xml" modbus_template = self.dir_name + "/templates/default/modbus/modbus.xml" self.databus = conpot_core.get_databus() self.databus.initialize(template) self.modbus = modbus_server.ModbusServer( modbus_template, "none", None, timeout=2 ) self.modbus_server = StreamServer(("127.0.0.1", 0), self.modbus.handle) self.modbus_server.start() # We have to use different slave IDs under different modes. In tcp mode, # only 255 and 0 make sense. However, modbus_tcp.TcpMaster explicitly # ignores slave ID 0. Therefore we can only use 255 in tcp mode. self.target_slave_id = 1 if self.modbus.mode == "serial" else 255
def test_modbus_logging(self): """ Objective: Test if modbus generates log messages as expected. Expected output is a dictionary with the following structure: {'timestamp': datetime.datetime(2013, 4, 23, 18, 47, 38, 532960), 'remote': ('127.0.0.1', 60991), 'data_type': 'modbus', 'id': '01bd90d6-76f4-43cb-874f-5c8f254367f5', 'data': {'function_code': 1, 'slave_id': 1, 'request': '0100010080', 'response': '0110ffffffffffffffffffffffffffffffff'}} """ self.databus.set_value('memoryModbusSlave1BlockA', [1 for b in range(0, 128)]) master = modbus_tcp.TcpMaster(host='127.0.0.1', port=self.modbus_server.server_port) master.set_timeout(1.0) #issue request to modbus server master.execute(slave=1, function_code=cst.READ_COILS, starting_address=1, quantity_of_x=128) #extract the generated logentries log_queue = conpot_core.get_sessionManager().log_queue conn_log_item = log_queue.get(True, 2) conn_expected_payload = {'type': 'NEW_CONNECTION'} self.assertDictEqual(conn_expected_payload, conn_log_item['data']) modbus_log_item = log_queue.get(True, 2) self.assertIsInstance(modbus_log_item['timestamp'], datetime) self.assertTrue('data' in modbus_log_item) # we expect session_id to be 36 characters long (32 x char, 4 x dashes) self.assertTrue(len(str(modbus_log_item['id'])), modbus_log_item) self.assertEqual('127.0.0.1', modbus_log_item['remote'][0]) self.assertEquals('modbus', modbus_log_item['data_type']) #testing the actual modbus data modbus_expected_payload = { 'function_code': 1, 'slave_id': 1, 'request': '000100000006010100010080', 'response': '0110ffffffffffffffffffffffffffffffff' } self.assertDictEqual(modbus_expected_payload, modbus_log_item['data'])
def tearDown(self): self.http_server.stop() # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.greenlet.kill() # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.modbus_server.stop() # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.http_server.stop() gevent.joinall([self.http_worker]) # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.http_server.cmd_responder.httpd.shutdown() self.http_server.cmd_responder.httpd.server_close() # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.kamstrup_management_server.stop() gevent.joinall([self.server_greenlet]) # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()
def tearDown(self): self.bacnet_server.stop() gevent.joinall([self.server_greenlet]) # tidy up (again)... conpot_core.get_sessionManager().purge_sessions()