-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
ntlm_upstream_proxy.py
216 lines (198 loc) · 8.36 KB
/
ntlm_upstream_proxy.py
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
import base64
import binascii
import logging
import socket
from typing import Any
from typing import Optional
from ntlm_auth import gss_channel_bindings
from ntlm_auth import ntlm
from mitmproxy import addonmanager
from mitmproxy import ctx
from mitmproxy import http
from mitmproxy.net.http import http1
from mitmproxy.proxy import commands
from mitmproxy.proxy import layer
from mitmproxy.proxy.context import Context
from mitmproxy.proxy.layers.http import HttpConnectUpstreamHook
from mitmproxy.proxy.layers.http import HttpLayer
from mitmproxy.proxy.layers.http import HttpStream
from mitmproxy.proxy.layers.http._upstream_proxy import HttpUpstreamProxy
class NTLMUpstreamAuth:
"""
This addon handles authentication to systems upstream from us for the
upstream proxy and reverse proxy mode. There are 3 cases:
- Upstream proxy CONNECT requests should have authentication added, and
subsequent already connected requests should not.
- Upstream proxy regular requests
- Reverse proxy regular requests (CONNECT is invalid in this mode)
"""
def load(self, loader: addonmanager.Loader) -> None:
logging.info("NTLMUpstreamAuth loader")
loader.add_option(
name="upstream_ntlm_auth",
typespec=Optional[str],
default=None,
help="""
Add HTTP NTLM authentication to upstream proxy requests.
Format: username:password.
""",
)
loader.add_option(
name="upstream_ntlm_domain",
typespec=Optional[str],
default=None,
help="""
Add HTTP NTLM domain for authentication to upstream proxy requests.
""",
)
loader.add_option(
name="upstream_proxy_address",
typespec=Optional[str],
default=None,
help="""
upstream poxy address.
""",
)
loader.add_option(
name="upstream_ntlm_compatibility",
typespec=int,
default=3,
help="""
Add HTTP NTLM compatibility for authentication to upstream proxy requests.
Valid values are 0-5 (Default: 3)
""",
)
logging.debug("AddOn: NTLM Upstream Authentication - Loaded")
def running(self):
def extract_flow_from_context(context: Context) -> http.HTTPFlow:
if context and context.layers:
for x in context.layers:
if isinstance(x, HttpLayer):
for _, stream in x.streams.items():
return (
stream.flow if isinstance(stream, HttpStream) else None
)
def build_connect_flow(
context: Context, connect_header: tuple
) -> http.HTTPFlow:
flow = extract_flow_from_context(context)
if not flow:
logging.error("failed to build connect flow")
raise
flow.request.content = b"" # we should send empty content for handshake
header_name, header_value = connect_header
flow.request.headers.add(header_name, header_value)
return flow
def patched_start_handshake(self) -> layer.CommandGenerator[None]:
assert self.conn.address
self.ntlm_context = CustomNTLMContext(ctx)
proxy_authorization = self.ntlm_context.get_ntlm_start_negotiate_message()
self.flow = build_connect_flow(
self.context, ("Proxy-Authorization", proxy_authorization)
)
yield HttpConnectUpstreamHook(self.flow)
raw = http1.assemble_request(self.flow.request)
yield commands.SendData(self.tunnel_connection, raw)
def extract_proxy_authenticate_msg(response_head: list) -> str:
for header in response_head:
if b"Proxy-Authenticate" in header:
challenge_message = str(bytes(header).decode("utf-8"))
try:
token = challenge_message.split(": ")[1]
except IndexError:
logging.error("Failed to extract challenge_message")
raise
return token
def patched_receive_handshake_data(
self, data
) -> layer.CommandGenerator[tuple[bool, str | None]]:
self.buf += data
response_head = self.buf.maybe_extract_lines()
if response_head:
response_head = [bytes(x) for x in response_head]
try:
response = http1.read_response_head(response_head)
except ValueError:
return True, None
challenge_message = extract_proxy_authenticate_msg(response_head)
if 200 <= response.status_code < 300:
if self.buf:
yield from self.receive_data(data)
del self.buf
return True, None
else:
if not challenge_message:
return True, None
proxy_authorization = (
self.ntlm_context.get_ntlm_challenge_response_message(
challenge_message
)
)
self.flow = build_connect_flow(
self.context, ("Proxy-Authorization", proxy_authorization)
)
raw = http1.assemble_request(self.flow.request)
yield commands.SendData(self.tunnel_connection, raw)
return False, None
else:
return False, None
HttpUpstreamProxy.start_handshake = patched_start_handshake
HttpUpstreamProxy.receive_handshake_data = patched_receive_handshake_data
def done(self):
logging.info("close ntlm session")
addons = [NTLMUpstreamAuth()]
class CustomNTLMContext:
def __init__(
self,
ctx,
preferred_type: str = "NTLM",
cbt_data: gss_channel_bindings.GssChannelBindingsStruct = None,
):
# TODO:// take care the cbt_data
auth: str = ctx.options.upstream_ntlm_auth
domain: str = str(ctx.options.upstream_ntlm_domain).upper()
ntlm_compatibility: int = ctx.options.upstream_ntlm_compatibility
username, password = tuple(auth.split(":"))
workstation = socket.gethostname().upper()
logging.debug(f'\nntlm context with the details: "{domain}\\{username}", *****')
self.preferred_type = preferred_type
self.ntlm_context = ntlm.NtlmContext(
username=username,
password=password,
domain=domain,
workstation=workstation,
ntlm_compatibility=ntlm_compatibility,
cbt_data=cbt_data,
)
def get_ntlm_start_negotiate_message(self) -> str:
negotiate_message = self.ntlm_context.step()
negotiate_message_base_64_in_bytes = base64.b64encode(negotiate_message)
negotiate_message_base_64_ascii = negotiate_message_base_64_in_bytes.decode(
"ascii"
)
negotiate_message_base_64_final = (
f"{self.preferred_type} {negotiate_message_base_64_ascii}"
)
logging.debug(
f"{self.preferred_type} Authentication, negotiate message: {negotiate_message_base_64_final}"
)
return negotiate_message_base_64_final
def get_ntlm_challenge_response_message(self, challenge_message: str) -> Any:
challenge_message = challenge_message.replace(self.preferred_type + " ", "", 1)
try:
challenge_message_ascii_bytes = base64.b64decode(
challenge_message, validate=True
)
except binascii.Error as err:
logging.debug(
f"{self.preferred_type} Authentication fail with error {err.__str__()}"
)
return False
authenticate_message = self.ntlm_context.step(challenge_message_ascii_bytes)
negotiate_message_base_64 = "{} {}".format(
self.preferred_type, base64.b64encode(authenticate_message).decode("ascii")
)
logging.debug(
f"{self.preferred_type} Authentication, response to challenge message: {negotiate_message_base_64}"
)
return negotiate_message_base_64