-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add Acquire collection loader #935
Open
Matthijsy
wants to merge
13
commits into
fox-it:main
Choose a base branch
from
Matthijsy:feature/zip-loader
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.
+160
−113
Open
Changes from 11 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
23347be
Add ZIP loader
08b5027
Move acquire logic into dedicated AcquireLoader
ee3cb91
Make aquire loader work with TAR files
c6c9f04
Merge branch 'main' into feature/zip-loader
29528b9
Fix comments
a0c5d89
Add tests
1ba4590
Update tests/loaders/test_acquire.py
Matthijsy 6fd5869
Update dissect/target/loaders/dir.py
Matthijsy eddc161
Apply suggestions from code review
Matthijsy 5ae9fec
Add ZIP acquire test data
00a606b
Add support for anon filesystems
4072e20
Apply suggestions from code review
Matthijsy d3d7f9d
Merge branch 'main' into feature/zip-loader
Matthijsy 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 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 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,55 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
import zipfile | ||
from pathlib import Path | ||
|
||
from dissect.target.filesystems.tar import TarFilesystem | ||
from dissect.target.filesystems.zip import ZipFilesystem | ||
from dissect.target.loader import Loader | ||
from dissect.target.loaders.dir import find_and_map_dirs | ||
from dissect.target.target import Target | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
FILESYSTEMS_ROOT = "fs" | ||
FILESYSTEMS_LEGACY_ROOT = "sysvol" | ||
|
||
|
||
def _get_root(path: Path) -> Path | None: | ||
if path.is_file(): | ||
fh = path.open("rb") | ||
if TarFilesystem._detect(fh): | ||
return TarFilesystem(fh).path() | ||
|
||
if ZipFilesystem._detect(fh): | ||
return zipfile.Path(path.open("rb")) | ||
|
||
return None | ||
|
||
|
||
class AcquireLoader(Loader): | ||
def __init__(self, path: Path, **kwargs): | ||
super().__init__(path) | ||
|
||
self.root = _get_root(path) | ||
|
||
@staticmethod | ||
def detect(path: Path) -> bool: | ||
root = _get_root(path) | ||
|
||
if not root: | ||
return False | ||
|
||
return root.joinpath(FILESYSTEMS_ROOT).exists() or root.joinpath(FILESYSTEMS_LEGACY_ROOT).exists() | ||
|
||
def map(self, target: Target) -> None: | ||
# Handle both root dir 'fs' and 'sysvol' (legacy) | ||
fs_root = self.root | ||
if fs_root.joinpath(FILESYSTEMS_ROOT).exists(): | ||
fs_root = fs_root.joinpath(FILESYSTEMS_ROOT) | ||
|
||
find_and_map_dirs( | ||
target, | ||
fs_root | ||
) | ||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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 |
---|---|---|
@@ -1,11 +1,13 @@ | ||
from __future__ import annotations | ||
|
||
import re | ||
import zipfile | ||
from collections import defaultdict | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from dissect.target.filesystem import LayerFilesystem | ||
from dissect.target.filesystems.tar import TarFilesystem | ||
from dissect.target.filesystems.dir import DirectoryFilesystem | ||
from dissect.target.filesystems.zip import ZipFilesystem | ||
from dissect.target.helpers import loaderutil | ||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -16,7 +18,7 @@ | |
from dissect.target import Target | ||
|
||
PREFIXES = ["", "fs"] | ||
|
||
ANON_FS_RE = re.compile(r"^fs[0-9]+$") | ||
|
||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class DirLoader(Loader): | ||
"""Load a directory as a filesystem.""" | ||
|
@@ -43,6 +45,7 @@ def map_dirs( | |
*, | ||
dirfs: type[DirectoryFilesystem] = DirectoryFilesystem, | ||
zipfs: type[ZipFilesystem] = ZipFilesystem, | ||
tarfs: type[TarFilesystem] = TarFilesystem, | ||
**kwargs, | ||
) -> None: | ||
"""Map directories as filesystems into the given target. | ||
|
@@ -53,6 +56,7 @@ def map_dirs( | |
os_type: The operating system type, used to determine how the filesystem should be mounted. | ||
dirfs: The filesystem class to use for directory filesystems. | ||
zipfs: The filesystem class to use for ZIP filesystems. | ||
tarfs: The filesystem class to use for TAR filesystems. | ||
""" | ||
alt_separator = "" | ||
case_sensitive = True | ||
|
@@ -70,6 +74,8 @@ def map_dirs( | |
|
||
if isinstance(path, zipfile.Path): | ||
dfs = zipfs(path.root.fp, path.at, alt_separator=alt_separator, case_sensitive=case_sensitive) | ||
elif hasattr(path, "_fs") and isinstance(path._fs, TarFilesystem): | ||
dfs = tarfs(path._fs.tar.fileobj, str(path), alt_separator=alt_separator, case_sensitive=case_sensitive) | ||
else: | ||
dfs = dirfs(path, alt_separator=alt_separator, case_sensitive=case_sensitive) | ||
|
||
|
@@ -86,7 +92,10 @@ def map_dirs( | |
vfs = dfs[0] | ||
|
||
fs_to_add.append(vfs) | ||
target.fs.mount(drive_letter.lower() + ":", vfs) | ||
mount_letter = drive_letter.lower() | ||
if mount_letter != "$fs$": | ||
mount_letter += ":" | ||
target.fs.mount(mount_letter, vfs) | ||
else: | ||
fs_to_add.extend(dfs) | ||
|
||
|
@@ -130,12 +139,21 @@ def find_dirs(path: Path) -> tuple[str, list[Path]]: | |
for p in path.iterdir(): | ||
# Look for directories like C or C: | ||
if p.is_dir() and (is_drive_letter_path(p) or p.name in ("sysvol", "$rootfs$")): | ||
dirs.append(p) | ||
if p.name == "sysvol": | ||
dirs.append(('c', p)) | ||
else: | ||
dirs.append((p.name[0], p)) | ||
Comment on lines
+143
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changes what the function returns. Not bad necessarily, but it might break some other loaders that use this function. So those would need to be changed. |
||
|
||
if not os_type: | ||
os_type = os_type_from_path(p) | ||
|
||
if not os_type: | ||
if p.name == "$fs$": | ||
dirs.append(('$fs$', p)) | ||
for anon_fs in p.iterdir(): | ||
if ANON_FS_RE.match(anon_fs.name): | ||
dirs.append(anon_fs) | ||
|
||
if len(dirs) == 0: | ||
os_type = os_type_from_path(path) | ||
dirs = [path] | ||
|
||
|
This file contains 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.
File renamed without changes.
File renamed without changes.
Horofic marked this conversation as resolved.
Show resolved
Hide resolved
|
Git LFS file not shown
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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,67 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from dissect.target import Target | ||
from dissect.target.loaders.acquire import AcquireLoader | ||
from dissect.target.loaders.tar import TarLoader | ||
from dissect.target.plugins.os.windows._os import WindowsPlugin | ||
from tests._utils import absolute_path | ||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
|
||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_tar_sensitive_drive_letter(target_bare: Target) -> None: | ||
tar_file = absolute_path("_data/loaders/acquire/uppercase_driveletter.tar") | ||
|
||
loader = AcquireLoader(Path(tar_file)) | ||
assert loader.detect(Path(tar_file)) | ||
loader.map(target_bare) | ||
|
||
# mounts = c: | ||
assert sorted(target_bare.fs.mounts.keys()) == ["c:"] | ||
|
||
# Initialize our own WindowsPlugin to override the detection | ||
target_bare._os_plugin = WindowsPlugin.create(target_bare, target_bare.fs.mounts["c:"]) | ||
target_bare._init_os() | ||
|
||
# sysvol is now added | ||
assert sorted(target_bare.fs.mounts.keys()) == ["c:", "sysvol"] | ||
|
||
# WindowsPlugin sets the case sensitivity to False | ||
assert target_bare.fs.get("C:/test.file").open().read() == b"hello_world" | ||
assert target_bare.fs.get("c:/test.file").open().read() == b"hello_world" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"archive, expected_drive_letter", | ||
[ | ||
("_data/loaders/acquire/test-windows-sysvol-absolute.tar", "c:"), # C: due to backwards compatibility | ||
("_data/loaders/acquire/test-windows-sysvol-relative.tar", "c:"), # C: due to backwards compatibility | ||
("_data/loaders/acquire/test-windows-fs-c-relative.tar", "c:"), | ||
("_data/loaders/acquire/test-windows-fs-c-absolute.tar", "c:"), | ||
("_data/loaders/acquire/test-windows-fs-x.tar", "x:"), | ||
("_data/loaders/acquire/test-windows-fs-c.zip", "c:"), | ||
Matthijsy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
) | ||
def test_tar_loader_windows_sysvol_formats(target_default: Target, archive: str, expected_drive_letter: str) -> None: | ||
path = Path(absolute_path(archive)) | ||
assert AcquireLoader.detect(path) | ||
|
||
loader = AcquireLoader(path) | ||
loader.map(target_default) | ||
|
||
assert WindowsPlugin.detect(target_default) | ||
# NOTE: for the sysvol archives, this also tests the backwards compatibility | ||
assert sorted(target_default.fs.mounts.keys()) == [expected_drive_letter] | ||
assert target_default.fs.get(f"{expected_drive_letter}/Windows/System32/foo.txt") | ||
|
||
|
||
def test_tar_anonymous_filesystems(target_default: Target) -> None: | ||
tar_file = Path(absolute_path("_data/loaders/acquire/test-anon-filesystems.tar")) | ||
assert AcquireLoader.detect(tar_file) | ||
|
||
loader = AcquireLoader(tar_file) | ||
loader.map(target_default) | ||
|
||
assert target_default.fs.get("$fs$/fs0/foo").open().read() == b"hello world\n" | ||
assert target_default.fs.get("$fs$/fs1/bar").open().read() == b"hello world\n" |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will be very slow on large tar files, where there will be a double performance penalty. First the entire tar file will be parsed just for this check, then if it exists, it will be parsed again in
__init__
. However, if it doesn't exist, it will still be parsed again in the actual tar loader.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree, but is there a way to prevent that? As we do need to check if the
fs
directory exists in the file, since otherwise the tar is not an acquire collect and thus need to be handled by theTarLoader
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Ideally" (not really, since it's only for performance and the code path itself will be more confusing) it's a subpath in the
zip
andtar
loaders. So you piggy back on the detection logic of those, and upon mapping you do a fast check to see if you need to divert logic to Acquire logic (which could exist inloaders/acquire.py
.At least, that's the "best" idea I can come up with right now.