def test_import_confirm(self, p): set_cwd(tempfile.mkdtemp()) p.return_value = True dirpath = init_legacy_analyses() os.makedirs(os.path.join(dirpath, "lib", "cuckoo", "common")) open(os.path.join( dirpath, "lib", "cuckoo", "common", "constants.py" ), "wb").write(constants_11_py) shutil.copytree( "tests/files/conf/110_plain", os.path.join(dirpath, "conf") ) filepath = os.path.join(dirpath, "conf", "cuckoo.conf") buf = open(filepath, "rb").read() open(filepath, "wb").write(buf.replace( "connection =", "connection = %s" % self.URI )) try: main.main( ("--cwd", cwd(), "import", dirpath), standalone_mode=False ) except CuckooOperationalError as e: assert "SQL database dump as the command" in e.message assert not is_linux() return db = Database() db.connect() assert db.engine.name == self.ENGINE assert open(cwd("logs", "a.txt", analysis=1), "rb").read() == "a" assert config("cuckoo:database:connection") == self.URI assert db.count_tasks() == 2
def test_import_confirm(self, p): set_cwd(tempfile.mkdtemp()) p.return_value = True dirpath = init_legacy_analyses() os.makedirs(os.path.join(dirpath, "lib", "cuckoo", "common")) open(os.path.join(dirpath, "lib", "cuckoo", "common", "constants.py"), "wb").write(constants_11_py) shutil.copytree("tests/files/conf/110_plain", os.path.join(dirpath, "conf")) filepath = os.path.join(dirpath, "conf", "cuckoo.conf") buf = open(filepath, "rb").read() open(filepath, "wb").write( buf.replace("connection =", "connection = %s" % self.URI)) try: main.main(("--cwd", cwd(), "import", dirpath), standalone_mode=False) except CuckooOperationalError as e: assert "SQL database dump as the command" in e.message assert not is_linux() return db = Database() db.connect() assert db.engine.name == self.ENGINE assert open(cwd("logs", "a.txt", analysis=1), "rb").read() == "a" assert config("cuckoo:database:connection") == self.URI assert db.count_tasks() == 2
def test_symlink(self): if not is_linux(): return # Include all Yara rules from binaries/ into memory/ as well. os.symlink(cwd("yara", "binaries"), cwd("yara", "memory", "bins")) init_yara() assert len(list(File.yara_rules["memory"])) == 5
def test_cuckoo_rooter(): try: if is_linux(): do_cuckoo_rooter() except: raise finally: mock.patch.stopall()
def test_sqldump(self, p): set_cwd(tempfile.mkdtemp()) p.return_value = True try: sqldump(self.URI, "/tmp") assert os.path.getsize(cwd("backup.sql")) except CuckooOperationalError as e: assert "SQL database dump as the command" in e.message assert not is_linux()
def start(self): """Retrieve an operational socks5 proxy and start redsocks with a config for this proxy.""" self.process = None self.conf_path = None if self.task.options.get("route", "") != "socks5": return False if not is_linux(): log.warning( "The Redsocks module is currently only supported on Linux") raise CuckooDisableModule country = self.task.options.get("socks5.country") socks5 = socks5_manager.acquire(country=country) if not socks5: if country: log.error( "Cannot start forwarding traffic over socks5. " "No operational socks5 server available for country: %s", country) else: log.error("Cannot start forwarding traffic over socks5. No " "operational socks5 server available") raise CuckooDisableModule log.debug("Using socks5 proxy in country: %s", socks5.country) local_ip = config("cuckoo:resultserver:ip") local_port = self.get_tcp_port(local_ip) logfile = cwd("redsocks.log", analysis=self.task.id) self.conf_path = self.gen_config(logfile, local_ip, local_port, socks5.host, socks5.port, socks5.username, socks5.password) try: self.process = subprocess.Popen( [self.options.redsocks, "-c", self.conf_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) except (OSError, ValueError) as e: log.exception("Failed to start redsocks (config=%s). Error: %s", self.conf_path, e) raise self.task.options["socks5.host"] = socks5.host self.task.options["socks5.port"] = socks5.port self.task.options["socks5.localport"] = local_port log.info("Started redsocks with PID: %s (local_port=%s, config=%s)", self.process.pid, local_port, self.conf_path)
class TestAppsWithCWD(object): def setup(self): set_cwd(tempfile.mkdtemp()) cuckoo_create() @mock.patch("cuckoo.main.load_signatures") @mock.patch("cuckoo.main.cuckoo_main") def test_main(self, p, q): p.side_effect = SystemExit(0) # Ensure that the "latest" binary value makes sense so that the # "run community command" exception is not thrown. mkdir(cwd("monitor", open(cwd("monitor", "latest")).read().strip())) main.main(("--cwd", cwd(), "-d", "--nolog"), standalone_mode=False) q.assert_called_once() @mock.patch("cuckoo.main.load_signatures") @mock.patch("cuckoo.main.log") def test_main_exception(self, p, q): q.side_effect = Exception("this is a test") with pytest.raises(SystemExit): main.main(("--cwd", cwd(), "-d", "--nolog"), standalone_mode=False) p.exception.assert_called_once() def test_api(self): with mock.patch("cuckoo.main.cuckoo_api") as p: p.return_value = None main.main(("--cwd", cwd(), "api"), standalone_mode=False) p.assert_called_once_with("localhost", 8090, False) if is_linux(): @mock.patch("cuckoo.main.cuckoo_rooter") def test_rooter_abort(self, p, capsys): p.side_effect = KeyboardInterrupt main.main(("--cwd", cwd(), "rooter"), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting the Cuckoo Rooter" in out def test_community(self): with mock.patch("cuckoo.main.fetch_community") as p: p.return_value = None main.main(("--cwd", cwd(), "community"), standalone_mode=False) p.assert_called_once_with(force=False, branch="master", filepath=None) @mock.patch("cuckoo.main.fetch_community") def test_community_abort(self, p, capsys): p.side_effect = KeyboardInterrupt main.main(("--cwd", cwd(), "community"), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting fetching of" in out def test_clean(self): with mock.patch("cuckoo.main.cuckoo_clean") as p: p.return_value = None main.main(("--cwd", cwd(), "clean"), standalone_mode=False) p.assert_called_once_with() @mock.patch("cuckoo.main.cuckoo_clean") def test_clean_abort(self, p, capsys): p.side_effect = KeyboardInterrupt main.main(("--cwd", cwd(), "clean"), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting cleaning up of" in out def test_submit(self): with mock.patch("cuckoo.main.submit_tasks") as p: p.return_value = [] main.main( ("--cwd", cwd(), "submit", Files.create( cwd(), "a.txt", "hello")), standalone_mode=False) @mock.patch("cuckoo.main.submit_tasks") def test_submit_abort(self, p, capsys): p.side_effect = KeyboardInterrupt main.main( ("--cwd", cwd(), "submit", Files.create(cwd(), "a.txt", "hello")), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting submission of" in out def test_dnsserve(self): with mock.patch("cuckoo.main.cuckoo_dnsserve") as p: p.return_value = None main.main(("--cwd", cwd(), "dnsserve"), standalone_mode=False) p.assert_called_once_with("0.0.0.0", 53, None, None) @mock.patch("cuckoo.main.cuckoo_dnsserve") def test_dnsserve_abort(self, p, capsys): p.side_effect = KeyboardInterrupt main.main(("--cwd", cwd(), "dnsserve"), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting Cuckoo DNS Serve" in out @mock.patch("django.core.management.execute_from_command_line") def test_web_noargs(self, p): curdir = os.getcwd() main.main(("--cwd", cwd(), "web"), standalone_mode=False) p.assert_called_once_with(("cuckoo", "runserver", "localhost:8000")) os.chdir(curdir) @mock.patch("django.core.management.execute_from_command_line") def test_web_args(self, p): curdir = os.getcwd() main.main(("--cwd", cwd(), "web", "foo", "bar"), standalone_mode=False) p.assert_called_once_with(("cuckoo", "foo", "bar")) os.chdir(curdir) def test_machine(self): with mock.patch("cuckoo.main.cuckoo_machine") as p: p.return_value = None main.main( ("--cwd", cwd(), "machine", "cuckoo2", "1.2.3.4", "--add"), standalone_mode=False) p.assert_called_once_with("cuckoo2", "add", "1.2.3.4", "windows", None, None, None, None, None) def test_machine_add(self): cuckoo_machine("cuckoo2", "add", "1.2.3.4", "windows", None, None, None, None, None) assert config("virtualbox:virtualbox:machines") == [ "cuckoo1", "cuckoo2", ] assert config("virtualbox:cuckoo2:ip") == "1.2.3.4" assert config("virtualbox:cuckoo2:platform") == "windows" def test_machine_delete(self): cuckoo_machine("cuckoo1", "delete", None, None, None, None, None, None, None) assert config("virtualbox:virtualbox:machines") == [] # TODO This might require a little tweak. with pytest.raises(CuckooConfigurationError) as e: config("virtualbox:cuckoo1:label", strict=True) e.match("No such configuration value exists") @mock.patch("click.confirm") @mock.patch("cuckoo.main.import_cuckoo") def test_import_confirm(self, p, q, capsys): p.return_value = None q.return_value = True dirpath = tempfile.mkdtemp() main.main(("--cwd", cwd(), "import", dirpath), standalone_mode=False) p.assert_called_once_with(None, "copy", dirpath) out, err = capsys.readouterr() assert "understand that, depending on the mode" in out @mock.patch("click.confirm") def test_import_noconfirm(self, p, capsys): p.return_value = False with pytest.raises(SystemExit) as e: main.main(("--cwd", cwd(), "import", tempfile.mkdtemp()), standalone_mode=False) e.match("Aborting operation") @mock.patch("click.confirm") @mock.patch("cuckoo.main.import_cuckoo") def test_import_abort(self, p, q, capsys): p.side_effect = KeyboardInterrupt q.return_value = True main.main(("--cwd", cwd(), "import", tempfile.mkdtemp()), standalone_mode=False) out, _ = capsys.readouterr() assert "Aborting import of Cuckoo instance" in out def test_dist_server(self): with mock.patch("cuckoo.main.cuckoo_distributed") as p: p.return_value = None main.main(("--cwd", cwd(), "distributed", "server"), standalone_mode=False) p.assert_called_once_with("localhost", 9003, False) def test_dist_instance(self): with mock.patch("cuckoo.main.cuckoo_distributed_instance") as p: p.return_value = None main.main(("--cwd", cwd(), "distributed", "instance", "name"), standalone_mode=False) p.assert_called_once_with("name") def test_dist_migrate(self): with mock.patch("cuckoo.main.subprocess.check_call") as p: p.return_value = None main.main(("--cwd", cwd(), "distributed", "migrate"), standalone_mode=False) p.assert_called_once_with( ["alembic", "-x", "cwd=%s" % cwd(), "upgrade", "head"], cwd=cwd("distributed", "migration", private=True))
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org # See the file 'docs/LICENSE' for copying permission. import errno import json import mock import pytest import socket import tempfile from cuckoo.apps import rooter as r from cuckoo.core.rooter import rooter from cuckoo.main import main, cuckoo_create from cuckoo.misc import is_linux, set_cwd, version if is_linux(): @mock.patch("cuckoo.main.subprocess") def test_verbose_mode(p): main.main(("-d", "rooter", "--sudo"), standalone_mode=False) p.call.assert_called_once() assert p.call.call_args[0][0][:4] == [ "sudo", mock.ANY, "--debug", "rooter", ] def test_version(): assert r.version() == { "version": version, "features": [], } def test_nic_available():
def test_is_macosx(): assert is_windows() is False assert is_linux() is False assert is_macosx() is True