dissect.database 1.2.dev10__py3-none-any.whl → 1.2.dev11__py3-none-any.whl

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.
@@ -0,0 +1,8 @@
1
+ from __future__ import annotations
2
+
3
+ from dissect.database.bbolt.bbolt import Bbolt
4
+
5
+ BoltDB = Bbolt
6
+ """Alias Bbolt to BoltDB."""
7
+
8
+ __all__ = ["Bbolt", "BoltDB"]
@@ -0,0 +1,214 @@
1
+ from __future__ import annotations
2
+
3
+ from pathlib import Path
4
+ from typing import TYPE_CHECKING, BinaryIO, Literal
5
+
6
+ from dissect.database.bbolt.c_bbolt import BucketLeafFlag, PageFlag, c_bbolt
7
+
8
+ if TYPE_CHECKING:
9
+ from collections.abc import Iterable
10
+
11
+
12
+ class Bbolt:
13
+ """bbolt database implementation.
14
+
15
+ References:
16
+ - https://github.com/etcd-io/bbolt
17
+ - https://www.qtmuniao.com/en/2020/11/29/bolt-data-organised/
18
+ - https://www.qtmuniao.com/en/2020/12/14/bolt-index-design/
19
+ """
20
+
21
+ def __init__(self, fh: BinaryIO | Path) -> None:
22
+ if isinstance(fh, Path):
23
+ self.path = fh
24
+ self.fh = fh.open("rb")
25
+ elif hasattr(fh, "read"):
26
+ self.path = None
27
+ self.fh = fh
28
+ else:
29
+ raise TypeError("Argument fh must be Path or BytesIO-like object")
30
+
31
+ self.meta = c_bbolt.Meta(self.fh)
32
+ self.version = self.meta.version
33
+ self.page_size = self.meta.pageSize
34
+
35
+ if self.meta.magic != c_bbolt.Magic:
36
+ raise ValueError(f"Unexpected bbolt meta magic value {self.meta.magic:x}")
37
+
38
+ self.root = Page(self, self.meta.root.root)
39
+
40
+ def __repr__(self) -> str:
41
+ return f"<Bbolt version={self.version} page_size={self.page_size} path={self.path or self.fh}>"
42
+
43
+ def _iter(self, type: Literal["page", "value"], path: str | None, sep: str) -> Page | bytes | None:
44
+ """Iterate over :class:`Page`` and :class:`Inode` until the requested page or key value is found."""
45
+ # Return the root page if no path was given
46
+ if not path or path == sep:
47
+ return self.root
48
+
49
+ page = self.root
50
+ parts = path.split(sep)
51
+ for i, part in enumerate(parts):
52
+ # Search for the current part in each inode of the current page
53
+ for inode in page.inodes():
54
+ if inode.key == part:
55
+ if inode.flags == BucketLeafFlag:
56
+ # Read the page inside the inode
57
+ if inode.value.startswith(16 * b"\x00"):
58
+ page = Page(self, 0, inode._value_offset + 0x10)
59
+
60
+ # Read the page this inode points to
61
+ else:
62
+ pgid = c_bbolt.InBucket(inode.value).root if not inode.pgid else inode.pgid
63
+ page = Page(self, pgid)
64
+
65
+ # If the new page has an inode 0 with BucketLeafFlag, read that bucket instead
66
+ if (
67
+ page.count
68
+ and (inode_nested := page.inode(0)).flags == BucketLeafFlag
69
+ and inode_nested.key == part
70
+ ):
71
+ page = Page(self, 0, inode_nested._value_offset + 0x10)
72
+
73
+ # Spec dictates we should ignore pages with no inodes
74
+ if not page.count:
75
+ continue
76
+
77
+ if i + 1 == len(parts):
78
+ if type == "page":
79
+ return page
80
+ if type == "value" and inode.value:
81
+ return inode.value
82
+
83
+ # We can stop iterating over more inodes of this page, continue to the next path part
84
+ break
85
+ else:
86
+ # Return early if we did not find the current part in any of the inodes in this page
87
+ return None
88
+
89
+ return None
90
+
91
+ def keys(self, path: str | None = None, *, sep: str = " ") -> list[str] | None:
92
+ """Get a list of key names from the given path."""
93
+ if not (page := self._iter("page", path, sep)):
94
+ return None
95
+ return [child.key for child in page.inodes()]
96
+
97
+ def get(self, path: str | None, *, sep: str = " ", decode: bool = True) -> str | bytes | None:
98
+ """Get a key value from the given path."""
99
+ if not (value := self._iter("value", path, sep)):
100
+ return None
101
+ return value.decode() if decode else value
102
+
103
+
104
+ class Page:
105
+ """Represents a bbolt page (meta, freelist, branch or leaf)."""
106
+
107
+ def __init__(self, db: Bbolt, pgid: int, offset: int | None = None) -> None:
108
+ self.db = db
109
+ self.pgid = pgid
110
+
111
+ if not offset:
112
+ self.offset = db.fh.seek(pgid * db.page_size)
113
+ else:
114
+ self.offset = db.fh.seek(offset)
115
+
116
+ self.page = c_bbolt.Page(db.fh)
117
+
118
+ self.flags = self.page.flags
119
+ self.count = self.page.count
120
+ self.overflow = self.page.overflow
121
+
122
+ if self.flags == PageFlag.Leaf and self.count:
123
+ self.key = self.inode(0).key
124
+ else:
125
+ self.key = None
126
+
127
+ def __repr__(self) -> str:
128
+ return f"<Page offset=0x{self.offset:x} pgid={self.pgid} flags={self.flags} count={self.count} overflow={self.overflow} key={self.key}>" # noqa: E501
129
+
130
+ def inode(self, i: int) -> Inode:
131
+ """Return the :class:`Inode` corresponding to the given index number."""
132
+ if i >= self.count or i < 0:
133
+ raise ValueError(f"invalid inode number: got {i}, should be 0-{self.count - 1}")
134
+
135
+ offset = self.offset + self.page.size + (i * 16)
136
+
137
+ if self.flags == PageFlag.Leaf:
138
+ return InodeLeaf(self, c_bbolt.leafPageElement, offset)
139
+
140
+ if self.flags == PageFlag.Branch:
141
+ return InodeBranch(self, c_bbolt.branchPageElement, offset)
142
+
143
+ raise NotImplementedError(self.pgid, self.flags)
144
+
145
+ def inodes(self) -> Iterable[InodeLeaf]:
146
+ """Yield all :class:`Inode` present in this :class:`Page`.
147
+
148
+ References:
149
+ - internal/common/inode.go @ ReadInodeFromPage
150
+ """
151
+ offset = self.offset + self.page.size
152
+ for i in range(self.count):
153
+ element_offset = offset + (i * 16)
154
+
155
+ if self.flags == PageFlag.Leaf:
156
+ yield InodeLeaf(self, c_bbolt.leafPageElement, element_offset)
157
+ elif self.flags == PageFlag.Branch:
158
+ # For easier enumeration we iterate all inodes from the page this branch inode points to,
159
+ # and do not yield the branch inode.
160
+ branch = InodeBranch(self, c_bbolt.branchPageElement, element_offset)
161
+ page = Page(self.db, branch.pgid)
162
+ yield from page.inodes()
163
+ else:
164
+ raise NotImplementedError(self.flags)
165
+
166
+
167
+ class Inode:
168
+ """Represents a bbolt Inode branch or leaf element.
169
+
170
+ Only for internal use, see :class:`InodeLeaf` and :class:`InodeBranch`.
171
+ """
172
+
173
+ def __init__(self, page: Page, structure: c_bbolt.leafPageElement | c_bbolt.branchPageElement, offset: int) -> None:
174
+ self.page = page
175
+ self.offset = offset
176
+
177
+ page.db.fh.seek(offset)
178
+ element = structure(page.db.fh)
179
+ self.element = element
180
+
181
+ self.flags = None
182
+ self.value = None
183
+ self.pgid = None
184
+
185
+ page.db.fh.seek(offset)
186
+ page.db.fh.read(element.pos)
187
+
188
+ self.key = page.db.fh.read(element.ksize).decode()
189
+
190
+
191
+ class InodeLeaf(Inode):
192
+ """Represents a bbolt Inode leaf element."""
193
+
194
+ def __init__(self, page: Page, structure: c_bbolt.leafPageElement, offset: int) -> None:
195
+ super().__init__(page, structure, offset)
196
+
197
+ self.flags = self.element.flags
198
+ self._value_offset = page.db.fh.tell()
199
+ self.value = page.db.fh.read(self.element.vsize)
200
+
201
+ def __repr__(self) -> str:
202
+ return f"<Inode type=leaf flags={self.flags} key={self.key} value={self.value}>"
203
+
204
+
205
+ class InodeBranch(Inode):
206
+ """Represents a bbolt Inode branch element."""
207
+
208
+ def __init__(self, page: Page, structure: c_bbolt.branchPageElement, offset: int) -> None:
209
+ super().__init__(page, structure, offset)
210
+
211
+ self.pgid = self.element.pgid
212
+
213
+ def __repr__(self) -> str:
214
+ return f"<Inode type=branch pgid={self.pgid} (first)key={self.key}>"
@@ -0,0 +1,71 @@
1
+ from __future__ import annotations
2
+
3
+ from dissect.cstruct import cstruct
4
+
5
+ # References:
6
+ # - https://github.com/etcd-io/bbolt/blob/main/internal/common/types.go
7
+ # - https://github.com/etcd-io/bbolt/blob/main/internal/common/meta.go
8
+ # - https://github.com/etcd-io/bbolt/blob/main/internal/common/bucket.go
9
+ # - https://github.com/etcd-io/bbolt/blob/main/internal/common/page.go
10
+ bbolt_def = """
11
+ typedef uint64 Pgid; // page identifier
12
+ typedef uint64 Txid; // transaction identifier
13
+
14
+ #define Magic 0xED0CDAED
15
+ #define BucketLeafFlag 0x01
16
+
17
+ enum PageFlag: uint16 {
18
+ Branch = 0x01,
19
+ Leaf = 0x02,
20
+ Meta = 0x04,
21
+ Freelist = 0x10,
22
+ };
23
+
24
+ struct InBucket {
25
+ Pgid root;
26
+ uint64 sequence;
27
+ };
28
+
29
+ struct Page {
30
+ Pgid id;
31
+ PageFlag flags; // type of the page: branch, leaf, meta or freelist
32
+ uint16 count; // number of key/value pairs in this page
33
+ uint32 overflow; // number of overflow pages
34
+ };
35
+
36
+ struct Meta {
37
+ Page page;
38
+ uint32 magic;
39
+ uint32 version;
40
+ uint32 pageSize;
41
+ uint32 flags;
42
+ InBucket root;
43
+ Pgid freelist;
44
+ Pgid pgid;
45
+ Txid txid;
46
+ uint64 checksum;
47
+ };
48
+
49
+ struct branchPageElement {
50
+ uint32 pos; // relative
51
+ uint32 ksize;
52
+ Pgid pgid;
53
+ };
54
+
55
+ struct leafPageElement {
56
+ uint32 flags; // 0x01 = BucketLeafFlag
57
+ uint32 pos; // relative
58
+ uint32 ksize;
59
+ uint32 vsize;
60
+ };
61
+
62
+ struct Inode {
63
+ uint32 flags;
64
+ Pgid pgid;
65
+ };
66
+ """
67
+
68
+ c_bbolt = cstruct().load(bbolt_def)
69
+
70
+ PageFlag = c_bbolt.PageFlag
71
+ BucketLeafFlag = c_bbolt.BucketLeafFlag
@@ -0,0 +1,111 @@
1
+ # Generated by cstruct-stubgen
2
+ from typing import BinaryIO, Literal, TypeAlias, overload
3
+
4
+ import dissect.cstruct as __cs__
5
+
6
+ class _c_bbolt(__cs__.cstruct):
7
+ Magic: Literal[3977042669] = ...
8
+ BucketLeafFlag: Literal[1] = ...
9
+ Pgid: TypeAlias = _c_bbolt.uint64
10
+ Txid: TypeAlias = _c_bbolt.uint64
11
+ class PageFlag(__cs__.Enum):
12
+ Branch = ...
13
+ Leaf = ...
14
+ Meta = ...
15
+ Freelist = ...
16
+
17
+ class InBucket(__cs__.Structure):
18
+ root: _c_bbolt.uint64
19
+ sequence: _c_bbolt.uint64
20
+ @overload
21
+ def __init__(self, root: _c_bbolt.uint64 | None = ..., sequence: _c_bbolt.uint64 | None = ...): ...
22
+ @overload
23
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
24
+
25
+ class Page(__cs__.Structure):
26
+ id: _c_bbolt.uint64
27
+ flags: _c_bbolt.PageFlag
28
+ count: _c_bbolt.uint16
29
+ overflow: _c_bbolt.uint32
30
+ @overload
31
+ def __init__(
32
+ self,
33
+ id: _c_bbolt.uint64 | None = ...,
34
+ flags: _c_bbolt.PageFlag | None = ...,
35
+ count: _c_bbolt.uint16 | None = ...,
36
+ overflow: _c_bbolt.uint32 | None = ...,
37
+ ): ...
38
+ @overload
39
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
40
+
41
+ class Meta(__cs__.Structure):
42
+ page: _c_bbolt.Page
43
+ magic: _c_bbolt.uint32
44
+ version: _c_bbolt.uint32
45
+ pageSize: _c_bbolt.uint32
46
+ flags: _c_bbolt.uint32
47
+ root: _c_bbolt.InBucket
48
+ freelist: _c_bbolt.uint64
49
+ pgid: _c_bbolt.uint64
50
+ txid: _c_bbolt.uint64
51
+ checksum: _c_bbolt.uint64
52
+ @overload
53
+ def __init__(
54
+ self,
55
+ page: _c_bbolt.Page | None = ...,
56
+ magic: _c_bbolt.uint32 | None = ...,
57
+ version: _c_bbolt.uint32 | None = ...,
58
+ pageSize: _c_bbolt.uint32 | None = ...,
59
+ flags: _c_bbolt.uint32 | None = ...,
60
+ root: _c_bbolt.InBucket | None = ...,
61
+ freelist: _c_bbolt.uint64 | None = ...,
62
+ pgid: _c_bbolt.uint64 | None = ...,
63
+ txid: _c_bbolt.uint64 | None = ...,
64
+ checksum: _c_bbolt.uint64 | None = ...,
65
+ ): ...
66
+ @overload
67
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
68
+
69
+ class branchPageElement(__cs__.Structure):
70
+ pos: _c_bbolt.uint32
71
+ ksize: _c_bbolt.uint32
72
+ pgid: _c_bbolt.uint64
73
+ @overload
74
+ def __init__(
75
+ self,
76
+ pos: _c_bbolt.uint32 | None = ...,
77
+ ksize: _c_bbolt.uint32 | None = ...,
78
+ pgid: _c_bbolt.uint64 | None = ...,
79
+ ): ...
80
+ @overload
81
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
82
+
83
+ class leafPageElement(__cs__.Structure):
84
+ flags: _c_bbolt.uint32
85
+ pos: _c_bbolt.uint32
86
+ ksize: _c_bbolt.uint32
87
+ vsize: _c_bbolt.uint32
88
+ @overload
89
+ def __init__(
90
+ self,
91
+ flags: _c_bbolt.uint32 | None = ...,
92
+ pos: _c_bbolt.uint32 | None = ...,
93
+ ksize: _c_bbolt.uint32 | None = ...,
94
+ vsize: _c_bbolt.uint32 | None = ...,
95
+ ): ...
96
+ @overload
97
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
98
+
99
+ class Inode(__cs__.Structure):
100
+ flags: _c_bbolt.uint32
101
+ pgid: _c_bbolt.uint64
102
+ @overload
103
+ def __init__(self, flags: _c_bbolt.uint32 | None = ..., pgid: _c_bbolt.uint64 | None = ...): ...
104
+ @overload
105
+ def __init__(self, fh: bytes | memoryview | bytearray | BinaryIO, /): ...
106
+
107
+ # Technically `c_bbolt` is an instance of `_c_bbolt`, but then we can't use it in type hints
108
+ c_bbolt: TypeAlias = _c_bbolt
109
+
110
+ PageFlag: TypeAlias = c_bbolt.PageFlag
111
+ BucketLeafFlag: Literal[1]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: dissect.database
3
- Version: 1.2.dev10
3
+ Version: 1.2.dev11
4
4
  Summary: A Dissect module implementing parsers for various database formats, including Berkeley DB, Microsofts Extensible Storage Engine (ESE) and SQLite3
