Example #1
0
class BannerService(ServerCustomService):
    """Simple service that will print out banner and hang."""
    def __init__(self, *args, **kwargs):
        """Initialize service."""
        super(BannerService, self).__init__(*args, **kwargs)
        self.server = None

    def _send_alert(self, originating_ip, originating_port):
        params = {
            "event_type": BANNER_ALERT_TYPE_NAME,
            "originating_ip": originating_ip,
            "originating_port": originating_port,
        }
        self.add_alert_to_queue(params)

    def on_server_start(self):
        """Start banner service."""
        requestHandler = BannerRequestHandler
        requestHandler.banner = self.service_args.get(BANNER)
        requestHandler.alert = self._send_alert

        port = int(self.service_args.get(PORT))
        self.server = ThreadingTCPServer(("", port), requestHandler)

        self.signal_ready()
        self.logger.info("Starting Banner service on port {}".format(port))
        self.server.serve_forever()

    def on_server_shutdown(self):
        """Stop banner service."""
        if self.server:
            self.server.shutdown()
            self.logger.info("Banner service stopped")
            self.server = None

    def test(self):
        """Test service alerts and return a list of triggered event types."""
        event_types = list()

        self.logger.debug("executing service test")
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(3)
        s.connect(("127.0.0.1", int(self.service_args.get(PORT))))
        s.close()
        event_types.append(BANNER_ALERT_TYPE_NAME)

        return event_types

    def __str__(self):
        """Service name."""
        return "Banner"
Example #2
0
class SockListener(BaseListener):
    handlerCls = SockServiceHandler

    def __init__(self, *args, **kwargs):
        super(SockListener, self).__init__(*args, **kwargs)
        # inject service_handler
        SockBoltHandler.service_handler = self.handler
        self.server = ThreadingTCPServer(self.address, SockBoltHandler)

    def run_forever(self):
        self.server.serve_forever()

    def shutdown(self):
        self.server.shutdown()
class SSHServer(object):
    """SSHServer object."""

    server = None

    def run(self, port):  # noqa: D102
        requestHandler = SSHRequestHandler
        requestHandler.alert = self.alert

        self.server = ThreadingTCPServer(("", port), requestHandler)
        # This prevents the timewait on the socket that prevents us from restarting the honeypot right
        # away after closing
        self.server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
                                      1)
        self.server.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER,
                                      struct.pack('ii', 1, 0))

        self.server.serve_forever()

    def shutdown(self):  # noqa: D102
        if not self.server:
            return
        self.server.shutdown()