Example #1
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
Example #2
0
 def test_udf_iso_noext(self):
     upacker = Zip7File(f("iso_udf_noext"))
     assert upacker.handles()
     assert upacker.supported()
     t = unpack("tests/files/iso_udf_noext")
     assert t.unpacker == "7zfile"
     assert len(t.children) == 1
     assert t.children[0].filename == "ATTACHME.EXE"
Example #3
0
    def test_garbage(self):
        t = Zip7File(f("garbage.bin"))
        assert t.handles() is False
        assert not t.f.selected
        with pytest.raises(UnpackException) as e:
            t.unpack()

        assert e.value.state == Errors.NOTHING_EXTRACTED
Example #4
0
 def test_garbage2(self):
     t = Zip7File(f("7z_garbage.7z"))
     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"
Example #5
0
    def test_garbage2(self):
        t = Zip7File(f("7z_garbage.7z"))
        assert t.handles() is True
        assert not t.f.selected
        files = t.unpack()
        assert len(files) == 1

        # The child file is garbage data. It should not be attempted
        # to unpack.
        assert not files[0].children
        assert files[0].mode is None
Example #6
0
 def test_payment_iso(self):
     t = Zip7File(f(b"payment.iso"))
     assert t.handles() is True
     assert not t.f.selected
     files = t.unpack()
     assert len(files) == 1
     assert hashlib.md5(files[0].contents).hexdigest() == ("eccd7c33037181277ae23f3c3b5baf74")
     assert not files[0].children
     assert files[0].relaname == (b"payment slip and bank confirmation document.exe")
     assert files[0].selected is True
     assert files[0].duplicate is False
Example #7
0
 def test_7z_plain(self):
     assert "7-zip archive" in f("7z_plain.7z").magic
     t = Zip7File(f("7z_plain.7z"))
     assert t.handles() is True
     assert not t.f.selected
     files = list(t.unpack())
     assert len(files) == 1
     assert files[0].relapath == "bar.txt"
     assert files[0].contents == "hello world\n"
     assert files[0].magic == "ASCII text"
     assert files[0].parentdirs == []
     assert not files[0].selected
Example #8
0
    def test_nested2_plain(self):
        assert "7-zip archive" in f(b"7z_nested2.7z").magic
        t = Zip7File(f(b"7z_nested2.7z"))
        assert t.handles() is True
        assert t.f.selected
        files = list(t.unpack())
        assert len(files) == 1

        assert files[0].relapath == b"deepfoo/foo/bar.txt"
        assert files[0].parentdirs == [b"deepfoo", 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
Example #9
0
    def test_nested2_plain(self):
        assert "7-zip archive" in f("7z_nested2.7z").magic
        t = Zip7File(f("7z_nested2.7z"))
        assert t.handles() is True
        files = list(t.unpack())
        assert len(files) == 1

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

        s = f("7z_nested2.7z").get_signature()
        assert s is None
Example #10
0
    def test_7z_plain(self):
        assert "7-zip archive" in f("7z_plain.7z").magic
        t = Zip7File(f("7z_plain.7z"))
        assert t.handles() is True
        files = list(t.unpack())
        assert len(files) == 1
        assert files[0].filepath == "bar.txt"
        assert files[0].contents == "hello world\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("7z_plain.7z").get_signature() is None
Example #11
0
def test_count_supported():
    count = 15

    if AceFile(None).supported():
        count += 1

    if CabFile(None).supported():
        count += 1

    if RarFile(None).supported():
        count += 1

    if Zip7File(None).supported():
        count += 7

    assert count == len(supported())
Example #12
0
def test_count_supported():
    count = 10

    if DaaFile(None).supported():
        count += 1

    if VHDFile(None).supported():
        count += 2

    if AceFile(None).supported():
        count += 1

    if CabFile(None).supported():
        count += 1

    if RarFile(None).supported():
        count += 1

    if Zip7File(None).supported():
        count += 5

    assert count == len(supported())
Example #13
0
def test_no7z_plain():
    assert "7-zip archive" in f("7z_plain.7z").magic
    t = Zip7File(f("7z_plain.7z"))
    assert t.handles() is True
Example #14
0
 def test_garbage(self):
     t = Zip7File(f("garbage.bin"))
     assert t.handles() is False
     assert not t.f.selected
     assert not t.unpack()
     assert t.f.mode == "failed"
Example #15
0
def test_suffix():
    assert "7-zip archive" in f(b"7z_suffix.docx").magic
    t = Zip7File(f(b"7z_suffix.docx"))
    assert t.handles() is True
    assert t.f.package == "7z"