-
Notifications
You must be signed in to change notification settings - Fork 47
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 ZIP loader #935
Open
Matthijsy
wants to merge
1
commit 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.
Open
Add ZIP loader #935
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 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,97 @@ | ||
import logging | ||
import re | ||
from pathlib import Path | ||
from typing import Union | ||
from zipfile import ZipFile | ||
|
||
from dissect.target import filesystem, target | ||
from dissect.target.filesystems.zip import ZipFilesystemDirectoryEntry, ZipFilesystemEntry | ||
from dissect.target.helpers import fsutil, loaderutil | ||
from dissect.target.loader import Loader | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
ANON_FS_RE = re.compile(r"^fs[0-9]+$") | ||
|
||
|
||
class ZipLoader(Loader): | ||
"""Load zip files.""" | ||
|
||
def __init__(self, path: Union[Path, str], **kwargs): | ||
super().__init__(path) | ||
|
||
self.zip = ZipFile(path) | ||
|
||
@staticmethod | ||
def detect(path: Path) -> bool: | ||
return path.name.lower().endswith((".zip")) | ||
|
||
def map(self, target: target.Target) -> None: | ||
volumes = {} | ||
|
||
for member in self.zip.infolist(): | ||
if member.filename == ".": | ||
continue | ||
|
||
if not member.filename.startswith(("/fs/", "fs/", "/sysvol/", "sysvol/")): | ||
# Not an acquire tar | ||
if "/" not in volumes: | ||
vol = filesystem.VirtualFilesystem(case_sensitive=True) | ||
vol.zip = self.zip | ||
volumes["/"] = vol | ||
target.filesystems.add(vol) | ||
|
||
volume = volumes["/"] | ||
mname = member.filename | ||
else: | ||
if member.filename.startswith(("/fs/", "fs/")): | ||
# Current acquire | ||
parts = member.filename.replace("fs/", "").split("/") | ||
if parts[0] == "": | ||
parts.pop(0) | ||
else: | ||
# Legacy acquire | ||
parts = member.filename.lstrip("/").split("/") | ||
volume_name = parts[0].lower() | ||
|
||
# NOTE: older versions of acquire would write to "sysvol" instead of a driver letter | ||
# Figuring out the sysvol from the drive letters is easier than the drive letter from "sysvol", | ||
# so this was swapped in acquire 3.12. Now we map all volumes to a drive letter and let the | ||
# Windows OS plugin figure out which is the sysvol | ||
# For backwards compatibility we're forced to keep this check, and assume that "c:" is our sysvol | ||
if volume_name == "sysvol": | ||
volume_name = "c:" | ||
|
||
if volume_name == "$fs$": | ||
if len(parts) == 1: | ||
# The fs/$fs$ entry is ignored, only the directories below it are processed. | ||
continue | ||
fs_name = parts[1] | ||
if ANON_FS_RE.match(fs_name): | ||
parts.pop(0) | ||
volume_name = f"{volume_name}/{fs_name}" | ||
|
||
if volume_name not in volumes: | ||
vol = filesystem.VirtualFilesystem(case_sensitive=False) | ||
vol.zip = self.zip | ||
volumes[volume_name] = vol | ||
target.filesystems.add(vol) | ||
|
||
volume = volumes[volume_name] | ||
mname = "/".join(parts[1:]) | ||
|
||
entry_cls = ZipFilesystemDirectoryEntry if member.is_dir() else ZipFilesystemEntry | ||
entry = entry_cls(volume, fsutil.normpath(mname), member) | ||
volume.map_file_entry(entry.path, entry) | ||
|
||
for vol_name, vol in volumes.items(): | ||
loaderutil.add_virtual_ntfs_filesystem( | ||
target, | ||
vol, | ||
usnjrnl_path=[ | ||
"$Extend/$Usnjrnl:$J", | ||
"$Extend/$Usnjrnl:J", # Old versions of acquire used $Usnjrnl:J | ||
], | ||
) | ||
|
||
target.fs.mount(vol_name, vol) |
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.
Could you add tests for this new functionality? This could be done later, if you decide to go on the
AcquireLoader
route.