Exemple #1
0
 def __init__(self, template, template_directory, args, timeout=0):
     self.timeout = timeout
     self.command_responder = CommandResponder(template)
     self.server_active = True
     self.server = None
     conpot_core.get_databus().observe_value('reboot_signal', self.reboot)
     logger.info('Kamstrup protocol server initialized.')
Exemple #2
0
 def __init__(self, template, template_directory, args, timeout=0):
     self.template = template
     self.timeout = timeout
     self.command_responder = CommandResponder()
     self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
     logger.info('Kamstrup management protocol server initialized.')
     self.server = None
Exemple #3
0
 def __init__(self, template, template_directory, args, timeout=0):
     self.timeout = timeout
     self.command_responder = CommandResponder(template)
     self.server_active = True
     self.server = None
     conpot_core.get_databus().observe_value('reboot_signal', self.reboot)
     logger.info('Kamstrup protocol server initialized.')
 def __init__(self, template, template_directory, args, timeout=0):
     self.template = template
     self.timeout = timeout
     self.command_responder = CommandResponder(template)
     self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
     logger.info('Kamstrup management protocol server initialized.')
     self.server = None
class KamstrupManagementServer(object):
    def __init__(self, template, template_directory, args, timeout=0):
        self.template = template
        self.timeout = timeout
        self.command_responder = CommandResponder(template)
        self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
        logger.info('Kamstrup management protocol server initialized.')

    def handle(self, sock, address):
        session = conpot_core.get_session('kamstrup_management_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'})

        try:
            sock.send(
                self.banner.format(
                    conpot_core.get_databus().get_value("mac_address")))

            while True:
                request = sock.recv(1024)
                if not request:
                    logger.info('Client disconnected. ({0})'.format(
                        session.id))
                    session.add_event({'type': 'CONNECTION_LOST'})
                    break

                logdata = {'request': request}
                response = self.command_responder.respond(request)
                logdata['response'] = response
                logger.debug(
                    'Kamstrup management traffic from {0}: {1} ({2})'.format(
                        address[0], logdata, session.id))
                session.add_event(logdata)
                gevent.sleep(0.25)  # TODO measure delay and/or RTT

                if response is None:
                    session.add_event({'type': 'CONNECTION_LOST'})
                    break
                sock.send(response)

        except socket.timeout:
            logger.debug('Socket timeout, remote: {0}. ({1})'.format(
                address[0], session.id))
            session.add_event({'type': 'CONNECTION_LOST'})

        sock.close()

    def start(self, host, port):
        connection = (host, port)
        server = StreamServer(connection, self.handle)
        logger.info(
            'Kamstrup management protocol server started on: {0}'.format(
                connection))
        server.start()
class KamstrupManagementServer(object):
    def __init__(self, template, template_directory, args, timeout=0):
        self.template = template
        self.timeout = timeout
        self.command_responder = CommandResponder(template)
        self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
        logger.info('Kamstrup management protocol server initialized.')
        self.server = None

    def handle(self, sock, address):
        session = conpot_core.get_session('kamstrup_management_protocol', address[0], address[1])
        logger.info('New Kamstrup connection from %s:%s. (%s)', address[0], address[1], session.id)
        session.add_event({'type': 'NEW_CONNECTION'})

        try:
            sock.send(self.banner.format(
                conpot_core.get_databus().get_value("mac_address")))

            while True:
                request = sock.recv(1024)
                if not request:
                    logger.info('Kamstrup client disconnected. (%s)', session.id)
                    session.add_event({'type': 'CONNECTION_LOST'})
                    break

                logdata = {'request': request}
                response = self.command_responder.respond(request)
                logdata['response'] = response
                logger.info('Kamstrup management traffic from %s: %s (%s)', address[0], logdata, session.id)
                session.add_event(logdata)
                gevent.sleep(0.25)  # TODO measure delay and/or RTT

                if response is None:
                    session.add_event({'type': 'CONNECTION_LOST'})
                    break
                sock.send(response)

        except socket.timeout:
            logger.debug('Socket timeout, remote: %s. (%s)', address[0], session.id)
            session.add_event({'type': 'CONNECTION_LOST'})

        sock.close()

    def start(self, host, port):
        connection = (host, port)
        self.server = StreamServer(connection, self.handle)
        logger.info('Kamstrup management protocol server started on: %s', connection)
        self.server.start()

    def stop(self):
        self.server.stop()
