dementor 1.0.0.dev16__tar.gz → 1.0.0.dev18__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.dev18}/PKG-INFO +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/__init__.py +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/loader.py +0 -3
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/mdns.py +12 -7
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/mysql.py +49 -44
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/smb.py +186 -29
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/PKG-INFO +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/pyproject.toml +1 -1
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/LICENSE +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/README.md +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/__main__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/Dementor.toml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/banner.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/www/error_page.html +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/www/upnp-default/dd.xml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/www/upnp-default/present.html +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/www/upnp-default/scpd.xml +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/assets/www/wpad.dat +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/config/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/config/attr.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/config/session.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/config/toml.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/config/util.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/db/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/db/connector.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/db/model.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/filters.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/log/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/log/hexdump.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/log/logger.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/log/stream.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/paths.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/ftp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/http.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/imap.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/ipp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/kerberos.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/ldap.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/llmnr.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/msrpc/__init__.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/msrpc/dcom.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/msrpc/epm.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/msrpc/rpc.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/msrpc.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/mssql.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/netbios.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/ntlm.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/pop3.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/quic.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/smtp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/spnego.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/ssdp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/upnp.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/protocols/x11.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/servers.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor/standalone.py +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/SOURCES.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/dependency_links.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/entry_points.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/requires.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/dementor.egg-info/top_level.txt +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/docs/README.pypi.md +0 -0
- {dementor-1.0.0.dev16 → dementor-1.0.0.dev18}/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,20 +64,32 @@ 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",
|
|
71
|
+
smb2.SMB2_DIALECT_WILDCARD: "SMB 2.???",
|
|
62
72
|
smb2.SMB2_DIALECT_30: "SMB 3.0",
|
|
63
73
|
smb2.SMB2_DIALECT_302: "SMB 3.0.2",
|
|
64
74
|
smb2.SMB2_DIALECT_311: "SMB 3.1.1",
|
|
65
|
-
# used in SMB1 requests
|
|
66
|
-
0x0000: "SMB 2.???",
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
SMB2_NEGOTIABLE_DIALECTS = set(SMB2_DIALECTS) - {smb2.SMB2_DIALECT_WILDCARD}
|
|
78
|
+
|
|
69
79
|
SMB2_DIALECT_REV = {v: k for k, v in SMB2_DIALECTS.items()}
|
|
70
80
|
|
|
81
|
+
SMB2_INTEGRITY_SHA512 = uint16.to_bytes(0x0001, order=LittleEndian)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
# (missing in impackets struct definitions)
|
|
85
|
+
# 2.2.3.1.7 SMB2_SIGNING_CAPABILITIES
|
|
86
|
+
@struct(order=LittleEndian)
|
|
87
|
+
class SMB2SigningCapabilities(struct_factory.mixin):
|
|
88
|
+
SigningAlgorithmCount: uint16_t
|
|
89
|
+
SigningAlgorithms: f[list[int], uint16[this.SigningAlgorithmCount]]
|
|
90
|
+
|
|
71
91
|
|
|
92
|
+
# --- Config ------------------------------------------------------------------
|
|
72
93
|
class SMBServerConfig(TomlConfig):
|
|
73
94
|
_section_ = "SMB"
|
|
74
95
|
_fields_ = [
|
|
@@ -125,6 +146,7 @@ def create_server_threads(session: SessionConfig):
|
|
|
125
146
|
return servers
|
|
126
147
|
|
|
127
148
|
|
|
149
|
+
# --- Functions ---------------------------------------------------------------
|
|
128
150
|
def SMB_get_server_time():
|
|
129
151
|
value = calendar.timegm(time.gmtime())
|
|
130
152
|
value *= 10000000
|
|
@@ -149,10 +171,64 @@ def SMB_get_command_name(command: int, smb_version: int) -> str:
|
|
|
149
171
|
return "Unknown"
|
|
150
172
|
|
|
151
173
|
|
|
152
|
-
# ---
|
|
153
|
-
def
|
|
174
|
+
# --- SMB3 --------------------------------------------------------------------
|
|
175
|
+
def SMB3_get_neg_context_pad(data_len: int) -> bytes:
|
|
176
|
+
return b"\xff" * ((8 - (data_len % 8)) % 8)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def SMB3_build_neg_context_list(context_objects: list[tuple[int, bytes]]) -> bytes:
|
|
180
|
+
# build Negotiate Contexts
|
|
181
|
+
context_list = b""
|
|
182
|
+
for caps_type, caps in context_objects:
|
|
183
|
+
context = smb3.SMB2NegotiateContext()
|
|
184
|
+
context["ContextType"] = caps_type
|
|
185
|
+
context["Data"] = caps
|
|
186
|
+
context["DataLength"] = len(caps)
|
|
187
|
+
|
|
188
|
+
context_list += context.getData()
|
|
189
|
+
context_list += SMB3_get_neg_context_pad(context["DataLength"])
|
|
190
|
+
return context_list
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def SMB3_get_target_capabilities(
|
|
194
|
+
handler: "SMBHandler", request: smb2.SMB2Negotiate
|
|
195
|
+
) -> tuple[int, ...]:
|
|
196
|
+
target_cipher = smb3.SMB2_ENCRYPTION_AES128_GCM
|
|
197
|
+
target_sign = 0x001 # SMB2_SIGNING_AES_CMAC
|
|
198
|
+
try:
|
|
199
|
+
context_data = smb3.SMB311ContextData(request["ClientStartTime"])
|
|
200
|
+
# remove header size from offset
|
|
201
|
+
context_list_offset = context_data["NegotiateContextOffset"] - 64
|
|
202
|
+
raw_context_list = request.rawData[context_list_offset:]
|
|
203
|
+
offset = 0
|
|
204
|
+
for _ in range(context_data["NegotiateContextCount"]):
|
|
205
|
+
context = smb3.SMB2NegotiateContext(raw_context_list[offset:])
|
|
206
|
+
match context["ContextType"]:
|
|
207
|
+
case smb3.SMB2_ENCRYPTION_CAPABILITIES:
|
|
208
|
+
req_enc_caps = smb3.SMB2EncryptionCapabilities(context["Data"])
|
|
209
|
+
target_cipher = uint16.from_bytes(
|
|
210
|
+
req_enc_caps["Ciphers"],
|
|
211
|
+
order=LittleEndian,
|
|
212
|
+
)
|
|
213
|
+
case 0x008:
|
|
214
|
+
req_sign_caps = SMB2SigningCapabilities.from_bytes(context["Data"])
|
|
215
|
+
target_sign = req_sign_caps.SigningAlgorithms[0]
|
|
216
|
+
|
|
217
|
+
offset += context["DataLength"] + 8
|
|
218
|
+
offset += (8 - (offset % 8)) % 8
|
|
219
|
+
except Exception as e:
|
|
220
|
+
handler.logger.debug(f"Warning: invalid negotiate context list: {e}")
|
|
221
|
+
return target_cipher, target_sign
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
# --- SMB2 --------------------------------------------------------------------
|
|
225
|
+
def smb2_negotiate(
|
|
226
|
+
handler: "SMBHandler",
|
|
227
|
+
target_revision: int,
|
|
228
|
+
request: smb2.SMB2Negotiate | None = None,
|
|
229
|
+
) -> smb2.SMB2Negotiate_Response:
|
|
154
230
|
command = smb2.SMB2Negotiate_Response()
|
|
155
|
-
command["
|
|
231
|
+
command["SecurityMode"] = 0x01 # signing enabled, but not enforced
|
|
156
232
|
command["DialectRevision"] = target_revision
|
|
157
233
|
command["ServerGuid"] = secrets.token_bytes(16)
|
|
158
234
|
command["Capabilities"] = 0x00
|
|
@@ -166,32 +242,90 @@ def smb2_negotiate(handler: "SMBHandler", target_revision: int):
|
|
|
166
242
|
blob = negTokenInit([SPNEGO_NTLMSSP_MECH])
|
|
167
243
|
command["Buffer"] = blob.getData()
|
|
168
244
|
command["SecurityBufferLength"] = len(command["Buffer"])
|
|
245
|
+
|
|
246
|
+
if target_revision == smb2.SMB2_DIALECT_311:
|
|
247
|
+
# 2.2.3.1.1 SMB2_PREAUTH_INTEGRITY_CAPABILITIES
|
|
248
|
+
# The format of the data in the Data field of this SMB2_NEGOTIATE_CONTEXT MUST
|
|
249
|
+
# take the same form specified in section 2.2.3.1.1 except that the
|
|
250
|
+
# HashAlgorithmCount field MUST be 1.
|
|
251
|
+
int_caps = smb3.SMB2PreAuthIntegrityCapabilities()
|
|
252
|
+
int_caps["HashAlgorithmCount"] = 1
|
|
253
|
+
int_caps["SaltLength"] = 32
|
|
254
|
+
int_caps["HashAlgorithms"] = SMB2_INTEGRITY_SHA512
|
|
255
|
+
int_caps["Salt"] = secrets.token_bytes(32)
|
|
256
|
+
|
|
257
|
+
# 2.2.3.1.2 SMB2_ENCRYPTION_CAPABILITIES
|
|
258
|
+
# The format of the data in the Data field of this SMB2_NEGOTIATE_CONTEXT MUST take
|
|
259
|
+
# the same form specified in section 2.2.3.1.2 except that the CipherCount field
|
|
260
|
+
# MUST be 1
|
|
261
|
+
target_cipher = smb3.SMB2_ENCRYPTION_AES128_GCM
|
|
262
|
+
target_sign = 0x001 # SMB2_SIGNING_AES_CMAC
|
|
263
|
+
if request:
|
|
264
|
+
target_cipher, target_sign = SMB3_get_target_capabilities(handler, request)
|
|
265
|
+
|
|
266
|
+
enc_caps = smb3.SMB2EncryptionCapabilities()
|
|
267
|
+
enc_caps["CipherCount"] = 1
|
|
268
|
+
enc_caps["Ciphers"] = uint16.to_bytes(target_cipher, order=LittleEndian)
|
|
269
|
+
|
|
270
|
+
# 2.2.3.1.7 SMB2_SIGNING_CAPABILITIES are missing in impackets collection
|
|
271
|
+
sign_caps = SMB2SigningCapabilities(
|
|
272
|
+
SigningAlgorithmCount=1, SigningAlgorithms=[target_sign]
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
# build Negotiate Contexts
|
|
276
|
+
context_data = SMB3_build_neg_context_list(
|
|
277
|
+
[
|
|
278
|
+
(smb3.SMB2_PREAUTH_INTEGRITY_CAPABILITIES, int_caps.getData()),
|
|
279
|
+
(smb3.SMB2_ENCRYPTION_CAPABILITIES, enc_caps.getData()),
|
|
280
|
+
(0x0008, sign_caps.to_bytes()),
|
|
281
|
+
]
|
|
282
|
+
)
|
|
283
|
+
|
|
284
|
+
offset: int = 0x80 + command["SecurityBufferLength"]
|
|
285
|
+
sec_buf_pad = SMB3_get_neg_context_pad(0x80 + command["SecurityBufferLength"])
|
|
286
|
+
command["NegotiateContextOffset"] = offset + len(sec_buf_pad)
|
|
287
|
+
command["NegotiateContextList"] = sec_buf_pad + context_data
|
|
288
|
+
command["NegotiateContextCount"] = 3
|
|
289
|
+
|
|
169
290
|
return command
|
|
170
291
|
|
|
171
292
|
|
|
172
293
|
def smb2_negotiate_protocol(handler: "SMBHandler", packet: smb2.SMB2Packet) -> None:
|
|
173
294
|
req = smb3.SMB2Negotiate(data=packet["Data"])
|
|
174
295
|
# Let's take the first dialect the clients wan't us to use
|
|
175
|
-
|
|
296
|
+
dialect_count: int = req["DialectCount"]
|
|
297
|
+
req_raw_dialects: list[int] = req["Dialects"]
|
|
298
|
+
if len(req_raw_dialects) < dialect_count:
|
|
299
|
+
# automatically adjust length
|
|
300
|
+
dialect_count = len(req_raw_dialects)
|
|
301
|
+
|
|
302
|
+
req_dialects: list[int] = req_raw_dialects[:dialect_count]
|
|
303
|
+
if len(req_dialects) == 0:
|
|
304
|
+
# 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
|
|
305
|
+
# If the DialectCount of the SMB2 NEGOTIATE Request is 0, the server MUST fail the request with
|
|
306
|
+
# STATUS_INVALID_PARAMETER.
|
|
307
|
+
handler.log_client("Client sent no dialects", "SMB_COM_NEGOTIATE")
|
|
308
|
+
handler.logger.fail("SMB Negotiation: Client failed to provide any dialects.")
|
|
309
|
+
raise BaseProtoHandler.TerminateConnection
|
|
310
|
+
|
|
176
311
|
str_req_dialects = ", ".join([SMB2_DIALECTS.get(d, hex(d)) for d in req_dialects])
|
|
177
312
|
guid = uuid.UUID(bytes_le=req["ClientGuid"])
|
|
178
313
|
handler.log_client(
|
|
179
314
|
f"requested dialects: {str_req_dialects} (client: {guid})", "SMB2_NEGOTIATE"
|
|
180
315
|
)
|
|
181
316
|
|
|
182
|
-
#
|
|
183
|
-
dialect =
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
317
|
+
# select greatest common dialect
|
|
318
|
+
dialect = max(
|
|
319
|
+
(d for d in req_dialects if d in SMB2_NEGOTIABLE_DIALECTS),
|
|
320
|
+
default=None,
|
|
321
|
+
)
|
|
188
322
|
if dialect is None:
|
|
189
323
|
handler.logger.fail(
|
|
190
324
|
f"Client requested unsupported dialects: {str_req_dialects}"
|
|
191
325
|
)
|
|
192
326
|
raise BaseProtoHandler.TerminateConnection
|
|
193
327
|
|
|
194
|
-
command = smb2_negotiate(handler, dialect)
|
|
328
|
+
command = smb2_negotiate(handler, dialect, req)
|
|
195
329
|
handler.log_server(
|
|
196
330
|
f"selected dialect: {SMB2_DIALECTS.get(dialect, hex(dialect))}",
|
|
197
331
|
"SMB2_NEGOTIATE",
|
|
@@ -229,8 +363,8 @@ def smb2_logoff(handler: "SMBHandler", packet: smb2.SMB2Packet) -> None:
|
|
|
229
363
|
)
|
|
230
364
|
|
|
231
365
|
|
|
232
|
-
# --- SMB1
|
|
233
|
-
def smb1_negotiate_protocol(handler, packet: smb.NewSMBPacket) -> None:
|
|
366
|
+
# --- SMB1 --------------------------------------------------------------------
|
|
367
|
+
def smb1_negotiate_protocol(handler: "SMBHandler", packet: smb.NewSMBPacket) -> None:
|
|
234
368
|
resp = smb.NewSMBPacket()
|
|
235
369
|
resp["Flags1"] = smb.SMB.FLAGS1_REPLY
|
|
236
370
|
resp["Pid"] = packet["Pid"]
|
|
@@ -238,19 +372,37 @@ def smb1_negotiate_protocol(handler, packet: smb.NewSMBPacket) -> None:
|
|
|
238
372
|
resp["Mid"] = packet["Mid"]
|
|
239
373
|
|
|
240
374
|
req = smb.SMBCommand(packet["Data"][0])
|
|
241
|
-
|
|
375
|
+
req_data_dialects: list[bytes] = req["Data"].split(b"\x02")[1:]
|
|
376
|
+
if len(req_data_dialects) == 0:
|
|
377
|
+
# 3.3.5.4 Receiving an SMB2 NEGOTIATE Request
|
|
378
|
+
# If the DialectCount of the SMB2 NEGOTIATE Request is 0, the server MUST fail the request with
|
|
379
|
+
# STATUS_INVALID_PARAMETER.
|
|
380
|
+
handler.log_client("Client sent no dialects", "SMB_COM_NEGOTIATE")
|
|
381
|
+
handler.logger.fail("SMB Negotiation: Client failed to provide any dialects.")
|
|
382
|
+
raise BaseProtoHandler.TerminateConnection
|
|
383
|
+
|
|
384
|
+
dialects: list[str] = [
|
|
242
385
|
dialect.rstrip(b"\x00").decode(errors="replace")
|
|
243
|
-
for dialect in
|
|
386
|
+
for dialect in req_data_dialects
|
|
244
387
|
]
|
|
245
388
|
handler.log_client(f"dialects: {', '.join(dialects)}", "SMB_COM_NEGOTIATE")
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if dialect in SMB2_DIALECT_REV and handler.smb_config.smb2_support
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
389
|
+
smb2_entries: dict[str, int] = {
|
|
390
|
+
dialect: index
|
|
391
|
+
for index, dialect in enumerate(dialects)
|
|
392
|
+
if dialect in SMB2_DIALECT_REV and handler.smb_config.smb2_support
|
|
393
|
+
}
|
|
394
|
+
if smb2_entries:
|
|
395
|
+
# 3.3.5.3.1 SMB 2.1 or SMB 3.x Support
|
|
396
|
+
# Otherwise, the server MUST scan the dialects provided for the dialect string "SMB 2.???". If the string
|
|
397
|
+
# is not present, continue to section 3.3.5.3.2. If the string is present, the server MUST respond with an
|
|
398
|
+
# SMB2 NEGOTIATE Response as specified in 2.2.4.
|
|
399
|
+
index, target_dialect = smb2_entries.get("SMB 2.???"), "SMB 2.???"
|
|
400
|
+
if index is None:
|
|
401
|
+
target_dialect = list(smb2_entries)[-1]
|
|
402
|
+
index = smb2_entries[target_dialect]
|
|
403
|
+
else:
|
|
404
|
+
index, target_dialect = 0, dialects[0]
|
|
405
|
+
|
|
254
406
|
if target_dialect in SMB2_DIALECT_REV:
|
|
255
407
|
# Requested dialect is SMB2 -> respond with SMB2
|
|
256
408
|
command = smb2_negotiate(handler, SMB2_DIALECT_REV[target_dialect])
|
|
@@ -301,7 +453,7 @@ def smb1_negotiate_protocol(handler, packet: smb.NewSMBPacket) -> None:
|
|
|
301
453
|
handler.send_data(resp.getData())
|
|
302
454
|
|
|
303
455
|
|
|
304
|
-
def smb1_session_setup(handler, packet: smb.NewSMBPacket) -> None:
|
|
456
|
+
def smb1_session_setup(handler: "SMBHandler", packet: smb.NewSMBPacket) -> None:
|
|
305
457
|
command = smb.SMBCommand(packet["Data"][0])
|
|
306
458
|
handler.log_client(f"session setup: {command.fields}", "SMB_COM_SESSION_SETUP_ANDX")
|
|
307
459
|
# handler.send_data(packet.getData())
|
|
@@ -341,6 +493,11 @@ def smb1_session_setup(handler, packet: smb.NewSMBPacket) -> None:
|
|
|
341
493
|
packet,
|
|
342
494
|
error_code=error_code,
|
|
343
495
|
)
|
|
496
|
+
else:
|
|
497
|
+
handler.logger.warning(
|
|
498
|
+
f"<SMB_COM_SESSION_SETUP_ANDX> Unsupported WordCount: {command['WordCount']}"
|
|
499
|
+
)
|
|
500
|
+
raise BaseProtoHandler.TerminateConnection
|
|
344
501
|
|
|
345
502
|
|
|
346
503
|
# --- Handler ---
|
|
@@ -579,7 +736,7 @@ class SMBHandler(BaseProtoHandler):
|
|
|
579
736
|
token = neg_token["ResponseToken"]
|
|
580
737
|
|
|
581
738
|
# NTLM authentication below
|
|
582
|
-
if len(token)
|
|
739
|
+
if len(token) <= 8:
|
|
583
740
|
self.logger.fail(
|
|
584
741
|
f"<{command_name}> Invalid NTLM token length: {len(token)}"
|
|
585
742
|
)
|
|
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
|