summaryrefslogtreecommitdiffstats
path: root/peekaboo/daemon.py
blob: 0e25945e45145433494c5205b2f1abc011ee9e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
###############################################################################
#                                                                             #
# Peekaboo Extended Email Attachment Behavior Observation Owl                 #
#                                                                             #
# daemon.py                                                                   #
###############################################################################
#                                                                             #
# Copyright (C) 2016-2018  science + computing ag                             #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or (at       #
# your option) any later version.                                             #
#                                                                             #
# This program is distributed in the hope that it will be useful, but         #
# WITHOUT ANY WARRANTY; without even the implied warranty of                  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU           #
# General Public License for more details.                                    #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################


import os
import sys
import grp
import pwd
import stat
import logging
import SocketServer
import socket
import signal
from time import sleep
import json
from threading import Thread
from argparse import ArgumentParser
from sdnotify import SystemdNotifier
from peekaboo import _owl, __version__
from peekaboo.config import PeekabooConfig, PeekabooRulesetConfiguration
from peekaboo.db import PeekabooDatabase
from peekaboo.toolbox.sampletools import ConnectionMap
from peekaboo.queuing import JobQueue
from peekaboo.sample import SampleFactory
from peekaboo.exceptions import PeekabooDatabaseError
from peekaboo.toolbox.cuckoo import Cuckoo, CuckooEmbed, CuckooApi


logger = logging.getLogger(__name__)


class SignalHandler():
    """
    Signal handler.
    
    @author: Felix Bauer
    """
    def __init__(self):
        """ register custom signal handler """
        self.listeners = []

        signal.signal(signal.SIGINT, self.signal_handler)
        signal.signal(signal.SIGTERM, self.signal_handler)
        signal.signal(signal.SIGCHLD, self.signal_handler)

    def register_listener(self, listener):
        """ Register a listener object which is expected to implement a very
        simple interface: Method shut_down() is called if SIGINT or SIGTERM
        arrive, reap_children() is called if SIGCHLD arrives. Both are expected
        to defer actual handling of the condition. """
        self.listeners.append(listener)

    def signal_handler(self, sig, frame):
        """ catch signal and call appropriate methods in registered listener
        classes """
        if sig == signal.SIGINT or sig == signal.SIGTERM:
            logger.debug("SIGINT/TERM")

            # these should take serious care about being called across threads
            for listener in self.listeners:
                listener.shut_down()

        if sig == signal.SIGCHLD:
            logger.debug("SIGCHLD")
            for listener in self.listeners:
                listener.reap_children()


class PeekabooStreamServer(SocketServer.ThreadingUnixStreamServer):
    """
    Asynchronous server.

    @author: Sebastian Deiss
    """
    def __init__(self, server_address, request_handler_cls, job_queue,
            sample_factory, bind_and_activate = True, request_queue_size = 10):
        self.server_address = server_address
        self.__job_queue = job_queue
        self.__sample_factory = sample_factory
        self.request_queue_size = request_queue_size
        self.allow_reuse_address = True
        
        SocketServer.ThreadingUnixStreamServer.__init__(self, server_address,
                                                        request_handler_cls,
                                                        bind_and_activate=bind_and_activate)

    @property
    def job_queue(self):
        return self.__job_queue

    @property
    def sample_factory(self):
        return self.__sample_factory

    def shutdown_request(self, request):
        """ Keep the connection alive until Cuckoo reports back, so the results can be send to the client. """
        # TODO: Find a better solution.
        pass

    def server_close(self):
        # no new connections from this point on
        os.remove(self.server_address)
        return SocketServer.ThreadingUnixStreamServer.server_close(self)


class PeekabooStreamRequestHandler(SocketServer.StreamRequestHandler):
    """
    Request handler used by PeekabooStreamServer to handle analysis requests.

    @author: Sebastian Deiss
    """
    def setup(self):
        SocketServer.StreamRequestHandler.setup(self)
        self.job_queue = self.server.job_queue
        self.sample_factory = self.server.sample_factory

    def handle(self):
        """
        Handles an analysis request. This is expected to be a JSON structure
        containing the path of the directory / file to analyse. Structure:

        [ { "full_name": "<path>",
            "name_declared": ...,
            ... },
          { ... },
          ... ]

        The maximum buffer size is 16 KiB, because JSON incurs some bloat.
        """
        self.request.sendall('Hallo das ist Peekaboo\n\n')
        request = self.request.recv(1024 * 16).rstrip()

        try:
            parts = json.loads(request)
        except:
            self.request.sendall('FEHLER: Ungueltiges JSON.')
            logger.error('Invalid JSON in request.')
            return

        if type(parts) not in (list, tuple):
            self.request.sendall('FEHLER: Ungueltiges Datenformat.')
            logger.error('Invalid data structure.')
            return

        for_analysis = []
        for part in parts:
            if not part.has_key('full_name'):
                self.request.sendall('FEHLER: Unvollstaendige Datenstruktur.')
                logger.error('Incomplete data structure.')
                return

            path = part['full_name']
            logger.info("Got run_analysis request for %s" % path)
            if not os.path.exists(path):
                self.request.sendall('FEHLER: Pfad existiert nicht oder '
                        'Zugriff verweigert.')
                logger.error('Path does not exist or no permission '
                        'to access it.')
                return

            if not os.path.isfile(path):
                self.request.sendall('FEHLER: Eingabe ist keine Datei.')
                logger.error('Input is not a file')
                return

            sample = self.sample_factory.make_sample(path, metainfo = part,
                    socket = self.request)
            for_analysis.append(sample)
            logger.debug('Created sample %s' % sample)

        # introduced after an issue where results were reported
        # before all files could be added.
        for sample in for_analysis:
            self.job_queue.submit(sample, self.__class__)


