dissect.hypervisor 3.19.dev1__tar.gz → 3.20__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.
Files changed (102) hide show
  1. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/PKG-INFO +3 -4
  2. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/__init__.py +2 -1
  3. dissect_hypervisor-3.20/dissect/hypervisor/disk/asif.py +262 -0
  4. dissect_hypervisor-3.20/dissect/hypervisor/disk/c_asif.py +37 -0
  5. dissect_hypervisor-3.20/dissect/hypervisor/disk/c_asif.pyi +68 -0
  6. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_qcow2.py +2 -5
  7. dissect_hypervisor-3.20/dissect/hypervisor/disk/c_qcow2.pyi +189 -0
  8. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/qcow2.py +193 -130
  9. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/vhdx.py +2 -2
  10. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/util/vmtar.py +2 -2
  11. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/PKG-INFO +3 -4
  12. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/SOURCES.txt +14 -0
  13. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/pyproject.toml +26 -9
  14. dissect_hypervisor-3.20/tests/_data/disk/asif/basic.asif.gz +0 -0
  15. dissect_hypervisor-3.20/tests/_data/disk/qcow2/backing-chain-1.qcow2.gz +0 -0
  16. dissect_hypervisor-3.20/tests/_data/disk/qcow2/backing-chain-2.qcow2.gz +0 -0
  17. dissect_hypervisor-3.20/tests/_data/disk/qcow2/backing-chain-3.qcow2.gz +0 -0
  18. dissect_hypervisor-3.20/tests/_data/disk/qcow2/basic.qcow2.gz +0 -0
  19. dissect_hypervisor-3.20/tests/_data/disk/qcow2/data-file.bin.gz +0 -0
  20. dissect_hypervisor-3.20/tests/_data/disk/qcow2/data-file.qcow2.gz +0 -0
  21. dissect_hypervisor-3.20/tests/_data/disk/qcow2/snapshot.qcow2.gz +0 -0
  22. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/conftest.py +32 -3
  23. dissect_hypervisor-3.20/tests/disk/test_asif.py +17 -0
  24. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/disk/test_hdd.py +11 -11
  25. dissect_hypervisor-3.20/tests/disk/test_qcow2.py +147 -0
  26. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tox.ini +8 -12
  27. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/.git-blame-ignore-revs +0 -0
  28. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/COPYRIGHT +0 -0
  29. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/LICENSE +0 -0
  30. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/MANIFEST.in +0 -0
  31. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/README.md +0 -0
  32. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/__init__.py +0 -0
  33. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/c_hyperv.py +0 -0
  34. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/hyperv.py +0 -0
  35. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/ovf.py +0 -0
  36. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/pvs.py +0 -0
  37. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/vbox.py +0 -0
  38. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/descriptor/vmx.py +0 -0
  39. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/__init__.py +0 -0
  40. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_hdd.py +0 -0
  41. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_vdi.py +0 -0
  42. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_vhd.py +0 -0
  43. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_vhdx.py +0 -0
  44. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/c_vmdk.py +0 -0
  45. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/hdd.py +0 -0
  46. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/vdi.py +0 -0
  47. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/vhd.py +0 -0
  48. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/disk/vmdk.py +0 -0
  49. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/exceptions.py +0 -0
  50. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/tools/__init__.py +0 -0
  51. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/tools/envelope.py +0 -0
  52. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/tools/vmtar.py +0 -0
  53. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/util/__init__.py +0 -0
  54. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect/hypervisor/util/envelope.py +0 -0
  55. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/dependency_links.txt +0 -0
  56. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/entry_points.txt +0 -0
  57. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/requires.txt +0 -0
  58. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/dissect.hypervisor.egg-info/top_level.txt +0 -0
  59. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/setup.cfg +0 -0
  60. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/__init__.py +0 -0
  61. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/descriptor/hyperv/test.VMRS +0 -0
  62. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/descriptor/hyperv/test.vmcx +0 -0
  63. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/descriptor/vmx/encrypted.vmx +0 -0
  64. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/expanding.hdd/DiskDescriptor.xml +0 -0
  65. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/expanding.hdd/expanding.hdd +0 -0
  66. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/expanding.hdd/expanding.hdd.0.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  67. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/plain.hdd/DiskDescriptor.xml +0 -0
  68. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/plain.hdd/plain.hdd +0 -0
  69. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/plain.hdd/plain.hdd.0.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  70. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/DiskDescriptor.xml +0 -0
  71. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd +0 -0
  72. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.0.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  73. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.1.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  74. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.2.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  75. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.3.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  76. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.4.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  77. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/hdd/split.hdd/split.hdd.5.{5fbaabe3-6958-40ff-92a7-860e329aab41}.hds.gz +0 -0
  78. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vhd/dynamic.vhd.gz +0 -0
  79. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vhd/fixed.vhd.gz +0 -0
  80. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vhdx/differencing.avhdx.gz +0 -0
  81. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vhdx/dynamic.vhdx.gz +0 -0
  82. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vhdx/fixed.vhdx.gz +0 -0
  83. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/disk/vmdk/sesparse.vmdk.gz +0 -0
  84. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/util/envelope/encryption.info +0 -0
  85. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/util/envelope/local.tgz.ve +0 -0
  86. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_data/util/vmtar/test.vgz +0 -0
  87. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_docs/Makefile +0 -0
  88. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_docs/conf.py +0 -0
  89. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/_docs/index.rst +0 -0
  90. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/__init__.py +0 -0
  91. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/test_hyperv.py +0 -0
  92. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/test_ovf.py +0 -0
  93. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/test_pvs.py +0 -0
  94. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/test_vbox.py +0 -0
  95. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/descriptor/test_vmx.py +0 -0
  96. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/disk/__init__.py +0 -0
  97. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/disk/test_vhd.py +0 -0
  98. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/disk/test_vhdx.py +0 -0
  99. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/disk/test_vmdk.py +0 -0
  100. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/util/__init__.py +0 -0
  101. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/util/test_envelope.py +0 -0
  102. {dissect_hypervisor-3.19.dev1 → dissect_hypervisor-3.20}/tests/util/test_vmtar.py +0 -0
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.hypervisor
3
- Version: 3.19.dev1
3
+ Version: 3.20
4
4
  Summary: A Dissect module implementing parsers for various hypervisor disk, backup and configuration files
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
- License: Affero General Public License v3
6
+ License-Expression: AGPL-3.0-or-later
7
7
  Project-URL: homepage, https://dissect.tools
