| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright: (c) 2020, Jordan Borean (@jborean93) <[email protected]> |
| 3 |
# MIT License (see LICENSE or https://opensource.org/licenses/MIT) |
| 4 |
|
| 5 |
import re |
| 6 |
import socket |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
import spnego |
| 11 |
import spnego._credssp as credssp |
| 12 |
from spnego._credssp_structures import NegoData, TSRequest |
| 13 |
from spnego.exceptions import ( |
| 14 |
BadBindingsError, |
| 15 |
FeatureMissingError, |
| 16 |
InvalidTokenError, |
| 17 |
NegotiateOptions, |
| 18 |
NoContextError, |
| 19 |
OperationNotAvailableError, |
| 20 |
SpnegoError, |
| 21 |
) |
| 22 |
from spnego.tls import default_tls_context |
| 23 |
|
| 24 |
|
| 25 |
@pytest.mark.parametrize( |
| 26 |
"expected, usage, nonce", |
| 27 |
[ |
| 28 |
(b"\x00\x01", "initiate", None), |
| 29 |
( |
| 30 |
b"\x45\x90\xC2\x6C\x83\x7D\xAC\xDF\x3D\xA8\xFA\xE5\x78\x26\x4D\x9E" |
| 31 |
b"\x45\x48\x6A\xFC\x39\x51\xF2\x1E\xF3\x57\x06\x4C\xD5\x5F\xBB\xB5", |
| 32 |
"initiate", |
| 33 |
b"\x00\x00", |
| 34 |
), |
| 35 |
(b"\x01\x01", "accept", None), |
| 36 |
( |
| 37 |
b"\xCD\xF4\xFF\xEE\x6C\xB6\x39\x5E\x5B\x69\x2C\x2D\xA3\x97\x38\x6F" |
| 38 |
b"\x6A\x1E\xAB\x2D\x3A\x62\x71\x81\x6A\x04\xC7\xA2\x91\x4D\x33\x3F", |
| 39 |
"accept", |
| 40 |
b"\x00\x00", |
| 41 |
), |
| 42 |
], |
| 43 |
) |
| 44 |
def test_pub_key_auth(expected, usage, nonce): |
| 45 |
actual = credssp._get_pub_key_auth(b"\x00\x01", usage, nonce=nonce) |
| 46 |
|
| 47 |
assert actual == expected |
| 48 |
|
| 49 |
|
| 50 |
@pytest.mark.parametrize( |
| 51 |
"protocol, cipher, expected", |
| 52 |
[ |
| 53 |
["TLSv1.3", "TLS_AES_128_GCM_SHA256", 17], |
| 54 |
["TLSv1.3", "TLS_AES_256_GCM_SHA384", 17], |
| 55 |
["TLSv1.2", "ECDHE-RSA-AES128-GCM-SHA256", 16], |
| 56 |
["TLSv1.2", "RC4-MD5", 16], |
| 57 |
["TLSv1.2", "ECDH-ECDSA-3DES-SHA256", 34], |
| 58 |
["TLSv1.2", "ECDH-RSA-AES-SHA384", 50], |
| 59 |
["TLSv1.2", "ECDH-RSA-AES", 2], |
| 60 |
], |
| 61 |
) |
| 62 |
def test_get_credssp_trailer_length(protocol, cipher, expected): |
| 63 |
actual = credssp._tls_trailer_length(30, protocol, cipher) |
| 64 |
assert actual == expected |
| 65 |
|
| 66 |
|
| 67 |
def test_credssp_no_iov(): |
| 68 |
assert not credssp.CredSSPProxy("username", "password").iov_available() |
| 69 |
|
| 70 |
|
| 71 |
def test_credssp_iov_not_available(): |
| 72 |
expected = ( |
| 73 |
"The system is missing the GSSAPI IOV extension headers or CredSSP is being requested, " |
| 74 |
"cannot utilize wrap_iov and unwrap_iov" |
| 75 |
) |
| 76 |
with pytest.raises(FeatureMissingError, match=re.escape(expected)): |
| 77 |
credssp.CredSSPProxy("username", "password", options=spnego.NegotiateOptions.wrapping_iov) |
| 78 |
|
| 79 |
|
| 80 |
def test_credssp_no_session_key(): |
| 81 |
with pytest.raises(FeatureMissingError, match="The protocol selected does not support getting the session key"): |
| 82 |
credssp.CredSSPProxy("user", "password", options=NegotiateOptions.session_key) |
| 83 |
|
| 84 |
|
| 85 |
def test_credssp_fail_getting_session_key(): |
| 86 |
context = credssp.CredSSPProxy("username", "password") |
| 87 |
|
| 88 |
with pytest.raises(OperationNotAvailableError, match="CredSSP does not have a session key to share"): |
| 89 |
_ = context.session_key |
| 90 |
|
| 91 |
|
| 92 |
def test_credssp_wrap_iov_fail(): |
| 93 |
with pytest.raises(OperationNotAvailableError, match="CredSSP does not offer IOV wrapping"): |
| 94 |
credssp.CredSSPProxy("username", "password").wrap_iov([]) |
| 95 |
|
| 96 |
|
| 97 |
def test_credssp_unwrap_iov_fail(): |
| 98 |
with pytest.raises(OperationNotAvailableError, match="CredSSP does not offer IOV wrapping"): |
| 99 |
credssp.CredSSPProxy("username", "password").unwrap_iov([]) |
| 100 |
|
| 101 |
|
| 102 |
def test_credssp_sign_fail(): |
| 103 |
with pytest.raises(OperationNotAvailableError, match="CredSSP does not offer signing"): |
| 104 |
credssp.CredSSPProxy("username", "password").sign(b"") |
| 105 |
|
| 106 |
|
| 107 |
def test_credssp_verify_fail(): |
| 108 |
with pytest.raises(OperationNotAvailableError, match="CredSSP does not offer verification"): |
| 109 |
credssp.CredSSPProxy("username", "password").verify(b"", b"") |
| 110 |
|
| 111 |
|
| 112 |
def test_credssp_wrap_no_context(): |
| 113 |
with pytest.raises(SpnegoError, match="Invalid TLS state when wrapping data"): |
| 114 |
credssp.CredSSPProxy("username", "password").wrap(b"data") |
| 115 |
|
| 116 |
|
| 117 |
def test_credssp_query_message_sizes_fail(): |
| 118 |
with pytest.raises(NoContextError, match="Cannot get message sizes until context has been established"): |
| 119 |
credssp.CredSSPProxy("username", "password").query_message_sizes() |
| 120 |
|
| 121 |
|
| 122 |
def test_credssp_invalid_handshake(ntlm_cred): |
| 123 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], protocol="credssp") |
| 124 |
s = credssp.CredSSPProxy(None, None, protocol="credssp", usage="accept") |
| 125 |
|
| 126 |
server_hello = s.step(c.step()) |
| 127 |
assert server_hello is not None |
| 128 |
|
| 129 |
with pytest.raises(InvalidTokenError, match="TLS handshake for CredSSP"): |
| 130 |
c.step(b"\x00" + server_hello) |
| 131 |
|
| 132 |
|
| 133 |
def test_credssp_server_without_pub_key(): |
| 134 |
context = default_tls_context(usage="accept") |
| 135 |
with pytest.raises(OperationNotAvailableError, match="Provided tls context does not have a public key set"): |
| 136 |
credssp.CredSSPProxy("username", "password", usage="accept", credssp_tls_context=context) |
| 137 |
|
| 138 |
|
| 139 |
@pytest.mark.parametrize("version", [2, 5]) |
| 140 |
def test_credssp_invalid_client_authentication_v2(version, ntlm_cred, monkeypatch): |
| 141 |
monkeypatch.setattr(credssp, "_CREDSSP_VERSION", version) |
| 142 |
|
| 143 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp") |
| 144 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept") |
| 145 |
|
| 146 |
# Set up the state so the server can send the error code |
| 147 |
server_tls_token = None |
| 148 |
while c._auth_context is None: |
| 149 |
client_tls_token = c.step(server_tls_token) |
| 150 |
assert not c.complete |
| 151 |
assert not s.complete |
| 152 |
|
| 153 |
server_tls_token = s.step(client_tls_token) |
| 154 |
assert not c.complete |
| 155 |
assert not s.complete |
| 156 |
|
| 157 |
bad_request = TSRequest(credssp._CREDSSP_VERSION, nego_tokens=NegoData(b"\x00")) |
| 158 |
actual = s.step(c.wrap(bad_request.pack()).data) |
| 159 |
|
| 160 |
# On CredSSP v2 or 5 there is no error message from the server, just no token |
| 161 |
assert actual is None |
| 162 |
|
| 163 |
|
| 164 |
@pytest.mark.parametrize("version", [3, 4, 6]) |
| 165 |
def test_credssp_invalid_client_authentication(version, ntlm_cred, monkeypatch): |
| 166 |
monkeypatch.setattr(credssp, "_CREDSSP_VERSION", version) |
| 167 |
|
| 168 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp") |
| 169 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept") |
| 170 |
|
| 171 |
# Set up the state so the server can send the error code |
| 172 |
server_tls_token = None |
| 173 |
while c._auth_context is None: |
| 174 |
client_tls_token = c.step(server_tls_token) |
| 175 |
assert not c.complete |
| 176 |
assert not s.complete |
| 177 |
|
| 178 |
server_tls_token = s.step(client_tls_token) |
| 179 |
assert not c.complete |
| 180 |
assert not s.complete |
| 181 |
|
| 182 |
assert server_tls_token is not None |
| 183 |
c.unwrap(server_tls_token) |
| 184 |
bad_request = TSRequest(credssp._CREDSSP_VERSION, nego_tokens=NegoData(b"\x00")) |
| 185 |
error_msg = s.step(c.wrap(bad_request.pack()).data) |
| 186 |
|
| 187 |
expected = re.escape("Received NTStatus in TSRequest from acceptor, Context: Authentication") |
| 188 |
with pytest.raises(InvalidTokenError, match=expected): |
| 189 |
c.step(error_msg) |
| 190 |
|
| 191 |
|
| 192 |
def test_credssp_no_pub_key_after_auth(ntlm_cred): |
| 193 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp") |
| 194 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept") |
| 195 |
|
| 196 |
server_tls_token = None |
| 197 |
while c._auth_context is None: |
| 198 |
client_tls_token = c.step(server_tls_token) |
| 199 |
assert not c.complete |
| 200 |
assert not s.complete |
| 201 |
|
| 202 |
server_tls_token = s.step(client_tls_token) |
| 203 |
assert not c.complete |
| 204 |
assert not s.complete |
| 205 |
|
| 206 |
ntlm3_pub_key = c.step(server_tls_token) |
| 207 |
|
| 208 |
# Send back a TSRequest without the public key. |
| 209 |
assert ntlm3_pub_key is not None |
| 210 |
s.unwrap(ntlm3_pub_key) |
| 211 |
ts_request = TSRequest(credssp._CREDSSP_VERSION) |
| 212 |
|
| 213 |
with pytest.raises(InvalidTokenError, match="Acceptor did not response with pubKeyAuth info"): |
| 214 |
c.step(s.wrap(ts_request.pack()).data) |
| 215 |
|
| 216 |
|
| 217 |
def test_credssp_pub_key_mismatch_initiator(ntlm_cred): |
| 218 |
options = NegotiateOptions.use_ntlm |
| 219 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp", options=options) |
| 220 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept", options=options) |
| 221 |
|
| 222 |
server_tls_token = None |
| 223 |
while c._auth_context is None: |
| 224 |
client_tls_token = c.step(server_tls_token) |
| 225 |
assert not c.complete |
| 226 |
assert not s.complete |
| 227 |
|
| 228 |
server_tls_token = s.step(client_tls_token) |
| 229 |
assert not c.complete |
| 230 |
assert not s.complete |
| 231 |
|
| 232 |
ntlm3_pub_key = c.step(server_tls_token) |
| 233 |
|
| 234 |
s_auth_context = s._auth_context |
| 235 |
assert s_auth_context is not None |
| 236 |
|
| 237 |
# Send back a TSRequest with a bad pub_key_auth value. |
| 238 |
assert ntlm3_pub_key is not None |
| 239 |
request = TSRequest.unpack(s.unwrap(ntlm3_pub_key).data) |
| 240 |
assert request.nego_tokens is not None |
| 241 |
s_auth_context.step(request.nego_tokens[0].nego_token) |
| 242 |
ts_request = TSRequest(credssp._CREDSSP_VERSION, pub_key_auth=s_auth_context.wrap(b"bad").data) |
| 243 |
|
| 244 |
with pytest.raises(BadBindingsError, match="Public key verification failed, potential man in the middle attack"): |
| 245 |
c.step(s.wrap(ts_request.pack()).data) |
| 246 |
|
| 247 |
|
| 248 |
def test_credssp_pub_key_mismatch_acceptor(ntlm_cred): |
| 249 |
options = NegotiateOptions.use_ntlm |
| 250 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp", options=options) |
| 251 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept", options=options) |
| 252 |
|
| 253 |
server_tls_token = None |
| 254 |
while c._auth_context is None: |
| 255 |
client_tls_token = c.step(server_tls_token) |
| 256 |
assert not c.complete |
| 257 |
assert not s.complete |
| 258 |
|
| 259 |
server_tls_token = s.step(client_tls_token) |
| 260 |
assert not c.complete |
| 261 |
assert not s.complete |
| 262 |
|
| 263 |
# Craft the TSRequest with the NTLMv3 token and invalid pub_key_auth |
| 264 |
assert server_tls_token is not None |
| 265 |
ntlm2_request = TSRequest.unpack(c.unwrap(server_tls_token).data) |
| 266 |
assert ntlm2_request.nego_tokens is not None |
| 267 |
ntlm3 = c._auth_context.step(ntlm2_request.nego_tokens[0].nego_token) |
| 268 |
assert ntlm3 is not None |
| 269 |
|
| 270 |
request = TSRequest( |
| 271 |
credssp._CREDSSP_VERSION, nego_tokens=NegoData(ntlm3), pub_key_auth=c._auth_context.wrap(b"bad").data |
| 272 |
) |
| 273 |
|
| 274 |
with pytest.raises(BadBindingsError, match="Public key verification failed, potential man in the middle attack"): |
| 275 |
s.step(c.wrap(request.pack()).data) |
| 276 |
|
| 277 |
|
| 278 |
def test_credssp_no_credential(ntlm_cred): |
| 279 |
options = NegotiateOptions.use_ntlm |
| 280 |
c = credssp.CredSSPProxy(ntlm_cred[0], ntlm_cred[1], hostname=socket.getfqdn(), protocol="credssp", options=options) |
| 281 |
s = credssp.CredSSPProxy(None, None, hostname=socket.getfqdn(), protocol="credssp", usage="accept", options=options) |
| 282 |
|
| 283 |
server_tls_token = None |
| 284 |
while c._auth_context is None: |
| 285 |
client_tls_token = c.step(server_tls_token) |
| 286 |
assert not c.complete |
| 287 |
assert not s.complete |
| 288 |
|
| 289 |
server_tls_token = s.step(client_tls_token) |
| 290 |
assert not c.complete |
| 291 |
assert not s.complete |
| 292 |
|
| 293 |
ntlm3_pub_key = c.step(server_tls_token) |
| 294 |
assert ntlm3_pub_key is not None |
| 295 |
|
| 296 |
pub_key_resp = s.step(ntlm3_pub_key) |
| 297 |
assert pub_key_resp is not None |
| 298 |
server_pub_key = TSRequest.unpack(c.unwrap(pub_key_resp).data) |
| 299 |
|
| 300 |
# Unpack the TSRequest properly and craft a TSRequest without a credential |
| 301 |
assert server_pub_key.pub_key_auth is not None |
| 302 |
c._auth_context.unwrap(server_pub_key.pub_key_auth) |
| 303 |
|
| 304 |
request = TSRequest(credssp._CREDSSP_VERSION) |
| 305 |
|
| 306 |
with pytest.raises(InvalidTokenError, match="No credential received on CredSSP TSRequest from initiator"): |
| 307 |
s.step(c.wrap(request.pack()).data) |