requests-credssp 2.1.0__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.
- requests_credssp/__init__.py +14 -0
- requests_credssp/credssp.py +306 -0
- requests_credssp/exceptions.py +54 -0
- requests_credssp/py.typed +0 -0
- requests_credssp-2.1.0.dist-info/METADATA +300 -0
- requests_credssp-2.1.0.dist-info/RECORD +9 -0
- requests_credssp-2.1.0.dist-info/WHEEL +5 -0
- requests_credssp-2.1.0.dist-info/licenses/LICENSE +21 -0
- requests_credssp-2.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Copyright: (c) 2018, Jordan Borean (@jborean93) <jborean93@gmail.com>
|
|
2
|
+
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from logging import NullHandler
|
|
8
|
+
|
|
9
|
+
from requests_credssp.credssp import HttpCredSSPAuth
|
|
10
|
+
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
logger.addHandler(NullHandler())
|
|
13
|
+
|
|
14
|
+
__all__ = ["HttpCredSSPAuth"]
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
# Copyright: (c) 2018, Jordan Borean (@jborean93) <jborean93@gmail.com>
|
|
2
|
+
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import base64
|
|
7
|
+
import collections.abc as c
|
|
8
|
+
import logging
|
|
9
|
+
import re
|
|
10
|
+
import ssl
|
|
11
|
+
import sys
|
|
12
|
+
import typing as t
|
|
13
|
+
from urllib.parse import urlparse
|
|
14
|
+
|
|
15
|
+
import spnego
|
|
16
|
+
import spnego.exceptions
|
|
17
|
+
import spnego.tls
|
|
18
|
+
from requests import PreparedRequest, Response
|
|
19
|
+
from requests.auth import AuthBase
|
|
20
|
+
|
|
21
|
+
from requests_credssp.exceptions import AuthenticationException, InvalidConfigurationException, NTStatusException
|
|
22
|
+
|
|
23
|
+
log = logging.getLogger(__name__)
|
|
24
|
+
|
|
25
|
+
_AuthMechanism = t.Literal["auto", "ntlm", "kerberos"]
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class CredSSPContext:
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
hostname: str,
|
|
32
|
+
username: str,
|
|
33
|
+
password: str,
|
|
34
|
+
auth_mechanism: _AuthMechanism = "auto",
|
|
35
|
+
disable_tlsv1_2: bool = False,
|
|
36
|
+
minimum_version: int = 2,
|
|
37
|
+
) -> None:
|
|
38
|
+
self.hostname = hostname
|
|
39
|
+
self.username = username
|
|
40
|
+
self.password = password
|
|
41
|
+
self.auth_mechanism: _AuthMechanism = auth_mechanism
|
|
42
|
+
self.minimum_version = minimum_version
|
|
43
|
+
|
|
44
|
+
credential = spnego.Password(username=username, password=password)
|
|
45
|
+
|
|
46
|
+
auth_kwargs: dict[str, t.Any] = {}
|
|
47
|
+
if auth_mechanism == "kerberos" or auth_mechanism == "ntlm":
|
|
48
|
+
auth_kwargs["credssp_negotiate_context"] = spnego.client(
|
|
49
|
+
credential,
|
|
50
|
+
hostname=hostname,
|
|
51
|
+
service="HTTP",
|
|
52
|
+
protocol=auth_mechanism,
|
|
53
|
+
)
|
|
54
|
+
elif auth_mechanism != "auto":
|
|
55
|
+
# Unreachable for type checked callers but still validated at runtime.
|
|
56
|
+
raise InvalidConfigurationException( # pyright: ignore[reportUnreachable]
|
|
57
|
+
f"Invalid auth_mechanism supplied {auth_mechanism}, must be auto, ntlm, or kerberos"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
if disable_tlsv1_2:
|
|
61
|
+
tls_context = spnego.tls.default_tls_context(usage="initiate")
|
|
62
|
+
|
|
63
|
+
# The minimum_version field requires OpenSSL 1.1.0g or newer, fallback to the deprecated method of setting
|
|
64
|
+
# the OP_NO_* options.
|
|
65
|
+
tls_version = getattr(ssl, "TLSVersion", None)
|
|
66
|
+
if hasattr(tls_context.context, "minimum_version") and tls_version:
|
|
67
|
+
tls_context.context.minimum_version = tls_version.TLSv1
|
|
68
|
+
else:
|
|
69
|
+
tls_context.context.options &= ~ssl.OP_NO_TLSv1 & ~ssl.OP_NO_TLSv1_1
|
|
70
|
+
|
|
71
|
+
auth_kwargs["credssp_tls_context"] = tls_context
|
|
72
|
+
|
|
73
|
+
if minimum_version > 2:
|
|
74
|
+
auth_kwargs["credssp_min_protocol"] = minimum_version
|
|
75
|
+
|
|
76
|
+
self._context = spnego.client(
|
|
77
|
+
credential,
|
|
78
|
+
hostname=hostname,
|
|
79
|
+
service="HTTP",
|
|
80
|
+
protocol="credssp",
|
|
81
|
+
**auth_kwargs,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def tls_connection(self) -> ssl.SSLObject | None:
|
|
86
|
+
ssl_object: ssl.SSLObject | None = self._context.get_extra_info("ssl_object") if self._context else None
|
|
87
|
+
if ssl_object is None:
|
|
88
|
+
return None
|
|
89
|
+
|
|
90
|
+
# Older pywinrm and pypsrp still use this to get the cipher for wrapping.
|
|
91
|
+
# Until they've been migrated over to (un)wrap_winrm it needs to stay.
|
|
92
|
+
def get_cipher_name() -> str | None:
|
|
93
|
+
details = ssl_object.cipher()
|
|
94
|
+
if details:
|
|
95
|
+
return details[0]
|
|
96
|
+
|
|
97
|
+
return None
|
|
98
|
+
|
|
99
|
+
# Set through setattr as SSLObject has no such attribute for the type checker.
|
|
100
|
+
setattr(ssl_object, "get_cipher_name", get_cipher_name) # noqa: B010
|
|
101
|
+
|
|
102
|
+
return ssl_object
|
|
103
|
+
|
|
104
|
+
def credssp_generator(self) -> c.Generator[tuple[bytes, str], bytes, None]:
|
|
105
|
+
"""
|
|
106
|
+
[MS-CSSP] 3.1.5 Processing Events and Sequencing Rules
|
|
107
|
+
https://msdn.microsoft.com/en-us/library/cc226791.aspx
|
|
108
|
+
|
|
109
|
+
Generator function that yields each CredSSP token to sent to the
|
|
110
|
+
server. CredSSP has multiple steps that must be run for the client to
|
|
111
|
+
successfully authenticate with the server and delegate the credentials.
|
|
112
|
+
"""
|
|
113
|
+
in_token: bytes | None = None
|
|
114
|
+
while not self._context.complete:
|
|
115
|
+
try:
|
|
116
|
+
out_token = self._context.step(in_token)
|
|
117
|
+
except spnego.exceptions.SpnegoError as e:
|
|
118
|
+
# For backwards compatibility, base_error is not set if an authentication failure happened. If it is
|
|
119
|
+
# set then it's treated as an error from the peer and raises as an NTStatusException.
|
|
120
|
+
if e.base_error is None or e.nt_status == 0xFFFFFFFF:
|
|
121
|
+
raise AuthenticationException(str(e)) from e
|
|
122
|
+
else:
|
|
123
|
+
# Value in SpnegoError is signed but NTStatusException expects an unsigned value
|
|
124
|
+
nt_status = e.nt_status
|
|
125
|
+
if nt_status < 0:
|
|
126
|
+
b_nt_status = nt_status.to_bytes(4, byteorder=sys.byteorder, signed=True)
|
|
127
|
+
nt_status = int.from_bytes(b_nt_status, byteorder=sys.byteorder, signed=False)
|
|
128
|
+
raise NTStatusException(nt_status, str(e)) from e
|
|
129
|
+
|
|
130
|
+
# The initiator only returns no token once the exchange is complete.
|
|
131
|
+
if out_token is None:
|
|
132
|
+
break
|
|
133
|
+
|
|
134
|
+
current_step: str = self._context.get_extra_info("auth_stage", "Unknown")
|
|
135
|
+
in_token = yield out_token, current_step
|
|
136
|
+
|
|
137
|
+
def wrap(
|
|
138
|
+
self,
|
|
139
|
+
data: bytes,
|
|
140
|
+
) -> bytes:
|
|
141
|
+
"""
|
|
142
|
+
Encrypts the data in preparation for sending to the server. The data is
|
|
143
|
+
encrypted using the TLS channel negotiated between the client and the
|
|
144
|
+
server.
|
|
145
|
+
|
|
146
|
+
:param data: a byte string of data to encrypt
|
|
147
|
+
:return: a byte string of the encrypted data
|
|
148
|
+
"""
|
|
149
|
+
return self._context.wrap(data).data
|
|
150
|
+
|
|
151
|
+
def wrap_winrm(
|
|
152
|
+
self,
|
|
153
|
+
data: bytes,
|
|
154
|
+
) -> tuple[bytes, bytes, int]:
|
|
155
|
+
return self._context.wrap_winrm(data)
|
|
156
|
+
|
|
157
|
+
def unwrap(
|
|
158
|
+
self,
|
|
159
|
+
data: bytes,
|
|
160
|
+
) -> bytes:
|
|
161
|
+
"""
|
|
162
|
+
Decrypts the data send by the server using the TLS channel negotiated
|
|
163
|
+
between the client and the server.
|
|
164
|
+
|
|
165
|
+
:param encrypted_data: the byte string of the encrypted data
|
|
166
|
+
:return: a byte string of the decrypted data
|
|
167
|
+
"""
|
|
168
|
+
return self._context.unwrap(data).data
|
|
169
|
+
|
|
170
|
+
def unwrap_winrm(
|
|
171
|
+
self,
|
|
172
|
+
header: bytes,
|
|
173
|
+
data: bytes,
|
|
174
|
+
) -> bytes:
|
|
175
|
+
return self._context.unwrap_winrm(header, data)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class HttpCredSSPAuth(AuthBase):
|
|
179
|
+
def __init__(
|
|
180
|
+
self,
|
|
181
|
+
username: str,
|
|
182
|
+
password: str,
|
|
183
|
+
auth_mechanism: _AuthMechanism = "auto",
|
|
184
|
+
disable_tlsv1_2: bool = False,
|
|
185
|
+
minimum_version: int = 2,
|
|
186
|
+
hostname_override: str | None = None,
|
|
187
|
+
) -> None:
|
|
188
|
+
"""
|
|
189
|
+
Initialises the CredSSP auth handler for dealing with requests.
|
|
190
|
+
|
|
191
|
+
:param username: The username including domain to auth with
|
|
192
|
+
(DOMAIN\\USER or USER@DOMAIN.LOCAL), use the user@DOMAIN.LOCAL form
|
|
193
|
+
when wanting to use Kerberos.
|
|
194
|
+
:param password: The password for the user above to delegate to the
|
|
195
|
+
server
|
|
196
|
+
:param auth_mechanism: The authentication mechanism to use
|
|
197
|
+
(default is auto): auto, ntlm, kerberos
|
|
198
|
+
:param disable_tlsv1_2: Disable TLSv1.2 authentication and revert back
|
|
199
|
+
to TLSv1.
|
|
200
|
+
:param minimum_version: The minimum server version that can be
|
|
201
|
+
authenticated against, set to 5 to ensure CVE 2018-0886 is always
|
|
202
|
+
mitigated and older hosts can't be used.
|
|
203
|
+
:param hostname_override: The hostname to use when building the
|
|
204
|
+
service principal name (HTTP/<hostname>) instead of the hostname
|
|
205
|
+
in the request URL. The context is still stored in contexts under
|
|
206
|
+
the URL hostname.
|
|
207
|
+
"""
|
|
208
|
+
self.username = username
|
|
209
|
+
self.password = password
|
|
210
|
+
self.auth_mechanism: _AuthMechanism = auth_mechanism
|
|
211
|
+
self.disable_tlsv1_2 = disable_tlsv1_2
|
|
212
|
+
self.minimum_version = minimum_version
|
|
213
|
+
self.hostname_override = hostname_override
|
|
214
|
+
self.contexts: dict[str, CredSSPContext] = {}
|
|
215
|
+
|
|
216
|
+
def __call__(self, request: PreparedRequest) -> PreparedRequest:
|
|
217
|
+
request.headers["Connection"] = "Keep-Alive"
|
|
218
|
+
# register_hook is untyped in types-requests.
|
|
219
|
+
request.register_hook("response", self.response_hook) # pyright: ignore[reportUnknownMemberType]
|
|
220
|
+
|
|
221
|
+
return request
|
|
222
|
+
|
|
223
|
+
def response_hook(self, response: Response, **kwargs: t.Any) -> Response:
|
|
224
|
+
if response.status_code == 401:
|
|
225
|
+
self._check_credssp_supported(response)
|
|
226
|
+
response = self.handle_401(response, **kwargs)
|
|
227
|
+
|
|
228
|
+
return response
|
|
229
|
+
|
|
230
|
+
def handle_401(self, response: Response, **kwargs: t.Any) -> Response:
|
|
231
|
+
host = urlparse(response.url).hostname or ""
|
|
232
|
+
context = CredSSPContext(
|
|
233
|
+
self.hostname_override or host,
|
|
234
|
+
self.username,
|
|
235
|
+
self.password,
|
|
236
|
+
self.auth_mechanism,
|
|
237
|
+
self.disable_tlsv1_2,
|
|
238
|
+
self.minimum_version,
|
|
239
|
+
)
|
|
240
|
+
self.contexts[host] = context
|
|
241
|
+
|
|
242
|
+
credssp_gen = context.credssp_generator()
|
|
243
|
+
credssp_regex = re.compile(r"CredSSP ([^,\s]*)$", re.I)
|
|
244
|
+
|
|
245
|
+
# loop through the CredSSP generator to exchange the tokens between the
|
|
246
|
+
# client and the server until either an error occurs or we reached the
|
|
247
|
+
# end of the exchange
|
|
248
|
+
out_token, step_name = next(credssp_gen)
|
|
249
|
+
while True:
|
|
250
|
+
try:
|
|
251
|
+
# consume content and release the original connection to allow
|
|
252
|
+
# the new request to reuse the same one.
|
|
253
|
+
_ = response.content
|
|
254
|
+
response.raw.release_conn()
|
|
255
|
+
|
|
256
|
+
# create the request with the CredSSP token present
|
|
257
|
+
request = response.request.copy()
|
|
258
|
+
self._set_credssp_token(request, out_token)
|
|
259
|
+
|
|
260
|
+
# send the request and get the response
|
|
261
|
+
response = response.connection.send(request, **kwargs)
|
|
262
|
+
if response.status_code == 200:
|
|
263
|
+
break
|
|
264
|
+
|
|
265
|
+
# attempt to retrieve the CredSSP token response
|
|
266
|
+
in_token = self._get_credssp_token(response, credssp_regex, step_name)
|
|
267
|
+
|
|
268
|
+
# send the input CredSSP token and get the next output token
|
|
269
|
+
out_token, step_name = credssp_gen.send(in_token)
|
|
270
|
+
except StopIteration:
|
|
271
|
+
break
|
|
272
|
+
|
|
273
|
+
return response
|
|
274
|
+
|
|
275
|
+
@staticmethod
|
|
276
|
+
def _check_credssp_supported(response: Response) -> None:
|
|
277
|
+
auth_supported = response.headers.get("www-authenticate", "")
|
|
278
|
+
if "CREDSSP" not in auth_supported.upper():
|
|
279
|
+
error_msg = (
|
|
280
|
+
"The server did not respond with CredSSP as an available authentication method - "
|
|
281
|
+
+ f"actual: '{auth_supported}'"
|
|
282
|
+
)
|
|
283
|
+
raise AuthenticationException(error_msg)
|
|
284
|
+
|
|
285
|
+
@staticmethod
|
|
286
|
+
def _set_credssp_token(request: PreparedRequest, token: bytes) -> None:
|
|
287
|
+
encoded_token = base64.b64encode(token).decode()
|
|
288
|
+
credssp_header = "CredSSP " + encoded_token
|
|
289
|
+
request.headers["Authorization"] = credssp_header
|
|
290
|
+
|
|
291
|
+
@staticmethod
|
|
292
|
+
def _get_credssp_token(response: Response, pattern: re.Pattern[str], step_name: str) -> bytes:
|
|
293
|
+
auth_header = response.headers.get("www-authenticate", "")
|
|
294
|
+
token_match = pattern.search(auth_header)
|
|
295
|
+
|
|
296
|
+
if not token_match:
|
|
297
|
+
error_msg = f"Server did not respond with a CredSSP token after step {step_name} - actual '{auth_header}'"
|
|
298
|
+
if response.status_code == 401:
|
|
299
|
+
error_msg += (
|
|
300
|
+
". The server rejected the authentication with 401 Unauthorized, check the username, password "
|
|
301
|
+
+ "and that the account is allowed to connect"
|
|
302
|
+
)
|
|
303
|
+
raise AuthenticationException(error_msg)
|
|
304
|
+
|
|
305
|
+
token = token_match.group(1)
|
|
306
|
+
return base64.b64decode(token)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# Copyright: (c) 2018, Jordan Borean (@jborean93) <jborean93@gmail.com>
|
|
2
|
+
# MIT License (see LICENSE or https://opensource.org/licenses/MIT)
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class NtStatusCodes:
|
|
8
|
+
STATUS_SUCCESS = 0x00000000
|
|
9
|
+
STATUS_INVALID_HANDLE = 0xC0000008
|
|
10
|
+
STATUS_LOGON_FAILURE = 0xC000006D
|
|
11
|
+
STATUS_ACCOUNT_RESTRICTION = 0xC000006E
|
|
12
|
+
STATUS_INVALID_LOGON_HOURS = 0xC000006F
|
|
13
|
+
STATUS_INVALID_WORKSTATION = 0xC0000070
|
|
14
|
+
STATUS_PASSWORD_EXPIRED = 0xC0000071
|
|
15
|
+
STATUS_ACCOUNT_DISABLED = 0xC0000072
|
|
16
|
+
STATUS_INSUFFICIENT_RESOURCES = 0xC000009A
|
|
17
|
+
STATUS_NOT_SUPPORTED = 0xC00000BB
|
|
18
|
+
|
|
19
|
+
# HRESULT Values - used in SPNEGO responses
|
|
20
|
+
SEC_E_INVALID_TOKEN = 0x80090308
|
|
21
|
+
SEC_E_MESSAGE_ALTERED = 0x8009030F
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class AuthenticationException(Exception):
|
|
25
|
+
# Authentication was rejected by the server
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class InvalidConfigurationException(Exception):
|
|
30
|
+
# Config option is invalid and illegal
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class NTStatusException(Exception):
|
|
35
|
+
# Exception when receiving NTSTATUS codes in the TSRequest from the server
|
|
36
|
+
|
|
37
|
+
@property
|
|
38
|
+
def status(self) -> int:
|
|
39
|
+
return self.args[0]
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def message(self) -> str:
|
|
43
|
+
status_name = None
|
|
44
|
+
for status, status_value in vars(NtStatusCodes).items():
|
|
45
|
+
if status_value == self.status:
|
|
46
|
+
status_name = status
|
|
47
|
+
break
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
f"Received error status from the server: ({self.status}) {status_name or 'UNKOWN_STATUS'} 0x{self.status:x}"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def __str__(self) -> str:
|
|
54
|
+
return self.message
|
|
File without changes
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: requests-credssp
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
Summary: HTTPS CredSSP authentication with the requests library.
|
|
5
|
+
Author-email: Jordan Borean <jborean93@gmail.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/jborean93/requests-credssp
|
|
8
|
+
Keywords: authentication,auth,microsoft,credssp,winrm
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: cryptography
|
|
20
|
+
Requires-Dist: pyspnego>=0.5.0
|
|
21
|
+
Requires-Dist: requests>=2.0.0
|
|
22
|
+
Provides-Extra: kerberos
|
|
23
|
+
Requires-Dist: pyspnego[kerberos]; sys_platform != "win32" and extra == "kerberos"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# requests-credssp
|
|
27
|
+
|
|
28
|
+
[](https://github.com/jborean93/requests-credssp/actions/workflows/ci.yml)
|
|
29
|
+
[](https://codecov.io/gh/jborean93/requests-credssp)
|
|
30
|
+
[](https://badge.fury.io/py/requests-credssp)
|
|
31
|
+
[](https://github.com/jborean93/request-credssp/blob/master/LICENSE)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
## About this library
|
|
35
|
+
|
|
36
|
+
This package allows for HTTPS CredSSP authentication using the requests
|
|
37
|
+
library. CredSSP is a Microsoft authentication that allows your credentials to
|
|
38
|
+
be delegated to a server giving you double hop authentication.
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
This library supports the following CredSSP features
|
|
44
|
+
|
|
45
|
+
* Protocol version 2 to 6
|
|
46
|
+
* Initial authentication with NTLM or Kerberos
|
|
47
|
+
* Message encryption support using the `wrap` and `unwrap` functions
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
## Requirements
|
|
51
|
+
|
|
52
|
+
The following Python libraries are required;
|
|
53
|
+
|
|
54
|
+
* Python 3.11+
|
|
55
|
+
* [cryptography](https://github.com/pyca/cryptography)
|
|
56
|
+
* [pyspnego](https://github.com/jborean93/pyspnego)
|
|
57
|
+
* [requests>=2.0.0](https://pypi.python.org/pypi/requests)
|
|
58
|
+
* For Kerberos authentication on Unix [python-gssapi](https://github.com/pythongssapi/python-gssapi) and [pykrb5](https://github.com/jborean93/pykrb5) installed with `requests-credssp[kerberos]`
|
|
59
|
+
|
|
60
|
+
By default, this library can authenticate with a Windows host using NTLM
|
|
61
|
+
messages, if Kerberos authentication is desired, please read the below.
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
## Installation
|
|
65
|
+
|
|
66
|
+
To install requests-credssp, simply run
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
pip install requests-credssp
|
|
70
|
+
|
|
71
|
+
# to install the optional Kerberos functionality, run (see below)
|
|
72
|
+
pip install requests-credssp[kerberos]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
### Kerberos on Linux
|
|
77
|
+
|
|
78
|
+
To add support for Kerberos authentication on a non-Windows host, the Kerberos
|
|
79
|
+
system headers must be installed and the `python-gssapi` library installed. To
|
|
80
|
+
install the Kerberos system headers you can install the following packages;
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
# Via Yum (Centos RHEL)
|
|
84
|
+
yum -y install python-devel krb5-devel krb5-libs krb5-workstation
|
|
85
|
+
|
|
86
|
+
# Via Dnf (Fedora)
|
|
87
|
+
dnf -y install python-devel krb5-devel krb5-libs krb5-workstation
|
|
88
|
+
|
|
89
|
+
# Via Apt (Ubuntu)
|
|
90
|
+
apt-get -y install python-dev libkrb5-dev krb5-user
|
|
91
|
+
|
|
92
|
+
# Via Portage (Gentoo)
|
|
93
|
+
emerge -av app-crypt/mit-krb5
|
|
94
|
+
emerge -av dev-python/setuptools
|
|
95
|
+
|
|
96
|
+
# Via pkg (FreeBSD)
|
|
97
|
+
sudo pkg install security/krb5
|
|
98
|
+
|
|
99
|
+
# Via OpenCSW (Solaris)
|
|
100
|
+
pkgadd -d http://get.opencsw.org/now
|
|
101
|
+
/opt/csw/bin/pkgutil -U
|
|
102
|
+
/opt/csw/bin/pkgutil -y -i libkrb5_3
|
|
103
|
+
|
|
104
|
+
# Via Pacman (Arch Linux)
|
|
105
|
+
pacman -S krb5
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Once installed, the Python Kerberos libraries can be installed with
|
|
109
|
+
|
|
110
|
+
```
|
|
111
|
+
pip install requests-credssp[kerberos]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Once installed, the file `/etc/krb5.conf` should be configured so it can talk
|
|
115
|
+
with the Kerberos KDC.
|
|
116
|
+
|
|
117
|
+
To add proper SPNEGO support with `python-gssapi`, the
|
|
118
|
+
[gss-ntlmssp](https://github.com/simo5/gss-ntlmssp) should also be installed
|
|
119
|
+
which adds NTLM as a supported GSSAPI mechanism required for proper SPNEGO
|
|
120
|
+
interoperability with Windows. This package can be installed with;
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
# Via Yum (Centos RHEL) - requires epel-release
|
|
124
|
+
yum -y install epel-release
|
|
125
|
+
yum -y install gssntlmssp
|
|
126
|
+
|
|
127
|
+
# Via Dnf (Fedora)
|
|
128
|
+
dnf -y install gssntlmssp
|
|
129
|
+
|
|
130
|
+
# Via Apt (Ubuntu)
|
|
131
|
+
apt-get -y install gss-ntlmssp
|
|
132
|
+
|
|
133
|
+
# Via Pacman (Arch Linux)
|
|
134
|
+
pacman -S gss-ntlmssp
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Additional Info
|
|
138
|
+
|
|
139
|
+
The CredSSP protocol is quite complex and uses a lot of other protocols or
|
|
140
|
+
standards to work properly. This unfortunately means some older hosts or
|
|
141
|
+
settings are incompatible or require some workarounds to get working. Currently
|
|
142
|
+
you can configure the following settings when initialising the CredSSP class;
|
|
143
|
+
|
|
144
|
+
* `auth_mechanism`: The authentication mechanism to use initially, default is `auto`
|
|
145
|
+
* `disable_tlsv1_2`: Whether to disable `TLSv1.2` support and work with older protocols like `TLSv1.0`, default is `False`
|
|
146
|
+
* `minimum_version`: The minimum CredSSP server version that is required by the client, default is `2`
|
|
147
|
+
* `hostname_override`: The hostname to use for the service principal name (`HTTP/<hostname>`) instead of the hostname in the URL, default is `None`
|
|
148
|
+
|
|
149
|
+
### Authentication Mechanisms
|
|
150
|
+
|
|
151
|
+
Part of the CredSSP protocol is to authenticate the user's credentials using
|
|
152
|
+
the SPNEGO protocol. The SPNEGO protocol is also called `Negotiate` and is
|
|
153
|
+
able to negotiate a common protocol between the client and the server which
|
|
154
|
+
can currently be either `NTLM` or `Kerberos`. Kerberos is a tricky protocol
|
|
155
|
+
to have set up but should be used wherever it is possible as NTLM uses older
|
|
156
|
+
standards that are considered broken from a security perspective.
|
|
157
|
+
|
|
158
|
+
Due to historical decisions and that Kerberos is not always available by
|
|
159
|
+
default, the base install of `requests-credssp` will only work with `NTLM`.
|
|
160
|
+
When the Kerberos packages are installed and configured, `requests-credssp`
|
|
161
|
+
will automatically attempt to use `Kerberos` if possible but fall back to
|
|
162
|
+
`NTLM` if it fails like it would with `SPNEGO`. If you wish to force either
|
|
163
|
+
`Kerberos` or `NTLM` instead of relying on the `SPNEGO` mechanism, you can set
|
|
164
|
+
`auth_mechanism=<auth_mech>` when creating `HttpCredSSPAuth` like so;
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
import requests
|
|
168
|
+
from requests_credssp import HttpCredSSPAuth
|
|
169
|
+
|
|
170
|
+
# use SPNEGO (default if omitted)
|
|
171
|
+
credssp_auth = HttpCredSSPAuth('domain\\user', 'password',
|
|
172
|
+
auth_mechanism='auto')
|
|
173
|
+
|
|
174
|
+
# only allow Kerberos
|
|
175
|
+
credssp_auth = HttpCredSSPAuth('user@REALM.COM', 'password',
|
|
176
|
+
auth_mechanism='kerberos')
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
# only allow NTLM
|
|
180
|
+
credssp_auth = HttpCredSSPAuth('domain\\user', 'password',
|
|
181
|
+
auth_mechanism='ntlm')
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
r = requests.get("https://server:5986/wsman", auth=credssp_auth)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### TLS Protocol Versions
|
|
188
|
+
|
|
189
|
+
As CredSSP uses TLS to encrypt the tokens that are transferred between the
|
|
190
|
+
client and the server, it is succeptible to differing implementations of SSL.
|
|
191
|
+
By default, `requests-credssp` will work with server's that offer TLSv1.2
|
|
192
|
+
but older Windows hosts that do not support this newer protocol version will
|
|
193
|
+
|
|
194
|
+
TLSv1.2 was added in Windows Server 2012 and Windows 8 where older hosts need
|
|
195
|
+
an optional update to be installed for it to work. If this update cannot be
|
|
196
|
+
installed or you are willing to accept the risks of using the older TLS
|
|
197
|
+
protocols, `requests-credssp` can be set to disable TLSv1.2 and work with
|
|
198
|
+
older protocols like so;
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
import requests
|
|
203
|
+
from requests_credssp import HttpCredSSPAuth
|
|
204
|
+
|
|
205
|
+
credssp_auth = HttpCredSSPAuth("domain\\user", "password", disable_tlsv1_2=True)
|
|
206
|
+
r = requests.get("https://server:5986/wsman", auth=credssp_auth)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### CredSSP Protocol Versions
|
|
210
|
+
|
|
211
|
+
Recently Microsoft has released a security update to CredSSP to mitigate
|
|
212
|
+
[CVE 2018-0886](https://support.microsoft.com/en-us/help/4093492/credssp-updates-for-cve-2018-0886-march-13-2018).
|
|
213
|
+
The update added 2 new CredSSP protocol versions, `5` and `6` which changes
|
|
214
|
+
the way the client and server authenticate each other. While these changes are
|
|
215
|
+
transparent to someone who uses this library, it may be prudent to set the
|
|
216
|
+
minimum version that this client would authenticate with. This means that any
|
|
217
|
+
older server's who have not been patched for this vulnerability will be
|
|
218
|
+
rejected.
|
|
219
|
+
|
|
220
|
+
To set a minimum protocol version that will only allow servers that have been
|
|
221
|
+
patched for `CVE 2018-0886`, set `minimum_version=5` when creating
|
|
222
|
+
`HttpCredSSPAuth` like so;
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
import requests
|
|
226
|
+
from requests_credssp import HttpCredSSPAuth
|
|
227
|
+
|
|
228
|
+
credssp_auth = HttpCredSSPAuth('domain\\user', 'password', minimum_version=5)
|
|
229
|
+
r = requests.get("https://server:5986/wsman", auth=credssp_auth)
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Message Encryption
|
|
233
|
+
|
|
234
|
+
You can use this library to encrypt and decrypt messages sent to and from the
|
|
235
|
+
server. Message encryption is done over the TLS channel that was negotiated in
|
|
236
|
+
the authentication stage. The below is an example of encrypting and decrypting
|
|
237
|
+
messages, note this is only a basic example and not a working script and the
|
|
238
|
+
actual implementation depends on the protocol that is reading the messages.
|
|
239
|
+
|
|
240
|
+
```python
|
|
241
|
+
import requests
|
|
242
|
+
from requests_credssp import HttpCredSSPAuth
|
|
243
|
+
|
|
244
|
+
# build the auth request and sent an empty message to authenticate
|
|
245
|
+
hostname = "server"
|
|
246
|
+
session = requests.Session()
|
|
247
|
+
session.auth = HttpCredSSPAuth("domain\\user", "password")
|
|
248
|
+
|
|
249
|
+
request = requests.Request("POST", "https://%s:5986/wsman" % server, data=None)
|
|
250
|
+
prepared_request = self.session.prepare_request(request)
|
|
251
|
+
response = session.send(prepared_request)
|
|
252
|
+
|
|
253
|
+
context = session.auth.contexts[hostname]
|
|
254
|
+
# encrypt the message using the wrap command
|
|
255
|
+
message = b"hi server"
|
|
256
|
+
encrypted_message = context.wrap(message)
|
|
257
|
+
|
|
258
|
+
# send the encrypted message and get the encrypted response
|
|
259
|
+
request = requests.Request("POST", "https://server:5986/wsman", data=encrypted_message)
|
|
260
|
+
prepared_request = self.session.prepare_request(request)
|
|
261
|
+
response = session.send(prepared_request)
|
|
262
|
+
|
|
263
|
+
# decrypt the encrypted response from the server
|
|
264
|
+
encrypted_response = response.content
|
|
265
|
+
decrypted_response = context.unwrap(encrypted_response)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
## Logging
|
|
270
|
+
|
|
271
|
+
This library uses the standard Python logging facilities. Log messages are
|
|
272
|
+
logged to the `requests_credssp` and `requests_credssp.credssp` named loggers.
|
|
273
|
+
|
|
274
|
+
If you are receiving any errors or wish to debug the CredSSP process you should
|
|
275
|
+
enable DEBUG level logs. These logs show fine grain information such as the
|
|
276
|
+
protocol and cipher negotiated and each CredSSP token used in the
|
|
277
|
+
authentication process.
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
## Development
|
|
281
|
+
|
|
282
|
+
Dependencies are managed with [uv](https://docs.astral.sh/uv/):
|
|
283
|
+
|
|
284
|
+
```bash
|
|
285
|
+
uv sync
|
|
286
|
+
uv run pytest
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Run `uv lock` after changing the dependencies in `pyproject.toml`.
|
|
290
|
+
|
|
291
|
+
## Releasing
|
|
292
|
+
|
|
293
|
+
1. Set the version in `pyproject.toml` and push a matching `v*` tag.
|
|
294
|
+
2. CI builds and tests the release and attaches it to a draft GitHub release.
|
|
295
|
+
3. Publishing the draft release uploads it to PyPI.
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
## Backlog
|
|
299
|
+
|
|
300
|
+
* Add support for different credential types like smart card and redirected credentials
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
requests_credssp/__init__.py,sha256=_beUFg-dwcVYZNBlwCgaZTI5y8NLEuSvD8S14eFh3rc,379
|
|
2
|
+
requests_credssp/credssp.py,sha256=NAGMtugFIDK3UYTXSVY8LybQMy7CEL9wK9SiaLD8U7Q,11940
|
|
3
|
+
requests_credssp/exceptions.py,sha256=na0gyaIna2_q25ng_5036kXf0a9z--lT-m2G01dU56k,1571
|
|
4
|
+
requests_credssp/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
requests_credssp-2.1.0.dist-info/licenses/LICENSE,sha256=8uJLgs8Wb_Us_8XaTvOOn7Yf0dwzFi9pcFHVxfgJKoE,1079
|
|
6
|
+
requests_credssp-2.1.0.dist-info/METADATA,sha256=pXfKfx_IED8fP-Nz8mWgA8zA3cCMx1IIDk8MHA3eZio,10897
|
|
7
|
+
requests_credssp-2.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
requests_credssp-2.1.0.dist-info/top_level.txt,sha256=Imchr1v9yvqPew-lEU9PFJpzpdJESDzFT6DXvFgEEm0,17
|
|
9
|
+
requests_credssp-2.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Jordan Borean, Red Hat
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests_credssp
|