File: test_encryption.py

package info (click to toggle)
pywinrm 0.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: python: 2,599; makefile: 7
file content (294 lines) | stat: -rw-r--r-- 11,393 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import base64
import struct

import pytest

from winrm.encryption import Encryption
from winrm.exceptions import WinRMError


def test_init_with_invalid_protocol():
    with pytest.raises(WinRMError) as excinfo:
        Encryption(None, "invalid_protocol")

    assert "Encryption for protocol 'invalid_protocol' not supported in pywinrm" in str(excinfo.value)


def test_encrypt_message():
    test_session = SessionTest()
    test_message = b"unencrypted message"
    test_endpoint = b"endpoint"

    encryption = Encryption(test_session, "ntlm")

    actual = encryption.prepare_encrypted_request(test_session, test_endpoint, test_message)
    expected_encrypted_message = b"dW5lbmNyeXB0ZWQgbWVzc2FnZQ=="
    expected_signature = b"1234"
    signature_length = struct.pack("<i", len(expected_signature))

    assert actual.headers == {
        "Content-Length": "272",
        "Content-Type": 'multipart/encrypted;protocol="application/HTTP-SPNEGO-session-encrypted";boundary="Encrypted Boundary"',
    }
    assert (
        actual.body == b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-SPNEGO-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=19\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + signature_length + expected_signature + expected_encrypted_message + b"--Encrypted Boundary--\r\n"
    )


def test_encrypt_large_credssp_message():
    test_session = SessionTest()
    test_message = b"unencrypted message " * 2048
    test_endpoint = "http://testhost.com"
    message_chunks = [test_message[i : i + 16384] for i in range(0, len(test_message), 16384)]

    encryption = Encryption(test_session, "credssp")

    actual = encryption.prepare_encrypted_request(test_session, test_endpoint, test_message)
    expected_encrypted_message1 = base64.b64encode(message_chunks[0])
    expected_encrypted_message2 = base64.b64encode(message_chunks[1])
    expected_encrypted_message3 = base64.b64encode(message_chunks[2])

    assert actual.headers == {
        "Content-Length": "55303",
        "Content-Type": 'multipart/x-multi-encrypted;protocol="application/HTTP-CredSSP-session-encrypted";boundary="Encrypted Boundary"',
    }

    assert (
        actual.body == b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=16384\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 32) + expected_encrypted_message1 + b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=16384\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 32) + expected_encrypted_message2 + b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=8192\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 32) + expected_encrypted_message3 + b"--Encrypted Boundary--\r\n"
    )


def test_decrypt_message():
    test_session = SessionTest()
    test_encrypted_message = b"dW5lbmNyeXB0ZWQgbWVzc2FnZQ=="
    test_signature = b"1234"
    test_signature_length = struct.pack("<i", len(test_signature))
    test_message = (
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-SPNEGO-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=19\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + test_signature_length + test_signature + test_encrypted_message + b"--Encrypted Boundary\r\n"
    )
    test_response = ResponseTest('protocol="application/HTTP-SPNEGO-session-encrypted"', test_message)

    encryption = Encryption(test_session, "ntlm")

    actual = encryption.parse_encrypted_response(test_response)

    assert actual == b"unencrypted message"


def test_decrypt_message_boundary_with_end_hyphens():
    test_session = SessionTest()
    test_encrypted_message = b"dW5lbmNyeXB0ZWQgbWVzc2FnZQ=="
    test_signature = b"1234"
    test_signature_length = struct.pack("<i", len(test_signature))
    test_message = (
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-SPNEGO-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=19\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + test_signature_length + test_signature + test_encrypted_message + b"--Encrypted Boundary--\r\n"
    )
    test_response = ResponseTest('protocol="application/HTTP-SPNEGO-session-encrypted"', test_message)

    encryption = Encryption(test_session, "ntlm")

    actual = encryption.parse_encrypted_response(test_response)

    assert actual == b"unencrypted message"


def test_decrypt_message_length_mismatch():
    test_session = SessionTest()
    test_encrypted_message = b"dW5lbmNyeXB0ZWQgbWVzc2FnZQ=="
    test_signature = b"1234"
    test_signature_length = struct.pack("<i", len(test_signature))
    test_message = (
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-SPNEGO-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=20\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + test_signature_length + test_signature + test_encrypted_message + b"--Encrypted Boundary--\r\n"
    )
    test_response = ResponseTest('protocol="application/HTTP-SPNEGO-session-encrypted"', test_message)

    encryption = Encryption(test_session, "ntlm")

    with pytest.raises(WinRMError) as excinfo:
        encryption.parse_encrypted_response(test_response)

    assert "Encrypted length from server does not match the expected size, message has been tampered with" in str(excinfo.value)


