Skip to content

Commit

Permalink
chore: ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jstucke committed Nov 12, 2024
1 parent a5a5ec6 commit 263f8ba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''
"""
This plugin unpacks all files via carving
'''
"""

from __future__ import annotations

import logging
Expand All @@ -15,13 +16,15 @@
from unblob.file_utils import File
from unblob.finder import search_chunks
from unblob.handlers import BUILTIN_HANDLERS
from unblob.models import TaskResult, PaddingChunk, UnknownChunk, Chunk
from unblob.processing import Task, remove_inner_chunks, calculate_unknown_chunks
from unblob.models import Chunk, PaddingChunk, TaskResult, UnknownChunk
from unblob.processing import Task, calculate_unknown_chunks, remove_inner_chunks

NAME = 'generic_carver'
MIME_PATTERNS = ['generic/carver']
VERSION = '1.0.0'

MIN_FILE_ENTROPY = 0.01

# deactivate internal logger of unblob because it can slow down searching chunks
structlog.configure(wrapper_class=structlog.make_filtering_bound_logger(logging.CRITICAL))

Expand Down Expand Up @@ -52,7 +55,7 @@ def unpack_function(file_path: str, tmp_dir: str) -> dict:
if filter_report:
report += f'\nFiltered chunks:\n{filter_report}'
except Exception as error:
report = f"Error {error} during unblob extraction:\n{traceback.format_exc()}"
report = f'Error {error} during unblob extraction:\n{traceback.format_exc()}'
return {'output': report}


Expand All @@ -76,7 +79,7 @@ def _create_report(chunk_list: list[dict]) -> str:
def _has_low_entropy(file: File, chunk: UnknownChunk) -> bool:
file.seek(chunk.start_offset)
content = file.read(chunk.size)
return avg_entropy(content) < 0.01
return avg_entropy(content) < MIN_FILE_ENTROPY


# ----> Do not edit below this line <----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_extraction(self):
in_file, self.tmp_dir.name, self.unpacker.unpacker_plugins['generic/carver']
)
files = set(files)
assert len(files) == 3, 'file number incorrect'
assert len(files) == 3, 'file number incorrect' # noqa: PLR2004
assert f'{self.tmp_dir.name}/100-887.zip' in files, 'hidden zip not identified correctly'
assert 'output' in meta_data

Expand All @@ -29,7 +29,7 @@ def test_filter(self):
str(in_file), self.tmp_dir.name, self.unpacker.unpacker_plugins['generic/carver']
)
files = set(files)
assert len(files) == 4, 'file number incorrect'
assert len(files) == 4, 'file number incorrect' # noqa: PLR2004
assert 'removed chunk 300-428' in meta_data['output']
for file in ('0-128.unknown', '128-300.zip', '428-562.sevenzip', '562-626.unknown'):
assert f'{self.tmp_dir.name}/{file}' in files
41 changes: 22 additions & 19 deletions fact_extractor/plugins/unpacking/squashFS/test/test_plugin_squashfs.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from pathlib import Path
from tempfile import TemporaryDirectory

import pytest
from tempfile import TemporaryDirectory

from test.unit.unpacker.test_unpacker import TestUnpackerBase

from ..code.squash_fs import _unpack_success, unpack_function, SQUASH_UNPACKER
from ..code.squash_fs import SQUASH_UNPACKER, _unpack_success, unpack_function

TEST_DATA_DIR = Path(__file__).parent / 'data'


@pytest.mark.parametrize(
'unpack_path, expected',
('unpack_path', 'expected'),
[
('/foo/bar/unpacker', False),
(TEST_DATA_DIR, True),
Expand All @@ -38,22 +38,25 @@ class TestSquashUnpacker(TestUnpackerBase):
def test_unpacker_selection_generic(self):
self.check_unpacker_selection('filesystem/squashfs', 'SquashFS')

@pytest.mark.parametrize(('file', 'expected'), [
('avm_be.sqfs4', 'sasquatch-v4be'),
('avm_le.sqfs4', 'sasquatch'),
('gzip.sqfs', 'sasquatch'),
('lz4.sqfs', 'sasquatch'),
('lzma.sqfs', 'sasquatch'),
('lzma1_be.sqfs3', 'sasquatch'),
('lzma1_le.sqfs3', 'sasquatch'),
('lzma_be.sqfs2', 'unsquashfs4-avm-be'),
('lzma_le.sqfs2', 'unsquashfs4-avm-be'),
('lzo.sqfs', 'sasquatch'),
('xz.sqfs', 'sasquatch'),
('zlib_be.sqfs3', 'sasquatch'),
('zlib_le.sqfs3', 'sasquatch'),
('zstd.sqfs', 'sasquatch'),
])
@pytest.mark.parametrize(
('file', 'expected'),
[
('avm_be.sqfs4', 'sasquatch-v4be'),
('avm_le.sqfs4', 'sasquatch'),
('gzip.sqfs', 'sasquatch'),
('lz4.sqfs', 'sasquatch'),
('lzma.sqfs', 'sasquatch'),
('lzma1_be.sqfs3', 'sasquatch'),
('lzma1_le.sqfs3', 'sasquatch'),
('lzma_be.sqfs2', 'unsquashfs4-avm-be'),
('lzma_le.sqfs2', 'unsquashfs4-avm-be'),
('lzo.sqfs', 'sasquatch'),
('xz.sqfs', 'sasquatch'),
('zlib_be.sqfs3', 'sasquatch'),
('zlib_le.sqfs3', 'sasquatch'),
('zstd.sqfs', 'sasquatch'),
],
)
def test_extraction_sqfs(self, file, expected):
meta_data = self.check_unpacking_of_standard_unpack_set(TEST_DATA_DIR / file)
assert meta_data['plugin_used'] == 'SquashFS'
Expand Down

0 comments on commit 263f8ba

Please sign in to comment.