-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathtar.py
More file actions
202 lines (162 loc) · 6.1 KB
/
Copy pathtar.py
File metadata and controls
202 lines (162 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from __future__ import annotations
import tarfile as tf
from io import BytesIO
from typing import TYPE_CHECKING
from dissect.target import filesystem
from dissect.target.filesystems.tar import (
TarFilesystemDirectoryEntry,
TarFilesystemEntry,
)
from dissect.target.helpers import fsutil, loaderutil
from dissect.target.helpers.lazy import import_lazy
from dissect.target.helpers.logging import get_logger
from dissect.target.loader import Loader, SubLoader
if TYPE_CHECKING:
from collections.abc import Iterable
from pathlib import Path
from dissect.target import target
log = get_logger(__name__)
TAR_EXT_COMP = (
".tar.gz",
".tar.xz",
".tar.bz",
".tar.bz2",
".tar.lzma",
".tar.lz",
".tgz",
".txz",
".tbz",
".tbz2",
".tlz",
".tlzma",
)
TAR_EXT = (".tar",)
TAR_MAGIC_COMP = (
# gzip
b"\x1f\x8b",
# bzip2
b"\x42\x5a\x68",
# xz
b"\xfd\x37\x7a\x58\x5a\x00",
# lzma
b"\x5d\x00\x00\x01\x00",
b"\x5d\x00\x00\x10\x00",
b"\x5d\x00\x00\x08\x00",
b"\x5d\x00\x00\x10\x00",
b"\x5d\x00\x00\x20\x00",
b"\x5d\x00\x00\x40\x00",
b"\x5d\x00\x00\x80\x00",
b"\x5d\x00\x00\x00\x01",
b"\x5d\x00\x00\x00\x02",
)
TAR_MAGIC = (tf.GNU_MAGIC, tf.POSIX_MAGIC)
WINDOWS_MEMBERS = (
"windows/system32",
"/windows/system32",
"winnt",
"/winnt",
)
class TarSubLoader(SubLoader[tf.TarFile]):
"""Tar implementation of a :class:`SubLoader`."""
def __init__(self, path: Path, tar: tf.TarFile, **kwargs):
super().__init__(path, tar, **kwargs)
self.tar = tar
@staticmethod
def detect(path: Path, tarfile: tf.TarFile) -> bool:
"""Only to be called internally by :class:`TarLoader`."""
raise NotImplementedError
def map(self, target: target.Target) -> None:
"""Only to be called internally by :class:`TarLoader`."""
raise NotImplementedError
class GenericTarSubLoader(TarSubLoader):
"""Generic tar sub loader."""
@staticmethod
def detect(path: Path, tarfile: tf.TarFile) -> bool:
return True
def map(self, target: target.Target) -> None:
volumes = {}
windows_found = False
for member in self.tar.getmembers():
if member.name == ".":
continue
if member.name.lower().startswith(WINDOWS_MEMBERS):
windows_found = True
if "/" in volumes:
# Root filesystem was already added
volumes["/"].case_sensitive = False
if "/" not in volumes:
vol = filesystem.VirtualFilesystem(case_sensitive=not windows_found)
vol.tar = self.tar
volumes["/"] = vol
target.filesystems.add(vol)
volume = volumes["/"]
mname = member.name
entry_cls = TarFilesystemDirectoryEntry if member.isdir() else TarFilesystemEntry
entry = entry_cls(volume, fsutil.normpath(mname), member)
try:
volume.map_file_entry(entry.path, entry)
except KeyError as e:
log.debug("Skipping directory member %r in tar as %r is already mapped: %s", member, entry.path, e)
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)
class TarLoader(Loader):
"""Load tar files."""
__subloaders__ = (
import_lazy("dissect.target.loaders.containerimage").ContainerImageTarSubLoader,
import_lazy("dissect.target.loaders.acquire").AcquireTarSubLoader,
import_lazy("dissect.target.loaders.uac").UacTarSubloader,
import_lazy("dissect.target.loaders.nscollector").NsCollectorTarSubLoader,
import_lazy("dissect.target.loaders.vmsupport").VmSupportTarSubloader,
GenericTarSubLoader, # should be last
)
def __init__(self, path: Path, **kwargs):
super().__init__(path, **kwargs)
if is_compressed(path):
log.warning(
"Tar file %r is compressed, which will affect performance. "
"Consider uncompressing the archive before passing the tar file to Dissect.",
path,
)
self.fh = path.open("rb")
self.tar = tf.open(mode="r:*", fileobj=self.fh) # noqa: SIM115
self.subloader = None
@staticmethod
def detect(path: Path) -> bool:
return path.name.lower().endswith(TAR_EXT + TAR_EXT_COMP) or is_tar_magic(path, TAR_MAGIC + TAR_MAGIC_COMP)
def map(self, target: target.Target) -> None:
for candidate in self.__subloaders__:
if candidate.detect(self.path, self.tar):
self.subloader = candidate(self.path, self.tar, parsed_path=self.parsed_path)
self.subloader.map(target)
break
def is_tar_magic(path: Path, magics: Iterable[bytes]) -> bool:
if not path.is_file():
return False
with path.open("rb") as fh:
# The minimum file size of an uncompressed tar is 512 bytes, but a compressed tar file could be smaller.
fh.seek(0)
buf = fh.read(tf.BLOCKSIZE)
headers = [buf[0:6]]
if len(buf) >= 265:
headers.append(buf[257 : 257 + 8])
for header in headers:
if header.startswith(magics):
# We could be dealing with a compressed file that is not actually a tar.
# To weed out a false positive we try to decompress and read ustar from
# the first 512 bytes (or less) of the file.
try:
tf.open(mode="r:*", fileobj=BytesIO(buf)) # noqa: SIM115
except (tf.ReadError, tf.CompressionError, ValueError, EOFError):
continue
return True
return False
def is_compressed(path: Path) -> bool:
return path.name.lower().endswith(TAR_EXT_COMP) or is_tar_magic(path, TAR_MAGIC_COMP)