def test_decrypt_large_credssp_message():
    test_session = SessionTest()

    test_unencrypted_message = b"unencrypted message " * 2048
    test_encrypted_message_chunks = [test_unencrypted_message[i : i + 16384] for i in range(0, len(test_unencrypted_message), 16384)]

    test_encrypted_message1 = base64.b64encode(test_encrypted_message_chunks[0])
    test_encrypted_message2 = base64.b64encode(test_encrypted_message_chunks[1])
    test_encrypted_message3 = base64.b64encode(test_encrypted_message_chunks[2])

    test_message = (
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=16384\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 5443) + test_encrypted_message1 + b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=16384\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 5443) + test_encrypted_message2 + b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/HTTP-CredSSP-session-encrypted\r\n"
        b"\tOriginalContent: type=application/soap+xml;charset=UTF-8;Length=8192\r\n"
        b"--Encrypted Boundary\r\n"
        b"\tContent-Type: application/octet-stream\r\n" + struct.pack("<i", 2711) + test_encrypted_message3 + b"--Encrypted Boundary--\r\n"
    )

    test_response = ResponseTest('protocol="application/HTTP-CredSSP-session-encrypted"', test_message)

    encryption = Encryption(test_session, "credssp")

    actual = encryption.parse_encrypted_response(test_response)

    assert actual == test_unencrypted_message


def test_decrypt_message_decryption_not_needed():
    test_session = SessionTest()
    test_response = ResponseTest("application/soap+xml", "unencrypted message")

    encryption = Encryption(test_session, "ntlm")

    actual = encryption.parse_encrypted_response(test_response)

    assert actual == "unencrypted message"


def test_get_credssp_trailer_length_gcm():
    test_session = SessionTest()
    encryption = Encryption(test_session, "credssp")
    expected = 16
    actual = encryption._get_credssp_trailer_length(30, "ECDHE-RSA-AES128-GCM-SHA256")

    assert actual == expected


def test_get_credssp_trailer_length_md5_rc4():
    test_session = SessionTest()
    encryption = Encryption(test_session, "credssp")
    expected = 16
    actual = encryption._get_credssp_trailer_length(30, "RC4-MD5")

    assert actual == expected


def test_get_credssp_trailer_length_sha256_3des():
    test_session = SessionTest()
    encryption = Encryption(test_session, "credssp")
    expected = 34
    actual = encryption._get_credssp_trailer_length(30, "ECDH-ECDSA-3DES-SHA256")

    assert actual == expected


def test_get_credssp_trailer_length_sha384_aes():
    test_session = SessionTest()
    encryption = Encryption(test_session, "credssp")
    expected = 50
    actual = encryption._get_credssp_trailer_length(30, "ECDH-RSA-AES-SHA384")

    assert actual == expected


def test_get_credssp_trailer_length_no_hash():
    test_session = SessionTest()
    encryption = Encryption(test_session, "credssp")
    expected = 2
    actual = encryption._get_credssp_trailer_length(30, "ECDH-RSA-AES")

    assert actual == expected


class SessionTest(object):
    def __init__(self):
        self.auth = AuthTest()

    def prepare_request(self, request):
        request.body = request.data
        return request


# used as a mock CredSSP context object
class CredSSPContext(object):

    def __init__(self):
        class TlsContext(object):
            def get_cipher_name(self):
                return "ECDH-RSA-AES256-SHA"

        self.tls_connection = TlsContext()
        self.session_security = SessionSecurityTest()

    # used with CredSSP
    def wrap(self, message):
        encoded_message, signature = self.session_security.wrap(message)
        return encoded_message

    # used with CredSSP
    def unwrap(self, message):
        decoded_mesage = self.session_security.unwrap(message, b"1234")
        return decoded_mesage


class AuthTest(object):
    def __init__(self):
        # used with NTLM
        self.session_security = SessionSecurityTest()

        # used with CredSSP
        self.contexts = {"testhost.com": CredSSPContext()}


class SessionSecurityTest(object):
    def wrap(self, message):
        encoded_message = base64.b64encode(message)
        signature = b"1234"
        return encoded_message, signature

    def unwrap(self, message, signature):
        assert signature == b"1234"
        decoded_message = base64.b64decode(message)
        return decoded_message


class RequestTest(object):
    def __init__(self):
        self.url = "http://testhost.com/path"


class ResponseTest(object):
    def __init__(self, content_type, content):
        self.headers = {"Content-Type": content_type}
        self.content = content
        self.text = content
        self.request = RequestTest()