dementor 1.0.0.dev16__tar.gz → 1.0.0.dev17__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.
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/PKG-INFO +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/__init__.py +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/loader.py +0 -3
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/mdns.py +12 -7
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/mysql.py +49 -44
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/smb.py +128 -11
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/PKG-INFO +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/pyproject.toml +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/LICENSE +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/README.md +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/__main__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/Dementor.toml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/banner.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/www/error_page.html +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/www/upnp-default/dd.xml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/www/upnp-default/present.html +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/www/upnp-default/scpd.xml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/assets/www/wpad.dat +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/config/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/config/attr.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/config/session.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/config/toml.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/config/util.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/db/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/db/connector.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/db/model.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/filters.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/log/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/log/hexdump.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/log/logger.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/log/stream.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/paths.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/ftp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/http.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/imap.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/ipp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/kerberos.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/ldap.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/llmnr.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/msrpc/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/msrpc/dcom.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/msrpc/epm.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/msrpc/rpc.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/msrpc.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/mssql.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/netbios.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/ntlm.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/pop3.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/quic.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/smtp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/spnego.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/ssdp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/upnp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/protocols/x11.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/servers.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor/standalone.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/SOURCES.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/dependency_links.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/entry_points.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/requires.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/dementor.egg-info/top_level.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/docs/README.pypi.md +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev17}/setup.cfg +0 -0
|
@@ -17,9 +17,6 @@
|
|
|
17
17
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
19
|
# SOFTWARE.
|
|
20
|
-
from typing import Any
|
|
21
|
-
|
|
22
|
-
|
|
23
20
|
import threading
|
|
24
21
|
import types
|
|
25
22
|
import os
|
|
@@ -39,12 +39,14 @@ from dementor.config.session import SessionConfig
|
|
|
39
39
|
if typing.TYPE_CHECKING:
|
|
40
40
|
from dementor.filters import Filters
|
|
41
41
|
|
|
42
|
+
# --- Constants ---------------------------------------------------------------
|
|
42
43
|
MDNS_IPV4_ADDR = "224.0.0.251"
|
|
43
44
|
MDNS_IPV6_ADDR = "ff02::fb"
|
|
44
45
|
|
|
45
46
|
QTYPES = {name: value for value, name in dns.dnsqtypes.items()}
|
|
46
47
|
|
|
47
48
|
|
|
49
|
+
# --- Configuration -----------------------------------------------------------
|
|
48
50
|
def apply_config(session: SessionConfig) -> None:
|
|
49
51
|
session.mdns_config = TomlConfig.build_config(MDNSConfig)
|
|
50
52
|
|
|
@@ -56,13 +58,6 @@ def create_server_threads(session: SessionConfig) -> list[ServerThread]:
|
|
|
56
58
|
return [ServerThread(session, MDNSServer)]
|
|
57
59
|
|
|
58
60
|
|
|
59
|
-
def normalized_name(host: str | bytes) -> str:
|
|
60
|
-
if isinstance(host, (bytes, bytearray)):
|
|
61
|
-
host = host.decode("utf-8", errors="replace")
|
|
62
|
-
|
|
63
|
-
return str(host).strip().removesuffix(".").removesuffix(".local")
|
|
64
|
-
|
|
65
|
-
|
|
66
61
|
class MDNSConfig(TomlConfig):
|
|
67
62
|
_section_ = "mDNS"
|
|
68
63
|
_fields_ = [
|
|
@@ -85,6 +80,14 @@ class MDNSConfig(TomlConfig):
|
|
|
85
80
|
self.mdns_qtypes = [x if isinstance(x, int) else QTYPES[x] for x in value]
|
|
86
81
|
|
|
87
82
|
|
|
83
|
+
# --- Utilities -----------------------------------------------------------
|
|
84
|
+
def normalized_name(host: str | bytes) -> str:
|
|
85
|
+
if isinstance(host, (bytes, bytearray)):
|
|
86
|
+
host = host.decode("utf-8", errors="replace")
|
|
87
|
+
|
|
88
|
+
return str(host).strip().removesuffix(".").removesuffix(".local")
|
|
89
|
+
|
|
90
|
+
|
|
88
91
|
# MDNS / LLMNR Answer Packet
|
|
89
92
|
def build_dns_answer(req_id: int, question: dns.DNSQR, config: SessionConfig):
|
|
90
93
|
answer = dns.DNSRR(
|
|
@@ -112,6 +115,7 @@ def build_dns_answer(req_id: int, question: dns.DNSQR, config: SessionConfig):
|
|
|
112
115
|
)
|
|
113
116
|
|
|
114
117
|
|
|
118
|
+
# --- Poisoner/Server ---------------------------------------------------------
|
|
115
119
|
class MDNSPoisoner(BaseProtoHandler):
|
|
116
120
|
def proto_logger(self):
|
|
117
121
|
return ProtocolLogger(
|
|
@@ -150,6 +154,7 @@ class MDNSPoisoner(BaseProtoHandler):
|
|
|
150
154
|
if "._tcp" not in normalized_qname and "._udp" not in normalized_qname:
|
|
151
155
|
if not normalized_qname.endswith(".arpa"):
|
|
152
156
|
log_to("dns", type="MDNS", name=normalized_qname)
|
|
157
|
+
|
|
153
158
|
name = markup.escape(normalized_qname)
|
|
154
159
|
self.logger.display(
|
|
155
160
|
f"Request for [i]{name}[/i] (class: {qclass}, type: {qtype})"
|
|
@@ -17,12 +17,11 @@
|
|
|
17
17
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
19
|
# SOFTWARE.
|
|
20
|
-
# pyright:
|
|
20
|
+
# pyright: basic
|
|
21
21
|
#
|
|
22
22
|
# Notes:
|
|
23
23
|
# - Implementation of the MySQL protocol according to the online documentation.
|
|
24
24
|
# https://dev.mysql.com/doc/dev/mysql-server/latest/PAGE_PROTOCOL.html
|
|
25
|
-
# pyright: reportUninitializedInstanceVariable=false
|
|
26
25
|
import typing
|
|
27
26
|
import enum
|
|
28
27
|
|
|
@@ -32,10 +31,13 @@ from collections import OrderedDict
|
|
|
32
31
|
from caterpillar.py import (
|
|
33
32
|
CString,
|
|
34
33
|
Const,
|
|
34
|
+
Invisible,
|
|
35
35
|
Prefixed,
|
|
36
36
|
String,
|
|
37
|
+
f,
|
|
37
38
|
pack,
|
|
38
39
|
struct,
|
|
40
|
+
struct_factory,
|
|
39
41
|
this,
|
|
40
42
|
uint24,
|
|
41
43
|
uint8,
|
|
@@ -48,6 +50,7 @@ from caterpillar.py import (
|
|
|
48
50
|
unpack,
|
|
49
51
|
)
|
|
50
52
|
from caterpillar.exception import DynamicSizeError, StructException
|
|
53
|
+
from caterpillar.types import cstr_t, uint16_t, uint24_t, uint32_t, uint8_t
|
|
51
54
|
|
|
52
55
|
from dementor.config.session import SessionConfig
|
|
53
56
|
from dementor.log.hexdump import hexdump
|
|
@@ -176,7 +179,7 @@ class LengthEncodedInteger:
|
|
|
176
179
|
def __size__(self, context):
|
|
177
180
|
raise DynamicSizeError("LengthEncodedInteger is dynamic")
|
|
178
181
|
|
|
179
|
-
def __unpack__(self, context):
|
|
182
|
+
def __unpack__(self, context) -> int:
|
|
180
183
|
(size,) = context._io.read(1)
|
|
181
184
|
if 0 <= size < 251:
|
|
182
185
|
return size
|
|
@@ -194,14 +197,14 @@ class LengthEncodedInteger:
|
|
|
194
197
|
case _:
|
|
195
198
|
raise StructException("invalid length-encoded integer")
|
|
196
199
|
|
|
197
|
-
def __pack__(self,
|
|
200
|
+
def __pack__(self, obj: int, context):
|
|
198
201
|
prefix = None
|
|
199
|
-
if
|
|
202
|
+
if obj < 251:
|
|
200
203
|
target = uint8
|
|
201
|
-
elif
|
|
204
|
+
elif obj < 0x10000:
|
|
202
205
|
prefix = b"\xfc"
|
|
203
206
|
target = uint16
|
|
204
|
-
elif
|
|
207
|
+
elif obj < 0x1000000:
|
|
205
208
|
prefix = b"\xfd"
|
|
206
209
|
target = uint24
|
|
207
210
|
else:
|
|
@@ -211,7 +214,7 @@ class LengthEncodedInteger:
|
|
|
211
214
|
if prefix:
|
|
212
215
|
context._io.write(prefix)
|
|
213
216
|
|
|
214
|
-
target.pack_single(
|
|
217
|
+
target.pack_single(obj, context)
|
|
215
218
|
|
|
216
219
|
|
|
217
220
|
# [Protocol::Packet]
|
|
@@ -219,23 +222,23 @@ class LengthEncodedInteger:
|
|
|
219
222
|
class Packet:
|
|
220
223
|
# Length of the payload. The number of bytes in the packet beyond the
|
|
221
224
|
# initial 4 bytes that make up the packet header.
|
|
222
|
-
payload_length:
|
|
225
|
+
payload_length: uint24_t
|
|
223
226
|
|
|
224
227
|
# The sequence-id is incremented with each packet and may wrap around.
|
|
225
228
|
# It starts at 0 and is reset to 0 when a new command begins in the
|
|
226
229
|
# Command Phase.
|
|
227
|
-
sequence_id:
|
|
230
|
+
sequence_id: uint8_t
|
|
228
231
|
|
|
229
232
|
# payload of the packet
|
|
230
|
-
payload: Bytes(this.payload_length)
|
|
233
|
+
payload: f[bytes, Bytes(this.payload_length)]
|
|
231
234
|
|
|
232
235
|
|
|
233
236
|
# [ERR_Packet]
|
|
234
237
|
@struct(order=LittleEndian)
|
|
235
238
|
class ERR_Packet:
|
|
236
|
-
header: Const(0xFF, uint8)
|
|
237
|
-
error_code:
|
|
238
|
-
error_message: String(...)
|
|
239
|
+
header: f[int, Const(0xFF, uint8)] = Invisible()
|
|
240
|
+
error_code: uint16_t
|
|
241
|
+
error_message: f[str, String(...)]
|
|
239
242
|
|
|
240
243
|
|
|
241
244
|
def _auth_plugin_length(context):
|
|
@@ -252,40 +255,40 @@ def _has_auth_plugin_data(context):
|
|
|
252
255
|
|
|
253
256
|
|
|
254
257
|
# [Protocol::HandshakeV10]
|
|
255
|
-
@struct(order=LittleEndian)
|
|
258
|
+
@struct(order=LittleEndian, kw_only=True)
|
|
256
259
|
class HandshakeV10:
|
|
257
|
-
protocol_version: Const(10, uint8)
|
|
258
|
-
server_version:
|
|
260
|
+
protocol_version: f[int, Const(10, uint8)] = Invisible()
|
|
261
|
+
server_version: cstr_t
|
|
259
262
|
|
|
260
263
|
# a.k.a. connection id
|
|
261
|
-
thread_id:
|
|
264
|
+
thread_id: uint32_t = 0
|
|
262
265
|
|
|
263
266
|
# first 8 bytes of the plugin provided data (scramble)
|
|
264
|
-
salt: Bytes(8)
|
|
267
|
+
salt: f[bytes, Bytes(8)]
|
|
265
268
|
|
|
266
269
|
# 0x00 byte, terminating the first part of a scramble
|
|
267
|
-
filler: Const(0x00, uint8)
|
|
270
|
+
filler: f[int, Const(0x00, uint8)] = Invisible()
|
|
268
271
|
|
|
269
272
|
# Why does it have to be that complicated?
|
|
270
273
|
# The lower 2 bytes of the Capabilities Flags
|
|
271
|
-
flags_lower:
|
|
274
|
+
flags_lower: uint16_t = 0x000
|
|
272
275
|
|
|
273
276
|
# default server a_protocol_character_set, only the lower 8-bits
|
|
274
|
-
character_set:
|
|
277
|
+
character_set: uint8_t = 0x3F
|
|
275
278
|
status_flags: SERVER_STATUS_flags_enum = 0x00
|
|
276
|
-
flags_upper:
|
|
279
|
+
flags_upper: uint16_t = 0x000
|
|
277
280
|
|
|
278
281
|
# length of the combined auth_plugin_data (scramble), if
|
|
279
282
|
# auth_plugin_data_len is > 0
|
|
280
|
-
auth_plugin_data_len:
|
|
281
|
-
reserved: Bytes(10) = b"\0" * 10
|
|
283
|
+
auth_plugin_data_len: uint8_t = 0x00
|
|
284
|
+
reserved: f[bytes, Bytes(10)] = b"\0" * 10
|
|
282
285
|
|
|
283
286
|
# Rest of the plugin provided data (scramble), $len=MAX(13, length of
|
|
284
287
|
# auth-plugin-data - 8)
|
|
285
|
-
salt2: Bytes(_auth_plugin_length)
|
|
288
|
+
salt2: f[bytes, Bytes(_auth_plugin_length)]
|
|
286
289
|
|
|
287
290
|
# name of the auth_method that the auth_plugin_data belongs to
|
|
288
|
-
auth_plugin_name: CString() // _has_auth_plugin_data
|
|
291
|
+
auth_plugin_name: f[str | None, CString() // _has_auth_plugin_data]
|
|
289
292
|
|
|
290
293
|
def set_flags(self, flags):
|
|
291
294
|
self.flags_lower = flags & 0xFFFF
|
|
@@ -310,41 +313,41 @@ def _client_connect_attrs(context):
|
|
|
310
313
|
# [Protocol::SSLRequest]
|
|
311
314
|
@struct(order=LittleEndian)
|
|
312
315
|
class SSLRequest:
|
|
313
|
-
capabilities:
|
|
314
|
-
max_packet_size:
|
|
315
|
-
character_set:
|
|
316
|
+
capabilities: uint32_t
|
|
317
|
+
max_packet_size: uint32_t
|
|
318
|
+
character_set: uint8_t
|
|
316
319
|
# everything else is not used
|
|
317
320
|
|
|
318
321
|
|
|
319
322
|
@struct(order=LittleEndian)
|
|
320
|
-
class ConnectionAttribute:
|
|
323
|
+
class ConnectionAttribute(struct_factory.mixin):
|
|
321
324
|
# will be stored as bytes rather than string
|
|
322
|
-
key: Prefixed(LengthEncodedInteger)
|
|
323
|
-
value: Prefixed(LengthEncodedInteger)
|
|
325
|
+
key: f[bytes, Prefixed(LengthEncodedInteger)]
|
|
326
|
+
value: f[bytes, Prefixed(LengthEncodedInteger)]
|
|
324
327
|
|
|
325
328
|
|
|
326
329
|
# [Protocol::HandshakeResponse]
|
|
327
|
-
@struct(order=LittleEndian)
|
|
330
|
+
@struct(order=LittleEndian, kw_only=True)
|
|
328
331
|
class HandshakeResponse:
|
|
329
|
-
client_flags:
|
|
330
|
-
max_packet_size:
|
|
331
|
-
charset:
|
|
332
|
+
client_flags: uint32_t
|
|
333
|
+
max_packet_size: uint32_t
|
|
334
|
+
charset: uint8_t
|
|
332
335
|
|
|
333
336
|
# filler to the size of the handhshake response packet. All 0s.
|
|
334
|
-
filler: Bytes(23) = b"\0" * 23
|
|
337
|
+
filler: f[bytes, Bytes(23)] = b"\0" * 23
|
|
335
338
|
|
|
336
339
|
# login user name
|
|
337
|
-
username:
|
|
340
|
+
username: cstr_t
|
|
338
341
|
|
|
339
342
|
# opaque authentication response data generated by Authentication Method
|
|
340
343
|
# indicated by the plugin name field.
|
|
341
344
|
# NOTE: we always assume a length encoded integer
|
|
342
|
-
auth_response: Prefixed(LengthEncodedInteger)
|
|
345
|
+
auth_response: f[bytes, Prefixed(LengthEncodedInteger)]
|
|
343
346
|
|
|
344
347
|
# initial database for the connection. This string should be interpreted
|
|
345
348
|
# using the character set indicated by character set field.
|
|
346
|
-
database: CString() // _client_connect_db
|
|
347
|
-
client_plugin_name: CString() // _client_auth_plugin
|
|
349
|
+
database: f[str | None, CString() // _client_connect_db]
|
|
350
|
+
client_plugin_name: f[str | None, CString() // _client_auth_plugin]
|
|
348
351
|
|
|
349
352
|
# From the docs:
|
|
350
353
|
# if capabilities & CLIENT_CONNECT_ATTRS {
|
|
@@ -353,9 +356,11 @@ class HandshakeResponse:
|
|
|
353
356
|
# string<lenenc> Value of the 1st client attribute
|
|
354
357
|
# .. (if more data in length of all key-values, more keys and values parts)
|
|
355
358
|
# }
|
|
356
|
-
conn_attrs:
|
|
359
|
+
conn_attrs: f[
|
|
360
|
+
list[ConnectionAttribute] | None,
|
|
357
361
|
Prefixed(LengthEncodedInteger, ConnectionAttribute[...])
|
|
358
|
-
|
|
362
|
+
// _client_connect_attrs,
|
|
363
|
+
]
|
|
359
364
|
# zstd_compression_level is dropped here
|
|
360
365
|
|
|
361
366
|
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
19
|
# SOFTWARE.
|
|
20
|
-
# pyright:
|
|
20
|
+
# pyright: basic
|
|
21
21
|
import uuid
|
|
22
22
|
import calendar
|
|
23
23
|
import time
|
|
@@ -36,6 +36,15 @@ from impacket import (
|
|
|
36
36
|
spnego,
|
|
37
37
|
smbserver,
|
|
38
38
|
)
|
|
39
|
+
from caterpillar.py import (
|
|
40
|
+
LittleEndian,
|
|
41
|
+
f,
|
|
42
|
+
struct,
|
|
43
|
+
struct_factory,
|
|
44
|
+
this,
|
|
45
|
+
uint16,
|
|
46
|
+
)
|
|
47
|
+
from caterpillar.types import uint16_t
|
|
39
48
|
|
|
40
49
|
from dementor.config.toml import TomlConfig, Attribute as A
|
|
41
50
|
from dementor.config.session import SessionConfig
|
|
@@ -55,7 +64,7 @@ from dementor.protocols.spnego import (
|
|
|
55
64
|
)
|
|
56
65
|
from dementor.servers import BaseProtoHandler, ThreadingTCPServer, ServerThread
|
|
57
66
|
|
|
58
|
-
|
|
67
|
+
# --- Constants ---------------------------------------------------------------
|
|
59
68
|
SMB2_DIALECTS = {
|
|
60
69
|
smb2.SMB2_DIALECT_002: "SMB 2.002",
|
|
61
70
|
smb2.SMB2_DIALECT_21: "SMB 2.1",
|
|
@@ -68,7 +77,18 @@ SMB2_DIALECTS = {
|
|
|
68
77
|
|
|
69
78
|
SMB2_DIALECT_REV = {v: k for k, v in SMB2_DIALECTS.items()}
|
|
70
79
|
|
|
80
|
+
SMB2_INTEGRITY_SHA512 = uint16.to_bytes(0x0001, order=LittleEndian)
|
|
81
|
+
|
|
71
82
|
|
|
83
|
+
# (missing in impackets struct definitions)
|
|
84
|
+
# 2.2.3.1.7 SMB2_SIGNING_CAPABILITIES
|
|
85
|
+
@struct(order=LittleEndian)
|
|
86
|
+
class SMB2SigningCapabilities(struct_factory.mixin):
|
|
87
|
+
SigningAlgorithmCount: uint16_t
|
|
88
|
+
SigningAlgorithms: f[list[int], uint16[this.SigningAlgorithmCount]]
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
# --- Config ------------------------------------------------------------------
|
|
72
92
|
class SMBServerConfig(TomlConfig):
|
|
73
93
|
_section_ = "SMB"
|
|
74
94
|
_fields_ = [
|
|
@@ -125,6 +145,7 @@ def create_server_threads(session: SessionConfig):
|
|
|
125
145
|
return servers
|
|
126
146
|
|
|
127
147
|
|
|
148
|
+
# --- Functions ---------------------------------------------------------------
|
|
128
149
|
def SMB_get_server_time():
|
|
129
150
|
value = calendar.timegm(time.gmtime())
|
|
130
151
|
value *= 10000000
|
|
@@ -149,10 +170,62 @@ def SMB_get_command_name(command: int, smb_version: int) -> str:
|
|
|
149
170
|
return "Unknown"
|
|
150
171
|
|
|
151
172
|
|
|
152
|
-
# ---
|
|
153
|
-
def
|
|
173
|
+
# --- SMB3 --------------------------------------------------------------------
|
|
174
|
+
def SMB3_get_neg_context_pad(data_len: int) -> bytes:
|
|
175
|
+
return b"\xff" * ((8 - (data_len % 8)) % 8)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def SMB3_build_neg_context_list(context_objects: list[tuple[int, bytes]]) -> bytes:
|
|
179
|
+
# build Negotiate Contexts
|
|
180
|
+
context_list = b""
|
|
181
|
+
for caps_type, caps in context_objects:
|
|
182
|
+
context = smb3.SMB2NegotiateContext()
|
|
183
|
+
context["ContextType"] = caps_type
|
|
184
|
+
context["Data"] = caps
|
|
185
|
+
context["DataLength"] = len(caps)
|
|
186
|
+
|
|
187
|
+
context_list += context.getData()
|
|
188
|
+
context_list += SMB3_get_neg_context_pad(context["DataLength"])
|
|
189
|
+
return context_list
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def SMB3_get_target_capabilities(
|
|
193
|
+
handler: "SMBHandler", request: smb2.SMB2Negotiate
|
|
194
|
+
) -> tuple[int, ...]:
|
|
195
|
+
target_cipher = smb3.SMB2_ENCRYPTION_AES128_GCM
|
|
196
|
+
target_sign = 0x001 # SMB2_SIGNING_AES_CMAC
|
|
197
|
+
try:
|
|
198
|
+
context_data = smb3.SMB311ContextData(request["ClientStartTime"])
|
|
199
|
+
raw_context_list = request.rawData[context_data["NegotiateContextOffset"] :]
|
|
200
|
+
offset = 0
|
|
201
|
+
for _ in range(context_data["NegotiateContextCount"]):
|
|
202
|
+
context = smb3.SMB2NegotiateContext(raw_context_list)
|
|
203
|
+
match context["ContextType"]:
|
|
204
|
+
case smb3.SMB2_ENCRYPTION_CAPABILITIES:
|
|
205
|
+
req_enc_caps = smb3.SMB2EncryptionCapabilities(context["Data"])
|
|
206
|
+
target_cipher = uint16.from_bytes(
|
|
207
|
+
req_enc_caps["Ciphers"],
|
|
208
|
+
order=LittleEndian,
|
|
209
|
+
)
|
|
210
|
+
case 0x008:
|
|
211
|
+
req_sign_caps = SMB2SigningCapabilities.from_bytes(context["Data"])
|
|
212
|
+
target_sign = req_sign_caps.SigningAlgorithms[0]
|
|
213
|
+
|
|
214
|
+
offset += context["DataLength"] + 8
|
|
215
|
+
offset += (8 - (offset % 8)) % 8
|
|
216
|
+
except Exception as e:
|
|
217
|
+
handler.logger.debug(f"Warning: invalid negotiate context list: {e}")
|
|
218
|
+
return target_cipher, target_sign
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
# --- SMB2 --------------------------------------------------------------------
|
|
222
|
+
def smb2_negotiate(
|
|
223
|
+
handler: "SMBHandler",
|
|
224
|
+
target_revision: int,
|
|
225
|
+
request: smb2.SMB2Negotiate | None = None,
|
|
226
|
+
) -> smb2.SMB2Negotiate_Response:
|
|
154
227
|
command = smb2.SMB2Negotiate_Response()
|
|
155
|
-
command["
|
|
228
|
+
command["SecurityMode"] = 0x01 # signing enabled, but not enforced
|
|
156
229
|
command["DialectRevision"] = target_revision
|
|
157
230
|
command["ServerGuid"] = secrets.token_bytes(16)
|
|
158
231
|
command["Capabilities"] = 0x00
|
|
@@ -166,6 +239,50 @@ def smb2_negotiate(handler: "SMBHandler", target_revision: int):
|
|
|
166
239
|
blob = negTokenInit([SPNEGO_NTLMSSP_MECH])
|
|
167
240
|
command["Buffer"] = blob.getData()
|
|
168
241
|
command["SecurityBufferLength"] = len(command["Buffer"])
|
|
242
|
+
|
|
243
|
+
if target_revision == smb2.SMB2_DIALECT_311:
|
|
244
|
+
# 2.2.3.1.1 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
|
|
245
|
+
# The format of the data in the Data field of this SMB2_NEGOTIATE_CONTEXT MUST
|
|
246
|
+
# take the same form specified in section 2.2.3.1.1 except that the
|
|
247
|
+
# HashAlgorithmCount field MUST be 1.
|
|
248
|
+
int_caps = smb3.SMB2PreAuthIntegrityCapabilities()
|
|
249
|
+
int_caps["HashAlgorithmCount"] = 1
|
|
250
|
+
int_caps["SaltLength"] = 32
|
|
251
|
+
int_caps["HashAlgorithms"] = SMB2_INTEGRITY_SHA512
|
|
252
|
+
int_caps["Salt"] = secrets.token_bytes(32)
|
|
253
|
+
|
|
254
|
+
# 2.2.3.1.2 SMB2_ENCRYPTION_CAPABILITIES
|
|
255
|
+
# The format of the data in the Data field of this SMB2_NEGOTIATE_CONTEXT MUST take
|
|
256
|
+
# the same form specified in section 2.2.3.1.2 except that the CipherCount field
|
|
257
|
+
# MUST be 1
|
|
258
|
+
target_cipher = smb3.SMB2_ENCRYPTION_AES128_GCM
|
|
259
|
+
target_sign = 0x001 # SMB2_SIGNING_AES_CMAC
|
|
260
|
+
if request:
|
|
261
|
+
target_cipher, target_sign = SMB3_get_target_capabilities(handler, request)
|
|
262
|
+
|
|
263
|
+
enc_caps = smb3.SMB2EncryptionCapabilities()
|
|
264
|
+
enc_caps["CipherCount"] = 1
|
|
265
|
+
enc_caps["Ciphers"] = uint16.to_bytes(target_cipher, order=LittleEndian)
|
|
266
|
+
|
|
267
|
+
# 2.2.3.1.7 SMB2_SIGNING_CAPABILITIES are missing in impackets collection
|
|
268
|
+
sign_caps = SMB2SigningCapabilities(
|
|
269
|
+
SigningAlgorithmCount=1, SigningAlgorithms=[target_sign]
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
# build Negotiate Contexts
|
|
273
|
+
context_data = SMB3_build_neg_context_list(
|
|
274
|
+
[
|
|
275
|
+
(smb3.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, int_caps.getData()),
|
|
276
|
+
(smb3.SMB2_ENCRYPTION_CAPABILITIES, enc_caps.getData()),
|
|
277
|
+
(0x0008, sign_caps.to_bytes()),
|
|
278
|
+
]
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
# print(len(command["Buffer"]))
|
|
282
|
+
command["NegotiateContextOffset"] = 0x80 + command["SecurityBufferLength"] + 2
|
|
283
|
+
command["NegotiateContextList"] = b"\xff\xff" + context_data
|
|
284
|
+
command["NegotiateContextCount"] = 3
|
|
285
|
+
|
|
169
286
|
return command
|
|
170
287
|
|
|
171
288
|
|
|
@@ -180,10 +297,10 @@ def smb2_negotiate_protocol(handler: "SMBHandler", packet: smb2.SMB2Packet) -> N
|
|
|
180
297
|
)
|
|
181
298
|
|
|
182
299
|
# REVISIT
|
|
183
|
-
dialect =
|
|
184
|
-
for candidate in req_dialects:
|
|
185
|
-
|
|
186
|
-
|
|
300
|
+
dialect = req_dialects[-1]
|
|
301
|
+
# for candidate in req_dialects:
|
|
302
|
+
# if candidate < 0x300:
|
|
303
|
+
# dialect = candidate
|
|
187
304
|
|
|
188
305
|
if dialect is None:
|
|
189
306
|
handler.logger.fail(
|
|
@@ -191,7 +308,7 @@ def smb2_negotiate_protocol(handler: "SMBHandler", packet: smb2.SMB2Packet) -> N
|
|
|
191
308
|
)
|
|
192
309
|
raise BaseProtoHandler.TerminateConnection
|
|
193
310
|
|
|
194
|
-
command = smb2_negotiate(handler, dialect)
|
|
311
|
+
command = smb2_negotiate(handler, dialect, req)
|
|
195
312
|
handler.log_server(
|
|
196
313
|
f"selected dialect: {SMB2_DIALECTS.get(dialect, hex(dialect))}",
|
|
197
314
|
"SMB2_NEGOTIATE",
|
|
@@ -229,7 +346,7 @@ def smb2_logoff(handler: "SMBHandler", packet: smb2.SMB2Packet) -> None:
|
|
|
229
346
|
)
|
|
230
347
|
|
|
231
348
|
|
|
232
|
-
# --- SMB1
|
|
349
|
+
# --- SMB1 --------------------------------------------------------------------
|
|
233
350
|
def smb1_negotiate_protocol(handler, packet: smb.NewSMBPacket) -> None:
|
|
234
351
|
resp = smb.NewSMBPacket()
|
|
235
352
|
resp["Flags1"] = smb.SMB.FLAGS1_REPLY
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|