class KamstrupManagementServer(object):
    def __init__(self, template, timeout=0):
        self.template = template
        self.timeout = timeout
        self.command_responder = CommandResponder(template)
        self.banner = "\r\nWelcome...\r\nConnected to [{0}]\r\n"
        logger.info('Kamstrup management protocol server initialized.')

    def handle(self, sock, address):
        session = conpot_core.get_session('kamstrup_management_protocol', address[0], address[1])
        logger.info('New connection from {0}:{1}. ({2})'.format(address[0], address[1], session.id))

        try:
            sock.send(self.banner.format(
                conpot_core.get_databus().get_value("mac_address")))

            while True:
                request = sock.recv(1024)
                if not request:
                    logger.info('Client disconnected. ({0})'.format(session.id))
                    break

                logdata = {'request': request}
                response = self.command_responder.respond(request)
                logdata['response'] = response
                logger.debug('Kamstrup management traffic from {0}: {1} ({2})'.format(address[0], logdata, session.id))
                session.add_event(logdata)
                gevent.sleep(0.25)  # TODO measure delay and/or RTT

                if response is None:
                    break
                sock.send(response)

        except socket.timeout:
            logger.debug('Socket timeout, remote: {0}. ({1})'.format(address[0], session.id))

        sock.close()

    def get_server(self, host, port):
        connection = (host, port)
        server = StreamServer(connection, self.handle)
        logger.info('Kamstrup management protocol server started on: {0}'.format(connection))
        return server
class KamstrupServer(object):
    def __init__(self, template, template_directory, args, timeout=0):
        self.timeout = timeout
        self.command_responder = CommandResponder(template)
        self.server_active = True
        conpot_core.get_databus().observe_value('reboot_signal', self.reboot)
        logger.info('Kamstrup protocol server initialized.')

    # pretending reboot... really just closing connecting while "rebooting"
    def reboot(self, key):
        assert (key == 'reboot_signal')
        self.server_active = False
        logger.debug('Pretending server reboot')
        gevent.spawn_later(2, self.set_reboot_done)

    def set_reboot_done(self):
        logger.debug('Stopped pretending reboot')
        self.server_active = True

    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 start(self, host, port):
        connection = (host, port)
        server = StreamServer(connection, self.handle)
        logger.info(
            'Kamstrup protocol server started on: {0}'.format(connection))
        server.start()
Exemple #9
0
class KamstrupServer(object):
    def __init__(self, template, template_directory, args, timeout=0):
        self.timeout = timeout
        self.command_responder = CommandResponder(template)
        self.server_active = True
        self.server = None
        conpot_core.get_databus().observe_value('reboot_signal', self.reboot)
        logger.info('Kamstrup protocol server initialized.')

    # pretending reboot... really just closing connecting while "rebooting"
    def reboot(self, key):
        assert(key == 'reboot_signal')
        self.server_active = False
        logger.info('Pretending server reboot')
        gevent.spawn_later(2, self.set_reboot_done)

    def set_reboot_done(self):
        logger.info('Stopped pretending reboot')
        self.server_active = True

    def handle(self, sock, address):
        session = conpot_core.get_session('kamstrup_protocol', address[0], address[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(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()

    def start(self, host, port):
        connection = (host, port)
        self.server = StreamServer(connection, self.handle)
        logger.info('Kamstrup protocol server started on: %s', connection)
        self.server.start()

    def stop(self):
        self.server.stop()