Beispiel #1
0
    def test_garbage(self):
        t = TarFile(f("garbage.bin"))
        assert t.handles() is False
        assert not t.f.selected

        with pytest.raises(NotSupportedError):
            t.unpack()
Beispiel #2
0
 def test_garbage2(self):
     t = TarFile(f(b"tar_garbage.tar"))
     assert t.handles() is True
     assert not t.f.selected
     files = t.unpack()
     assert len(files) == 1
     assert not files[0].children
     assert files[0].mode == "failed"
Beispiel #3
0
 def test_garbage2(self):
     t = TarFile(f("tar_garbage.tar"))
     assert t.handles() is True
     assert not t.f.selected
     files = t.unpack()
     assert len(files) == 1
     assert not files[0].children
     assert files[0].mode == "failed"
Beispiel #4
0
    def test_garbage2(self):
        t = TarFile(f("tar_garbage.tar"))
        assert t.handles() is True
        assert not t.f.selected
        files = t.unpack()

        # The child file is garbage data. It should not be attempted
        # to unpack.
        assert len(files) == 1
        assert not files[0].children
        assert files[0].mode is None
Beispiel #5
0
 def test_tar_plain(self):
     assert "POSIX tar" in f("tar_plain.tar").magic
     t = TarFile(f("tar_plain.tar"))
     assert t.handles() is True
     assert not t.f.selected
     files = list(t.unpack())
     assert len(files) == 1
     assert files[0].relapath == "sflock.txt"
     assert files[0].contents == "sflock_plain_tar\n"
     assert files[0].magic == "ASCII text"
     assert files[0].parentdirs == []
     assert not files[0].selected
Beispiel #6
0
    def test_tar_noext(self):
        t = TarFile(f("tar_noext"))
        assert t.handles() is True
        assert not t.f.selected
        files = list(t.unpack())
        assert len(files) == 1

        assert files[0].relapath == "foo/bar.txt"
        assert files[0].parentdirs == ["foo"]
        assert files[0].contents == b"hello world\n"
        assert not files[0].password
        assert files[0].magic == "ASCII text"
        assert not files[0].selected
Beispiel #7
0
    def test_nested_plain(self):
        assert "POSIX tar archive" in f(b"tar_nested.tar").magic
        t = TarFile(f(b"tar_nested.tar"))
        assert t.handles() is True
        assert not t.f.selected
        files = list(t.unpack())
        assert len(files) == 1

        assert files[0].relapath == b"foo/bar.txt"
        assert files[0].parentdirs == [b"foo"]
        assert files[0].contents == b"hello world\n"
        assert not files[0].password
        assert files[0].magic == "ASCII text"
        assert not files[0].selected
Beispiel #8
0
    def test_nested_plain(self):
        assert "POSIX tar archive" in f("tar_nested.tar").magic
        t = TarFile(f("tar_nested.tar"))
        assert t.handles() is True
        assert not t.f.selected
        files = list(t.unpack())
        assert len(files) == 1

        assert files[0].relapath == "foo/bar.txt"
        assert files[0].parentdirs == ["foo"]
        assert files[0].contents == "hello world\n"
        assert not files[0].password
        assert files[0].magic == "ASCII text"
        assert not files[0].selected
Beispiel #9
0
    def test_nested_plain(self):
        assert "POSIX tar archive" in f("tar_nested.tar").magic
        t = TarFile(f("tar_nested.tar"))
        assert t.handles() is True
        files = list(t.unpack())
        assert len(files) == 1

        assert files[0].filepath == "foo/bar.txt"
        assert files[0].parentdirs == ["foo"]
        assert files[0].contents == "hello world\n"
        assert not files[0].password
        assert files[0].magic == "ASCII text"

        s = f("tar_nested.tar").get_signature()
        assert s is None
Beispiel #10
0
    def test_tar_plain(self):
        assert "POSIX tar" in f("tar_plain.tar").magic
        t = TarFile(f("tar_plain.tar"))
        assert t.handles() is True
        files = list(t.unpack())
        assert len(files) == 1
        assert files[0].filepath == "sflock.txt"
        assert files[0].contents == "sflock_plain_tar\n"
        assert files[0].magic == "ASCII text"
        assert files[0].parentdirs == []

        # TODO A combination of file extension, file magic, and initial bytes
        # signature should be used instead of just the bytes (as this call
        # should not yield None).
        assert f("tar_plain.tar").get_signature() is None
Beispiel #11
0
    def test_tar_plain2(self):
        assert "POSIX tar" in f("tar_plain2.tar").magic
        t = TarFile(f("tar_plain2.tar"))
        assert t.handles() is True
        files = list(t.unpack())
        assert len(files) == 2
        assert files[0].filepath == "sflock.txt"
        assert files[0].contents == "sflock_plain_tar\n"
        assert files[0].magic == "ASCII text"
        assert files[0].parentdirs == []
        assert files[1].filepath == "sflock2.txt"
        assert files[1].contents == "sflock_plain_tar2\n"
        assert files[1].magic == "ASCII text"
        assert files[1].parentdirs == []

        # TODO See item above for tar_plain.tar.
        assert f("tar_plain2.tar").get_signature() is None
Beispiel #12
0
    def extract_archive(cls, f):
        logger.debug(f"Extracting {f.filename}")
        content = f.blob

        if f.password:
            # Sflock expects byte string
            pw = f.password.encode("utf-8")
        else:
            pw = None

        if f.extension == "zip":
            if "v5.1" in f.content_guess:
                # Unzip is not capable to process this version, 7z is required (Zip7File)
                archive_file = Zip7File(
                    SflockFile(contents=content, password=pw))
            else:
                archive_file = ZipFile(
                    SflockFile(contents=content, password=pw))
        elif f.extension == "rar":
            archive_file = RarFile(SflockFile(contents=content, password=pw))
        elif f.extension == "tar":
            archive_file = TarFile(SflockFile(contents=content, password=pw))
        else:  # Fallback to zip
            archive_file = Zip7File(SflockFile(contents=content, password=pw))

        files_in_zip = list(archive_file.unpack(password=pw, duplicates=[]))
        extracted_files = []

        for zf in files_in_zip:
            h = HashFactory.get_hashstruct_from_bytes(zf.contents)
            cg = zf.magic
            fn = zf.filename.decode("utf-8")
            ext = fn.rsplit(".", 1)[-1] if "." in fn else ""

            f.extractions.append(
                Extraction(content_guess=cg,
                           extension=ext,
                           description=fn,
                           hash=h))

            file_struct = File(
                content_guess=cg,
                extension=ext,
                encoding='application/octet-stream',  # alternative: "hex"
                filename=fn,
                hash=h,
                blob=zf.contents,
                timestamp=f.timestamp)
            extracted_files.append(file_struct)
            logger.info(f"Extracted {zf.filename}")

            f.is_enriched = True

        return f, extracted_files
Beispiel #13
0
 def test_garbage(self):
     t = TarFile(f(b"garbage.bin"))
     assert t.handles() is False
     assert not t.f.selected
     assert not t.unpack()
     assert t.f.mode == "failed"
Beispiel #14
0
 def test_garbage(self):
     t = TarFile(f("garbage.bin"))
     assert t.handles() is False
     assert not t.f.selected
     assert not t.unpack()
     assert t.f.mode == "failed"