dissect.evidence 3.12.dev1__tar.gz → 3.13__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dissect_evidence-3.13/MANIFEST.in +4 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/PKG-INFO +4 -2
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/README.md +1 -1
- dissect_evidence-3.13/dissect/evidence/__init__.py +12 -0
- dissect_evidence-3.13/dissect/evidence/ad1/__init__.py +25 -0
- dissect_evidence-3.13/dissect/evidence/ad1/ad1.py +446 -0
- dissect_evidence-3.13/dissect/evidence/ad1/c_ad1.py +185 -0
- dissect_evidence-3.13/dissect/evidence/ad1/c_ad1.pyi +218 -0
- dissect_evidence-3.13/dissect/evidence/ad1/stream.py +78 -0
- dissect_evidence-3.13/dissect/evidence/adcrypt/__init__.py +12 -0
- dissect_evidence-3.13/dissect/evidence/adcrypt/adcrypt.py +152 -0
- dissect_evidence-3.13/dissect/evidence/adcrypt/c_adcrypt.py +40 -0
- dissect_evidence-3.13/dissect/evidence/adcrypt/c_adcrypt.pyi +58 -0
- dissect_evidence-3.13/dissect/evidence/adcrypt/stream.py +40 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/asdf/asdf.py +3 -51
- dissect_evidence-3.13/dissect/evidence/asdf/c_asdf.py +52 -0
- dissect_evidence-3.13/dissect/evidence/asdf/c_asdf.pyi +95 -0
- dissect_evidence-3.13/dissect/evidence/ewf/__init__.py +27 -0
- dissect_evidence-3.13/dissect/evidence/ewf/c_ewf.py +89 -0
- dissect_evidence-3.13/dissect/evidence/ewf/c_ewf.pyi +156 -0
- {dissect_evidence-3.12.dev1/dissect/evidence → dissect_evidence-3.13/dissect/evidence/ewf}/ewf.py +7 -92
- dissect_evidence-3.12.dev1/dissect/evidence/exceptions.py → dissect_evidence-3.13/dissect/evidence/exception.py +17 -2
- dissect_evidence-3.13/dissect/evidence/tools/adcrypt.py +71 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/asdf/repair.py +1 -1
- dissect_evidence-3.13/dissect/evidence/tools/util.py +32 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect.evidence.egg-info/PKG-INFO +4 -2
- dissect_evidence-3.13/dissect.evidence.egg-info/SOURCES.txt +55 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect.evidence.egg-info/entry_points.txt +1 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect.evidence.egg-info/requires.txt +3 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/pyproject.toml +8 -2
- {dissect_evidence-3.12.dev1/tests/docs → dissect_evidence-3.13/tests/_docs}/conf.py +7 -0
- dissect_evidence-3.13/tests/_utils.py +7 -0
- dissect_evidence-3.13/tests/conftest.py +90 -0
- dissect_evidence-3.13/tests/test_ad1.py +260 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/tests/test_asdf.py +2 -2
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/tests/test_ewf.py +4 -4
- dissect_evidence-3.13/tests/test_exception.py +21 -0
- dissect_evidence-3.13/tests/tools/__init__.py +0 -0
- dissect_evidence-3.13/tests/tools/test_adcrypt.py +74 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/tox.ini +4 -4
- dissect_evidence-3.12.dev1/MANIFEST.in +0 -2
- dissect_evidence-3.12.dev1/dissect/evidence/__init__.py +0 -8
- dissect_evidence-3.12.dev1/dissect/evidence/ad1.py +0 -252
- dissect_evidence-3.12.dev1/dissect.evidence.egg-info/SOURCES.txt +0 -39
- dissect_evidence-3.12.dev1/tests/conftest.py +0 -47
- dissect_evidence-3.12.dev1/tests/data/ad1_long.ad1 +0 -0
- dissect_evidence-3.12.dev1/tests/data/ad1_test.ad1 +0 -0
- dissect_evidence-3.12.dev1/tests/data/ad1_test_compressed.ad1 +0 -0
- dissect_evidence-3.12.dev1/tests/data/ewf.E01 +0 -0
- dissect_evidence-3.12.dev1/tests/test_ad1.py +0 -42
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/.git-blame-ignore-revs +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/COPYRIGHT +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/LICENSE +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/asdf/__init__.py +0 -0
- /dissect_evidence-3.12.dev1/dissect/evidence/asdf/streams.py → /dissect_evidence-3.13/dissect/evidence/asdf/stream.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/__init__.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/asdf/__init__.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/asdf/dd.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/asdf/meta.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect/evidence/tools/asdf/verify.py +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect.evidence.egg-info/dependency_links.txt +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/dissect.evidence.egg-info/top_level.txt +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/setup.cfg +0 -0
- {dissect_evidence-3.12.dev1 → dissect_evidence-3.13}/tests/__init__.py +0 -0
- {dissect_evidence-3.12.dev1/tests/docs → dissect_evidence-3.13/tests/_docs}/Makefile +0 -0
- /dissect_evidence-3.12.dev1/dissect/evidence/aff4.py → /dissect_evidence-3.13/tests/_docs/__init__.py +0 -0
- {dissect_evidence-3.12.dev1/tests/docs → dissect_evidence-3.13/tests/_docs}/index.rst +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dissect.evidence
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.13
|
|
4
4
|
Summary: A Dissect module implementing a parsers for various forensic evidence file containers, currently: AD1, ASDF and EWF
|
|
5
5
|
Author-email: Dissect Team <dissect@fox-it.com>
|
|
6
6
|
License-Expression: AGPL-3.0-or-later
|
|
@@ -23,6 +23,8 @@ License-File: LICENSE
|
|
|
23
23
|
License-File: COPYRIGHT
|
|
24
24
|
Requires-Dist: dissect.cstruct<5,>=4
|
|
25
25
|
Requires-Dist: dissect.util<4,>=3
|
|
26
|
+
Provides-Extra: full
|
|
27
|
+
Requires-Dist: pycryptodome; extra == "full"
|
|
26
28
|
Provides-Extra: dev
|
|
27
29
|
Requires-Dist: dissect.cstruct<5.0.dev,>=4.0.dev; extra == "dev"
|
|
28
30
|
Requires-Dist: dissect.util<4.0.dev,>=3.0.dev; extra == "dev"
|
|
@@ -30,7 +32,7 @@ Dynamic: license-file
|
|
|
30
32
|
|
|
31
33
|
# dissect.evidence
|
|
32
34
|
|
|
33
|
-
A Dissect module implementing
|
|
35
|
+
A Dissect module implementing parsers for various forensic evidence file containers, currently: AD1, ADCRYPT, ASDF and EWF. For
|
|
34
36
|
more information, please see [the documentation](https://docs.dissect.tools/en/latest/projects/dissect.evidence/index.html).
|
|
35
37
|
|
|
36
38
|
## Requirements
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# dissect.evidence
|
|
2
2
|
|
|
3
|
-
A Dissect module implementing
|
|
3
|
+
A Dissect module implementing parsers for various forensic evidence file containers, currently: AD1, ADCRYPT, ASDF and EWF. For
|
|
4
4
|
more information, please see [the documentation](https://docs.dissect.tools/en/latest/projects/dissect.evidence/index.html).
|
|
5
5
|
|
|
6
6
|
## Requirements
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dissect.evidence.ad1.ad1 import AD1
|
|
4
|
+
from dissect.evidence.asdf.asdf import AsdfSnapshot, AsdfStream
|
|
5
|
+
from dissect.evidence.ewf.ewf import EWF
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"AD1",
|
|
9
|
+
"EWF",
|
|
10
|
+
"AsdfSnapshot",
|
|
11
|
+
"AsdfStream",
|
|
12
|
+
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dissect.evidence.ad1.ad1 import AD1, FileEntry, FileMeta, LogicalImage, MetaType, SegmentFile
|
|
4
|
+
from dissect.evidence.ad1.stream import AD1Stream, FileStream
|
|
5
|
+
from dissect.evidence.exception import (
|
|
6
|
+
Error,
|
|
7
|
+
FileNotFoundError,
|
|
8
|
+
NotADirectoryError,
|
|
9
|
+
NotASymlinkError,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"AD1",
|
|
14
|
+
"AD1Stream",
|
|
15
|
+
"Error",
|
|
16
|
+
"FileEntry",
|
|
17
|
+
"FileMeta",
|
|
18
|
+
"FileNotFoundError",
|
|
19
|
+
"FileStream",
|
|
20
|
+
"LogicalImage",
|
|
21
|
+
"MetaType",
|
|
22
|
+
"NotADirectoryError",
|
|
23
|
+
"NotASymlinkError",
|
|
24
|
+
"SegmentFile",
|
|
25
|
+
]
|
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
from functools import cached_property
|
|
6
|
+
from pathlib import Path, PurePath, PurePosixPath, PureWindowsPath
|
|
7
|
+
from typing import TYPE_CHECKING, BinaryIO
|
|
8
|
+
|
|
9
|
+
from dissect.evidence.ad1.c_ad1 import c_ad1
|
|
10
|
+
from dissect.evidence.ad1.stream import AD1Stream, FileStream
|
|
11
|
+
from dissect.evidence.adcrypt.adcrypt import ADCrypt, is_adcrypt
|
|
12
|
+
from dissect.evidence.exception import FileNotFoundError, NotADirectoryError, NotASymlinkError
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from collections.abc import Iterator
|
|
16
|
+
|
|
17
|
+
EntryType = c_ad1.EntryType
|
|
18
|
+
MetaType = c_ad1.MetaType
|
|
19
|
+
FileClassType = c_ad1.FileClassType
|
|
20
|
+
|
|
21
|
+
MAX_OPEN_SEGMENTS = 128
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def find_files(path: Path) -> list[Path]:
|
|
25
|
+
files = set()
|
|
26
|
+
for file in path.parent.iterdir():
|
|
27
|
+
if file.stem == path.stem and re.match(r"^\.ad[0-9]+$", file.suffix.lower()):
|
|
28
|
+
files.add(file)
|
|
29
|
+
return sorted(files, key=lambda file: int(file.suffix[3:]))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class AD1:
|
|
33
|
+
"""AccessData Logical Image (AD1v4) implementation.
|
|
34
|
+
|
|
35
|
+
Supports ``zlib`` compressed images and ADCRYPT encrypted images.
|
|
36
|
+
|
|
37
|
+
Should be initialized using a list of segment paths or file-like objects, e.g.::
|
|
38
|
+
|
|
39
|
+
fs = AD1([Path("file.ad1"), Path("file.ad2")])
|
|
40
|
+
fs = AD1([Path("file.ad1").open("rb"), Path("file.ad2").open("rb")])
|
|
41
|
+
|
|
42
|
+
If the AD1 container is ADCRYPT encrypted, it can be unlocked using either a passphrase or private key::
|
|
43
|
+
|
|
44
|
+
fs.unlock(passphrase="my secret passphrase")
|
|
45
|
+
fs.unlock(private_key=Path("path/to/private/key.pem"))
|
|
46
|
+
|
|
47
|
+
Resources:
|
|
48
|
+
- Reverse engineering FTK Imager
|
|
49
|
+
- https://github.com/pcbje/pyad1/blob/master/documentation/AccessData%20Format%20(AD1).asciidoc
|
|
50
|
+
- https://github.com/al3ks1s/AD1-tools
|
|
51
|
+
- https://web.archive.org/web/20231013073319/https://tmairi.github.io/posts/dissecting-the-ad1-file-format/
|
|
52
|
+
- https://al3ks1s.fr/posts/adventures-part-1/
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, fh: BinaryIO | list[BinaryIO]):
|
|
56
|
+
fhs = [fh] if not isinstance(fh, list) else fh
|
|
57
|
+
self.fh = fhs
|
|
58
|
+
self.root = VirtualEntry(self, "/")
|
|
59
|
+
|
|
60
|
+
self._segments: dict[int, SegmentFile] = {}
|
|
61
|
+
self._segment_lru = []
|
|
62
|
+
self._segment_offsets = []
|
|
63
|
+
|
|
64
|
+
self.size = 0
|
|
65
|
+
self.stream: AD1Stream | None = None
|
|
66
|
+
self.logical_image: LogicalImage | None = None
|
|
67
|
+
|
|
68
|
+
if not self.fh:
|
|
69
|
+
raise ValueError("No segment files provided for AD1 container")
|
|
70
|
+
|
|
71
|
+
self.adcrypt = None
|
|
72
|
+
|
|
73
|
+
first_segment = self.segment(0)
|
|
74
|
+
if is_adcrypt(first_segment.fh):
|
|
75
|
+
self.adcrypt = ADCrypt(first_segment.fh)
|
|
76
|
+
else:
|
|
77
|
+
self._open_ad1()
|
|
78
|
+
|
|
79
|
+
def is_adcrypt(self) -> bool:
|
|
80
|
+
"""Return whether the AD1 container is ADCRYPT encrypted."""
|
|
81
|
+
return self.adcrypt is not None
|
|
82
|
+
|
|
83
|
+
def is_locked(self) -> bool:
|
|
84
|
+
"""Return whether the ADCRYPT container is locked."""
|
|
85
|
+
return self.is_adcrypt() and self.adcrypt.is_locked()
|
|
86
|
+
|
|
87
|
+
def segment(self, idx: int) -> SegmentFile:
|
|
88
|
+
"""Open a segment by index.
|
|
89
|
+
|
|
90
|
+
Implements a simple LRU cache to limit the number of open segments.
|
|
91
|
+
|
|
92
|
+
Args:
|
|
93
|
+
idx: Index or URI of the segment to open.
|
|
94
|
+
"""
|
|
95
|
+
# Poor mans LRU
|
|
96
|
+
if idx in self._segments:
|
|
97
|
+
self._segment_lru.remove(idx)
|
|
98
|
+
self._segment_lru.append(idx)
|
|
99
|
+
return self._segments[idx]
|
|
100
|
+
|
|
101
|
+
if len(self._segment_lru) >= MAX_OPEN_SEGMENTS:
|
|
102
|
+
oldest_idx = self._segment_lru.pop(0)
|
|
103
|
+
oldest_segment = self._segments.pop(oldest_idx)
|
|
104
|
+
|
|
105
|
+
# Don't close it if we received it as a file-like object
|
|
106
|
+
if not hasattr(self.fh[oldest_idx], "read"):
|
|
107
|
+
oldest_segment.fh.close()
|
|
108
|
+
|
|
109
|
+
del oldest_segment
|
|
110
|
+
|
|
111
|
+
fh = self.fh[idx]
|
|
112
|
+
if not hasattr(fh, "read"):
|
|
113
|
+
fh = fh.open("rb") if isinstance(fh, Path) else Path(fh).open("rb") # noqa: SIM115
|
|
114
|
+
|
|
115
|
+
if self.is_adcrypt() and not self.is_locked():
|
|
116
|
+
fh = self.adcrypt.wrap(fh, idx)
|
|
117
|
+
|
|
118
|
+
segment = SegmentFile(fh)
|
|
119
|
+
|
|
120
|
+
self._segments[idx] = segment
|
|
121
|
+
self._segment_lru.append(idx)
|
|
122
|
+
|
|
123
|
+
return segment
|
|
124
|
+
|
|
125
|
+
def unlock(self, *, passphrase: str | bytes | None = None, private_key: Path | bytes | None = None) -> None:
|
|
126
|
+
"""Unlock the ADCRYPT container with a given passphrase or private key.
|
|
127
|
+
|
|
128
|
+
Args:
|
|
129
|
+
passphrase: The passphrase to unlock the container.
|
|
130
|
+
private_key: The private key to unlock the container.
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
RuntimeError: If required dependencies are missing.
|
|
134
|
+
ValueError: If unlocking failed.
|
|
135
|
+
"""
|
|
136
|
+
self.adcrypt.unlock(passphrase=passphrase, private_key=private_key)
|
|
137
|
+
|
|
138
|
+
# Reset LRU
|
|
139
|
+
self._segments = {}
|
|
140
|
+
self._segment_lru = []
|
|
141
|
+
|
|
142
|
+
# Open the AD1
|
|
143
|
+
self._open_ad1()
|
|
144
|
+
|
|
145
|
+
def _open_ad1(self) -> None:
|
|
146
|
+
self._segment_offsets = []
|
|
147
|
+
|
|
148
|
+
offset = 0
|
|
149
|
+
for i in range(len(self.fh)):
|
|
150
|
+
segment = self.segment(i)
|
|
151
|
+
if segment.header.magic != c_ad1.ADSEGMENTEDFILE_MAGIC.encode():
|
|
152
|
+
raise ValueError(f"Invalid AD1 segment file magic in segment {i}")
|
|
153
|
+
|
|
154
|
+
if segment.number != i + 1:
|
|
155
|
+
raise ValueError(f"Invalid AD1 segment number in segment {i}, got {segment.number}, expected {i + 1}")
|
|
156
|
+
|
|
157
|
+
offset += segment.size
|
|
158
|
+
self._segment_offsets.append(offset)
|
|
159
|
+
|
|
160
|
+
self.size = offset
|
|
161
|
+
self.stream = AD1Stream(self)
|
|
162
|
+
|
|
163
|
+
# The first .ad1 file contains a logical image header
|
|
164
|
+
first_segment = self.segment(0)
|
|
165
|
+
first_segment.fh.seek(first_segment.header.logical_image_offset)
|
|
166
|
+
self.logical_image = LogicalImage(first_segment.fh)
|
|
167
|
+
|
|
168
|
+
# We need to create some fake entries for all parts leading up to `logical_image.name`
|
|
169
|
+
# This name commonly contains the full path each entry in the container is relative to
|
|
170
|
+
_hallicinate_root_entries(self)
|
|
171
|
+
|
|
172
|
+
def entry(self, path: str, entry: FileEntry | None = None) -> FileEntry:
|
|
173
|
+
"""Return a :class:`FileEntry` based on the given absolute ``path``.
|
|
174
|
+
|
|
175
|
+
Args:
|
|
176
|
+
path: Absolute path within the AD1 container.
|
|
177
|
+
entry: The starting entry for relative paths. Defaults to the root entry.
|
|
178
|
+
|
|
179
|
+
Raises:
|
|
180
|
+
ValueError: If the ADCRYPT container is locked.
|
|
181
|
+
FileNotFoundError if the given ``path`` is not found in the container.
|
|
182
|
+
|
|
183
|
+
Returns:
|
|
184
|
+
:class:`FileEntry` when the given ``path`` is found.
|
|
185
|
+
"""
|
|
186
|
+
if self.is_locked():
|
|
187
|
+
raise ValueError("AD1 container is locked by ADCRYPT")
|
|
188
|
+
|
|
189
|
+
entry = entry or self.root
|
|
190
|
+
|
|
191
|
+
for part in path.split("/"):
|
|
192
|
+
if not part:
|
|
193
|
+
continue
|
|
194
|
+
|
|
195
|
+
for child in entry.iterdir():
|
|
196
|
+
if child.name == part and child.type != EntryType.Deleted:
|
|
197
|
+
entry = child
|
|
198
|
+
break
|
|
199
|
+
else:
|
|
200
|
+
raise FileNotFoundError(f"File not found: {path}")
|
|
201
|
+
|
|
202
|
+
return entry
|
|
203
|
+
|
|
204
|
+
def get(self, path: str) -> FileEntry:
|
|
205
|
+
"""Shortcut for ``AD1.entry(path)``."""
|
|
206
|
+
return self.entry(path)
|
|
207
|
+
|
|
208
|
+
def open(self, path: str) -> FileStream:
|
|
209
|
+
"""Shortcut for ``AD1.entry(path).open()``."""
|
|
210
|
+
return self.entry(path).open()
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _hallicinate_root_entries(ad1: AD1) -> None:
|
|
214
|
+
# We need to create some fake entries for all parts leading up to `logical_image.name`
|
|
215
|
+
# This name commonly contains the full path each entry in the container is relative to
|
|
216
|
+
# Not always though, so do some poor mans heuristics
|
|
217
|
+
root_name = ad1.logical_image.name
|
|
218
|
+
if root_name == "Custom Content Image([Multi])":
|
|
219
|
+
ad1.root.entry.child = ad1.logical_image.header.first_file_offset
|
|
220
|
+
if len(ad1.root.children) != 1:
|
|
221
|
+
raise ValueError("Unexpected number of root children for Custom Content Image([Multi])")
|
|
222
|
+
|
|
223
|
+
root_name = ad1.root.children[0].name.split(":", 1)[-1]
|
|
224
|
+
first_file_entry = ad1.root.children[0].entry
|
|
225
|
+
is_multi_image = True
|
|
226
|
+
else:
|
|
227
|
+
is_multi_image = False
|
|
228
|
+
|
|
229
|
+
root_path = PureWindowsPath(root_name) if "/" not in root_name and "\\" in root_name else PurePosixPath(root_name)
|
|
230
|
+
parts = list(root_path.parts)
|
|
231
|
+
parent = _create_root_entries(ad1, ad1.root, root_path, parts)
|
|
232
|
+
|
|
233
|
+
if is_multi_image:
|
|
234
|
+
parent.name = parent.name
|
|
235
|
+
parent.offset = ad1.logical_image.header.first_file_offset
|
|
236
|
+
parent.entry = first_file_entry
|
|
237
|
+
else:
|
|
238
|
+
# Add the first file offset as the first child offset of the last root part
|
|
239
|
+
parent.entry.child = ad1.logical_image.header.first_file_offset
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def _create_root_entries(ad1: AD1, parent: FileEntry, path: PurePath, parts: list[str]) -> FileEntry:
|
|
243
|
+
while parts:
|
|
244
|
+
part = parts.pop(0)
|
|
245
|
+
if path.drive and part == f"{path.drive}\\":
|
|
246
|
+
part = path.drive
|
|
247
|
+
|
|
248
|
+
entry = VirtualEntry(ad1, part)
|
|
249
|
+
parent.children = [entry]
|
|
250
|
+
parent = entry
|
|
251
|
+
|
|
252
|
+
return parent
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
class SegmentFile:
|
|
256
|
+
"""Represents an AD1 segmented file."""
|
|
257
|
+
|
|
258
|
+
def __init__(self, fh: BinaryIO):
|
|
259
|
+
self.fh = fh
|
|
260
|
+
self.fh.seek(0)
|
|
261
|
+
self.header = c_ad1.SegmentedFileHeader(self.fh)
|
|
262
|
+
self.number = self.header.segment_number
|
|
263
|
+
self.count = self.header.segment_count
|
|
264
|
+
self.size = self.header.segment_size - 512 # Subtract header size
|
|
265
|
+
|
|
266
|
+
def __repr__(self) -> str:
|
|
267
|
+
return f"<SegmentFile number={self.number} count={self.count} size={self.size}>"
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
class LogicalImage:
|
|
271
|
+
"""Represents an AD1 logical image."""
|
|
272
|
+
|
|
273
|
+
def __init__(self, fh: BinaryIO):
|
|
274
|
+
self.fh = fh
|
|
275
|
+
self.header = c_ad1.LogicalImageHeader(fh)
|
|
276
|
+
self.name = self.header.name.decode()
|
|
277
|
+
self.version = self.header.version
|
|
278
|
+
self.offset = self.header.first_file_offset
|
|
279
|
+
self.chunk_size = self.header.chunk_size
|
|
280
|
+
|
|
281
|
+
def __repr__(self) -> str:
|
|
282
|
+
return (
|
|
283
|
+
f"<LogicalImage version={self.version} name={self.name} offset={self.offset} chunk_size={self.chunk_size}>"
|
|
284
|
+
)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
class FileEntry:
|
|
288
|
+
"""Represents a file entry in an AD1 logical image."""
|
|
289
|
+
|
|
290
|
+
def __init__(self, ad1: AD1, offset: int):
|
|
291
|
+
self.ad1 = ad1
|
|
292
|
+
self.offset = offset
|
|
293
|
+
|
|
294
|
+
def __repr__(self) -> str:
|
|
295
|
+
return f"<{self.__class__.__name__} type={self.type.name} name={self.name!r} size={self.size}>"
|
|
296
|
+
|
|
297
|
+
@cached_property
|
|
298
|
+
def entry(self) -> c_ad1.FileEntry:
|
|
299
|
+
self.ad1.stream.seek(self.offset)
|
|
300
|
+
return c_ad1.FileEntry(self.ad1.stream)
|
|
301
|
+
|
|
302
|
+
@cached_property
|
|
303
|
+
def name(self) -> str:
|
|
304
|
+
return self.entry.name.decode()
|
|
305
|
+
|
|
306
|
+
@cached_property
|
|
307
|
+
def type(self) -> EntryType:
|
|
308
|
+
return self.entry.type
|
|
309
|
+
|
|
310
|
+
@cached_property
|
|
311
|
+
def meta(self) -> dict[MetaType, FileMeta]:
|
|
312
|
+
result = {}
|
|
313
|
+
|
|
314
|
+
offset = self.entry.meta
|
|
315
|
+
while offset != 0:
|
|
316
|
+
meta = FileMeta(self.ad1, offset)
|
|
317
|
+
offset = meta.next
|
|
318
|
+
result[meta.type] = meta
|
|
319
|
+
|
|
320
|
+
return result
|
|
321
|
+
|
|
322
|
+
@cached_property
|
|
323
|
+
def children(self) -> list[FileEntry]:
|
|
324
|
+
result = []
|
|
325
|
+
|
|
326
|
+
offset = self.entry.child
|
|
327
|
+
while offset != 0:
|
|
328
|
+
child = FileEntry(self.ad1, offset)
|
|
329
|
+
result.append(child)
|
|
330
|
+
offset = child.entry.next
|
|
331
|
+
|
|
332
|
+
return result
|
|
333
|
+
|
|
334
|
+
@cached_property
|
|
335
|
+
def size(self) -> int:
|
|
336
|
+
if meta := self.meta.get(MetaType.FileSize):
|
|
337
|
+
return int(meta.data)
|
|
338
|
+
return 0
|
|
339
|
+
|
|
340
|
+
@cached_property
|
|
341
|
+
def atime(self) -> datetime:
|
|
342
|
+
if meta := self.meta.get(MetaType.DateAccessed):
|
|
343
|
+
return convert_ts(meta.data)
|
|
344
|
+
return datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
345
|
+
|
|
346
|
+
@cached_property
|
|
347
|
+
def ctime(self) -> datetime:
|
|
348
|
+
if meta := self.meta.get(MetaType.MFTFileDateChanged, self.meta.get(MetaType.DateModified)):
|
|
349
|
+
return convert_ts(meta.data)
|
|
350
|
+
return datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
351
|
+
|
|
352
|
+
@cached_property
|
|
353
|
+
def mtime(self) -> datetime:
|
|
354
|
+
if meta := self.meta.get(MetaType.DateModified):
|
|
355
|
+
return convert_ts(meta.data)
|
|
356
|
+
return datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
357
|
+
|
|
358
|
+
@cached_property
|
|
359
|
+
def btime(self) -> datetime:
|
|
360
|
+
if meta := self.meta.get(MetaType.DateCreated):
|
|
361
|
+
return convert_ts(meta.data)
|
|
362
|
+
return datetime(1970, 1, 1, 0, 0, 0, tzinfo=timezone.utc)
|
|
363
|
+
|
|
364
|
+
@cached_property
|
|
365
|
+
def md5(self) -> str | None:
|
|
366
|
+
if meta := self.meta.get(MetaType.MD5):
|
|
367
|
+
return meta.data.decode()
|
|
368
|
+
return None
|
|
369
|
+
|
|
370
|
+
@cached_property
|
|
371
|
+
def sha1(self) -> str | None:
|
|
372
|
+
if meta := self.meta.get(MetaType.SHA1):
|
|
373
|
+
return meta.data.decode()
|
|
374
|
+
return None
|
|
375
|
+
|
|
376
|
+
def is_file(self) -> bool:
|
|
377
|
+
return self.type in (EntryType.File, EntryType.Unknown_File)
|
|
378
|
+
|
|
379
|
+
def is_dir(self) -> bool:
|
|
380
|
+
return self.type == EntryType.Directory
|
|
381
|
+
|
|
382
|
+
def is_symlink(self) -> bool:
|
|
383
|
+
if meta := self.meta.get(MetaType.FileClass):
|
|
384
|
+
return int.from_bytes(meta.data, "little") == FileClassType.ReparsePoint
|
|
385
|
+
return False
|
|
386
|
+
|
|
387
|
+
def listdir(self) -> list[str]:
|
|
388
|
+
if not self.is_dir():
|
|
389
|
+
raise NotADirectoryError(self.name)
|
|
390
|
+
return [child.name for child in self.children]
|
|
391
|
+
|
|
392
|
+
def iterdir(self) -> Iterator[FileEntry]:
|
|
393
|
+
if not self.is_dir():
|
|
394
|
+
raise NotADirectoryError(self.name)
|
|
395
|
+
yield from self.children
|
|
396
|
+
|
|
397
|
+
def readlink(self) -> str:
|
|
398
|
+
if not self.is_symlink():
|
|
399
|
+
raise NotASymlinkError(self.name)
|
|
400
|
+
|
|
401
|
+
# TODO: Investigate symlinks for unix-like filesystems.
|
|
402
|
+
|
|
403
|
+
reparse_point = c_ad1.ReparsePoint(self.open())
|
|
404
|
+
return reparse_point.link.strip("\00").split("\00")[-1]
|
|
405
|
+
|
|
406
|
+
def open(self) -> FileStream:
|
|
407
|
+
"""Open the file entry for reading."""
|
|
408
|
+
if self.is_dir():
|
|
409
|
+
raise IsADirectoryError(self.name)
|
|
410
|
+
return FileStream(self)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
class VirtualEntry(FileEntry):
|
|
414
|
+
"""Represents the root entry in an AD1 logical image."""
|
|
415
|
+
|
|
416
|
+
def __init__(self, ad1: AD1, name: str):
|
|
417
|
+
super().__init__(ad1, -1)
|
|
418
|
+
self.entry = c_ad1.FileEntry(name=name.encode(), type=EntryType.Directory, size=0)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
class FileMeta:
|
|
422
|
+
"""Represents a single AD1 logical file metadata item found inside :class:`FileEntry`."""
|
|
423
|
+
|
|
424
|
+
def __init__(self, ad1: AD1, offset: int):
|
|
425
|
+
self.ad1 = ad1
|
|
426
|
+
self.offset = offset
|
|
427
|
+
|
|
428
|
+
self.ad1.stream.seek(offset)
|
|
429
|
+
self.entry = c_ad1.FileMeta(self.ad1.stream)
|
|
430
|
+
|
|
431
|
+
self.category = self.entry.category
|
|
432
|
+
self.type = self.entry.type
|
|
433
|
+
self.data = self.entry.data
|
|
434
|
+
|
|
435
|
+
self.next = self.entry.next
|
|
436
|
+
|
|
437
|
+
def __repr__(self) -> str:
|
|
438
|
+
return f"<FileMeta category={self.category} type={self.type} data={self.data}>"
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
def convert_ts(value: bytes) -> datetime:
|
|
442
|
+
"""Convert an AD1 timestamp to datetime object. Assuming this is UTC."""
|
|
443
|
+
|
|
444
|
+
# DateCreated does not (always) have ``.%f`` precision.
|
|
445
|
+
fmt = "%Y%m%dT%H%M%S.%f" if b"." in value else "%Y%m%dT%H%M%S"
|
|
446
|
+
return datetime.strptime(value.decode(), fmt).replace(tzinfo=timezone.utc)
|