Ejemplo n.º 1
0
def test_temppath():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()

    assert temppath() == tempfile.gettempdir()

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "",
            },
        },
    })
    assert temppath() == tempfile.gettempdir()

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/tmp",
            },
        },
    })
    assert temppath() == tempfile.gettempdir()

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/custom/directory",
            },
        },
    })
    assert temppath() == "/custom/directory"
Ejemplo n.º 2
0
 def test_empty_move(self):
     oldfilepath = Files.temp_put("hello")
     movesql("sqlite:///%s" % oldfilepath, "move", temppath())
     assert not os.path.exists(oldfilepath)
     assert os.path.exists(cwd("cuckoo.db"))
     assert not os.path.islink(cwd("cuckoo.db"))
     assert open(cwd("cuckoo.db"), "rb").read() == "hello"
Ejemplo n.º 3
0
 def test_empty_move(self):
     oldfilepath = Files.temp_put("hello")
     movesql("sqlite:///%s" % oldfilepath, "move", temppath())
     assert not os.path.exists(oldfilepath)
     assert os.path.exists(cwd("cuckoo.db"))
     assert not os.path.islink(cwd("cuckoo.db"))
     assert open(cwd("cuckoo.db"), "rb").read() == "hello"
Ejemplo n.º 4
0
 def test_create_file_abs(self):
     filepath = os.path.join(temppath(), "foobar.txt")
     r = self.post("/tasks/create/file", data={
         "file": werkzeug.FileStorage(io.BytesIO("foobar"), filepath),
     })
     t = db.view_task(json.loads(r.data)["task_id"])
     assert open(t.target, "rb").read() == "foobar"
     assert t.target != filepath
     assert t.target.endswith("foobar.txt")
Ejemplo n.º 5
0
def test_tmp_permissions_true():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create(
        cfg={"cuckoo": {
            "cuckoo": {
                "tmppath": tempfile.mkdtemp(),
            }
        }})
    assert os.path.isdir(temppath())
Ejemplo n.º 6
0
 def test_create_file_abs(self):
     filepath = os.path.join(temppath(), "foobar.txt")
     r = self.app.post("/tasks/create/file", data={
         "file": werkzeug.FileStorage(io.BytesIO("foobar"), filepath),
     })
     t = db.view_task(json.loads(r.data)["task_id"])
     assert open(t.target, "rb").read() == "foobar"
     assert t.target != filepath
     assert t.target.endswith("foobar.txt")
Ejemplo n.º 7
0
def test_tmp_permissions_true():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": tempfile.mkdtemp(),
            }
        }
    })
    assert os.path.isdir(temppath())
Ejemplo n.º 8
0
 def test_create_submit_abs(self):
     filepath = os.path.join(temppath(), "foobar.bat")
     r = self.post("/tasks/create/submit", data={
         "file": werkzeug.FileStorage(io.BytesIO("foobar"), filepath),
     })
     task_ids = json.loads(r.data)["task_ids"]
     assert len(task_ids) == 1
     t = db.view_task(task_ids[0])
     assert open(t.target, "rb").read() == "foobar"
     assert t.target != filepath
     assert t.target.endswith("foobar.bat")
Ejemplo n.º 9
0
 def test_create_submit_abs(self):
     filepath = os.path.join(temppath(), "foobar.bat")
     r = self.post("/tasks/create/submit", data={
         "file": werkzeug.FileStorage(io.BytesIO("foobar"), filepath),
     })
     task_ids = json.loads(r.data)["task_ids"]
     assert len(task_ids) == 1
     t = db.view_task(task_ids[0])
     assert open(t.target, "rb").read() == "foobar"
     assert t.target != filepath
     assert t.target.endswith("foobar.bat")
Ejemplo n.º 10
0
def test_temppath():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()

    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "",
            },
        },
    })
    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/tmp",
            },
        },
    })
    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/custom/directory",
            },
        },
    })
    assert temppath() == "/custom/directory"
