From 2e3425f4caaa85fbd4a39ae75f16a7cd9ba6bf3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=BCcahid=20KIR?= Date: Wed, 15 May 2024 11:12:22 +0200 Subject: [PATCH 1/2] fix(ubi): add exception handler for SeekError Truncated UBI images can lead to SeekError being raised. We now handle that situation gracefully. --- .../filesystem/ubi/ubi/__input__/orange.ubi.truncated.img | 3 +++ .../orange.ubi.truncated.img_extract/0-3407872.ubi | 3 +++ .../img-941083063_vol-configuration.ubifs | 3 +++ .../orange1.txt | 3 +++ .../orange2.txt | 3 +++ .../0-3407872.ubi_extract/img-941083063_vol-rootfs.ubifs | 3 +++ .../3407872-3430400.unknown | 3 +++ unblob/handlers/filesystem/ubi.py | 8 +++++--- 8 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 tests/integration/filesystem/ubi/ubi/__input__/orange.ubi.truncated.img create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange1.txt create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange2.txt create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-rootfs.ubifs create mode 100644 tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/3407872-3430400.unknown diff --git a/tests/integration/filesystem/ubi/ubi/__input__/orange.ubi.truncated.img b/tests/integration/filesystem/ubi/ubi/__input__/orange.ubi.truncated.img new file mode 100644 index 0000000000..eccb234671 --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__input__/orange.ubi.truncated.img @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8dbf22935bc3b694e896c2a2ae922d76672c26a7a387449a475d63bfd087afe9 +size 3430400 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi new file mode 100644 index 0000000000..f37276df45 --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:08eebf3761a5722d486ce36acec62c0146d6b80bef006c2b6273def740b81d8b +size 3407872 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs new file mode 100644 index 0000000000..385f97c16d --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f18265fb4d15e5af50245ffa8b7c62836ed0eb8aceb9e3856b145ea097994645 +size 2064384 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange1.txt b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange1.txt new file mode 100644 index 0000000000..71584bfa65 --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange1.txt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:26cf6bc4cd9bc86de0392173cfc2546c5ece07f79816999945bf33a1bd15c680 +size 1445004 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange2.txt b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange2.txt new file mode 100644 index 0000000000..f449a6f498 --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-configuration.ubifs_extract/orange2.txt @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:3e13517c0da9b7e065f2f816ae6cd2b4d8aae24c4b6a36b68320d6eb16e2d66d +size 1445005 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-rootfs.ubifs b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-rootfs.ubifs new file mode 100644 index 0000000000..368ddbd543 --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/0-3407872.ubi_extract/img-941083063_vol-rootfs.ubifs @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d62c3a983b734754fe1b8420bc502ada8cef3868d364e25e5258414bcdd011f0 +size 1032192 diff --git a/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/3407872-3430400.unknown b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/3407872-3430400.unknown new file mode 100644 index 0000000000..1f6ed22d6d --- /dev/null +++ b/tests/integration/filesystem/ubi/ubi/__output__/orange.ubi.truncated.img_extract/3407872-3430400.unknown @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:991e478ff0b45569f77c8ce44a4d231ee8e75402539b64df52a9fa42540ff760 +size 22528 diff --git a/unblob/handlers/filesystem/ubi.py b/unblob/handlers/filesystem/ubi.py index 8224f972c0..071ab10b05 100644 --- a/unblob/handlers/filesystem/ubi.py +++ b/unblob/handlers/filesystem/ubi.py @@ -7,7 +7,7 @@ from unblob.extractors import Command -from ...file_utils import InvalidInputFormat, get_endian, iterate_patterns +from ...file_utils import InvalidInputFormat, SeekError, get_endian, iterate_patterns from ...iter_utils import get_intervals from ...models import File, Handler, HexString, StructHandler, ValidChunk @@ -137,8 +137,10 @@ def _walk_ubi(self, file: File, peb_size: int) -> int: first_bytes = file.read(len(self._UBI_EC_HEADER)) if first_bytes == b"" or first_bytes != self._UBI_EC_HEADER: break - file.seek(offset + peb_size) - + try: + file.seek(offset + peb_size) + except SeekError: + break return offset def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: From 91431448cc07e88e593458d83fa13c5cd9023173 Mon Sep 17 00:00:00 2001 From: Quentin Kaiser Date: Wed, 5 Jun 2024 12:08:49 +0200 Subject: [PATCH 2/2] fix(ubifs): attempts to continue extracting files even with bad block reads. Corrupt samples require the '-w' option so that we can actually extract content. --- unblob/handlers/filesystem/ubi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unblob/handlers/filesystem/ubi.py b/unblob/handlers/filesystem/ubi.py index 071ab10b05..c586cde2b4 100644 --- a/unblob/handlers/filesystem/ubi.py +++ b/unblob/handlers/filesystem/ubi.py @@ -81,7 +81,7 @@ class UBIFSHandler(StructHandler): """ HEADER_STRUCT = "ubifs_sb_node_t" - EXTRACTOR = Command("ubireader_extract_files", "{inpath}", "-o", "{outdir}") + EXTRACTOR = Command("ubireader_extract_files", "{inpath}", "-w", "-o", "{outdir}") def calculate_chunk(self, file: File, start_offset: int) -> Optional[ValidChunk]: endian = get_endian(file, self._BIG_ENDIAN_MAGIC)