Beispiel #1
0
    def __init__(self, work_dir, config, curses_screen=None):

        """
            Main class which runs Beeswarm in Client mode.

        :param work_dir: Working directory (usually the current working directory)
        :param config_arg: Beeswarm configuration dictionary.
        :param curses_screen: Contains a curses screen object, if UI is enabled. Default is None.
        """
        self.run_flag = True
        self.curses_screen = curses_screen

        with open('beeswarmcfg.json', 'r') as config_file:
            self.config = json.load(config_file, object_hook=asciify)

        # write ZMQ keys to files - as expected by pyzmq
        extract_keys(work_dir, config)

        BaitSession.client_id = self.config['general']['id']
        # TODO: Handle peering in other place
        BaitSession.honeypot_id = self.config['general']['id']

        if self.config['public_ip']['fetch_ip']:
            self.my_ip = urllib2.urlopen('http://api-sth01.exip.org/?call=ip').read()
            logger.info('Fetched {0} as my external ip.'.format(self.my_ip))
        else:
            self.my_ip = '127.0.0.1'

        self.status = {
            'mode': 'Client',
            'total_bees': 0,
            'active_bees': 0,
            'enabled_bees': [],
            'client_id': self.config['general']['client_id'],
            'managment_url': self.config['beeswarm_server']['managment_url'],
            'ip_address': self.my_ip
        }

        self.dispatchers = {}
        self.dispatcher_greenlets = []

        if self.curses_screen is not None:
            self.uihandler = ClientUIHandler(self.status, self.curses_screen)
            Greenlet.spawn(self.show_status_ui)
Beispiel #2
0
class Client(object):
    def __init__(self, work_dir, config, curses_screen=None):

        """
            Main class which runs Beeswarm in Client mode.

        :param work_dir: Working directory (usually the current working directory)
        :param config_arg: Beeswarm configuration dictionary.
        :param curses_screen: Contains a curses screen object, if UI is enabled. Default is None.
        """
        self.run_flag = True
        self.curses_screen = curses_screen

        with open('beeswarmcfg.json', 'r') as config_file:
            self.config = json.load(config_file, object_hook=asciify)

        # write ZMQ keys to files - as expected by pyzmq
        extract_keys(work_dir, config)

        BaitSession.client_id = self.config['general']['id']
        # TODO: Handle peering in other place
        BaitSession.honeypot_id = self.config['general']['id']

        if self.config['public_ip']['fetch_ip']:
            self.my_ip = urllib2.urlopen('http://api-sth01.exip.org/?call=ip').read()
            logger.info('Fetched {0} as my external ip.'.format(self.my_ip))
        else:
            self.my_ip = '127.0.0.1'

        self.status = {
            'mode': 'Client',
            'total_bees': 0,
            'active_bees': 0,
            'enabled_bees': [],
            'client_id': self.config['general']['client_id'],
            'managment_url': self.config['beeswarm_server']['managment_url'],
            'ip_address': self.my_ip
        }

        self.dispatchers = {}
        self.dispatcher_greenlets = []

        if self.curses_screen is not None:
            self.uihandler = ClientUIHandler(self.status, self.curses_screen)
            Greenlet.spawn(self.show_status_ui)

    def show_status_ui(self):
        self.uihandler.run()

    def start(self):
        """
            Starts sending client bees to the configured Honeypot.
        """
        logger.info('Starting client.')

        sessions = {}

        #greenlet to consume and maintain data in sessions list
        self.sessions_consumer = consumer.Consumer(sessions, self.config, self.status)
        gevent.spawn(self.sessions_consumer.start_handling)

        capabilities = []
        for b in clientbase.ClientBase.__subclasses__():
            capability_name = b.__name__.lower()

            if capability_name not in self.config['capabilities']:
                logger.warning(
                    "Not loading {0} bee because it has no option in configuration file.".format(b.__name__))
                continue
                #skip loading if disabled
            if not self.config['capabilities'][capability_name]['enabled']:
                logger.warning(
                    "Not loading {0} bee because it is disabled in the configuration file.".format(b.__name__))
                continue

            options = self.config['capabilities'][capability_name]
            bee = b(sessions, options)
            capabilities.append(bee)
            self.status['enabled_bees'].append(capability_name)
            logger.debug('Adding {0} as a capability'.format(bee.__class__.__name__))

        self.dispatcher_greenlets = []
        for bee in capabilities:
            dispatcher = BeeDispatcher(self.config, bee, self.my_ip)
            self.dispatchers[bee.__class__.__name__] = dispatcher
            current_greenlet = Greenlet(dispatcher.start)
            self.dispatcher_greenlets.append(current_greenlet)
            current_greenlet.start()

        drop_privileges()
        gevent.joinall(self.dispatcher_greenlets)

    def stop(self):
        """
            Stop sending bees.
        """
        for g in self.dispatcher_greenlets:
            g.kill()
        if self.curses_screen is not None:
            self.uihandler.stop()
        self.sessions_consumer.stop_handling()
        logger.info('All clients stopped')
        sys.exit(0)