-
Notifications
You must be signed in to change notification settings - Fork 72
Improve detection for iOS targets #1085
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
Merged
Merged
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2c0bf46
improve detection for iOS targets
JSCU-CNI 97a5b3f
Apply suggestions from code review
JSCU-CNI 9058424
restructure macOS and iOS plugins
JSCU-CNI 3043f9f
fix linter
JSCU-CNI 8f223fe
implement review feedback and more osx->macos renaming
JSCU-CNI 1616c03
fix linter
JSCU-CNI 9f2ab3b
register IOSPlugin as DarwinPlugin
JSCU-CNI 691f9bb
add tests for iOS
JSCU-CNI deda63f
Apply suggestions from code review
JSCU-CNI 7020309
implement review feedback
JSCU-CNI 889ef35
improve typing of Config dataclass
JSCU-CNI 050242b
switch to Config.load w/ classmethod
JSCU-CNI 2c375a7
assert _os
JSCU-CNI 8631f02
fix tests
JSCU-CNI 4c08a16
Merge branch 'main' into improvement/ios-detection
JSCU-CNI d31a767
fix tests
JSCU-CNI adde3af
Merge branch 'main' into improvement/ios-detection
Schamper 4bafc51
Merge branch 'main' into improvement/ios-detection
Schamper 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
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
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
File renamed without changes.
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,61 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
JSCU-CNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from dissect.target.filesystem import Filesystem | ||
| from dissect.target.plugins.os.unix.bsd._os import BsdPlugin | ||
| from dissect.target.target import Target | ||
|
|
||
| # https://en.wikipedia.org/wiki/Mach-O | ||
| ARCH_MAP = { | ||
| b"\x0c\x00\x00\x01": "arm64", # big endian, x64 | ||
| b"\x01\x00\x00\x0c": "arm64", # little endian, x64 | ||
| b"\x0c\x00\x00\x00": "arm32", # big endian, x32 | ||
| b"\x00\x00\x00\x0c": "arm32", # little endian, x32 | ||
| } | ||
|
|
||
|
|
||
| class DarwinPlugin(BsdPlugin): | ||
JSCU-CNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Darwin plugin.""" | ||
|
|
||
| def __init__(self, target: Target): | ||
| super().__init__(target) | ||
|
|
||
| @classmethod | ||
| def detect(cls, target: Target) -> Filesystem | None: | ||
| for fs in target.filesystems: | ||
| if (fs.exists("/Library") and fs.exists("/Applications")) or fs.exists("/private/var/mobile"): | ||
| return fs | ||
|
|
||
|
|
||
| def detect_macho_arch(paths: list[str | Path], fs: Filesystem | None = None) -> str | None: | ||
| """Detect the architecture of the system by reading the Mach-O headers of the provided binaries. | ||
|
|
||
| We could use the mach-o magic headers (feedface, feedfacf, cafebabe), but the mach-o cpu type | ||
| also contains bitness. | ||
|
|
||
| Args: | ||
| paths: List of strings or ``Path`` objects. | ||
| fs: Optional filesystem to search the provided paths in. Required if ``paths`` is a list of strings. | ||
|
|
||
| Returns: | ||
| Detected architecture (e.g. ``arm64``) or ``None``. | ||
|
|
||
| Resources: | ||
| - https://github.com/opensource-apple/cctools/blob/master/include/mach/machine.h | ||
| """ | ||
| for path in paths: | ||
| if isinstance(path, str): | ||
| if not fs: | ||
| raise ValueError("Provided string paths but no filesystem!") | ||
| path = fs.path(path) | ||
|
|
||
| if not path.is_file(): | ||
| continue | ||
|
|
||
| try: | ||
| with path.open("rb") as fh: | ||
| fh.seek(4) | ||
| return ARCH_MAP.get(fh.read(4)) # mach-o cpu type | ||
| except Exception: | ||
| pass | ||
File renamed without changes.
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,98 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import plistlib | ||
JSCU-CNI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import Any, Iterator | ||
|
|
||
| from dissect.target.filesystem import Filesystem, VirtualFilesystem | ||
| from dissect.target.helpers.record import IOSUserRecord | ||
| from dissect.target.plugin import OperatingSystem, export | ||
| from dissect.target.plugins.os.unix.bsd.darwin._os import ( | ||
| DarwinPlugin, | ||
| detect_macho_arch, | ||
| ) | ||
| from dissect.target.target import Target | ||
|
|
||
|
|
||
| class IOSPlugin(DarwinPlugin): | ||
| """Apple iOS plugin. | ||
|
|
||
| Resources: | ||
| - https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html | ||
| - https://corp.digitalcorpora.org/corpora/mobile/iOS17/ | ||
| """ # noqa: E501 | ||
|
|
||
| SYSTEM = "/private/var/preferences/SystemConfiguration/preferences.plist" | ||
| GLOBAL = "/private/var/mobile/Library/Preferences/.GlobalPreferences.plist" | ||
| VERSION = "/System/Library/CoreServices/SystemVersion.plist" | ||
|
|
||
| # /private/etc/master.passwd is a copy of /private/etc/passwd | ||
| PASSWD_FILES = ["/private/etc/passwd"] | ||
|
|
||
| def __init__(self, target: Target): | ||
| super().__init__(target) | ||
|
|
||
| self._config = Config.load( | ||
| target.fs.path(self.SYSTEM), | ||
| target.fs.path(self.GLOBAL), | ||
| target.fs.path(self.VERSION), | ||
| ) | ||
|
|
||
| @classmethod | ||
| def detect(cls, target: Target) -> Filesystem | None: | ||
| for fs in target.filesystems: | ||
| if fs.exists("/private/var/preferences") and fs.exists("/private/var/mobile"): | ||
| return fs | ||
|
|
||
| @classmethod | ||
| def create(cls, target: Target, sysvol: VirtualFilesystem) -> None: | ||
| target.fs.mount("/", sysvol) | ||
| return cls(target) | ||
|
|
||
| @export(property=True) | ||
| def hostname(self) -> str | None: | ||
| try: | ||
| # ComputerName can contain invalid utf characters, so we use HostName instead. | ||
| return self._config.SYSTEM["System"]["System"]["HostName"] | ||
| except KeyError: | ||
| pass | ||
|
|
||
| @export(property=True) | ||
| def ips(self) -> list: | ||
| return [] | ||
|
|
||
| @export(property=True) | ||
| def version(self) -> str: | ||
| return f'{self._config.VERSION["ProductName"]} {self._config.VERSION["ProductVersion"]} ({self._config.VERSION["ProductBuildVersion"]})' # noqa: E501 | ||
|
|
||
| @export(record=IOSUserRecord) | ||
| def users(self) -> Iterator[IOSUserRecord]: | ||
| for user in super().users(): | ||
| yield IOSUserRecord(**user._asdict()) | ||
|
|
||
| @export(property=True) | ||
| def os(self) -> str: | ||
| return OperatingSystem.IOS.value | ||
|
|
||
| @export(property=True) | ||
| def architecture(self) -> str | None: | ||
| if arch := detect_macho_arch(["/bin/df", "/bin/ps", "/sbin/fsck", "/sbin/mount"], fs=self.target.fs): | ||
| return f"{arch}-ios" | ||
|
|
||
|
|
||
| @dataclass | ||
| class Config: | ||
| SYSTEM: dict[str, Any] | ||
| GLOBAL: dict[str, Any] | ||
| VERSION: dict[str, Any] | ||
|
|
||
| @classmethod | ||
| def load(cls, *args: list[Path]) -> Config: | ||
| plists = [] | ||
| for path in args: | ||
| if path.is_file(): | ||
| plists.append(plistlib.load(path.open("rb"))) | ||
| else: | ||
| plists.append({}) | ||
| return cls(*plists) | ||
Oops, something went wrong.
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.