def run():
    """ Runs the Peekaboo daemon. """
    arg_parser = ArgumentParser(
        description='Peekaboo Extended Email Attachment Behavior Observation Owl'
    )
    arg_parser.add_argument(
        '-c', '--config',
        action='store',
        required=False,
        default=os.path.join('./peekaboo.conf'),
        help='The configuration file for Peekaboo.'
    )
    arg_parser.add_argument(
        '-d', '--debug',
        action='store_true',
        required=False,
        default=False,
        help="Run Peekaboo in debug mode regardless of what's specified in the configuration."
    )
    arg_parser.add_argument(
        '-D', '--daemon',
        action='store_true',
        required=False,
        default=False,
        help='Run Peekaboo in daemon mode (suppresses the logo to be written to STDOUT).'
    )
    args = arg_parser.parse_args()

    if not args.daemon:
        print(_owl)
    else:
        print('Starting Peekaboo %s.' % __version__)

    # read configuration
    if not os.path.isfile(args.config):
        print('Failed to read config, files does not exist.') # logger doesn't exist here
        sys.exit(1)

    config = PeekabooConfig(args.config)

    # Check if CLI arguments override the configuration
    if args.debug:
        config.change_log_level('DEBUG')

    # Log the configuration options if we are in debug mode
    if config.log_level == logging.DEBUG:
        logger.debug(config.__str__())

    # establish a connection to the database
    try:
        db_con = PeekabooDatabase(config.db_url)
    except PeekabooDatabaseError as e:
        logging.exception(e)
        sys.exit(1)
    except Exception as e:
        logger.critical('Failed to establish a connection to the database.')
        logger.exception(e)
        sys.exit(1)

    # Import debug module if we are in debug mode
    debugger = None
    if config.use_debug_module:
        from peekaboo.debug import PeekabooDebugger
        debugger = PeekabooDebugger()
        debugger.start()

    if os.getuid() == 0:
        logger.warning('Peekaboo should not run as root.')
        # drop privileges to user
        os.setgid(grp.getgrnam(config.group)[2])
        os.setuid(pwd.getpwnam(config.user)[2])
        # set $HOME to the users home directory
        # (VirtualBox must access the configs)
        os.environ['HOME'] = pwd.getpwnam(config.user)[5]
        logger.info("Dropped privileges to user %s and group %s"
                    % (config.user, config.group))
        logger.debug('$HOME is ' + os.environ['HOME'])

    # write PID file
    pid = str(os.getpid())
    with open(config.pid_file, "w") as pidfile:
        pidfile.write("%s\n" % pid)

    systemd = SystemdNotifier()

    # workers of the job queue need the ruleset configuration to create the
    # ruleset engine with it
    ruleset_config = PeekabooRulesetConfiguration(config.ruleset_config)
    job_queue = JobQueue(worker_count = config.worker_count,
            ruleset_config = ruleset_config)
    connection_map = ConnectionMap()

    if config.cuckoo_mode == "embed":
        cuckoo = CuckooEmbed(job_queue, connection_map, config.interpreter,
                config.cuckoo_exec, config.cuckoo_submit,
                config.cuckoo_storage)
    # otherwise it's the new API method and default
    else:
        cuckoo = CuckooApi(job_queue, connection_map, config.cuckoo_url,
                config.cuckoo_poll_interval)

    sig_handler = SignalHandler()
    sig_handler.register_listener(cuckoo)

    # Factory producing almost identical samples providing them with global
    # config values and references to other objects they need, such as cuckoo,
    # database connection and connection map.
    sample_factory = SampleFactory(cuckoo, db_con, connection_map,
                config.sample_base_dir, config.job_hash_regex,
                config.keep_mail_data)

    # Try three times to start SocketServer
    for i in range(0, 3):
        try:
            # We only want to accept 2 * worker_count connections.
            server = PeekabooStreamServer(config.sock_file,
                    PeekabooStreamRequestHandler,
                    job_queue = job_queue,
                    sample_factory = sample_factory,
                    request_queue_size = config.worker_count * 2)
            break
        except socket.error, msg:
            logger.warning("SocketServer couldn't start (%i): %s" % (i, msg))
    if not server:
        logger.error('Fatal: Couldn\'t initialise Peekaboo Server')
        sys.exit(1)

    runner = Thread(target=server.serve_forever)
    runner.daemon = True

    rc = 1
    try:
        runner.start()
        logger.info('Peekaboo server is listening on %s' % server.server_address)

        os.chmod(config.sock_file, stat.S_IWOTH | stat.S_IREAD |
                                   stat.S_IWRITE | stat.S_IRGRP |
                                   stat.S_IWGRP | stat.S_IWOTH)

        systemd.notify("READY=1")
        # If this dies Peekaboo dies, since this is the main thread. (legacy)
        rc = cuckoo.do()
    except Exception as e:
        logger.exception(e)
    finally:
        server.shutdown()
        server.server_close()
        job_queue.shut