dementor 1.0.0.dev6__tar.gz → 1.0.0.dev7__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.
Potentially problematic release.
This version of dementor might be problematic. Click here for more details.
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/PKG-INFO +1 -1
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/__init__.py +1 -1
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/assets/Dementor.toml +34 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/config/session.py +1 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/config/util.py +0 -1
- dementor-1.0.0.dev7/dementor/protocols/mysql.py +502 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/servers.py +14 -3
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/PKG-INFO +1 -1
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/SOURCES.txt +4 -1
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/pyproject.toml +1 -1
- dementor-1.0.0.dev7/test/test_ssl_client.py +29 -0
- dementor-1.0.0.dev7/test/test_ssl_server.py +31 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/LICENSE +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/README.md +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/__main__.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/assets/banner.txt +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/assets/www/error_page.html +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/assets/www/wpad.dat +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/config/__init__.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/config/attr.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/config/toml.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/database.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/filters.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/loader.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/logger.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/paths.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/__init__.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/ftp.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/http.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/imap.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/kerberos.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/ldap.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/llmnr.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/mdns.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/msrpc/__init__.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/msrpc/dcom.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/msrpc/epm.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/msrpc/rpc.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/msrpc.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/mssql.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/netbios.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/ntlm.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/pop3.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/quic.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/smb.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/smtp.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/protocols/spnego.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor/standalone.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/dependency_links.txt +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/entry_points.txt +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/requires.txt +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/dementor.egg-info/top_level.txt +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/docs/README.pypi.md +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/setup.cfg +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/test/test_dcerpc.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/test/test_dcom.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/test/test_epm.py +0 -0
- {dementor-1.0.0.dev6 → dementor-1.0.0.dev7}/test/test_mssql_auth.py +0 -0
|
@@ -117,6 +117,11 @@ WinRM = true
|
|
|
117
117
|
|
|
118
118
|
MSSQL = true
|
|
119
119
|
|
|
120
|
+
# Enables a simple MySQL server on its configured port. This setting does not
|
|
121
|
+
# require root privileges by default.
|
|
122
|
+
|
|
123
|
+
MySQL = true
|
|
124
|
+
|
|
120
125
|
# Enables POP3 servers. Requires root privileges by default (depends on port
|
|
121
126
|
# configuration)
|
|
122
127
|
# The default value is: true
|
|
@@ -694,6 +699,35 @@ Port = 110
|
|
|
694
699
|
# Key = "/path/to/key"
|
|
695
700
|
|
|
696
701
|
|
|
702
|
+
# =============================================================================
|
|
703
|
+
# MySQL
|
|
704
|
+
# =============================================================================
|
|
705
|
+
[MySQL]
|
|
706
|
+
|
|
707
|
+
# Port for the MySQL server.
|
|
708
|
+
|
|
709
|
+
Port = 3306
|
|
710
|
+
|
|
711
|
+
# Numeric error code returned to the client.
|
|
712
|
+
|
|
713
|
+
# ErrorCode = 1205
|
|
714
|
+
|
|
715
|
+
# Optional custom error message returned with the error code.
|
|
716
|
+
|
|
717
|
+
# ErrorMessage = "Access Denied"
|
|
718
|
+
|
|
719
|
+
# Specifies the reported version of the MySQL server. This version string is returned
|
|
720
|
+
# in responses and may influence client behavior.
|
|
721
|
+
|
|
722
|
+
ServerVersion = "8.0.42"
|
|
723
|
+
|
|
724
|
+
# TLS configuration is also available and will be applied if TLS is set to true.
|
|
725
|
+
|
|
726
|
+
# TLS = false
|
|
727
|
+
# Cert = /path/to/cert"
|
|
728
|
+
# Key = "/path/to/key"
|
|
729
|
+
|
|
730
|
+
|
|
697
731
|
# =============================================================================
|
|
698
732
|
# Logging
|
|
699
733
|
# =============================================================================
|
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
# Copyright (c) 2025-Present MatrixEditor
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
# copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
# SOFTWARE.
|
|
20
|
+
# pyright: reportInvalidTypeForm=false, reportCallIssue=false
|
|
21
|
+
#
|
|
22
|
+
# Notes:
|
|
23
|
+
# - Implementation of the MySQL protocol according to the online documentation.
|
|
24
|
+
# https://dev.mysql.com/doc/dev/mysql-server/latest/PAGE_PROTOCOL.html
|
|
25
|
+
|
|
26
|
+
import enum
|
|
27
|
+
import secrets
|
|
28
|
+
|
|
29
|
+
from typing import Any
|
|
30
|
+
from collections import OrderedDict
|
|
31
|
+
|
|
32
|
+
from caterpillar.py import (
|
|
33
|
+
CString,
|
|
34
|
+
Const,
|
|
35
|
+
Prefixed,
|
|
36
|
+
String,
|
|
37
|
+
pack,
|
|
38
|
+
struct,
|
|
39
|
+
this,
|
|
40
|
+
uint24,
|
|
41
|
+
uint8,
|
|
42
|
+
Bytes,
|
|
43
|
+
LittleEndian,
|
|
44
|
+
uint16,
|
|
45
|
+
uint32,
|
|
46
|
+
uint64,
|
|
47
|
+
singleton,
|
|
48
|
+
unpack,
|
|
49
|
+
)
|
|
50
|
+
from caterpillar.exception import DynamicSizeError, StructException
|
|
51
|
+
|
|
52
|
+
from dementor.servers import (
|
|
53
|
+
ThreadingTCPServer,
|
|
54
|
+
ServerThread,
|
|
55
|
+
BaseProtoHandler,
|
|
56
|
+
create_tls_context,
|
|
57
|
+
)
|
|
58
|
+
from dementor.logger import ProtocolLogger
|
|
59
|
+
from dementor.config.attr import Attribute as A, ATTR_TLS, ATTR_CERT, ATTR_KEY
|
|
60
|
+
from dementor.config.toml import TomlConfig
|
|
61
|
+
from dementor.database import _CLEARTEXT
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class MySQLConfig(TomlConfig):
|
|
65
|
+
_section_ = "MySQL"
|
|
66
|
+
_fields_ = [
|
|
67
|
+
A("mysql_port", "Port", 3306),
|
|
68
|
+
A("mysql_plugin_name", "AuthPlugin", "mysql_clear_password"),
|
|
69
|
+
A("mysql_version", "ServerVersion", "8.0.42"),
|
|
70
|
+
A("mysql_error_code", "ErrorCode", 1045),
|
|
71
|
+
A("mysql_error_message", "ErrorMessage", "Access denied for user"),
|
|
72
|
+
ATTR_CERT,
|
|
73
|
+
ATTR_KEY,
|
|
74
|
+
ATTR_TLS,
|
|
75
|
+
]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def apply_config(session):
|
|
79
|
+
session.mysql_config = TomlConfig.build_config(MySQLConfig)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def create_server_threads(session):
|
|
83
|
+
return [
|
|
84
|
+
ServerThread(
|
|
85
|
+
session,
|
|
86
|
+
MySQLServer,
|
|
87
|
+
server_address=(session.bind_address, session.mysql_config.mysql_port),
|
|
88
|
+
)
|
|
89
|
+
]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
# --- MySQL Protocol ---
|
|
93
|
+
|
|
94
|
+
# Capabilities
|
|
95
|
+
CLIENT_LONG_PASSWORD = 1
|
|
96
|
+
CLIENT_FOUND_ROWS = 2
|
|
97
|
+
CLIENT_LONG_FLAG = 4
|
|
98
|
+
CLIENT_CONNECT_WITH_DB = 8
|
|
99
|
+
CLIENT_NO_SCHEMA = 16
|
|
100
|
+
CLIENT_COMPRESS = 32
|
|
101
|
+
CLIENT_ODBC = 64
|
|
102
|
+
CLIENT_LOCAL_FILES = 128
|
|
103
|
+
CLIENT_IGNORE_SPACE = 256
|
|
104
|
+
CLIENT_PROTOCOL_41 = 512
|
|
105
|
+
CLIENT_INTERACTIVE = 1024
|
|
106
|
+
CLIENT_SSL = 2048
|
|
107
|
+
CLIENT_IGNORE_SIGPIPE = 4096
|
|
108
|
+
CLIENT_TRANSACTIONS = 8192
|
|
109
|
+
CLIENT_RESERVED = 16384
|
|
110
|
+
CLIENT_RESERVED2 = 32768
|
|
111
|
+
CLIENT_MULTI_STATEMENTS = 1 << 16
|
|
112
|
+
CLIENT_MULTI_RESULTS = 1 << 17
|
|
113
|
+
CLIENT_PS_MULTI_RESULTS = 1 << 18
|
|
114
|
+
CLIENT_PLUGIN_AUTH = 1 << 19
|
|
115
|
+
CLIENT_CONNECT_ATTRS = 1 << 20
|
|
116
|
+
CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 1 << 21
|
|
117
|
+
CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS = 1 << 22
|
|
118
|
+
CLIENT_SESSION_TRACK = 1 << 23
|
|
119
|
+
CLIENT_DEPRECATE_EOF = 1 << 24
|
|
120
|
+
CLIENT_OPTIONAL_RESULTSET_METADATA = 1 << 25
|
|
121
|
+
CLIENT_ZSTD_COMPRESSION_ALGORITHM = 1 << 26
|
|
122
|
+
CLIENT_QUERY_ATTRIBUTES = 1 << 27
|
|
123
|
+
MULTI_FACTOR_AUTHENTICATION = 1 << 28
|
|
124
|
+
CLIENT_CAPABILITY_EXTENSION = 1 << 29
|
|
125
|
+
CLIENT_SSL_VERIFY_SERVER_CERT = 1 << 30
|
|
126
|
+
CLIENT_REMEMBER_OPTIONS = 1 << 31
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class SERVER_STATUS_flags_enum(enum.IntEnum):
|
|
130
|
+
__struct__ = uint16
|
|
131
|
+
|
|
132
|
+
SERVER_STATUS_IN_TRANS = 1
|
|
133
|
+
SERVER_STATUS_AUTOCOMMIT = 2
|
|
134
|
+
SERVER_MORE_RESULTS_EXISTS = 8
|
|
135
|
+
SERVER_QUERY_NO_GOOD_INDEX_USED = 16
|
|
136
|
+
SERVER_QUERY_NO_INDEX_USED = 32
|
|
137
|
+
SERVER_STATUS_CURSOR_EXISTS = 64
|
|
138
|
+
SERVER_STATUS_LAST_ROW_SENT = 128
|
|
139
|
+
SERVER_STATUS_DB_DROPPED = 256
|
|
140
|
+
SERVER_STATUS_NO_BACKSLASH_ESCAPES = 512
|
|
141
|
+
SERVER_STATUS_METADATA_CHANGED = 1024
|
|
142
|
+
SERVER_QUERY_WAS_SLOW = 2048
|
|
143
|
+
SERVER_PS_OUT_PARAMS = 4096
|
|
144
|
+
SERVER_STATUS_IN_TRANS_READONLY = 8192
|
|
145
|
+
SERVER_SESSION_STATE_CHANGED = 1 << 14
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# [Protocol::LengthEncodedInteger]
|
|
149
|
+
# An integer that consumes 1, 3, 4, or 9 bytes, depending on its numeric value
|
|
150
|
+
# GT / Eq LT Stored as
|
|
151
|
+
# 0 251 1-byte integer
|
|
152
|
+
# 251 216 0xFC + 2-byte integer
|
|
153
|
+
# 216 224 0xFD + 3-byte integer
|
|
154
|
+
# 224 264 0xFE + 8-byte integer
|
|
155
|
+
@singleton
|
|
156
|
+
class LengthEncodedInteger:
|
|
157
|
+
def __type__(self):
|
|
158
|
+
return int
|
|
159
|
+
|
|
160
|
+
def __size__(self, context):
|
|
161
|
+
raise DynamicSizeError("LengthEncodedInteger is dynamic")
|
|
162
|
+
|
|
163
|
+
def __unpack__(self, context):
|
|
164
|
+
(size,) = context._io.read(1)
|
|
165
|
+
if 0 <= size < 251:
|
|
166
|
+
return size
|
|
167
|
+
|
|
168
|
+
match size:
|
|
169
|
+
case 0xFC:
|
|
170
|
+
return uint16.unpack_single(context)
|
|
171
|
+
|
|
172
|
+
case 0xFD:
|
|
173
|
+
return uint24.unpack_single(context)
|
|
174
|
+
|
|
175
|
+
case 0xFE:
|
|
176
|
+
return uint64.unpack_single(context)
|
|
177
|
+
|
|
178
|
+
case _:
|
|
179
|
+
raise StructException("invalid length-encoded integer")
|
|
180
|
+
|
|
181
|
+
def __pack__(self, value: int, context):
|
|
182
|
+
prefix = None
|
|
183
|
+
if value < 251:
|
|
184
|
+
target = uint8
|
|
185
|
+
elif value < 0x10000:
|
|
186
|
+
prefix = b"\xfc"
|
|
187
|
+
target = uint16
|
|
188
|
+
elif value < 0x1000000:
|
|
189
|
+
prefix = b"\xfd"
|
|
190
|
+
target = uint24
|
|
191
|
+
else:
|
|
192
|
+
prefix = b"\xfe"
|
|
193
|
+
target = uint64
|
|
194
|
+
|
|
195
|
+
if prefix:
|
|
196
|
+
context._io.write(prefix)
|
|
197
|
+
|
|
198
|
+
target.pack_single(value, context)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
# [Protocol::Packet]
|
|
202
|
+
@struct(order=LittleEndian)
|
|
203
|
+
class Packet:
|
|
204
|
+
# Length of the payload. The number of bytes in the packet beyond the
|
|
205
|
+
# initial 4 bytes that make up the packet header.
|
|
206
|
+
payload_length: uint24
|
|
207
|
+
|
|
208
|
+
# The sequence-id is incremented with each packet and may wrap around.
|
|
209
|
+
# It starts at 0 and is reset to 0 when a new command begins in the
|
|
210
|
+
# Command Phase.
|
|
211
|
+
sequence_id: uint8
|
|
212
|
+
|
|
213
|
+
# payload of the packet
|
|
214
|
+
payload: Bytes(this.payload_length)
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
# [ERR_Packet]
|
|
218
|
+
@struct(order=LittleEndian)
|
|
219
|
+
class ERR_Packet:
|
|
220
|
+
header: Const(0xFF, uint8)
|
|
221
|
+
error_code: uint16
|
|
222
|
+
error_message: String(...)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _auth_plugin_length(context):
|
|
226
|
+
# Rest of the plugin provided data (scramble), $len=MAX(13, length of auth-plugin-data - 8)
|
|
227
|
+
if _has_auth_plugin_data(context):
|
|
228
|
+
return max(13, context._obj.auth_plugin_data_len - 8)
|
|
229
|
+
|
|
230
|
+
return 0
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _has_auth_plugin_data(context):
|
|
234
|
+
flags = context._obj.flags_lower | (context._obj.flags_upper << 16)
|
|
235
|
+
return flags & CLIENT_PLUGIN_AUTH != 0
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
# [Protocol::HandshakeV10]
|
|
239
|
+
@struct(order=LittleEndian)
|
|
240
|
+
class HandshakeV10:
|
|
241
|
+
protocol_version: Const(10, uint8)
|
|
242
|
+
server_version: CString()
|
|
243
|
+
|
|
244
|
+
# a.k.a. connection id
|
|
245
|
+
thread_id: uint32 = 0
|
|
246
|
+
|
|
247
|
+
# first 8 bytes of the plugin provided data (scramble)
|
|
248
|
+
salt: Bytes(8)
|
|
249
|
+
|
|
250
|
+
# 0x00 byte, terminating the first part of a scramble
|
|
251
|
+
filler: Const(0x00, uint8)
|
|
252
|
+
|
|
253
|
+
# Why does it have to be that complicated?
|
|
254
|
+
# The lower 2 bytes of the Capabilities Flags
|
|
255
|
+
flags_lower: uint16 = 0x000
|
|
256
|
+
|
|
257
|
+
# default server a_protocol_character_set, only the lower 8-bits
|
|
258
|
+
character_set: uint8 = 0x3F
|
|
259
|
+
status_flags: SERVER_STATUS_flags_enum = 0x00
|
|
260
|
+
flags_upper: uint16 = 0x000
|
|
261
|
+
|
|
262
|
+
# length of the combined auth_plugin_data (scramble), if
|
|
263
|
+
# auth_plugin_data_len is > 0
|
|
264
|
+
auth_plugin_data_len: uint8 = 0x00
|
|
265
|
+
reserved: Bytes(10) = b"\0" * 10
|
|
266
|
+
|
|
267
|
+
# Rest of the plugin provided data (scramble), $len=MAX(13, length of
|
|
268
|
+
# auth-plugin-data - 8)
|
|
269
|
+
salt2: Bytes(_auth_plugin_length)
|
|
270
|
+
|
|
271
|
+
# name of the auth_method that the auth_plugin_data belongs to
|
|
272
|
+
auth_plugin_name: CString() // _has_auth_plugin_data
|
|
273
|
+
|
|
274
|
+
def set_flags(self, flags):
|
|
275
|
+
self.flags_lower = flags & 0xFFFF
|
|
276
|
+
self.flags_upper = (flags >> 16) & 0xFFFF
|
|
277
|
+
|
|
278
|
+
def get_flags(self):
|
|
279
|
+
return self.flags_lower | (self.flags_upper << 16)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _client_connect_db(context):
|
|
283
|
+
return context._obj.client_flags & CLIENT_CONNECT_WITH_DB != 0
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
def _client_auth_plugin(context):
|
|
287
|
+
return context._obj.client_flags & CLIENT_PLUGIN_AUTH != 0
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _client_connect_attrs(context):
|
|
291
|
+
return context._obj.client_flags & CLIENT_CONNECT_ATTRS != 0
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
# [Protocol::SSLRequest]
|
|
295
|
+
@struct(order=LittleEndian)
|
|
296
|
+
class SSLRequest:
|
|
297
|
+
capabilities: uint32
|
|
298
|
+
max_packet_size: uint32
|
|
299
|
+
character_set: uint8
|
|
300
|
+
# everything else is not used
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
@struct(order=LittleEndian)
|
|
304
|
+
class ConnectionAttribute:
|
|
305
|
+
# will be stored as bytes rather than string
|
|
306
|
+
key: Prefixed(LengthEncodedInteger)
|
|
307
|
+
value: Prefixed(LengthEncodedInteger)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
# [Protocol::HandshakeResponse]
|
|
311
|
+
@struct(order=LittleEndian)
|
|
312
|
+
class HandshakeResponse:
|
|
313
|
+
client_flags: uint32
|
|
314
|
+
max_packet_size: uint32
|
|
315
|
+
charset: uint8
|
|
316
|
+
|
|
317
|
+
# filler to the size of the handhshake response packet. All 0s.
|
|
318
|
+
filler: Bytes(23) = b"\0" * 23
|
|
319
|
+
|
|
320
|
+
# login user name
|
|
321
|
+
username: CString()
|
|
322
|
+
|
|
323
|
+
# opaque authentication response data generated by Authentication Method
|
|
324
|
+
# indicated by the plugin name field.
|
|
325
|
+
# NOTE: we always assume a length encoded integer
|
|
326
|
+
auth_response: Prefixed(LengthEncodedInteger)
|
|
327
|
+
|
|
328
|
+
# initial database for the connection. This string should be interpreted
|
|
329
|
+
# using the character set indicated by character set field.
|
|
330
|
+
database: CString() // _client_connect_db
|
|
331
|
+
client_plugin_name: CString() // _client_auth_plugin
|
|
332
|
+
|
|
333
|
+
# From the docs:
|
|
334
|
+
# if capabilities & CLIENT_CONNECT_ATTRS {
|
|
335
|
+
# int<lenenc> length of all key-values
|
|
336
|
+
# string<lenenc> Name of the 1st client attribute
|
|
337
|
+
# string<lenenc> Value of the 1st client attribute
|
|
338
|
+
# .. (if more data in length of all key-values, more keys and values parts)
|
|
339
|
+
# }
|
|
340
|
+
conn_attrs: (
|
|
341
|
+
Prefixed(LengthEncodedInteger, ConnectionAttribute[...])
|
|
342
|
+
) // _client_connect_attrs
|
|
343
|
+
# zstd_compression_level is dropped here
|
|
344
|
+
|
|
345
|
+
|
|
346
|
+
# --- MySQL Handler ---
|
|
347
|
+
class MySQLHandler(BaseProtoHandler):
|
|
348
|
+
@property
|
|
349
|
+
def mysql_config(self):
|
|
350
|
+
return self.config.mysql_config
|
|
351
|
+
|
|
352
|
+
def proto_logger(self) -> ProtocolLogger:
|
|
353
|
+
return ProtocolLogger(
|
|
354
|
+
extra={
|
|
355
|
+
"protocol": "MySQL",
|
|
356
|
+
"protocol_color": "medium_orchid3",
|
|
357
|
+
"host": self.client_host,
|
|
358
|
+
"port": self.server.server_address[1],
|
|
359
|
+
}
|
|
360
|
+
)
|
|
361
|
+
|
|
362
|
+
def push(self, payload, prev_id: int | None = None) -> None:
|
|
363
|
+
data = pack(payload)
|
|
364
|
+
packet = Packet(
|
|
365
|
+
payload_length=len(data),
|
|
366
|
+
sequence_id=prev_id + 1 if prev_id is not None else 0,
|
|
367
|
+
payload=data,
|
|
368
|
+
)
|
|
369
|
+
self.send(pack(packet))
|
|
370
|
+
|
|
371
|
+
def recv_unpack(self) -> Any:
|
|
372
|
+
try:
|
|
373
|
+
data = self.recv(8192)
|
|
374
|
+
if not data:
|
|
375
|
+
return None
|
|
376
|
+
|
|
377
|
+
return unpack(Packet, data)
|
|
378
|
+
except OSError:
|
|
379
|
+
raise # will terminate the connection
|
|
380
|
+
|
|
381
|
+
except Exception as e:
|
|
382
|
+
self.logger.error(f"Failed to decode MySQL packet: {e}")
|
|
383
|
+
return None
|
|
384
|
+
|
|
385
|
+
def setup(self) -> None:
|
|
386
|
+
# plugin = self.config.mysql_config.mysql_plugin_name
|
|
387
|
+
plugin = "mysql_clear_password"
|
|
388
|
+
self.logger.display(
|
|
389
|
+
f"New Connection to MySQL Server (requesting handshake for {plugin})"
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
def finish(self) -> None:
|
|
393
|
+
self.logger.debug("Connection to MySQL Server closed")
|
|
394
|
+
|
|
395
|
+
def handle_data(self, data, transport) -> None:
|
|
396
|
+
transport.settimeout(2)
|
|
397
|
+
# Connection Phase:
|
|
398
|
+
# It starts with the client connect()ing to the server which may send a
|
|
399
|
+
# ERR packet and finish the handshake or send a Initial Handshake Packet
|
|
400
|
+
# which the client answers with a Handshake Response Packet.
|
|
401
|
+
flags = 0xFFFFFFFF
|
|
402
|
+
flags ^= CLIENT_CAPABILITY_EXTENSION
|
|
403
|
+
if not self.mysql_config.use_ssl:
|
|
404
|
+
flags ^= CLIENT_SSL
|
|
405
|
+
flags ^= CLIENT_SSL_VERIFY_SERVER_CERT
|
|
406
|
+
|
|
407
|
+
# NOTE: the configuration value won't have any effect here for now
|
|
408
|
+
plugin_name = "mysql_clear_password"
|
|
409
|
+
greeting = HandshakeV10(
|
|
410
|
+
server_version=self.mysql_config.mysql_version,
|
|
411
|
+
thread_id=10,
|
|
412
|
+
salt=b"A" * 8,
|
|
413
|
+
status_flags=SERVER_STATUS_flags_enum.SERVER_STATUS_AUTOCOMMIT,
|
|
414
|
+
auth_plugin_data_len=21, # REVISIT: maybe add automatic calculation here
|
|
415
|
+
salt2=b"A" * 12 + b"\0",
|
|
416
|
+
auth_plugin_name=plugin_name,
|
|
417
|
+
)
|
|
418
|
+
greeting.set_flags(flags)
|
|
419
|
+
self.push(greeting)
|
|
420
|
+
|
|
421
|
+
packet = self.recv_unpack()
|
|
422
|
+
if packet is None:
|
|
423
|
+
return
|
|
424
|
+
|
|
425
|
+
# After this, optionally, the client can request an SSL connection to be established
|
|
426
|
+
# with the Protocol::SSLRequest packet and then the client sends the Protocol::HandshakeResponse
|
|
427
|
+
# packet.
|
|
428
|
+
try:
|
|
429
|
+
ssl_request: SSLRequest = unpack(SSLRequest, packet.payload)
|
|
430
|
+
except Exception as e:
|
|
431
|
+
return self.logger.error(f"Failed to decode MySQL SSLRequest: {e}")
|
|
432
|
+
|
|
433
|
+
if ssl_request.capabilities & CLIENT_SSL != 0:
|
|
434
|
+
if not self.mysql_config.use_ssl:
|
|
435
|
+
self.logger.fail(
|
|
436
|
+
"Client requested SSL, but MySQL server is not configured to use SSL"
|
|
437
|
+
)
|
|
438
|
+
return # terminate connection
|
|
439
|
+
else:
|
|
440
|
+
self.logger.display("Client is requesting upgrade to SSL")
|
|
441
|
+
|
|
442
|
+
self.context = create_tls_context(
|
|
443
|
+
self.mysql_config, self.server, force=True
|
|
444
|
+
)
|
|
445
|
+
if self.context is None:
|
|
446
|
+
return
|
|
447
|
+
|
|
448
|
+
self.request = self.context.wrap_socket(transport, server_side=True)
|
|
449
|
+
packet = self.recv_unpack()
|
|
450
|
+
if packet is None:
|
|
451
|
+
return
|
|
452
|
+
|
|
453
|
+
try:
|
|
454
|
+
response: HandshakeResponse = unpack(HandshakeResponse, packet.payload)
|
|
455
|
+
except Exception as e:
|
|
456
|
+
return self.logger.error(f"Failed to decode MySQL HandshakeResponse: {e}")
|
|
457
|
+
|
|
458
|
+
resp_plugin_name = response.client_plugin_name or plugin_name
|
|
459
|
+
if resp_plugin_name != plugin_name:
|
|
460
|
+
self.logger.fail(
|
|
461
|
+
f"Expected authentication plugin {plugin_name}, but got {resp_plugin_name} from client"
|
|
462
|
+
)
|
|
463
|
+
|
|
464
|
+
method = getattr(self, resp_plugin_name, None)
|
|
465
|
+
if method:
|
|
466
|
+
method(greeting, response)
|
|
467
|
+
else:
|
|
468
|
+
self.logger.debug(f"Unknown authentication plugin: {resp_plugin_name}")
|
|
469
|
+
|
|
470
|
+
def mysql_clear_password(self, greeting: HandshakeV10, response: HandshakeResponse):
|
|
471
|
+
username = response.username
|
|
472
|
+
password = response.auth_response.decode(errors="replace").strip("\x00")
|
|
473
|
+
|
|
474
|
+
extras = {
|
|
475
|
+
conn_attr.key.decode(errors="replace"): conn_attr.value.decode(
|
|
476
|
+
errors="replace"
|
|
477
|
+
)
|
|
478
|
+
for conn_attr in (response.conn_attrs or [])
|
|
479
|
+
}
|
|
480
|
+
if response.database:
|
|
481
|
+
extras["database"] = response.database
|
|
482
|
+
|
|
483
|
+
self.config.db.add_auth(
|
|
484
|
+
client=self.client_address,
|
|
485
|
+
username=username,
|
|
486
|
+
password=password,
|
|
487
|
+
logger=self.logger,
|
|
488
|
+
credtype=_CLEARTEXT,
|
|
489
|
+
extras=OrderedDict(sorted(extras.items())),
|
|
490
|
+
)
|
|
491
|
+
|
|
492
|
+
error = ERR_Packet(
|
|
493
|
+
error_code=self.mysql_config.mysql_error_code,
|
|
494
|
+
error_message=f"#28000{self.mysql_config.mysql_error_message}",
|
|
495
|
+
)
|
|
496
|
+
self.push(error)
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
class MySQLServer(ThreadingTCPServer):
|
|
500
|
+
default_port = 3306
|
|
501
|
+
default_handler_class = MySQLHandler
|
|
502
|
+
service_name = "MySQL"
|
|
@@ -60,9 +60,10 @@ class ServerThread(threading.Thread):
|
|
|
60
60
|
dm_logger.error(
|
|
61
61
|
f"Failed to start server for {self.service_name}: Permission Denied!"
|
|
62
62
|
)
|
|
63
|
-
|
|
63
|
+
else:
|
|
64
|
+
dm_logger.error(f"Failed to start server for {self.service_name}: {e}")
|
|
64
65
|
except Exception as e:
|
|
65
|
-
|
|
66
|
+
dm_logger.exception(f"Failed to start server for {self.service_name}: {e}")
|
|
66
67
|
|
|
67
68
|
|
|
68
69
|
class BaseProtoHandler(BaseRequestHandler, ProtocolLoggerMixin):
|
|
@@ -124,6 +125,14 @@ class BaseProtoHandler(BaseRequestHandler, ProtocolLoggerMixin):
|
|
|
124
125
|
return self.client_address[1]
|
|
125
126
|
|
|
126
127
|
|
|
128
|
+
class BaseServerProtoHandler(BaseProtoHandler):
|
|
129
|
+
def __init__(
|
|
130
|
+
self, config: SessionConfig, server_config, request, client_address, server
|
|
131
|
+
) -> None:
|
|
132
|
+
self.server_config = server_config
|
|
133
|
+
super().__init__(config, request, client_address, server)
|
|
134
|
+
|
|
135
|
+
|
|
127
136
|
class ThreadingUDPServer(socketserver.ThreadingMixIn, socketserver.UDPServer):
|
|
128
137
|
default_port: int
|
|
129
138
|
default_handler_class: type
|
|
@@ -192,7 +201,9 @@ class ThreadingTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
|
|
192
201
|
self.RequestHandlerClass(self.config, request, client_address, self)
|
|
193
202
|
|
|
194
203
|
|
|
195
|
-
def create_tls_context(
|
|
204
|
+
def create_tls_context(
|
|
205
|
+
server_config, server=None, force=False
|
|
206
|
+
) -> ssl.SSLContext | None:
|
|
196
207
|
if getattr(server_config, "use_ssl", False) or force:
|
|
197
208
|
# if defined use ssl
|
|
198
209
|
cert_path = pathlib.Path(str(getattr(server_config, "certfile", None)))
|
|
@@ -35,6 +35,7 @@ dementor/protocols/llmnr.py
|
|
|
35
35
|
dementor/protocols/mdns.py
|
|
36
36
|
dementor/protocols/msrpc.py
|
|
37
37
|
dementor/protocols/mssql.py
|
|
38
|
+
dementor/protocols/mysql.py
|
|
38
39
|
dementor/protocols/netbios.py
|
|
39
40
|
dementor/protocols/ntlm.py
|
|
40
41
|
dementor/protocols/pop3.py
|
|
@@ -50,4 +51,6 @@ docs/README.pypi.md
|
|
|
50
51
|
test/test_dcerpc.py
|
|
51
52
|
test/test_dcom.py
|
|
52
53
|
test/test_epm.py
|
|
53
|
-
test/test_mssql_auth.py
|
|
54
|
+
test/test_mssql_auth.py
|
|
55
|
+
test/test_ssl_client.py
|
|
56
|
+
test/test_ssl_server.py
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
import ssl
|
|
3
|
+
|
|
4
|
+
def create_tls_client(host, port):
|
|
5
|
+
# Set up the socket
|
|
6
|
+
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
7
|
+
|
|
8
|
+
# Connect to the server
|
|
9
|
+
client_socket.connect((host, port))
|
|
10
|
+
|
|
11
|
+
# Send an initial message to the server (before upgrading to TLS)
|
|
12
|
+
client_socket.sendall("Hello Server, I want to start a TLS connection!".encode('utf-8'))
|
|
13
|
+
|
|
14
|
+
# Wait for the server's response (not encrypted)
|
|
15
|
+
response = client_socket.recv(1024).decode('utf-8')
|
|
16
|
+
print(f"Received from server: {response}")
|
|
17
|
+
|
|
18
|
+
# Upgrade the connection to TLS
|
|
19
|
+
print("Upgrading to TLS...")
|
|
20
|
+
tls_socket = ssl.wrap_socket(client_socket, keyfile=None, certfile=None, server_side=False, ssl_version=ssl.PROTOCOL_TLS)
|
|
21
|
+
|
|
22
|
+
# Send a message securely
|
|
23
|
+
tls_socket.sendall("Hello from the secure client!".encode('utf-8'))
|
|
24
|
+
|
|
25
|
+
# Close the TLS connection after communication
|
|
26
|
+
tls_socket.close()
|
|
27
|
+
|
|
28
|
+
if __name__ == '__main__':
|
|
29
|
+
create_tls_client('localhost', 5000)
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import socket
|
|
2
|
+
import ssl
|
|
3
|
+
|
|
4
|
+
def create_tls_server(host, port):
|
|
5
|
+
# Set up the socket
|
|
6
|
+
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
7
|
+
server_socket.bind((host, port))
|
|
8
|
+
server_socket.listen(1)
|
|
9
|
+
print(f"Server listening on {host}:{port}...")
|
|
10
|
+
|
|
11
|
+
# Accept client connection
|
|
12
|
+
client_socket, client_address = server_socket.accept()
|
|
13
|
+
print(f"Connection from {client_address}")
|
|
14
|
+
|
|
15
|
+
# Perform the initial handshake (not encrypted)
|
|
16
|
+
initial_message = client_socket.recv(1024).decode('utf-8')
|
|
17
|
+
print(f"Received initial message: {initial_message}")
|
|
18
|
+
|
|
19
|
+
# Upgrade the connection to TLS
|
|
20
|
+
print("Upgrading to TLS...")
|
|
21
|
+
tls_socket = ssl.wrap_socket(client_socket, server_side=True, keyfile="server.key", certfile="server.crt")
|
|
22
|
+
|
|
23
|
+
# Now communicate securely
|
|
24
|
+
tls_socket.sendall("Hello, TLS-secure client!".encode('utf-8'))
|
|
25
|
+
|
|
26
|
+
# Close the TLS connection after communication
|
|
27
|
+
tls_socket.close()
|
|
28
|
+
server_socket.close()
|
|
29
|
+
|
|
30
|
+
if __name__ == '__main__':
|
|
31
|
+
create_tls_server('localhost', 5000)
|
|
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
|