@@ -54,139 +54,139 @@ def on_login(self, context, connection):
5454
5555class Modify_Func :
5656 # Slightly modified version of impackets computeResponseNTLMv2
57- def mod_computeResponseNTLMv2 (flags , serverChallenge , clientChallenge , serverName , domain , user , password , lmhash = '' , nthash = '' ,
58- use_ntlmv2 = ntlm .USE_NTLMv2 , channel_binding_value = b'' ):
57+ def mod_computeResponseNTLMv2 (flags , serverChallenge , clientChallenge , serverName , domain , user , password , lmhash = "" , nthash = "" ,
58+ use_ntlmv2 = ntlm .USE_NTLMv2 , channel_binding_value = b"" ):
5959
60- responseServerVersion = b' \x01 '
61- hiResponseServerVersion = b' \x01 '
60+ responseServerVersion = b" \x01 "
61+ hiResponseServerVersion = b" \x01 "
6262 responseKeyNT = ntlm .NTOWFv2 (user , password , domain , nthash )
6363
6464 av_pairs = ntlm .AV_PAIRS (serverName )
6565 # In order to support SPN target name validation, we have to add this to the serverName av_pairs. Otherwise we will
6666 # get access denied
6767 # This is set at Local Security Policy -> Local Policies -> Security Options -> Server SPN target name validation
6868 # level
69- av_pairs [ntlm .NTLMSSP_AV_TARGET_NAME ] = ' cifs/' .encode (' utf-16le' ) + av_pairs [ntlm .NTLMSSP_AV_HOSTNAME ][1 ]
69+ av_pairs [ntlm .NTLMSSP_AV_TARGET_NAME ] = " cifs/" .encode (" utf-16le" ) + av_pairs [ntlm .NTLMSSP_AV_HOSTNAME ][1 ]
7070 if av_pairs [ntlm .NTLMSSP_AV_TIME ] is not None :
7171 aTime = av_pairs [ntlm .NTLMSSP_AV_TIME ][1 ]
7272 else :
73- aTime = struct .pack ('<q' , (116444736000000000 + calendar .timegm (time .gmtime ()) * 10000000 ))
73+ aTime = struct .pack ("<q" , (116444736000000000 + calendar .timegm (time .gmtime ()) * 10000000 ))
7474 av_pairs [ntlm .NTLMSSP_AV_TIME ] = aTime
75- av_pairs [ntlm .NTLMSSP_AV_FLAGS ] = b' \x02 ' + b' \x00 ' * 3
75+ av_pairs [ntlm .NTLMSSP_AV_FLAGS ] = b" \x02 " + b" \x00 " * 3
7676 serverName = av_pairs .getData ()
7777
7878 if len (channel_binding_value ) > 0 :
7979 av_pairs [ntlm .NTLMSSP_AV_CHANNEL_BINDINGS ] = channel_binding_value
8080
8181 # Format according to:
8282 # https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/aee311d6-21a7-4470-92a5-c4ecb022a87b
83- temp = responseServerVersion # RespType 1 byte
84- temp += hiResponseServerVersion # HiRespType 1 byte
85- temp += b' \x00 ' * 2 # Reserved1 2 bytes
86- temp += b' \x00 ' * 4 # Reserved2 4 bytes
87- temp += aTime # TimeStamp 8 bytes
88- temp += clientChallenge # ChallengeFromClient 8 bytes
89- temp += b' \x00 ' * 4 # Reserved 4 bytes
90- temp += av_pairs .getData () # AvPairs variable
83+ temp = responseServerVersion # RespType 1 byte
84+ temp += hiResponseServerVersion # HiRespType 1 byte
85+ temp += b" \x00 " * 2 # Reserved1 2 bytes
86+ temp += b" \x00 " * 4 # Reserved2 4 bytes
87+ temp += aTime # TimeStamp 8 bytes
88+ temp += clientChallenge # ChallengeFromClient 8 bytes
89+ temp += b" \x00 " * 4 # Reserved 4 bytes
90+ temp += av_pairs .getData () # AvPairs variable
9191
9292 ntProofStr = ntlm .hmac_md5 (responseKeyNT , serverChallenge + temp )
9393
9494 ntChallengeResponse = ntProofStr + temp
9595 lmChallengeResponse = ntlm .hmac_md5 (responseKeyNT , serverChallenge + clientChallenge ) + clientChallenge
9696 sessionBaseKey = ntlm .hmac_md5 (responseKeyNT , ntProofStr )
9797
98- if user == '' and password == '' :
98+ if user == "" and password == "" :
9999 # Special case for anonymous authentication
100- ntChallengeResponse = ''
101- lmChallengeResponse = ''
100+ ntChallengeResponse = ""
101+ lmChallengeResponse = ""
102102
103103 return ntChallengeResponse , lmChallengeResponse , sessionBaseKey
104104
105- def mod_getNTLMSSPType3 (type1 , type2 , user , password , domain , lmhash = '' , nthash = '' , use_ntlmv2 = ntlm .USE_NTLMv2 , channel_binding_value = b'' ):
105+ def mod_getNTLMSSPType3 (type1 , type2 , user , password , domain , lmhash = "" , nthash = "" , use_ntlmv2 = ntlm .USE_NTLMv2 , channel_binding_value = b"" ):
106106 # Safety check in case somebody sent password = None.. That's not allowed. Setting it to '' and hope for the best.
107107 if password is None :
108- password = ''
108+ password = ""
109109
110110 # Let's do some encoding checks before moving on. Kind of dirty, but found effective when dealing with
111111 # international characters.
112112 import sys
113113 encoding = sys .getfilesystemencoding ()
114114 if encoding is not None :
115115 try :
116- user .encode (' utf-16le' )
117- except :
116+ user .encode (" utf-16le" )
117+ except Exception :
118118 user = user .decode (encoding )
119119 try :
120- password .encode (' utf-16le' )
121- except :
120+ password .encode (" utf-16le" )
121+ except Exception :
122122 password = password .decode (encoding )
123123 try :
124- domain .encode (' utf-16le' )
125- except :
124+ domain .encode (" utf-16le" )
125+ except Exception :
126126 domain = user .decode (encoding )
127127
128128 ntlmChallenge = ntlm .NTLMAuthChallenge (type2 )
129129
130130 # Let's start with the original flags sent in the type1 message
131- responseFlags = type1 [' flags' ]
131+ responseFlags = type1 [" flags" ]
132132
133133 # Token received and parsed. Depending on the authentication
134134 # method we will create a valid ChallengeResponse
135- ntlmChallengeResponse = ntlm .NTLMAuthChallengeResponse (user , password , ntlmChallenge [' challenge' ])
135+ ntlmChallengeResponse = ntlm .NTLMAuthChallengeResponse (user , password , ntlmChallenge [" challenge" ])
136136
137- clientChallenge = ntlm .b ("" .join ([random .choice (string .digits + string .ascii_letters ) for _ in range (8 )]))
137+ clientChallenge = ntlm .b ("" .join ([random .choice (string .digits + string .ascii_letters ) for _ in range (8 )]))
138138
139- serverName = ntlmChallenge [' TargetInfoFields' ]
139+ serverName = ntlmChallenge [" TargetInfoFields" ]
140140
141- ntResponse , lmResponse , sessionBaseKey = ntlm .computeResponse (ntlmChallenge [' flags' ], ntlmChallenge [' challenge' ],
141+ ntResponse , lmResponse , sessionBaseKey = ntlm .computeResponse (ntlmChallenge [" flags" ], ntlmChallenge [" challenge" ],
142142 clientChallenge , serverName , domain , user , password ,
143- lmhash , nthash , use_ntlmv2 , channel_binding_value = channel_binding_value )
143+ lmhash , nthash , use_ntlmv2 , channel_binding_value = channel_binding_value )
144144
145145 # Let's check the return flags
146- if (ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY ) == 0 :
146+ if (ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY ) == 0 :
147147 # No extended session security, taking it out
148148 responseFlags &= 0xffffffff ^ ntlm .NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY
149- if (ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_128 ) == 0 :
149+ if (ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_128 ) == 0 :
150150 # No support for 128 key len, taking it out
151151 responseFlags &= 0xffffffff ^ ntlm .NTLMSSP_NEGOTIATE_128
152- if (ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_KEY_EXCH ) == 0 :
152+ if (ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_KEY_EXCH ) == 0 :
153153 # No key exchange supported, taking it out
154154 responseFlags &= 0xffffffff ^ ntlm .NTLMSSP_NEGOTIATE_KEY_EXCH
155155
156156 # drop the mic need to unset these flags
157157 # https://github.com/fortra/impacket/blob/master/impacket/examples/ntlmrelayx/clients/ldaprelayclient.py#L72
158- if ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_SEAL == ntlm .NTLMSSP_NEGOTIATE_SEAL :
158+ if ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_SEAL == ntlm .NTLMSSP_NEGOTIATE_SEAL :
159159 responseFlags ^= ntlm .NTLMSSP_NEGOTIATE_SEAL
160- if ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_SIGN == ntlm .NTLMSSP_NEGOTIATE_SIGN :
160+ if ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_SIGN == ntlm .NTLMSSP_NEGOTIATE_SIGN :
161161 responseFlags ^= ntlm .NTLMSSP_NEGOTIATE_SIGN
162- if ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_ALWAYS_SIGN == ntlm .NTLMSSP_NEGOTIATE_ALWAYS_SIGN :
162+ if ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_ALWAYS_SIGN == ntlm .NTLMSSP_NEGOTIATE_ALWAYS_SIGN :
163163 responseFlags ^= ntlm .NTLMSSP_NEGOTIATE_ALWAYS_SIGN
164164
165165
166- keyExchangeKey = ntlm .KXKEY (ntlmChallenge [' flags' ], sessionBaseKey , lmResponse , ntlmChallenge [' challenge' ], password ,
166+ keyExchangeKey = ntlm .KXKEY (ntlmChallenge [" flags" ], sessionBaseKey , lmResponse , ntlmChallenge [" challenge" ], password ,
167167 lmhash , nthash , use_ntlmv2 )
168168
169169 # Special case for anonymous login
170- if user == '' and password == '' and lmhash == '' and nthash == '' :
171- keyExchangeKey = b' \x00 ' * 16
170+ if user == "" and password == "" and lmhash == "" and nthash == "" :
171+ keyExchangeKey = b" \x00 " * 16
172172
173173
174- if ntlmChallenge [' flags' ] & ntlm .NTLMSSP_NEGOTIATE_KEY_EXCH :
175- exportedSessionKey = ntlm .b ("" .join ([random .choice (string .digits + string .ascii_letters ) for _ in range (16 )]))
174+ if ntlmChallenge [" flags" ] & ntlm .NTLMSSP_NEGOTIATE_KEY_EXCH :
175+ exportedSessionKey = ntlm .b ("" .join ([random .choice (string .digits + string .ascii_letters ) for _ in range (16 )]))
176176 encryptedRandomSessionKey = ntlm .generateEncryptedSessionKey (keyExchangeKey , exportedSessionKey )
177177 else :
178178 encryptedRandomSessionKey = None
179- exportedSessionKey = keyExchangeKey
179+ exportedSessionKey = keyExchangeKey
180180
181- ntlmChallengeResponse [' flags' ] = responseFlags
182- ntlmChallengeResponse [' domain_name' ] = domain .encode (' utf-16le' )
183- ntlmChallengeResponse [' host_name' ] = type1 .getWorkstation ().encode (' utf-16le' )
184- if lmResponse == '' :
185- ntlmChallengeResponse [' lanman' ] = b' \x00 '
181+ ntlmChallengeResponse [" flags" ] = responseFlags
182+ ntlmChallengeResponse [" domain_name" ] = domain .encode (" utf-16le" )
183+ ntlmChallengeResponse [" host_name" ] = type1 .getWorkstation ().encode (" utf-16le" )
184+ if lmResponse == "" :
185+ ntlmChallengeResponse [" lanman" ] = b" \x00 "
186186 else :
187- ntlmChallengeResponse [' lanman' ] = lmResponse
188- ntlmChallengeResponse [' ntlm' ] = ntResponse
187+ ntlmChallengeResponse [" lanman" ] = lmResponse
188+ ntlmChallengeResponse [" ntlm" ] = ntResponse
189189 if encryptedRandomSessionKey is not None :
190- ntlmChallengeResponse [' session_key' ] = encryptedRandomSessionKey
190+ ntlmChallengeResponse [" session_key" ] = encryptedRandomSessionKey
191191
192192 return ntlmChallengeResponse , exportedSessionKey
0 commit comments