8
8
  Project-URL: documentation, https://docs.dissect.tools/en/latest/projects/dissect.hypervisor
9
9
  Project-URL: repository, https://github.com/fox-it/dissect.hypervisor
@@ -11,14 +11,13 @@ Classifier: Development Status :: 5 - Production/Stable
11
11
  Classifier: Environment :: Console
12
12
  Classifier: Intended Audience :: Developers
13
13
  Classifier: Intended Audience :: Information Technology
14
- Classifier: License :: OSI Approved
15
14
  Classifier: Operating System :: OS Independent
16
15
  Classifier: Programming Language :: Python :: 3
17
16
  Classifier: Topic :: Internet :: Log Analysis
18
17
  Classifier: Topic :: Scientific/Engineering :: Information Analysis
19
18
  Classifier: Topic :: Security
20
19
  Classifier: Topic :: Utilities
21
- Requires-Python: ~=3.9
20
+ Requires-Python: >=3.10
22
21
  Description-Content-Type: text/markdown
23
22
  License-File: LICENSE
24
23
  License-File: COPYRIGHT
@@ -1,8 +1,9 @@
1
1
  from dissect.hypervisor.descriptor import hyperv, ovf, pvs, vbox, vmx
2
- from dissect.hypervisor.disk import hdd, qcow2, vdi, vhd, vhdx, vmdk
2
+ from dissect.hypervisor.disk import asif, hdd, qcow2, vdi, vhd, vhdx, vmdk
3
3
  from dissect.hypervisor.util import envelope, vmtar
4
4
 
