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() 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): # get the conpot directory self.dir_name = os.path.dirname(conpot.__file__) self.request_parser = request_parser.KamstrupRequestParser() self.command_responder = CommandResponder( self.dir_name + "/templates/kamstrup_382/kamstrup_meter/kamstrup_meter.xml") self.kamstrup_management_server, self.server_greenlet = spawn_test_server( KamstrupServer, "kamstrup_382", "kamstrup_meter")
def handle(self, sock, address): session = conpot_core.get_session('kamstrup_protocol', address[0], address[1]) logger.info('New connection from {0}:{1}. ({2})'.format( address[0], address[1], session.id)) session.add_event({'type': 'NEW_CONNECTION'}) server_active = True parser = request_parser.KamstrupRequestParser() try: while server_active: raw_request = sock.recv(1024) if not raw_request: logger.info('Client disconnected. ({0})'.format( session.id)) session.add_event({'type': 'CONNECTION_LOST'}) break for x in raw_request: parser.add_byte(x) while True: request = parser.get_request() if not request: session.add_event({'type': 'CONNECTION_LOST'}) break else: logdata = { 'request': binascii.hexlify(bytearray(request.message_bytes)) } response = self.command_responder.respond(request) # real Kamstrup meters has delay in this interval gevent.sleep(random.uniform(0.24, 0.34)) if response: serialized_response = response.serialize() logdata['response'] = binascii.hexlify( serialized_response) logger.debug( 'Kamstrup traffic from {0}: {1} ({2})'.format( address[0], logdata, session.id)) sock.send(serialized_response) session.add_event(logdata) else: session.add_event(logdata) break except socket.timeout: logger.debug('Socket timeout, remote: {0}. ({1})'.format( address[0], session.id)) session.add_event({'type': 'CONNECTION_LOST'}) sock.close()
def handle(self, sock, address): session = conpot_core.get_session( "kamstrup_protocol", address[0], address[1], sock.getsockname()[0], sock.getsockname()[1], ) logger.info( "New Kamstrup connection from %s:%s. (%s)", address[0], address[1], session.id, ) session.add_event({"type": "NEW_CONNECTION"}) self.server_active = True parser = request_parser.KamstrupRequestParser() try: while self.server_active: raw_request = sock.recv(1024) if not raw_request: logger.info("Kamstrup client disconnected. (%s)", session.id) session.add_event({"type": "CONNECTION_LOST"}) break for x in raw_request: parser.add_byte(chr_py3(x)) while True: request = parser.get_request() if not request: session.add_event({"type": "CONNECTION_LOST"}) break else: logdata = { "request": binascii.hexlify(bytearray(request.message_bytes)) } response = self.command_responder.respond(request) # real Kamstrup meters has delay in this interval gevent.sleep(random.uniform(0.24, 0.34)) if response: serialized_response = response.serialize() logdata["response"] = binascii.hexlify( serialized_response) logger.info( "Kamstrup traffic from %s: %s (%s)", address[0], logdata, session.id, ) sock.send(serialized_response) session.add_event(logdata) else: session.add_event(logdata) break except socket.timeout: logger.debug("Socket timeout, remote: %s. (%s)", address[0], session.id) session.add_event({"type": "CONNECTION_LOST"}) sock.close()