5
5
  Author-email: Dissect Team <dissect@fox-it.com>
6
6
  License-Expression: Apache-2.0
@@ -1,5 +1,9 @@
1
1
  dissect/database/__init__.py,sha256=Az1CdhzhUPPtDZq1CUNdGX3ivjpPvZAMqh2zowa2uoE,278
2
2
  dissect/database/exception.py,sha256=WvEuRLfk69N4jAMJADSKtexy7xJ_or487kOt6ZDjF94,70
3
+ dissect/database/bbolt/__init__.py,sha256=EBOb3jYh3d0VaIy75q4jsSOTsiRiL68DbZaG4VXrqjE,159
4
+ dissect/database/bbolt/bbolt.py,sha256=d-HfP4Q9k5chFk3dadlM6iNZ4oiFX33HcPfGbVbguUY,7867
5
+ dissect/database/bbolt/c_bbolt.py,sha256=lSEgaxJ_YiwRB8ybU9fT9Cn0IxcApbVlM2gxVQ1DKpQ,1804
6
+ dissect/database/bbolt/c_bbolt.pyi,sha256=ucYdECYBJQPk2GTfFZDS0mp6yvhAMkUTRua8lRwAov4,3805
3
7
  dissect/database/bsd/__init__.py,sha256=6koD6y5cWvde8r4AOtc0VeGsj49slcxUyL8BXXPdYgk,93
