Skip to content

Commit ebf1f28

Browse files
committed
Report recognized chunks
1 parent 61a52e3 commit ebf1f28

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

unblob/models.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from .file_utils import Endian, File, InvalidInputFormat, StructParser
1212
from .parser import hexstring2regex
13-
from .report import ErrorReport, Report
13+
from .report import ChunkReport, ErrorReport, Report
1414

1515
logger = get_logger()
1616

@@ -90,6 +90,16 @@ def extract(self, inpath: Path, outdir: Path):
9090

9191
self.handler.extract(inpath, outdir)
9292

93+
def as_report(self, extraction_reports: List[Report]) -> ChunkReport:
94+
return ChunkReport(
95+
start_offset=self.start_offset,
96+
end_offset=self.end_offset,
97+
size=self.size,
98+
handler_name=self.handler.NAME,
99+
is_encrypted=self.is_encrypted,
100+
extraction_reports=extraction_reports,
101+
)
102+
93103

94104
@attr.define(repr=False)
95105
class UnknownChunk(Chunk):
@@ -125,7 +135,14 @@ def errors(self) -> List[ErrorReport]:
125135
reports = itertools.chain.from_iterable(
126136
r.reports for r in self.results
127137
)
128-
return [r for r in reports if isinstance(r, ErrorReport)]
138+
interesting_reports = (r for r in reports if isinstance(r, (ErrorReport, ChunkReport)))
139+
errors = []
140+
for report in interesting_reports:
141+
if isinstance(report, ErrorReport):
142+
errors.append(report)
143+
else:
144+
errors.extend(r for r in report.extraction_reports if isinstance(r, ErrorReport))
145+
return errors
129146

130147
def register(self, result: TaskResult):
131148
self.results.append(result)

unblob/processing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ def _extract_chunk(self, file, chunk: ValidChunk):
256256
extract_dir = self.carve_dir / (inpath.name + self.config.extract_suffix)
257257
carved_path = inpath
258258

259+
extraction_reports = []
259260
try:
260261
chunk.extract(inpath, extract_dir)
261262

@@ -264,12 +265,13 @@ def _extract_chunk(self, file, chunk: ValidChunk):
264265
carved_path.unlink()
265266

266267
except ExtractError as e:
267-
for report in e.reports:
268-
self.result.add_report(report)
268+
extraction_reports.extend(e.reports)
269269

270270
except Exception as exc:
271271
logger.exception("Unknown error happened while extracting chunk")
272-
self.result.add_report(UnknownError(exception=exc))
272+
extraction_reports.append(UnknownError(exception=exc))
273+
274+
self.result.add_report(chunk.as_report(extraction_reports))
273275

274276
# we want to get consistent partial output even in case of unforeseen problems
275277
fix_extracted_directory(extract_dir, self.result)

unblob/report.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,19 @@ def from_path(cls, path: Path):
143143
@attr.define(kw_only=True)
144144
class FileMagicReport(Report):
145145
magic: str
146+
mime_type: str
146147

147148
@classmethod
148149
def from_path(cls, path: Path):
149150
detected = magic.detect_from_filename(path)
150-
return cls(magic=detected.name)
151+
return cls(magic=detected.name, mime_type=detected.mime_type)
152+
153+
154+
@attr.define(kw_only=True)
155+
class ChunkReport(Report):
156+
handler_name: str
157+
start_offset: int
158+
end_offset: int
159+
size: int
160+
is_encrypted: bool
161+
extraction_reports: List[Report]

vulture_whitelist.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from unblob.file_utils import File, iterbits, round_down
66
from unblob.models import _JSONEncoder
77
from unblob.parser import _HexStringToRegex
8+
from unblob.report import ChunkReport
89

910
_HexStringToRegex.literal
1011
_HexStringToRegex.wildcard
@@ -14,6 +15,8 @@
1415

1516
_JSONEncoder.default
1617

18+
ChunkReport.handler_name
19+
1720
sys.breakpointhook
1821
cli.cli.context_class
1922

0 commit comments

Comments
 (0)