Ejemplo n.º 11
0
def test_temppath():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()

    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "",
            },
        },
    })
    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/tmp",
            },
        },
    })
    assert temppath() == os.path.join(
        tempfile.gettempdir(), "cuckoo-tmp-%s" % getuser()
    )

    set_cwd(tempfile.mkdtemp())
    cuckoo_create(cfg={
        "cuckoo": {
            "cuckoo": {
                "tmppath": "/custom/directory",
            },
        },
    })
    assert temppath() == "/custom/directory"
Ejemplo n.º 12
0
def ensure_tmpdir():
    """Verify if the current user can read and create files in the
    cuckoo temporary directory (and creates it, if needed)."""
    try:
        if not os.path.isdir(temppath()):
            mkdir(temppath())
    except OSError as e:
        # Currently we only handle EACCES.
        if e.errno != errno.EACCES:
            raise

    if os.path.isdir(temppath()) and os.access(temppath(), os.R_OK | os.W_OK):
        return True

    print red(
        "Cuckoo cannot read or write files into the temporary directory '%s',"
        " please make sure the user running Cuckoo has the ability to do so. "
        "If the directory does not yet exist and the parent directory is "
        "owned by root, then please create and chown the directory with root."
        % temppath())
    return False
Ejemplo n.º 13
0
 def test_status(self, p, client):
     set_cwd(tempfile.mkdtemp())
     cuckoo_create(cfg={
         "reporting": {
             "moloch": {
                 "enabled": True,
             },
         },
     })
     db.connect()
     r = client.get("/cuckoo/api/status/")
     assert r.status_code == 200
     assert p.call_args_list[0][0][0].startswith(temppath())
Ejemplo n.º 14
0
def ensure_tmpdir():
    """Verifies if the current user can read and create files in the
    cuckoo temporary directory (and creates it, if needed)."""
    try:
        if not os.path.isdir(temppath()):
            mkdir(temppath())
    except OSError as e:
        # Currently we only handle EACCES.
        if e.errno != errno.EACCES:
            raise

    if os.path.isdir(temppath()) and os.access(temppath(), os.R_OK | os.W_OK):
        return True

    print red(
        "Cuckoo cannot read or write files into the temporary directory '%s',"
        " please make sure the user running Cuckoo has the ability to do so. "
        "If the directory does not yet exist and the parent directory is "
        "owned by root, then please create and chown the directory with root."
        % temppath()
    )
    return False
Ejemplo n.º 15
0
    def test_empty_symlink(self):
        oldfilepath = Files.temp_put("hello")
        try:
            movesql("sqlite:///%s" % oldfilepath, "symlink", temppath())

            # Following is non-windows.
            assert os.path.exists(oldfilepath)
            assert os.path.exists(cwd("cuckoo.db"))
            assert os.path.islink(cwd("cuckoo.db"))
            assert open(cwd("cuckoo.db"), "rb").read() == "hello"
        except RuntimeError as e:
            assert is_windows()
            assert "'symlink'" in e.message
Ejemplo n.º 16
0
 def test_status(self, p, client):
     set_cwd(tempfile.mkdtemp())
     cuckoo_create(cfg={
         "reporting": {
             "moloch": {
                 "enabled": True,
             },
         },
     })
     db.connect()
     r = client.get("/cuckoo/api/status/")
     assert r.status_code == 200
     assert p.call_args_list[0][0][0].startswith(temppath())
Ejemplo n.º 17
0
    def test_empty_symlink(self):
        oldfilepath = Files.temp_put("hello")
        try:
            movesql("sqlite:///%s" % oldfilepath, "symlink", temppath())

            # Following is non-windows.
            assert os.path.exists(oldfilepath)
            assert os.path.exists(cwd("cuckoo.db"))
            assert os.path.islink(cwd("cuckoo.db"))
            assert open(cwd("cuckoo.db"), "rb").read() == "hello"
        except RuntimeError as e:
            assert is_windows()
            assert "'symlink'" in e.message