4
8
  dissect/database/bsd/c_db.py,sha256=YM3nwWO6U_s2Typr9OXbPCjquLCKJxeroJdG9FKVNVw,29314
5
9
  dissect/database/bsd/c_db.pyi,sha256=VopMrd4a41T7A4n0M_ZYgPNIao3kXy0gW-fpnx5volk,20560
@@ -161,10 +165,10 @@ dissect/database/sqlite3/encryption/__init__.py,sha256=kJdFWXD9Z_O_QipC-_A9dlVfR
161
165
  dissect/database/sqlite3/encryption/sqlcipher/__init__.py,sha256=kJdFWXD9Z_O_QipC-_A9dlVfR6AOPSOoT8WBhpFbSsE,238
162
166
  dissect/database/sqlite3/encryption/sqlcipher/exception.py,sha256=GKNtzcnAKlWkvjLluruA8LfzCwjRRWubibbH8WM9l2o,121
163
167
  dissect/database/sqlite3/encryption/sqlcipher/sqlcipher.py,sha256=y_oJRKZqoJBeOQaBbniesZKm1sVTFvXZ466rJYZj2xE,11217
164
- dissect_database-1.2.dev10.dist-info/licenses/COPYRIGHT,sha256=pFH-OBYz6Xj23UB0Odz5IhoTR8nsTbJQNlCRV_wMaiE,317
165
- dissect_database-1.2.dev10.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
166
- dissect_database-1.2.dev10.dist-info/METADATA,sha256=V508YEp3xBaiq_9Y8tn_58GMSRwLaAHZzzm1mHp4vUM,5541
167
- dissect_database-1.2.dev10.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
168
- dissect_database-1.2.dev10.dist-info/entry_points.txt,sha256=ZVVKj3Nzjkgm1kBXGWyGNVUJzTbmVgivv9lgFcuLkpk,343
169
- dissect_database-1.2.dev10.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
170
- dissect_database-1.2.dev10.dist-info/RECORD,,
168
+ dissect_database-1.2.dev11.dist-info/licenses/COPYRIGHT,sha256=pFH-OBYz6Xj23UB0Odz5IhoTR8nsTbJQNlCRV_wMaiE,317
169
+ dissect_database-1.2.dev11.dist-info/licenses/LICENSE,sha256=PhUqiw6jAh2KbBdVRPBq_hfAvfcTBin7nZ3CK7NQbTM,11341
170
+ dissect_database-1.2.dev11.dist-info/METADATA,sha256=u1M5sVlCm-pv9ILfk9_gpQolkOCw3g8xsxrYpyE8Bqo,5541
171
+ dissect_database-1.2.dev11.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
172
+ dissect_database-1.2.dev11.dist-info/entry_points.txt,sha256=ZVVKj3Nzjkgm1kBXGWyGNVUJzTbmVgivv9lgFcuLkpk,343
173
+ dissect_database-1.2.dev11.dist-info/top_level.txt,sha256=Mn-CQzEYsAbkxrUI0TnplHuXnGVKzxpDw_po_sXpvv4,8
174
+ dissect_database-1.2.dev11.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (82.0.1)
2
+ Generator: setuptools (84.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5