5
5
  __all__ = [
6
+ "asif",
6
7
  "envelope",
7
8
  "hdd",
8
9
  "hyperv",
@@ -0,0 +1,262 @@
1
+ from __future__ import annotations
2
+
3
+ import plistlib
4
+ from functools import cached_property, lru_cache
5
+ from typing import Any, BinaryIO
6
+ from uuid import UUID
7
+
8
+ from dissect.util.stream import AlignedStream
9
+
10
+ from dissect.hypervisor.disk.c_asif import c_asif
11
+ from dissect.hypervisor.exceptions import InvalidSignature
12
+
13
+
14
+ class ASIF:
15
+ """Apple Sparse Image Format (ASIF) disk image.
16
+
17
+ ASIF disk images are a virtual disk format introduced in macOS Tahoe. They can be used in Apple's Virtualization
18
+ framework, as well as through Disk Utility.
19
+
20
+ An ASIF file is pretty straight forward. There's a small header which, among some other details, contains two
21
+ directory offsets. Each directory contains a list of tables, which in turn contain a list of data entries. Each data
22
+ entry points to a chunk of data in the ASIF file. The chunk size is defined in the header and is typically 1 MiB.
23
+ The chunk size is always a multiple of the block size, which is also defined in the header (typically 512 bytes).
24
+ Each directory has a version number, and the directory with the highest version number is the active directory. This
25
+ allows for atomic updates of the directory/table data.
26
+
27
+ The maximum virtual disk size seems to be just under 4 PiB, with a small portion at the end reserved for metadata.
28
+ The actual size of the virtual disk is defined in the header, as well as the maximum size the disk can grow to.
29
+
30
+ The offset to the metadata block is typically ``(4 PiB - 1 chunk)``, meaning it's within the reserved area.
31
+ The metadata block contains a small header and a plist. The plist should contain an ``internal metadata`` and
32
+ ``user metadata`` dictionary. Besides a "stable uuid", it's unclear what the metadata is used for or how to set it.
33
+
34
+ Args:
35
+ fh: File-like object containing the ASIF image.
36
+
37
+ Resources:
38
+ - Reversing ``diskimagescontroller``
39
+ - https://developer.apple.com/documentation/virtualization/vzdiskimagestoragedeviceattachment/
40
+ """
41
+
42
+ def __init__(self, fh: BinaryIO):
43
+ self.fh = fh
44
+
45
+ self.header = c_asif.asif_header(fh)
46
+ if self.header.header_signature != c_asif.ASIF_HEADER_SIGNATURE:
47
+ raise InvalidSignature(
48
+ f"Not a valid ASIF image (expected {c_asif.ASIF_HEADER_SIGNATURE:#x}, "
49
+ f"got {self.header.header_signature:#x})"
50
+ )
51
+
52
+ self.guid = UUID(bytes=self.header.guid)
53
+ self.block_size = self.header.block_size
54
+ self.chunk_size = self.header.chunk_size
55
+ self.size = self.header.sector_count * self.block_size
56
+ self.max_size = self.header.max_sector_count * self.block_size
57
+
58
+ # The following math is taken from the assembly with some creative variable naming
59
+ # It's possible that some of this can be simplified or the names improved
60
+ self._blocks_per_chunk = self.chunk_size // self.block_size
61
+
62
+ # This check doesn't really make sense, but keep it in for now
63
+ reserved_size = 4 * self.chunk_size
64
+ self._num_reserved_table_entries = (
65
+ 1 if reserved_size < self._blocks_per_chunk else reserved_size // self._blocks_per_chunk
66
+ )
67
+
68
+ self._max_table_entries = self.chunk_size >> 3
69
+ self._num_table_entries = self._max_table_entries - (
70
+ self._max_table_entries % (self._num_reserved_table_entries + 1)
71
+ )
72
+ self._num_reserved_directory_entries = (self._num_reserved_table_entries + self._num_table_entries) // (
73
+ self._num_reserved_table_entries + 1
74
+ )
75
+ self._num_usable_entries = self._num_table_entries - self._num_reserved_directory_entries
76
+ # This is the size in bytes of data covered by a single table
77
+ self._size_per_table = self._num_usable_entries * self.chunk_size
78
+
79
+ max_size = self.block_size * self.header.max_sector_count
80
+ self._num_directory_entries = (self._size_per_table + max_size - 1) // self._size_per_table
81
+
82
+ self._aligned_table_size = (
83
+ (self.block_size + 8 * self._num_table_entries - 1) // self.block_size * self.block_size
84
+ )
85
+
86
+ self.directories = sorted(
87
+ (Directory(self, offset) for offset in self.header.directory_offsets),
88
+ key=lambda d: d.version,
89
+ reverse=True,
90
+ )
91
+ self.active_directory = self.directories[0]
92
+
93
+ self.metadata_header = None
94
+ self.metadata: dict[str, Any] = {}
95
+ if self.header.metadata_chunk:
96
+ # Open the file in reserved mode to read from the reserved area
97
+ with self.open(reserved=True) as disk:
98
+ metadata_offset = self.header.metadata_chunk * self.chunk_size
99
+ disk.seek(metadata_offset)
100
+ self.metadata_header = c_asif.asif_meta_header(disk)
101
+
102
+ if self.metadata_header.header_signature != c_asif.ASIF_META_HEADER_SIGNATURE:
103
+ raise InvalidSignature(
104
+ f"Invalid a ASIF metadata header (expected {c_asif.ASIF_META_HEADER_SIGNATURE:#x}, "
105
+ f"got {self.metadata_header.header_signature:#x})"
106
+ )
107
+
108
+ disk.seek(metadata_offset + self.metadata_header.header_size)
109
+ self.metadata = plistlib.loads(disk.read(self.metadata_header.data_size).strip(b"\x00"))
110
+
111
+ @property
112
+ def internal_metadata(self) -> dict[str, Any]:
113
+ """Get internal metadata from the ASIF image.
114
+
115
+ Returns:
116
+ A dictionary containing the internal metadata.
117
+ """
118
+ return self.metadata.get("internal metadata", {})
119
+
120
+ @property
121
+ def user_metadata(self) -> dict[str, Any]:
122
+ """Get user metadata from the ASIF image.
123
+
124
+ Returns:
125
+ A dictionary containing the user metadata.
126
+ """
127
+ return self.metadata.get("user metadata", {})
128
+
129
+ def open(self, reserved: bool = False) -> DataStream:
130
+ """Open a stream to read the ASIF image data.
131
+
132
+ Args:
133
+ reserved: Whether to allow reading into the reserved area of the ASIF image.
134
+
135
+ Returns:
136
+ A stream-like object that can be used to read the image data.
137
+ """
138
+ return DataStream(self, reserved)
139
+
140
+
141
+ class Directory:
142
+ """ASIF Directory.
143
+
144
+ A directory has a version (``uint64``) followed by a list of table entries (``uint64[]``).
145
+ The version number is used to determine the active directory, with the highest version being the active one.
146
+ Each table entry is a chunk number and points to a table in the ASIF image.
147
+
148
+ Args:
149
+ asif: The ASIF image this directory belongs to.
150
+ offset: Offset of the directory in the ASIF image.
151
+ """
152
+
153
+ def __init__(self, asif: ASIF, offset: int):
154
+ self.asif = asif
155
+ self.offset = offset
156
+
157
+ self.asif.fh.seek(offset)
158
+ self.version = c_asif.uint64(self.asif.fh)
159
+
160
+ self.table = lru_cache(128)(self.table)
161
+
162
+ def __repr__(self) -> str:
163
+ return f"<Directory offset={self.offset:#x} version={self.version}>"
164
+
165
+ @cached_property
166
+ def entries(self) -> list[int]:
167
+ """List of table entries in the directory."""
168
+ # Seek over the version
169
+ self.asif.fh.seek(self.offset + 8)
170
+ return c_asif.uint64[self.asif._num_directory_entries](self.asif.fh)
171
+
172
+ def table(self, index: int) -> Table:
173
+ """Get a table from the directory.
174
+
175
+ Args:
176
+ index: Index of the table in the directory.
177
+ """
178
+ if index >= self.asif._num_directory_entries:
179
+ raise IndexError("Table index out of range")
180
+ return Table(self, index)
181
+
182
+
183
+ class Table:
184
+ """ASIF Table.
185
+
186
+ A table contains a list of data entries (``uint64[]``). Each data entry is a chunk number and points to a chunk of
187
+ data in the ASIF image. Each table covers a fixed amount of data in the virtual disk.
188
+
189
+ Data entries have 55 bits usable for the chunk number and 9 bits reserved for flags.
190
+
191
+ .. rubric :: Encoding
192
+ .. code-block:: c
193
+
194
+ 0b00000000 01111111 11111111 11111111 11111111 11111111 11111111 11111111 (chunk number)
195
+ 0b00111111 10000000 00000000 00000000 00000000 00000000 00000000 00000000 (reserved)
196
+ 0b01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 (entry dirty)
197
+ 0b10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 (content dirty)
198
+
199
+ Args:
200
+ directory: The directory this table belongs to.
201
+ index: Index of the table in the directory.
202
+ """
203
+
204
+ def __init__(self, directory: Directory, index: int):
205
+ self.asif = directory.asif
206
+ self.directory = directory
207
+ self.index = index
208
+
209
+ self.offset = self.directory.entries[index] * self.asif.chunk_size
210
+ self.virtual_offset = index * self.asif._size_per_table
211
+
212
+ def __repr__(self) -> str:
213
+ return f"<Table index={self.index} offset={self.offset:#x} virtual_offset={self.virtual_offset:#x}>"
214
+
215
+ @cached_property
216
+ def entries(self) -> list[int]:
217
+ """List of data entries in the table."""
218
+ self.asif.fh.seek(self.offset)
219
+ return c_asif.uint64[self.asif._num_table_entries](self.asif.fh)
220
+
221
+
222
+ class DataStream(AlignedStream):
223
+ """Stream to read data from an ASIF image.
224
+
225
+ Args:
226
+ asif: The ASIF image to read from.
227
+ reserved: Whether to allow reading into the reserved area of the ASIF image.
228
+ """
229
+
230
+ def __init__(self, asif: ASIF, reserved: bool = False):
231
+ super().__init__(asif.max_size if reserved else asif.size, align=asif.chunk_size)
232
+ self.asif = asif
233
+ self.reserved = reserved
234
+ self.directory = asif.active_directory
235
+
236
+ def _read(self, offset: int, length: int) -> bytes:
237
+ result = []
238
+ while length:
239
+ table = self.directory.table(offset // self.asif._size_per_table)
240
+ relative_block_index = (offset // self.asif.block_size) - (table.virtual_offset // self.asif.block_size)
241
+ data_idx = (
242
+ relative_block_index // self.asif._blocks_per_chunk
243
+ + relative_block_index // self.asif._blocks_per_chunk * self.asif._num_reserved_table_entries
244
+ ) // self.asif._num_reserved_table_entries
245
+
246
+ # 0x8000000000000000 = content dirty bit
247
+ # 0x4000000000000000 = entry dirty bit
248
+ # 0x3F80000000000000 = reserved bits
249
+ chunk = table.entries[data_idx] & 0x7FFFFFFFFFFFFF
250
+ raw_offset = chunk * self.asif.chunk_size
251
+
252
+ read_length = min(length, self.asif.chunk_size)
253
+ if chunk == 0:
254
+ result.append(b"\x00" * read_length)
255
+ else:
256
+ self.asif.fh.seek(raw_offset)
257
+ result.append(self.asif.fh.read(read_length))
258
+
259
+ offset += read_length
260
+ length -= read_length
261
+
262
+ return b"".join(result)
@@ -0,0 +1,37 @@
1
+ from __future__ import annotations
2
+
3
+ from dissect.cstruct import cstruct
4
+
5
+ asif_def = """
6
+ #define ASIF_HEADER_SIGNATURE 0x73686477 // 'shdw'
7
+ #define ASIF_META_HEADER_SIGNATURE 0x6D657461 // 'meta'
8
+
9
+ struct asif_header {
10
+ uint32 header_signature;
11
+ uint32 header_version;
12
+ uint32 header_size;
13
+ uint32 header_flags;
14
+ uint64 directory_offsets[2];
15
+ char guid[16];
16
+ uint64 sector_count;
17
+ uint64 max_sector_count;
18
+ uint32 chunk_size;
19
+ uint16 block_size;
20
+ uint16 total_segments;
21
+ uint64 metadata_chunk;
22
+ char unk_50[16];
23
+ uint32 read_only_flags;
24
+ uint32 metadata_flags;
25
+ uint32 metadata_read_only_flags;
26
+ };
27
+
28
+ struct asif_meta_header {
29
+ uint32 header_signature;
30
+ uint32 header_version;
31
+ uint32 header_size;
32
+ uint64 data_size;
33
+ uint64 unk_14;
34
+ };
35
+ """
36
+
37
+ c_asif = cstruct(endian=">").load(asif_def)
@@ -0,0 +1,68 @@
1
+ # Generated by cstruct-stubgen
2
+ from typing import BinaryIO, Literal, TypeAlias, overload
3
+
4
+ import dissect.cstruct as __cs__
5
+
6
+ class _c_asif(__cs__.cstruct):
7
+ ASIF_HEADER_SIGNATURE: Literal[1936221303] = ...
8
+ ASIF_META_HEADER_SIGNATURE: Literal[1835365473] = ...
9
+ class asif_header(__cs__.Structure):
10
+ header_signature: _c_asif.uint32
11
+ header_version: _c_asif.uint32
12
+ header_size: _c_asif.uint32
13
+ header_flags: _c_asif.uint32
14
+ directory_offsets: __cs__.Array[_c_asif.uint64]
15
+ guid: __cs__.CharArray
16
+ sector_count: _c_asif.uint64
17
+ max_sector_count: _c_asif.uint64
18
+ chunk_size: _c_asif.uint32
19
+ block_size: _c_asif.uint16
20
+ total_segments: _c_asif.uint16
21
+ metadata_chunk: _c_asif.uint64
22
+ unk_50: __cs__.CharArray
23
+ read_only_flags: _c_asif.uint32
24
+ metadata_flags: _c_asif.uint32
25
+ metadata_read_only_flags: _c_asif.uint32
26
+ @overload
27
+ def __init__(
28
+ self,
29
+ header_signature: _c_asif.uint32 | None = ...,
30
+ header_version: _c_asif.uint32 | None = ...,
31
+ header_size: _c_asif.uint32 | None = ...,
32
+ header_flags: _c_asif.uint32 | None = ...,
33
+ directory_offsets: __cs__.Array[_c_asif.uint64] | None = ...,
34
+ guid: __cs__.CharArray | None = ...,
35
+ sector_count: _c_asif.uint64 | None = ...,
36
+ max_sector_count: _c_asif.uint64 | None = ...,
37
+ chunk_size: _c_asif.uint32 | None = ...,
38
+ block_size: _c_asif.uint16 | None = ...,
39
+ total_segments: _c_asif.uint16 | None = ...,
40
+ metadata_chunk: _c_asif.uint64 | None = ...,
41
+ unk_50: __cs__.CharArray | None = ...,
42
+ read_only_flags: _c_asif.uint32 | None = ...,
43
+ metadata_flags: _c_asif.uint32 | None = ...,
44
+ metadata_read_only_flags: _c_asif.uint32 | None = ...,
45
+ ): ...
46
+ @overload
47
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
48
+
49
+ class asif_meta_header(__cs__.Structure):
50
+ header_signature: _c_asif.uint32
51
+ header_version: _c_asif.uint32
52
+ header_size: _c_asif.uint32
53
+ data_size: _c_asif.uint64
54
+ unk_14: _c_asif.uint64
55
+ @overload
56
+ def __init__(
57
+ self,
58
+ header_signature: _c_asif.uint32 | None = ...,
59
+ header_version: _c_asif.uint32 | None = ...,
60
+ header_size: _c_asif.uint32 | None = ...,
61
+ data_size: _c_asif.uint64 | None = ...,
62
+ unk_14: _c_asif.uint64 | None = ...,
63
+ ): ...
64
+ @overload
65
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
66
+
67
+ # Technically `c_asif` is an instance of `_c_asif`, but then we can't use it in type hints
68
+ c_asif: TypeAlias = _c_asif
@@ -171,9 +171,6 @@ UNALLOCATED_SUBCLUSTER_TYPES = (
171
171
  )
172
172
 
173
173
 
174
- def ctz(value: int, size: int = 32) -> int:
174
+ def ctz(value: int) -> int:
175
175
  """Count the number of zero bits in an integer of a given size."""
176
- for i in range(size):
177
- if value & (1 << i):
178
- return i
179
- return 0
176
+ return (value & -value).bit_length() - 1 if value else 0
@@ -0,0 +1,189 @@
1
+ # Generated by cstruct-stubgen
2
+ from typing import BinaryIO, Literal, TypeAlias, overload
3
+
4
+ import dissect.cstruct as __cs__
5
+
6
+ class _c_qcow2(__cs__.cstruct):
7
+ MIN_CLUSTER_BITS: Literal[9] = ...
8
+ MAX_CLUSTER_BITS: Literal[21] = ...
9
+ QCOW2_COMPRESSED_SECTOR_SIZE: Literal[512] = ...
10
+ QCOW2_COMPRESSION_TYPE_ZLIB: Literal[0] = ...
11
+ QCOW2_COMPRESSION_TYPE_ZSTD: Literal[1] = ...
12
+ L1E_SIZE: Literal[8] = ...
13
+ L2E_SIZE_NORMAL: Literal[8] = ...
14
+ L2E_SIZE_EXTENDED: Literal[16] = ...
15
+ L1E_OFFSET_MASK: Literal[72057594037927424] = ...
16
+ L2E_OFFSET_MASK: Literal[72057594037927424] = ...
17
+ L2E_COMPRESSED_OFFSET_SIZE_MASK: Literal[4611686018427387903] = ...
18
+ QCOW_OFLAG_COPIED: Literal[9223372036854775808] = ...
19
+ QCOW_OFLAG_COMPRESSED: Literal[4611686018427387904] = ...
20
+ QCOW_OFLAG_ZERO: Literal[1] = ...
21
+ QCOW_EXTL2_SUBCLUSTERS_PER_CLUSTER: Literal[32] = ...
22
+ QCOW2_INCOMPAT_DIRTY_BITNR: Literal[0] = ...
23
+ QCOW2_INCOMPAT_CORRUPT_BITNR: Literal[1] = ...
24
+ QCOW2_INCOMPAT_DATA_FILE_BITNR: Literal[2] = ...
25
+ QCOW2_INCOMPAT_COMPRESSION_BITNR: Literal[3] = ...
26
+ QCOW2_INCOMPAT_EXTL2_BITNR: Literal[4] = ...
27
+ QCOW2_INCOMPAT_DIRTY: Literal[1] = ...
28
+ QCOW2_INCOMPAT_CORRUPT: Literal[2] = ...
29
+ QCOW2_INCOMPAT_DATA_FILE: Literal[4] = ...
30
+ QCOW2_INCOMPAT_COMPRESSION: Literal[8] = ...
31
+ QCOW2_INCOMPAT_EXTL2: Literal[16] = ...
32
+ QCOW2_EXT_MAGIC_END: Literal[0] = ...
33
+ QCOW2_EXT_MAGIC_BACKING_FORMAT: Literal[3799591626] = ...
34
+ QCOW2_EXT_MAGIC_FEATURE_TABLE: Literal[1745090647] = ...
35
+ QCOW2_EXT_MAGIC_CRYPTO_HEADER: Literal[87539319] = ...
36
+ QCOW2_EXT_MAGIC_BITMAPS: Literal[595929205] = ...
37
+ QCOW2_EXT_MAGIC_DATA_FILE: Literal[1145132097] = ...
38
+ class QCowHeader(__cs__.Structure):
39
+ magic: _c_qcow2.uint32
40
+ version: _c_qcow2.uint32
41
+ backing_file_offset: _c_qcow2.uint64
42
+ backing_file_size: _c_qcow2.uint32
43
+ cluster_bits: _c_qcow2.uint32
44
+ size: _c_qcow2.uint64
45
+ crypt_method: _c_qcow2.uint32
46
+ l1_size: _c_qcow2.uint32
47
+ l1_table_offset: _c_qcow2.uint64
48
+ refcount_table_offset: _c_qcow2.uint64
49
+ refcount_table_clusters: _c_qcow2.uint32
50
+ nb_snapshots: _c_qcow2.uint32
51
+ snapshots_offset: _c_qcow2.uint64
52
+ incompatible_features: _c_qcow2.uint64
53
+ compatible_features: _c_qcow2.uint64
54
+ autoclear_features: _c_qcow2.uint64
55
+ refcount_order: _c_qcow2.uint32
56
+ header_length: _c_qcow2.uint32
57
+ compression_type: _c_qcow2.uint8
58
+ padding: __cs__.Array[_c_qcow2.uint8]
59
+ @overload
60
+ def __init__(
61
+ self,
62
+ magic: _c_qcow2.uint32 | None = ...,
63
+ version: _c_qcow2.uint32 | None = ...,
64
+ backing_file_offset: _c_qcow2.uint64 | None = ...,
65
+ backing_file_size: _c_qcow2.uint32 | None = ...,
66
+ cluster_bits: _c_qcow2.uint32 | None = ...,
67
+ size: _c_qcow2.uint64 | None = ...,
68
+ crypt_method: _c_qcow2.uint32 | None = ...,
69
+ l1_size: _c_qcow2.uint32 | None = ...,
70
+ l1_table_offset: _c_qcow2.uint64 | None = ...,
71
+ refcount_table_offset: _c_qcow2.uint64 | None = ...,
72
+ refcount_table_clusters: _c_qcow2.uint32 | None = ...,
73
+ nb_snapshots: _c_qcow2.uint32 | None = ...,
74
+ snapshots_offset: _c_qcow2.uint64 | None = ...,
75
+ incompatible_features: _c_qcow2.uint64 | None = ...,
76
+ compatible_features: _c_qcow2.uint64 | None = ...,
77
+ autoclear_features: _c_qcow2.uint64 | None = ...,
78
+ refcount_order: _c_qcow2.uint32 | None = ...,
79
+ header_length: _c_qcow2.uint32 | None = ...,
80
+ compression_type: _c_qcow2.uint8 | None = ...,
81
+ padding: __cs__.Array[_c_qcow2.uint8] | None = ...,
82
+ ): ...
83
+ @overload
84
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
85
+
86
+ class QCowExtension(__cs__.Structure):
87
+ magic: _c_qcow2.uint32
88
+ len: _c_qcow2.uint32
89
+ @overload
90
+ def __init__(self, magic: _c_qcow2.uint32 | None = ..., len: _c_qcow2.uint32 | None = ...): ...
91
+ @overload
92
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
93
+
94
+ class Qcow2CryptoHeaderExtension(__cs__.Structure):
95
+ offset: _c_qcow2.uint64
96
+ length: _c_qcow2.uint64
97
+ @overload
98
+ def __init__(self, offset: _c_qcow2.uint64 | None = ..., length: _c_qcow2.uint64 | None = ...): ...
99
+ @overload
100
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
101
+
102
+ class Qcow2BitmapHeaderExt(__cs__.Structure):
103
+ nb_bitmaps: _c_qcow2.uint32
104
+ reserved32: _c_qcow2.uint32
105
+ bitmap_directory_size: _c_qcow2.uint64
106
+ bitmap_directory_offset: _c_qcow2.uint64
107
+ @overload
108
+ def __init__(
109
+ self,
110
+ nb_bitmaps: _c_qcow2.uint32 | None = ...,
111
+ reserved32: _c_qcow2.uint32 | None = ...,
112
+ bitmap_directory_size: _c_qcow2.uint64 | None = ...,
113
+ bitmap_directory_offset: _c_qcow2.uint64 | None = ...,
114
+ ): ...
115
+ @overload
116
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
117
+
118
+ class QCowSnapshotHeader(__cs__.Structure):
119
+ l1_table_offset: _c_qcow2.uint64
120
+ l1_size: _c_qcow2.uint32
121
+ id_str_size: _c_qcow2.uint16
122
+ name_size: _c_qcow2.uint16
123
+ date_sec: _c_qcow2.uint32
124
+ date_nsec: _c_qcow2.uint32
125
+ vm_clock_nsec: _c_qcow2.uint64
126
+ vm_state_size: _c_qcow2.uint32
127
+ extra_data_size: _c_qcow2.uint32
128
+ @overload
129
+ def __init__(
130
+ self,
131
+ l1_table_offset: _c_qcow2.uint64 | None = ...,
132
+ l1_size: _c_qcow2.uint32 | None = ...,
133
+ id_str_size: _c_qcow2.uint16 | None = ...,
134
+ name_size: _c_qcow2.uint16 | None = ...,
135
+ date_sec: _c_qcow2.uint32 | None = ...,
136
+ date_nsec: _c_qcow2.uint32 | None = ...,
137
+ vm_clock_nsec: _c_qcow2.uint64 | None = ...,
138
+ vm_state_size: _c_qcow2.uint32 | None = ...,
139
+ extra_data_size: _c_qcow2.uint32 | None = ...,
140
+ ): ...
141
+ @overload
142
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
143
+
144
+ class QCowSnapshotExtraData(__cs__.Structure):
145
+ vm_state_size_large: _c_qcow2.uint64
146
+ disk_size: _c_qcow2.uint64
147
+ icount: _c_qcow2.uint64
148
+ @overload
149
+ def __init__(
150
+ self,
151
+ vm_state_size_large: _c_qcow2.uint64 | None = ...,
152
+ disk_size: _c_qcow2.uint64 | None = ...,
153
+ icount: _c_qcow2.uint64 | None = ...,
154
+ ): ...
155
+ @overload
156
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
157
+
158
+ class QCow2ClusterType(__cs__.Enum):
159
+ QCOW2_CLUSTER_UNALLOCATED = ...
160
+ QCOW2_CLUSTER_ZERO_PLAIN = ...
161
+ QCOW2_CLUSTER_ZERO_ALLOC = ...
162
+ QCOW2_CLUSTER_NORMAL = ...
163
+ QCOW2_CLUSTER_COMPRESSED = ...
164
+
165
+ class QCow2SubclusterType(__cs__.Enum):
166
+ QCOW2_SUBCLUSTER_UNALLOCATED_PLAIN = ...
167
+ QCOW2_SUBCLUSTER_UNALLOCATED_ALLOC = ...
168
+ QCOW2_SUBCLUSTER_ZERO_PLAIN = ...
169
+ QCOW2_SUBCLUSTER_ZERO_ALLOC = ...
170
+ QCOW2_SUBCLUSTER_NORMAL = ...
171
+ QCOW2_SUBCLUSTER_COMPRESSED = ...
172
+ QCOW2_SUBCLUSTER_INVALID = ...
173
+
174
+ # Technically `c_qcow2` is an instance of `_c_qcow2`, but then we can't use it in type hints
175
+ c_qcow2: TypeAlias = _c_qcow2
176
+
177
+ QCOW2_MAGIC: int
178
+ QCOW2_MAGIC_BYTES: bytes
179
+
180
+ QCOW2_INCOMPAT_MASK: int
181
+
182
+ QCow2ClusterType: TypeAlias = _c_qcow2.QCow2ClusterType
183
+ QCow2SubclusterType: TypeAlias = _c_qcow2.QCow2SubclusterType
184
+
185
+ NORMAL_SUBCLUSTER_TYPES: tuple[QCow2SubclusterType, ...]
186
+ ZERO_SUBCLUSTER_TYPES: tuple[QCow2SubclusterType, ...]
187
+ UNALLOCATED_SUBCLUSTER_TYPES: tuple[QCow2SubclusterType, ...]
188
+
189
+ def ctz(value: int) -> int: ...