-
Notifications
You must be signed in to change notification settings - Fork 113
feat(handler): add APFS container handler #1494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
elektrischermoench
wants to merge
1
commit into
onekey-sec:main
Choose a base branch
from
elektrischermoench:feat/handler-apfs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import io | ||
|
|
||
| from unblob.extractors import Command | ||
| from unblob.file_utils import InvalidInputFormat | ||
| from unblob.models import ( | ||
| File, | ||
| HandlerDoc, | ||
| HandlerType, | ||
| HexString, | ||
| Reference, | ||
| StructHandler, | ||
| ValidChunk, | ||
| ) | ||
|
|
||
| _MIN_BLOCK_SIZE = 512 | ||
| _MAX_BLOCK_SIZE = 1024 * 1024 # 1 MB sanity cap | ||
|
|
||
|
|
||
| def _fletcher64_valid(block: bytes) -> bool: | ||
| mod = 0xFFFFFFFF | ||
| sum1 = sum2 = 0 | ||
| for offset in range(8, len(block), 4): | ||
| sum1 = (sum1 + int.from_bytes(block[offset : offset + 4], "little")) % mod | ||
| sum2 = (sum2 + sum1) % mod | ||
| check1 = mod - ((sum1 + sum2) % mod) | ||
| check2 = mod - ((sum1 + check1) % mod) | ||
| expected = check1 | (check2 << 32) | ||
| return expected == int.from_bytes(block[0:8], "little") | ||
|
|
||
|
|
||
| class APFSHandler(StructHandler): | ||
| NAME = "apfs" | ||
|
|
||
| PATTERNS = [ | ||
| # Match the superblock's object header, not just the "NXSB" magic, so we don't | ||
| # fire on every occurrence of that string: | ||
| # 01 00 00 80 o_type = 0x80000001 (NX_SUPERBLOCK | PHYSICAL flag), little-endian | ||
| # ?? ?? ?? ?? o_subtype | ||
| # 4E 58 53 42 nx_magic "NXSB" | ||
| HexString("01 00 00 80 ?? ?? ?? ?? 4E 58 53 42"), | ||
| ] | ||
|
|
||
| # The match starts at o_type, 24 bytes into the superblock; shift start back to block 0. | ||
| PATTERN_MATCH_OFFSET = -24 | ||
|
|
||
| # APFS container superblock layout (all fields little-endian): | ||
| # offset 0: obj_phys_t (32 bytes) — Fletcher-64 checksum + oid + xid + type + subtype | ||
| # offset 32: nx_magic uint32 "NXSB" (0x4253584e) | ||
| # offset 36: nx_block_size uint32 | ||
| # offset 40: nx_block_count uint64 | ||
| C_DEFINITIONS = r""" | ||
| typedef struct apfs_nx_superblock { | ||
| char o_cksum[8]; // Fletcher-64 checksum | ||
| uint64 o_oid; // object identifier | ||
| uint64 o_xid; // transaction identifier | ||
| uint32 o_type; // object type | ||
| uint32 o_subtype; // object subtype | ||
| char nx_magic[4]; // "NXSB" | ||
| uint32 nx_block_size; // block size in bytes | ||
| uint64 nx_block_count; // number of blocks in the container | ||
| } apfs_nx_superblock_t; | ||
| """ | ||
| HEADER_STRUCT = "apfs_nx_superblock_t" | ||
|
|
||
| EXTRACTOR = Command("7z", "x", "-y", "{inpath}", "-o{outdir}") | ||
|
|
||
| DOC = HandlerDoc( | ||
| name="APFS", | ||
| description="Apple File System (APFS) is Apple's proprietary filesystem introduced in macOS High Sierra and iOS 10.3, replacing HFS+. It features copy-on-write semantics, space sharing between volumes, native encryption, snapshots, and sparse files.", | ||
| handler_type=HandlerType.FILESYSTEM, | ||
| vendor="Apple", | ||
| references=[ | ||
| Reference( | ||
| title="Apple File System Reference", | ||
| url="https://developer.apple.com/support/downloads/Apple-File-System-Reference.pdf", | ||
| ), | ||
| ], | ||
| limitations=[], | ||
| ) | ||
|
|
||
| def is_valid_header(self, header) -> bool: | ||
| # o_type is already guaranteed by PATTERNS; only the container geometry needs checking. | ||
| return ( | ||
| _MIN_BLOCK_SIZE <= header.nx_block_size <= _MAX_BLOCK_SIZE | ||
| and header.nx_block_count > 0 | ||
| ) | ||
|
|
||
| def calculate_chunk(self, file: File, start_offset: int) -> ValidChunk | None: | ||
| header = self.parse_header(file) | ||
|
|
||
| if not self.is_valid_header(header): | ||
| raise InvalidInputFormat("Invalid APFS container superblock") | ||
|
|
||
| file.seek(start_offset, io.SEEK_SET) | ||
| if not _fletcher64_valid(file.read(header.nx_block_size)): | ||
| raise InvalidInputFormat("APFS superblock Fletcher-64 checksum mismatch") | ||
|
|
||
| return ValidChunk( | ||
| start_offset=start_offset, | ||
| end_offset=start_offset + header.nx_block_size * header.nx_block_count, | ||
| ) | ||
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/apfs/__output__/apfs.img_extract/apple.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/apfs/__output__/apfs.img_extract/banana.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/apfs/__output__/apfs.img_extract/dir/cherry.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/apfs/__output__/apfs_padding.img_extract/0-512.padding
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
tests/integration/filesystem/apfs/__output__/apfs_padding.img_extract/512-8389120.apfs
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...on/filesystem/apfs/__output__/apfs_padding.img_extract/512-8389120.apfs_extract/apple.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...n/filesystem/apfs/__output__/apfs_padding.img_extract/512-8389120.apfs_extract/banana.txt
Git LFS file not shown
3 changes: 3 additions & 0 deletions
3
...lesystem/apfs/__output__/apfs_padding.img_extract/512-8389120.apfs_extract/dir/cherry.txt
Git LFS file not shown
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.