def main(cwd): decide_cwd(cwd) if config("cuckoo:database:connection") is None and \ not os.path.exists(get_cwd("cuckoo.db")): exit('Invalid Cuckoo Working Directory provided.') read_config() if not isinstance(THRESHOLD, int): print_help() exit() try: logging.basicConfig(level=logging.INFO, format='%(asctime)s, %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S', filename='{}/purge.log'.format(get_cwd("log"))) except IOError: exit('Error writing to log file: {}/purge.log'.format(get_cwd("log"))) global cuckoo_db, cuckoo_web, archiver cuckoo_db = CuckooDatabase() cuckoo_web = CuckooWeb() archiver = Archiver() if not DAEMON: while low_storage(): purge_day() return True else: while True: if low_storage(): task = cuckoo_db.oldest_id() purge((task.id, task.submit_id, task.sample_id)) else: sleep(600)
def test_default_cwd(self, p): set_cwd(tempfile.mkdtemp()) cuckoo_create() with chdir(cwd()): decide_cwd(".") cuckoo_init(logging.INFO, self.ctx) p.assert_called_once_with("cuckoo community")
def init(cls, cwd, event_ip, event_port, webserver): decide_cwd(cwd) cls.cwd = cwd cls.event_ip = event_ip cls.event_port = event_port if not webserver.startswith("http://") or \ not webserver.startswith("https://"): webserver = "http://%s" % webserver cls.webserver = webserver
def main(ctx, debug, quiet, nolog, maxcount, user, cwd): """Invokes the Cuckoo daemon or one of its subcommands. To be able to use different Cuckoo configurations on the same machine with the same Cuckoo installation, we use the so-called Cuckoo Working Directory (aka "CWD"). A default CWD is available, but may be overridden through the following options - listed in order of precedence. \b * Command-line option (--cwd) * Environment option ("CUCKOO_CWD") * Environment option ("CUCKOO") * Current directory (if the ".cwd" file exists) * Default value ("~/.cuckoo") """ decide_cwd(cwd) # Drop privileges. user and drop_privileges(user) ctx.user = user ctx.log = not nolog if quiet: level = logging.WARN elif debug: level = logging.DEBUG else: level = logging.INFO ctx.level = level # A subcommand will be invoked, so don't run Cuckoo itself. if ctx.invoked_subcommand: return try: cuckoo_init(level, ctx) cuckoo_main(maxcount) except CuckooCriticalError as e: message = red("{0}: {1}".format(e.__class__.__name__, e)) if len(log.handlers): log.critical(message) else: sys.stderr.write("{0}\n".format(message)) sys.exit(1) except SystemExit as e: if e.code: print e except: # Deal with an unhandled exception. message = exception_message() print message, traceback.format_exc()
# Copyright (C) 2018 Cuckoo Foundation. # This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org # See the file 'docs/LICENSE' for copying permission. import os import cuckoo from cuckoo.misc import load_signatures, decide_cwd def signature(name): for signature in cuckoo.signatures: if signature.name == name or signature.__class__.__name__ == name: return signature # For reasons. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings") # Initialize a fake CWD and actually load all Cuckoo Signature, once. # TODO Create a temporary CWD with a symbolic link to our $CWD/signatures/. decide_cwd(os.path.join(os.path.dirname(__file__), "..", "modules")) load_signatures()
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org # See the file 'docs/LICENSE' for copying permission. import os import sys from django.template.base import TemplateSyntaxError from cuckoo.common.colors import red from cuckoo.common.elastic import elastic from cuckoo.common.mongo import mongo from cuckoo.core.startup import init_rooter, init_routing from cuckoo.misc import cwd, decide_cwd if cwd(root=True) is None: decide_cwd(exists=True) # Connect to MongoDB (mandatory). if not mongo.init(): sys.exit(red( "In order to use the Cuckoo Web Interface it is required to have " "MongoDB up-and-running and enabled in Cuckoo. Please refer to our " "official documentation as well as the $CWD/conf/reporting.conf file." )) mongo.connect() # Connect to ElasticSearch (optional). elastic.init() elastic.connect()
def test_decide_cwd(): orig_cuckoo_cwd = os.environ.pop("CUCKOO_CWD", None) orig_cuckoo = os.environ.pop("CUCKOO", None) dirpath1 = tempfile.mkdtemp() dirpath2 = tempfile.mkdtemp() dirpath3 = tempfile.mkdtemp() assert decide_cwd(dirpath1) == dirpath1 assert decide_cwd() == os.path.abspath(os.path.expanduser("~/.cuckoo")) curdir = os.getcwd() os.chdir(dirpath2) open(".cwd", "wb").write("A"*40) assert decide_cwd() == os.path.abspath(".") os.chdir(curdir) os.environ["CUCKOO"] = dirpath2 assert decide_cwd(dirpath1) == dirpath1 assert decide_cwd() == dirpath2 os.environ["CUCKOO_CWD"] = dirpath3 assert decide_cwd(dirpath1) == dirpath1 assert decide_cwd() == dirpath3 with pytest.raises(CuckooStartupError) as e: decide_cwd(tempfile.mktemp(), exists=True) e.match("is not present") with pytest.raises(CuckooStartupError) as e: decide_cwd(dirpath1, exists=True) e.match("is not a proper CWD") Files.create(dirpath1, ".cwd", "A"*40) assert decide_cwd(dirpath1, exists=True) == dirpath1 # Cleanup. if orig_cuckoo: os.environ["CUCKOO"] = orig_cuckoo else: os.environ.pop("CUCKOO", None) if orig_cuckoo_cwd: os.environ["CUCKOO_CWD"] = orig_cuckoo_cwd else: os.environ.pop("CUCKOO_CWD", None)
from cuckoo.core.database import Database from cuckoo.misc import decide_cwd import time import subprocess import os import signal import threading import requests CUCKOO_DIR = "~/.cuckoo" decide_cwd(CUCKOO_DIR) CREATE_SNAPSHOT = "VBoxManage snapshot User1 take cuckoo" REMOVE_SNAPSHOT = "VBoxManage snapshot User1 delete cuckoo" CALL_CUCKOO = "cuckoo submit " KILL_CUCKOO = "ps -ef | grep cuckoo | awk '{print $2}' | xargs kill" def threaded(func): subprocess.call(func, shell=True) def my_call(func): # newpid = os.fork() # if newpid == 0: # subprocess.call(func, shell=True) # exit() # else: # return newpid t = threading.Thread(target=threaded, args=(func, )) t.start() # thread.start_new_thread(threaded, func)
def test_hardcoded_cwd_with_quote(self, p): set_cwd(tempfile.mkdtemp("foo ' bar")) cuckoo_create() decide_cwd(cwd()) cuckoo_init(logging.INFO, self.ctx) p.assert_called_once_with('cuckoo --cwd "%s" community' % cwd())
def test_hardcoded_cwd(self, p): set_cwd(tempfile.mkdtemp()) cuckoo_create() decide_cwd(cwd()) cuckoo_init(logging.INFO, self.ctx) p.assert_called_once_with("cuckoo --cwd %s community" % cwd())