| 1 |
from __future__ import print_function |
| 2 |
# Copyright (c) 2003-2016 CORE Security Technologies: |
| 3 |
# |
| 4 |
# This software is provided under under a slightly modified version |
| 5 |
# of the Apache Software License. See the accompanying LICENSE file |
| 6 |
# for more information. |
| 7 |
# |
| 8 |
import base64 |
| 9 |
import struct |
| 10 |
import calendar |
| 11 |
import time |
| 12 |
import hashlib |
| 13 |
import random |
| 14 |
import string |
| 15 |
import binascii |
| 16 |
|
| 17 |
from impacket.structure import Structure |
| 18 |
from impacket import LOG |
| 19 |
|
| 20 |
|
| 21 |
# This is important. NTLMv2 is not negotiated by the client or server. |
| 22 |
# It is used if set locally on both sides. Change this item if you don't want to use |
| 23 |
# NTLMv2 by default and fall back to NTLMv1 (with EXTENDED_SESSION_SECURITY or not) |
| 24 |
# Check the following links: |
| 25 |
# http://davenport.sourceforge.net/ntlm.html |
| 26 |
# http://blogs.msdn.com/b/openspecification/archive/2010/04/20/ntlm-keys-and-sundry-stuff.aspx |
| 27 |
# http://social.msdn.microsoft.com/Forums/en-US/os_interopscenarios/thread/c8f488ed-1b96-4e06-bd65-390aa41138d1/ |
| 28 |
# So I'm setting a global variable to control this, this can also be set programmatically |
| 29 |
|
| 30 |
USE_NTLMv2 = True # if false will fall back to NTLMv1 (or NTLMv1 with ESS a.k.a NTLM2) |
| 31 |
|
| 32 |
|
| 33 |
def computeResponse(flags, serverChallenge, clientChallenge, serverName, domain, user, password, lmhash='', nthash='', |
| 34 |
use_ntlmv2=USE_NTLMv2): |
| 35 |
if use_ntlmv2: |
| 36 |
return computeResponseNTLMv2(flags, serverChallenge, clientChallenge, serverName, domain, user, password, |
| 37 |
lmhash, nthash, use_ntlmv2=use_ntlmv2) |
| 38 |
else: |
| 39 |
return computeResponseNTLMv1(flags, serverChallenge, clientChallenge, serverName, domain, user, password, |
| 40 |
lmhash, nthash, use_ntlmv2=use_ntlmv2) |
| 41 |
try: |
| 42 |
POW = None |
| 43 |
from Crypto.Cipher import ARC4 |
| 44 |
from Crypto.Cipher import DES |
| 45 |
from Crypto.Hash import MD4 |
| 46 |
except Exception: |
| 47 |
try: |
| 48 |
import POW |
| 49 |
except Exception: |
| 50 |
LOG.critical("Warning: You don't have any crypto installed. You need PyCrypto") |
| 51 |
LOG.critical("See http://www.pycrypto.org/") |
| 52 |
|
| 53 |
NTLM_AUTH_NONE = 1 |
| 54 |
NTLM_AUTH_CONNECT = 2 |
| 55 |
NTLM_AUTH_CALL = 3 |
| 56 |
NTLM_AUTH_PKT = 4 |
| 57 |
NTLM_AUTH_PKT_INTEGRITY = 5 |
| 58 |
NTLM_AUTH_PKT_PRIVACY = 6 |
| 59 |
|
| 60 |
# If set, requests 56-bit encryption. If the client sends NTLMSSP_NEGOTIATE_SEAL or NTLMSSP_NEGOTIATE_SIGN |
| 61 |
# with NTLMSSP_NEGOTIATE_56 to the server in the NEGOTIATE_MESSAGE, the server MUST return NTLMSSP_NEGOTIATE_56 to |
| 62 |
# the client in the CHALLENGE_MESSAGE. Otherwise it is ignored. If both NTLMSSP_NEGOTIATE_56 and NTLMSSP_NEGOTIATE_128 |
| 63 |
# are requested and supported by the client and server, NTLMSSP_NEGOTIATE_56 and NTLMSSP_NEGOTIATE_128 will both be |
| 64 |
# returned to the client. Clients and servers that set NTLMSSP_NEGOTIATE_SEAL SHOULD set NTLMSSP_NEGOTIATE_56 if it is |
| 65 |
# supported. An alternate name for this field is NTLMSSP_NEGOTIATE_56. |
| 66 |
NTLMSSP_NEGOTIATE_56 = 0x80000000 |
| 67 |
|
| 68 |
# If set, requests an explicit key exchange. This capability SHOULD be used because it improves security for message |
| 69 |
# integrity or confidentiality. See sections 3.2.5.1.2, 3.2.5.2.1, and 3.2.5.2.2 for details. An alternate name for |
| 70 |
# this field is NTLMSSP_NEGOTIATE_KEY_EXCH. |
| 71 |
NTLMSSP_NEGOTIATE_KEY_EXCH = 0x40000000 |
| 72 |
|
| 73 |
# If set, requests 128-bit session key negotiation. An alternate name for this field is NTLMSSP_NEGOTIATE_128. |
| 74 |
# If the client sends NTLMSSP_NEGOTIATE_128 to the server in the NEGOTIATE_MESSAGE, the server MUST return |
| 75 |
# NTLMSSP_NEGOTIATE_128 to the client in the CHALLENGE_MESSAGE only if the client sets NTLMSSP_NEGOTIATE_SEAL or |
| 76 |
# NTLMSSP_NEGOTIATE_SIGN. Otherwise it is ignored. If both NTLMSSP_NEGOTIATE_56 and NTLMSSP_NEGOTIATE_128 are |
| 77 |
# requested and supported by the client and server, NTLMSSP_NEGOTIATE_56 and NTLMSSP_NEGOTIATE_128 will both be |
| 78 |
# returned to the client. Clients and servers that set NTLMSSP_NEGOTIATE_SEAL SHOULD set NTLMSSP_NEGOTIATE_128 if it |
| 79 |
# is supported. An alternate name for this field is NTLMSSP_NEGOTIATE_128 |
| 80 |
NTLMSSP_NEGOTIATE_128 = 0x20000000 |
| 81 |
|
| 82 |
NTLMSSP_RESERVED_1 = 0x10000000 |
| 83 |
NTLMSSP_RESERVED_2 = 0x08000000 |
| 84 |
NTLMSSP_RESERVED_3 = 0x04000000 |
| 85 |
|
| 86 |
# If set, requests the protocol version number. The data corresponding to this flag is provided in the Version field |
| 87 |
# of the NEGOTIATE_MESSAGE, the CHALLENGE_MESSAGE, and the AUTHENTICATE_MESSAGE.<22> An alternate name for this field |
| 88 |
# is NTLMSSP_NEGOTIATE_VERSION |
| 89 |
NTLMSSP_NEGOTIATE_VERSION = 0x02000000 |
| 90 |
NTLMSSP_RESERVED_4 = 0x01000000 |
| 91 |
|
| 92 |
# If set, indicates that the TargetInfo fields in the CHALLENGE_MESSAGE (section 2.2.1.2) are populated. |
| 93 |
# An alternate name for this field is NTLMSSP_NEGOTIATE_TARGET_INFO. |
| 94 |
NTLMSSP_NEGOTIATE_TARGET_INFO = 0x00800000 |
| 95 |
|
| 96 |
# If set, requests the usage of the LMOWF (section 3.3). An alternate name for this field is |
| 97 |
# NTLMSSP_REQUEST_NON_NT_SESSION_KEY. |
| 98 |
NTLMSSP_REQUEST_NON_NT_SESSION_KEY = 0x00400000 |
| 99 |
NTLMSSP_RESERVED_5 = 0x00200000 |
| 100 |
|
| 101 |
# If set, requests an identify level token. An alternate name for this field is NTLMSSP_NEGOTIATE_IDENTIFY |
| 102 |
NTLMSSP_NEGOTIATE_IDENTIFY = 0x00100000 |
| 103 |
|
| 104 |
# If set, requests usage of the NTLM v2 session security. NTLM v2 session security is a misnomer because it is not |
| 105 |
# NTLM v2. It is NTLM v1 using the extended session security that is also in NTLM v2. NTLMSSP_NEGOTIATE_LM_KEY and |
| 106 |
# NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY are mutually exclusive. If both NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY |
| 107 |
# and NTLMSSP_NEGOTIATE_LM_KEY are requested, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY alone MUST be returned to the |
| 108 |
# client. NTLM v2 authentication session key generation MUST be supported by both the client and the DC in order to be |
| 109 |
# used, and extended session security signing and sealing requires support from the client and the server in order to |
| 110 |
# be used.<23> An alternate name for this field is NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY |
| 111 |
NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY = 0x00080000 |
| 112 |
NTLMSSP_NEGOTIATE_NTLM2 = 0x00080000 |
| 113 |
NTLMSSP_TARGET_TYPE_SHARE = 0x00040000 |
| 114 |
|
| 115 |
# If set, TargetName MUST be a server name. The data corresponding to this flag is provided by the server in the |
| 116 |
# TargetName field of the CHALLENGE_MESSAGE. If this bit is set, then NTLMSSP_TARGET_TYPE_DOMAIN MUST NOT be set. |
| 117 |
# This flag MUST be ignored in the NEGOTIATE_MESSAGE and the AUTHENTICATE_MESSAGE. An alternate name for this field |
| 118 |
# is NTLMSSP_TARGET_TYPE_SERVER |
| 119 |
NTLMSSP_TARGET_TYPE_SERVER = 0x00020000 |
| 120 |
|
| 121 |
# If set, TargetName MUST be a domain name. The data corresponding to this flag is provided by the server in the |
| 122 |
# TargetName field of the CHALLENGE_MESSAGE. If set, then NTLMSSP_TARGET_TYPE_SERVER MUST NOT be set. This flag MUST |
| 123 |
# be ignored in the NEGOTIATE_MESSAGE and the AUTHENTICATE_MESSAGE. An alternate name for this field is |
| 124 |
# NTLMSSP_TARGET_TYPE_DOMAIN. |
| 125 |
NTLMSSP_TARGET_TYPE_DOMAIN = 0x00010000 |
| 126 |
|
| 127 |
# If set, requests the presence of a signature block on all messages. NTLMSSP_NEGOTIATE_ALWAYS_SIGN MUST be set in the |
| 128 |
# NEGOTIATE_MESSAGE to the server and the CHALLENGE_MESSAGE to the client. NTLMSSP_NEGOTIATE_ALWAYS_SIGN is overridden |
| 129 |
# by NTLMSSP_NEGOTIATE_SIGN and NTLMSSP_NEGOTIATE_SEAL, if they are supported. An alternate name for this field is |
| 130 |
# NTLMSSP_NEGOTIATE_ALWAYS_SIGN. |
| 131 |
NTLMSSP_NEGOTIATE_ALWAYS_SIGN = 0x00008000 # forces the other end to sign packets |
| 132 |
NTLMSSP_RESERVED_6 = 0x00004000 |
| 133 |
|
| 134 |
# This flag indicates whether the Workstation field is present. If this flag is not set, the Workstation field MUST be |
| 135 |
# ignored. If this flag is set, the length field of the Workstation field specifies whether the workstation name is |
| 136 |
# nonempty or not.<24> An alternate name for this field is NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED. |
| 137 |
NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED = 0x00002000 |
| 138 |
|
| 139 |
# If set, the domain name is provided (section 2.2.1.1).<25> An alternate name for this field is |
| 140 |
# NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED |
| 141 |
NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED = 0x00001000 |
| 142 |
NTLMSSP_RESERVED_7 = 0x00000800 |
| 143 |
|
| 144 |
|
| 145 |
# If set, LM authentication is not allowed and only NT authentication is used. |
| 146 |
NTLMSSP_NEGOTIATE_NT_ONLY = 0x00000400 |
| 147 |
|
| 148 |
# If set, requests usage of the NTLM v1 session security protocol. NTLMSSP_NEGOTIATE_NTLM MUST be set in the |
| 149 |
# NEGOTIATE_MESSAGE to the server and the CHALLENGE_MESSAGE to the client. An alternate name for this field is |
| 150 |
# NTLMSSP_NEGOTIATE_NTLM |
| 151 |
NTLMSSP_NEGOTIATE_NTLM = 0x00000200 |
| 152 |
NTLMSSP_RESERVED_8 = 0x00000100 |
| 153 |
|
| 154 |
# If set, requests LAN Manager (LM) session key computation. NTLMSSP_NEGOTIATE_LM_KEY and |
| 155 |
# NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY are mutually exclusive. If both NTLMSSP_NEGOTIATE_LM_KEY and |
| 156 |
# NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY are requested, NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY alone MUST be |
| 157 |
# returned to the client. NTLM v2 authentication session key generation MUST be supported by both the client and the |
| 158 |
# DC in order to be used, and extended session security signing and sealing requires support from the client and the |
| 159 |
# server to be used. An alternate name for this field is NTLMSSP_NEGOTIATE_LM_KEY. |
| 160 |
NTLMSSP_NEGOTIATE_LM_KEY = 0x00000080 |
| 161 |
|
| 162 |
# If set, requests connectionless authentication. If NTLMSSP_NEGOTIATE_DATAGRAM is set, then NTLMSSP_NEGOTIATE_KEY_EXCH |
| 163 |
# MUST always be set in the AUTHENTICATE_MESSAGE to the server and the CHALLENGE_MESSAGE to the client. An alternate |
| 164 |
# name for this field is NTLMSSP_NEGOTIATE_DATAGRAM. |
| 165 |
NTLMSSP_NEGOTIATE_DATAGRAM = 0x00000040 |
| 166 |
|
| 167 |
# If set, requests session key negotiation for message confidentiality. If the client sends NTLMSSP_NEGOTIATE_SEAL to |
| 168 |
# the server in the NEGOTIATE_MESSAGE, the server MUST return NTLMSSP_NEGOTIATE_SEAL to the client in the |
| 169 |
# CHALLENGE_MESSAGE. Clients and servers that set NTLMSSP_NEGOTIATE_SEAL SHOULD always set NTLMSSP_NEGOTIATE_56 and |
| 170 |
# NTLMSSP_NEGOTIATE_128, if they are supported. An alternate name for this field is NTLMSSP_NEGOTIATE_SEAL. |
| 171 |
NTLMSSP_NEGOTIATE_SEAL = 0x00000020 |
| 172 |
|
| 173 |
# If set, requests session key negotiation for message signatures. If the client sends NTLMSSP_NEGOTIATE_SIGN to the |
| 174 |
# server in the NEGOTIATE_MESSAGE, the server MUST return NTLMSSP_NEGOTIATE_SIGN to the client in the CHALLENGE_MESSAGE. |
| 175 |
# An alternate name for this field is NTLMSSP_NEGOTIATE_SIGN. |
| 176 |
NTLMSSP_NEGOTIATE_SIGN = 0x00000010 # means packet is signed, if verifier is wrong it fails |
| 177 |
NTLMSSP_RESERVED_9 = 0x00000008 |
| 178 |
|
| 179 |
# If set, a TargetName field of the CHALLENGE_MESSAGE (section 2.2.1.2) MUST be supplied. An alternate name for this |
| 180 |
# field is NTLMSSP_REQUEST_TARGET. |
| 181 |
NTLMSSP_REQUEST_TARGET = 0x00000004 |
| 182 |
|
| 183 |
# If set, requests OEM character set encoding. An alternate name for this field is NTLM_NEGOTIATE_OEM. See bit A for |
| 184 |
# details. |
| 185 |
NTLM_NEGOTIATE_OEM = 0x00000002 |
| 186 |
|
| 187 |
# If set, requests Unicode character set encoding. An alternate name for this field is NTLMSSP_NEGOTIATE_UNICODE. |
| 188 |
NTLMSSP_NEGOTIATE_UNICODE = 0x00000001 |
| 189 |
|
| 190 |
# AV_PAIR constants |
| 191 |
NTLMSSP_AV_EOL = 0x00 |
| 192 |
NTLMSSP_AV_HOSTNAME = 0x01 |
| 193 |
NTLMSSP_AV_DOMAINNAME = 0x02 |
| 194 |
NTLMSSP_AV_DNS_HOSTNAME = 0x03 |
| 195 |
NTLMSSP_AV_DNS_DOMAINNAME = 0x04 |
| 196 |
NTLMSSP_AV_DNS_TREENAME = 0x05 |
| 197 |
NTLMSSP_AV_FLAGS = 0x06 |
| 198 |
NTLMSSP_AV_TIME = 0x07 |
| 199 |
NTLMSSP_AV_RESTRICTIONS = 0x08 |
| 200 |
NTLMSSP_AV_TARGET_NAME = 0x09 |
| 201 |
NTLMSSP_AV_CHANNEL_BINDINGS = 0x0a |
| 202 |
|
| 203 |
class AV_PAIRS(): |
| 204 |
def __init__(self, data = None): |
| 205 |
self.fields = {} |
| 206 |
if data is not None: |
| 207 |
self.fromString(data) |
| 208 |
|
| 209 |
def __setitem__(self,key,value): |
| 210 |
self.fields[key] = (len(value),value) |
| 211 |
|
| 212 |
def __getitem__(self, key): |
| 213 |
if key in self.fields: |
| 214 |
return self.fields[key] |
| 215 |
return None |
| 216 |
|
| 217 |
def __delitem__(self, key): |
| 218 |
del self.fields[key] |
| 219 |
|
| 220 |
def __len__(self): |
| 221 |
return len(self.getData()) |
| 222 |
|
| 223 |
def __str__(self): |
| 224 |
return len(self.getData()) |
| 225 |
|
| 226 |
def fromString(self, data): |
| 227 |
tInfo = data |
| 228 |
fType = 0xff |
| 229 |
while fType is not NTLMSSP_AV_EOL: |
| 230 |
fType = struct.unpack('<H',tInfo[:struct.calcsize('<H')])[0] |
| 231 |
tInfo = tInfo[struct.calcsize('<H'):] |
| 232 |
length = struct.unpack('<H',tInfo[:struct.calcsize('<H')])[0] |
| 233 |
tInfo = tInfo[struct.calcsize('<H'):] |
| 234 |
content = tInfo[:length] |
| 235 |
self.fields[fType]=(length,content) |
| 236 |
tInfo = tInfo[length:] |
| 237 |
|
| 238 |
def dump(self): |
| 239 |
for i in self.fields.keys(): |
| 240 |
print("%s: {%r}" % (i,self[i])) |
| 241 |
|
| 242 |
def getData(self): |
| 243 |
if NTLMSSP_AV_EOL in self.fields: |
| 244 |
del self.fields[NTLMSSP_AV_EOL] |
| 245 |
ans = '' |
| 246 |
for i in self.fields.keys(): |
| 247 |
ans+= struct.pack('<HH', i, self[i][0]) |
| 248 |
ans+= self[i][1] |
| 249 |
|
| 250 |
# end with a NTLMSSP_AV_EOL |
| 251 |
ans += struct.pack('<HH', NTLMSSP_AV_EOL, 0) |
| 252 |
|
| 253 |
return ans |
| 254 |
|
| 255 |
class NTLMAuthMixin: |
| 256 |
def get_os_version(self): |
| 257 |
if self['os_version'] == '': |
| 258 |
return None |
| 259 |
else: |
| 260 |
mayor_v = struct.unpack('B',self['os_version'][0])[0] |
| 261 |
minor_v = struct.unpack('B',self['os_version'][1])[0] |
| 262 |
build_v = struct.unpack('H',self['os_version'][2:4]) |
| 263 |
return (mayor_v,minor_v,build_v) |
| 264 |
|
| 265 |
class NTLMAuthNegotiate(Structure, NTLMAuthMixin): |
| 266 |
|
| 267 |
structure = ( |
| 268 |
('','"NTLMSSP\x00'), |
| 269 |
('message_type','<L=1'), |
| 270 |
('flags','<L'), |
| 271 |
('domain_len','<H-domain_name'), |
| 272 |
('domain_max_len','<H-domain_name'), |
| 273 |
('domain_offset','<L=0'), |
| 274 |
('host_len','<H-host_name'), |
| 275 |
('host_maxlen','<H-host_name'), |
| 276 |
('host_offset','<L=0'), |
| 277 |
('os_version',':'), |
| 278 |
('host_name',':'), |
| 279 |
('domain_name',':')) |
| 280 |
|
| 281 |
def __init__(self): |
| 282 |
Structure.__init__(self) |
| 283 |
self['flags']= ( |
| 284 |
NTLMSSP_NEGOTIATE_128 | |
| 285 |
NTLMSSP_NEGOTIATE_KEY_EXCH| |
| 286 |
# NTLMSSP_LM_KEY | |
| 287 |
NTLMSSP_NEGOTIATE_NTLM | |
| 288 |
NTLMSSP_NEGOTIATE_UNICODE | |
| 289 |
# NTLMSSP_ALWAYS_SIGN | |
| 290 |
NTLMSSP_NEGOTIATE_SIGN | |
| 291 |
NTLMSSP_NEGOTIATE_SEAL | |
| 292 |
# NTLMSSP_TARGET | |
| 293 |
0) |
| 294 |
self['host_name']='' |
| 295 |
self['domain_name']='' |
| 296 |
self['os_version']='' |
| 297 |
|
| 298 |
def getData(self): |
| 299 |
if len(self.fields['host_name']) > 0: |
| 300 |
self['flags'] |= NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED |
| 301 |
if len(self.fields['domain_name']) > 0: |
| 302 |
self['flags'] |= NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED |
| 303 |
if len(self.fields['os_version']) > 0: |
| 304 |
self['flags'] |= NTLMSSP_NEGOTIATE_VERSION |
| 305 |
if (self['flags'] & NTLMSSP_NEGOTIATE_VERSION) == NTLMSSP_NEGOTIATE_VERSION: |
| 306 |
version_len = 8 |
| 307 |
else: |
| 308 |
version_len = 0 |
| 309 |
if (self['flags'] & NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED) == NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED: |
| 310 |
self['host_offset']=32 + version_len |
| 311 |
if (self['flags'] & NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED) == NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED: |
| 312 |
self['domain_offset']=32+len(self['host_name']) + version_len |
| 313 |
return Structure.getData(self) |
| 314 |
|
| 315 |
def fromString(self,data): |
| 316 |
Structure.fromString(self,data) |
| 317 |
|
| 318 |
domain_offset = self['domain_offset'] |
| 319 |
domain_end = self['domain_len'] + domain_offset |
| 320 |
self['domain_name'] = data[ domain_offset : domain_end ] |
| 321 |
|
| 322 |
host_offset = self['host_offset'] |
| 323 |
host_end = self['host_len'] + host_offset |
| 324 |
self['host_name'] = data[ host_offset : host_end ] |
| 325 |
|
| 326 |
hasOsInfo = self['flags'] & NTLMSSP_NEGOTIATE_VERSION |
| 327 |
if len(data) >= 36 and hasOsInfo: |
| 328 |
self['os_version'] = data[32:40] |
| 329 |
else: |
| 330 |
self['os_version'] = '' |
| 331 |
|
| 332 |
class NTLMAuthChallenge(Structure): |
| 333 |
|
| 334 |
structure = ( |
| 335 |
('','"NTLMSSP\x00'), |
| 336 |
('message_type','<L=2'), |
| 337 |
('domain_len','<H-domain_name'), |
| 338 |
('domain_max_len','<H-domain_name'), |
| 339 |
('domain_offset','<L=40'), |
| 340 |
('flags','<L=0'), |
| 341 |
('challenge','8s'), |
| 342 |
('reserved','8s=""'), |
| 343 |
('TargetInfoFields_len','<H-TargetInfoFields'), |
| 344 |
('TargetInfoFields_max_len','<H-TargetInfoFields'), |
| 345 |
('TargetInfoFields_offset','<L'), |
| 346 |
('VersionLen','_-Version','self.checkVersion(self["flags"])'), |
| 347 |
('Version',':'), |
| 348 |
('domain_name',':'), |
| 349 |
('TargetInfoFields',':')) |
| 350 |
|
| 351 |
def checkVersion(self, flags): |
| 352 |
if flags is not None: |
| 353 |
if flags & NTLMSSP_NEGOTIATE_VERSION == 0: |
| 354 |
return 0 |
| 355 |
return 8 |
| 356 |
|
| 357 |
def getData(self): |
| 358 |
if self['TargetInfoFields'] is not None and type(self['TargetInfoFields']) is not str: |
| 359 |
raw_av_fields = self['TargetInfoFields'].getData() |
| 360 |
self['TargetInfoFields'] = raw_av_fields |
| 361 |
return Structure.getData(self) |
| 362 |
|
| 363 |
def fromString(self,data): |
| 364 |
Structure.fromString(self,data) |
| 365 |
# Just in case there's more data after the TargetInfoFields |
| 366 |
self['TargetInfoFields'] = self['TargetInfoFields'][:self['TargetInfoFields_len']] |
| 367 |
# We gotta process the TargetInfoFields |
| 368 |
#if self['TargetInfoFields_len'] > 0: |
| 369 |
# av_pairs = AV_PAIRS(self['TargetInfoFields'][:self['TargetInfoFields_len']]) |
| 370 |
# self['TargetInfoFields'] = av_pairs |
| 371 |
|
| 372 |
return self |
| 373 |
|
| 374 |
class NTLMAuthChallengeResponse(Structure, NTLMAuthMixin): |
| 375 |
|
| 376 |
structure = ( |
| 377 |
('','"NTLMSSP\x00'), |
| 378 |
('message_type','<L=3'), |
| 379 |
('lanman_len','<H-lanman'), |
| 380 |
('lanman_max_len','<H-lanman'), |
| 381 |
('lanman_offset','<L'), |
| 382 |
('ntlm_len','<H-ntlm'), |
| 383 |
('ntlm_max_len','<H-ntlm'), |
| 384 |
('ntlm_offset','<L'), |
| 385 |
('domain_len','<H-domain_name'), |
| 386 |
('domain_max_len','<H-domain_name'), |
| 387 |
('domain_offset','<L'), |
| 388 |
('user_len','<H-user_name'), |
| 389 |
('user_max_len','<H-user_name'), |
| 390 |
('user_offset','<L'), |
| 391 |
('host_len','<H-host_name'), |
| 392 |
('host_max_len','<H-host_name'), |
| 393 |
('host_offset','<L'), |
| 394 |
('session_key_len','<H-session_key'), |
| 395 |
('session_key_max_len','<H-session_key'), |
| 396 |
('session_key_offset','<L'), |
| 397 |
('flags','<L'), |
| 398 |
('VersionLen','_-Version','self.checkVersion(self["flags"])'), |
| 399 |
('Version',':=""'), |
| 400 |
('MICLen','_-MIC','self.checkMIC(self["flags"])'), |
| 401 |
('MIC',':=""'), |
| 402 |
('domain_name',':'), |
| 403 |
('user_name',':'), |
| 404 |
('host_name',':'), |
| 405 |
('lanman',':'), |
| 406 |
('ntlm',':'), |
| 407 |
('session_key',':')) |
| 408 |
|
| 409 |
def __init__(self, username = '', password = '', challenge = '', lmhash = '', nthash = '', flags = 0): |
| 410 |
Structure.__init__(self) |
| 411 |
self['session_key']='' |
| 412 |
self['user_name']=username.encode('utf-16le') |
| 413 |
self['domain_name']='' #"CLON".encode('utf-16le') |
| 414 |
self['host_name']='' #"BETS".encode('utf-16le') |
| 415 |
self['flags'] = ( #authResp['flags'] |
| 416 |
# we think (beto & gera) that his flags force a memory conten leakage when a windows 2000 answers using uninitializaed verifiers |
| 417 |
NTLMSSP_NEGOTIATE_128 | |
| 418 |
NTLMSSP_NEGOTIATE_KEY_EXCH| |
| 419 |
# NTLMSSP_LM_KEY | |
| 420 |
NTLMSSP_NEGOTIATE_NTLM | |
| 421 |
NTLMSSP_NEGOTIATE_UNICODE | |
| 422 |
# NTLMSSP_ALWAYS_SIGN | |
| 423 |
NTLMSSP_NEGOTIATE_SIGN | |
| 424 |
NTLMSSP_NEGOTIATE_SEAL | |
| 425 |
# NTLMSSP_TARGET | |
| 426 |
0) |
| 427 |
# Here we do the stuff |
| 428 |
if username and ( lmhash != '' or nthash != ''): |
| 429 |
self['lanman'] = get_ntlmv1_response(lmhash, challenge) |
| 430 |
self['ntlm'] = get_ntlmv1_response(nthash, challenge) |
| 431 |
elif (username and password): |
| 432 |
lmhash = compute_lmhash(password) |
| 433 |
nthash = compute_nthash(password) |
| 434 |
self['lanman']=get_ntlmv1_response(lmhash, challenge) |
| 435 |
self['ntlm']=get_ntlmv1_response(nthash, challenge) # This is not used for LM_KEY nor NTLM_KEY |
| 436 |
else: |
| 437 |
self['lanman'] = '' |
| 438 |
self['ntlm'] = '' |
| 439 |
if not self['host_name']: |
| 440 |
self['host_name'] = 'NULL'.encode('utf-16le') # for NULL session there must be a hostname |
| 441 |
|
| 442 |
def checkVersion(self, flags): |
| 443 |
if flags is not None: |
| 444 |
if flags & NTLMSSP_NEGOTIATE_VERSION == 0: |
| 445 |
return 0 |
| 446 |
return 8 |
| 447 |
|
| 448 |
def checkMIC(self, flags): |
| 449 |
# TODO: Find a proper way to check the MIC is in there |
| 450 |
if flags is not None: |
| 451 |
if flags & NTLMSSP_NEGOTIATE_VERSION == 0: |
| 452 |
return 0 |
| 453 |
return 16 |
| 454 |
|
| 455 |
def getData(self): |
| 456 |
self['domain_offset']=64+self.checkMIC(self["flags"])+self.checkVersion(self["flags"]) |
| 457 |
self['user_offset']=64+self.checkMIC(self["flags"])+self.checkVersion(self["flags"])+len(self['domain_name']) |
| 458 |
self['host_offset']=self['user_offset']+len(self['user_name']) |
| 459 |
self['lanman_offset']=self['host_offset']+len(self['host_name']) |
| 460 |
self['ntlm_offset']=self['lanman_offset']+len(self['lanman']) |
| 461 |
self['session_key_offset']=self['ntlm_offset']+len(self['ntlm']) |
| 462 |
return Structure.getData(self) |
| 463 |
|
| 464 |
def fromString(self,data): |
| 465 |
Structure.fromString(self,data) |
| 466 |
# [MS-NLMP] page 27 |
| 467 |
# Payload data can be present in any order within the Payload field, |
| 468 |
# with variable-length padding before or after the data |
| 469 |
|
| 470 |
domain_offset = self['domain_offset'] |
| 471 |
domain_end = self['domain_len'] + domain_offset |
| 472 |
self['domain_name'] = data[ domain_offset : domain_end ] |
| 473 |
|
| 474 |
host_offset = self['host_offset'] |
| 475 |
host_end = self['host_len'] + host_offset |
| 476 |
self['host_name'] = data[ host_offset: host_end ] |
| 477 |
|
| 478 |
user_offset = self['user_offset'] |
| 479 |
user_end = self['user_len'] + user_offset |
| 480 |
self['user_name'] = data[ user_offset: user_end ] |
| 481 |
|
| 482 |
ntlm_offset = self['ntlm_offset'] |
| 483 |
ntlm_end = self['ntlm_len'] + ntlm_offset |
| 484 |
self['ntlm'] = data[ ntlm_offset : ntlm_end ] |
| 485 |
|
| 486 |
lanman_offset = self['lanman_offset'] |
| 487 |
lanman_end = self['lanman_len'] + lanman_offset |
| 488 |
self['lanman'] = data[ lanman_offset : lanman_end] |
| 489 |
|
| 490 |
#if len(data) >= 36: |
| 491 |
# self['os_version'] = data[32:36] |
| 492 |
#else: |
| 493 |
# self['os_version'] = '' |
| 494 |
|
| 495 |
class ImpacketStructure(Structure): |
| 496 |
def set_parent(self, other): |
| 497 |
self.parent = other |
| 498 |
|
| 499 |
def get_packet(self): |
| 500 |
return str(self) |
| 501 |
|
| 502 |
def get_size(self): |
| 503 |
return len(self) |
| 504 |
|
| 505 |
class ExtendedOrNotMessageSignature(Structure): |
| 506 |
def __init__(self, flags = 0, **kargs): |
| 507 |
if flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 508 |
self.structure = self.extendedMessageSignature |
| 509 |
else: |
| 510 |
self.structure = self.MessageSignature |
| 511 |
return Structure.__init__(self, **kargs) |
| 512 |
|
| 513 |
class NTLMMessageSignature(ExtendedOrNotMessageSignature): |
| 514 |
extendedMessageSignature = ( |
| 515 |
('Version','<L=1'), |
| 516 |
('Checksum','<q'), |
| 517 |
('SeqNum','<i'), |
| 518 |
) |
| 519 |
|
| 520 |
MessageSignature = ( |
| 521 |
('Version','<L=1'), |
| 522 |
('RandomPad','<i=0'), |
| 523 |
('Checksum','<i'), |
| 524 |
('SeqNum','<i'), |
| 525 |
) |
| 526 |
|
| 527 |
KNOWN_DES_INPUT = "KGS!@#$%" |
| 528 |
|
| 529 |
def __expand_DES_key( key): |
| 530 |
# Expand the key from a 7-byte password key into a 8-byte DES key |
| 531 |
key = key[:7] |
| 532 |
key += '\x00'*(7-len(key)) |
| 533 |
s = chr(((ord(key[0]) >> 1) & 0x7f) << 1) |
| 534 |
s = s + chr(((ord(key[0]) & 0x01) << 6 | ((ord(key[1]) >> 2) & 0x3f)) << 1) |
| 535 |
s = s + chr(((ord(key[1]) & 0x03) << 5 | ((ord(key[2]) >> 3) & 0x1f)) << 1) |
| 536 |
s = s + chr(((ord(key[2]) & 0x07) << 4 | ((ord(key[3]) >> 4) & 0x0f)) << 1) |
| 537 |
s = s + chr(((ord(key[3]) & 0x0f) << 3 | ((ord(key[4]) >> 5) & 0x07)) << 1) |
| 538 |
s = s + chr(((ord(key[4]) & 0x1f) << 2 | ((ord(key[5]) >> 6) & 0x03)) << 1) |
| 539 |
s = s + chr(((ord(key[5]) & 0x3f) << 1 | ((ord(key[6]) >> 7) & 0x01)) << 1) |
| 540 |
s = s + chr((ord(key[6]) & 0x7f) << 1) |
| 541 |
return s |
| 542 |
|
| 543 |
def __DES_block(key, msg): |
| 544 |
if POW: |
| 545 |
cipher = POW.Symmetric(POW.DES_ECB) |
| 546 |
cipher.encryptInit(__expand_DES_key(key)) |
| 547 |
return cipher.update(msg) |
| 548 |
else: |
| 549 |
cipher = DES.new(__expand_DES_key(key),DES.MODE_ECB) |
| 550 |
return cipher.encrypt(msg) |
| 551 |
|
| 552 |
def ntlmssp_DES_encrypt(key, challenge): |
| 553 |
answer = __DES_block(key[:7], challenge) |
| 554 |
answer += __DES_block(key[7:14], challenge) |
| 555 |
answer += __DES_block(key[14:], challenge) |
| 556 |
return answer |
| 557 |
|
| 558 |
# High level functions to use NTLMSSP |
| 559 |
|
| 560 |
def getNTLMSSPType1(workstation='', domain='', signingRequired = False, use_ntlmv2 = USE_NTLMv2): |
| 561 |
# Let's do some encoding checks before moving on. Kind of dirty, but found effective when dealing with |
| 562 |
# international characters. |
| 563 |
import sys |
| 564 |
encoding = sys.getfilesystemencoding() |
| 565 |
if encoding is not None: |
| 566 |
try: |
| 567 |
workstation.encode('utf-16le') |
| 568 |
except: |
| 569 |
workstation = workstation.decode(encoding) |
| 570 |
try: |
| 571 |
domain.encode('utf-16le') |
| 572 |
except: |
| 573 |
domain = domain.decode(encoding) |
| 574 |
|
| 575 |
# Let's prepare a Type 1 NTLMSSP Message |
| 576 |
auth = NTLMAuthNegotiate() |
| 577 |
auth['flags']=0 |
| 578 |
if signingRequired: |
| 579 |
auth['flags'] = NTLMSSP_NEGOTIATE_KEY_EXCH | NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL |
| 580 |
if use_ntlmv2: |
| 581 |
auth['flags'] |= NTLMSSP_NEGOTIATE_TARGET_INFO |
| 582 |
auth['flags'] |= NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY | NTLMSSP_NEGOTIATE_UNICODE | NTLMSSP_REQUEST_TARGET | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_56 |
| 583 |
auth['domain_name'] = domain.encode('utf-16le') |
| 584 |
return auth |
| 585 |
|
| 586 |
def getNTLMSSPType3(type1, type2, user, password, domain, lmhash = '', nthash = '', use_ntlmv2 = USE_NTLMv2): |
| 587 |
|
| 588 |
# Let's do some encoding checks before moving on. Kind of dirty, but found effective when dealing with |
| 589 |
# international characters. |
| 590 |
import sys |
| 591 |
encoding = sys.getfilesystemencoding() |
| 592 |
if encoding is not None: |
| 593 |
try: |
| 594 |
user.encode('utf-16le') |
| 595 |
except: |
| 596 |
user = user.decode(encoding) |
| 597 |
try: |
| 598 |
password.encode('utf-16le') |
| 599 |
except: |
| 600 |
password = password.decode(encoding) |
| 601 |
try: |
| 602 |
domain.encode('utf-16le') |
| 603 |
except: |
| 604 |
domain = user.decode(encoding) |
| 605 |
|
| 606 |
ntlmChallenge = NTLMAuthChallenge(type2) |
| 607 |
|
| 608 |
# Let's start with the original flags sent in the type1 message |
| 609 |
responseFlags = type1['flags'] |
| 610 |
|
| 611 |
# Token received and parsed. Depending on the authentication |
| 612 |
# method we will create a valid ChallengeResponse |
| 613 |
ntlmChallengeResponse = NTLMAuthChallengeResponse(user, password, ntlmChallenge['challenge']) |
| 614 |
|
| 615 |
clientChallenge = "".join([random.choice(string.digits+string.letters) for i in range(8)]) |
| 616 |
|
| 617 |
serverName = ntlmChallenge['TargetInfoFields'] |
| 618 |
|
| 619 |
ntResponse, lmResponse, sessionBaseKey = computeResponse(ntlmChallenge['flags'], ntlmChallenge['challenge'], clientChallenge, serverName, domain, user, password, lmhash, nthash, use_ntlmv2 ) |
| 620 |
|
| 621 |
# Let's check the return flags |
| 622 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY) == 0: |
| 623 |
# No extended session security, taking it out |
| 624 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY |
| 625 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_128 ) == 0: |
| 626 |
# No support for 128 key len, taking it out |
| 627 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_128 |
| 628 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_KEY_EXCH) == 0: |
| 629 |
# No key exchange supported, taking it out |
| 630 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_KEY_EXCH |
| 631 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_SEAL) == 0: |
| 632 |
# No sign available, taking it out |
| 633 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_SEAL |
| 634 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_SIGN) == 0: |
| 635 |
# No sign available, taking it out |
| 636 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_SIGN |
| 637 |
if (ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_ALWAYS_SIGN) == 0: |
| 638 |
# No sign available, taking it out |
| 639 |
responseFlags &= 0xffffffff ^ NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
| 640 |
|
| 641 |
keyExchangeKey = KXKEY(ntlmChallenge['flags'],sessionBaseKey, lmResponse, ntlmChallenge['challenge'], password, lmhash, nthash,use_ntlmv2) |
| 642 |
|
| 643 |
# Special case for anonymous login |
| 644 |
if user == '' and password == '' and lmhash == '' and nthash == '': |
| 645 |
keyExchangeKey = '\x00'*16 |
| 646 |
|
| 647 |
# If we set up key exchange, let's fill the right variables |
| 648 |
if ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_KEY_EXCH: |
| 649 |
# not exactly what I call random tho :\ |
| 650 |
# exportedSessionKey = this is the key we should use to sign |
| 651 |
exportedSessionKey = "".join([random.choice(string.digits+string.letters) for i in range(16)]) |
| 652 |
#exportedSessionKey = "A"*16 |
| 653 |
#print "keyExchangeKey %r" % keyExchangeKey |
| 654 |
# Let's generate the right session key based on the challenge flags |
| 655 |
#if responseFlags & NTLMSSP_NTLM2_KEY: |
| 656 |
# Extended session security enabled |
| 657 |
# if responseFlags & NTLMSSP_KEY_128: |
| 658 |
# Full key |
| 659 |
# exportedSessionKey = exportedSessionKey |
| 660 |
# elif responseFlags & NTLMSSP_KEY_56: |
| 661 |
# Only 56-bit key |
| 662 |
# exportedSessionKey = exportedSessionKey[:7] |
| 663 |
# else: |
| 664 |
# exportedSessionKey = exportedSessionKey[:5] |
| 665 |
#elif responseFlags & NTLMSSP_KEY_56: |
| 666 |
# No extended session security, just 56 bits key |
| 667 |
# exportedSessionKey = exportedSessionKey[:7] + '\xa0' |
| 668 |
#else: |
| 669 |
# exportedSessionKey = exportedSessionKey[:5] + '\xe5\x38\xb0' |
| 670 |
|
| 671 |
encryptedRandomSessionKey = generateEncryptedSessionKey(keyExchangeKey, exportedSessionKey) |
| 672 |
else: |
| 673 |
encryptedRandomSessionKey = None |
| 674 |
# [MS-NLMP] page 46 |
| 675 |
exportedSessionKey = keyExchangeKey |
| 676 |
|
| 677 |
ntlmChallengeResponse['flags'] = responseFlags |
| 678 |
ntlmChallengeResponse['domain_name'] = domain.encode('utf-16le') |
| 679 |
ntlmChallengeResponse['lanman'] = lmResponse |
| 680 |
ntlmChallengeResponse['ntlm'] = ntResponse |
| 681 |
if encryptedRandomSessionKey is not None: |
| 682 |
ntlmChallengeResponse['session_key'] = encryptedRandomSessionKey |
| 683 |
|
| 684 |
return ntlmChallengeResponse, exportedSessionKey |
| 685 |
|
| 686 |
|
| 687 |
# NTLMv1 Algorithm |
| 688 |
|
| 689 |
def generateSessionKeyV1(password, lmhash, nthash): |
| 690 |
if POW: |
| 691 |
hash = POW.Digest(POW.MD4_DIGEST) |
| 692 |
else: |
| 693 |
hash = MD4.new() |
| 694 |
hash.update(NTOWFv1(password, lmhash, nthash)) |
| 695 |
return hash.digest() |
| 696 |
|
| 697 |
def computeResponseNTLMv1(flags, serverChallenge, clientChallenge, serverName, domain, user, password, lmhash='', nthash='', use_ntlmv2 = USE_NTLMv2): |
| 698 |
if (user == '' and password == ''): |
| 699 |
# Special case for anonymous authentication |
| 700 |
lmResponse = '' |
| 701 |
ntResponse = '' |
| 702 |
else: |
| 703 |
lmhash = LMOWFv1(password, lmhash, nthash) |
| 704 |
nthash = NTOWFv1(password, lmhash, nthash) |
| 705 |
if flags & NTLMSSP_NEGOTIATE_LM_KEY: |
| 706 |
ntResponse = '' |
| 707 |
lmResponse = get_ntlmv1_response(lmhash, serverChallenge) |
| 708 |
elif flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 709 |
md5 = hashlib.new('md5') |
| 710 |
chall = (serverChallenge + clientChallenge) |
| 711 |
md5.update(chall) |
| 712 |
ntResponse = ntlmssp_DES_encrypt(nthash, md5.digest()[:8]) |
| 713 |
lmResponse = clientChallenge + '\x00'*16 |
| 714 |
else: |
| 715 |
ntResponse = get_ntlmv1_response(nthash,serverChallenge) |
| 716 |
lmResponse = get_ntlmv1_response(lmhash, serverChallenge) |
| 717 |
|
| 718 |
sessionBaseKey = generateSessionKeyV1(password, lmhash, nthash) |
| 719 |
return ntResponse, lmResponse, sessionBaseKey |
| 720 |
|
| 721 |
def compute_lmhash(password): |
| 722 |
# This is done according to Samba's encryption specification (docs/html/ENCRYPTION.html) |
| 723 |
password = password.upper() |
| 724 |
lmhash = __DES_block(password[:7], KNOWN_DES_INPUT) |
| 725 |
lmhash += __DES_block(password[7:14], KNOWN_DES_INPUT) |
| 726 |
return lmhash |
| 727 |
|
| 728 |
def NTOWFv1(password, lmhash = '', nthash=''): |
| 729 |
if nthash != '': |
| 730 |
return nthash |
| 731 |
return compute_nthash(password) |
| 732 |
|
| 733 |
def LMOWFv1(password, lmhash = '', nthash=''): |
| 734 |
if lmhash != '': |
| 735 |
return lmhash |
| 736 |
return compute_lmhash(password) |
| 737 |
|
| 738 |
def compute_nthash(password): |
| 739 |
# This is done according to Samba's encryption specification (docs/html/ENCRYPTION.html) |
| 740 |
try: |
| 741 |
password = unicode(password).encode('utf_16le') |
| 742 |
except NameError: # unicode() was removed in Python 3 |
| 743 |
password = str(password).encode('utf_16le') |
| 744 |
except UnicodeDecodeError: |
| 745 |
import sys |
| 746 |
password = password.decode(sys.getfilesystemencoding()).encode('utf_16le') |
| 747 |
|
| 748 |
if POW: |
| 749 |
hash = POW.Digest(POW.MD4_DIGEST) |
| 750 |
else: |
| 751 |
hash = MD4.new() |
| 752 |
hash.update(password) |
| 753 |
return hash.digest() |
| 754 |
|
| 755 |
def get_ntlmv1_response(key, challenge): |
| 756 |
return ntlmssp_DES_encrypt(key, challenge) |
| 757 |
|
| 758 |
# NTLMv2 Algorithm - as described in MS-NLMP Section 3.3.2 |
| 759 |
|
| 760 |
# Crypto Stuff |
| 761 |
|
| 762 |
def MAC(flags, handle, signingKey, seqNum, message): |
| 763 |
# [MS-NLMP] Section 3.4.4 |
| 764 |
# Returns the right messageSignature depending on the flags |
| 765 |
messageSignature = NTLMMessageSignature(flags) |
| 766 |
if flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 767 |
if flags & NTLMSSP_NEGOTIATE_KEY_EXCH: |
| 768 |
messageSignature['Version'] = 1 |
| 769 |
messageSignature['Checksum'] = struct.unpack('<q',handle(hmac_md5(signingKey, struct.pack('<i',seqNum)+message)[:8]))[0] |
| 770 |
messageSignature['SeqNum'] = seqNum |
| 771 |
seqNum += 1 |
| 772 |
else: |
| 773 |
messageSignature['Version'] = 1 |
| 774 |
messageSignature['Checksum'] = struct.unpack('<q',hmac_md5(signingKey, struct.pack('<i',seqNum)+message)[:8])[0] |
| 775 |
messageSignature['SeqNum'] = seqNum |
| 776 |
seqNum += 1 |
| 777 |
else: |
| 778 |
messageSignature['Version'] = 1 |
| 779 |
messageSignature['Checksum'] = struct.pack('<i',binascii.crc32(message)) |
| 780 |
messageSignature['RandomPad'] = 0 |
| 781 |
messageSignature['RandomPad'] = handle(struct.pack('<i',messageSignature['RandomPad'])) |
| 782 |
messageSignature['Checksum'] = struct.unpack('<i',handle(messageSignature['Checksum']))[0] |
| 783 |
messageSignature['SeqNum'] = handle('\x00\x00\x00\x00') |
| 784 |
messageSignature['SeqNum'] = struct.unpack('<i',messageSignature['SeqNum'])[0] ^ seqNum |
| 785 |
messageSignature['RandomPad'] = 0 |
| 786 |
|
| 787 |
return messageSignature |
| 788 |
|
| 789 |
def SEAL(flags, signingKey, sealingKey, messageToSign, messageToEncrypt, seqNum, handle): |
| 790 |
sealedMessage = handle(messageToEncrypt) |
| 791 |
signature = MAC(flags, handle, signingKey, seqNum, messageToSign) |
| 792 |
return sealedMessage, signature |
| 793 |
|
| 794 |
def SIGN(flags, signingKey, message, seqNum, handle): |
| 795 |
return MAC(flags, handle, signingKey, seqNum, message) |
| 796 |
|
| 797 |
def SIGNKEY(flags, randomSessionKey, mode = 'Client'): |
| 798 |
if flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 799 |
if mode == 'Client': |
| 800 |
md5 = hashlib.new('md5') |
| 801 |
md5.update(randomSessionKey + "session key to client-to-server signing key magic constant\x00") |
| 802 |
signKey = md5.digest() |
| 803 |
else: |
| 804 |
md5 = hashlib.new('md5') |
| 805 |
md5.update(randomSessionKey + "session key to server-to-client signing key magic constant\x00") |
| 806 |
signKey = md5.digest() |
| 807 |
else: |
| 808 |
signKey = None |
| 809 |
return signKey |
| 810 |
|
| 811 |
def SEALKEY(flags, randomSessionKey, mode = 'Client'): |
| 812 |
if flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 813 |
if flags & NTLMSSP_NEGOTIATE_128: |
| 814 |
sealKey = randomSessionKey |
| 815 |
elif flags & NTLMSSP_NEGOTIATE_56: |
| 816 |
sealKey = randomSessionKey[:7] |
| 817 |
else: |
| 818 |
sealKey = randomSessionKey[:5] |
| 819 |
|
| 820 |
if mode == 'Client': |
| 821 |
md5 = hashlib.new('md5') |
| 822 |
md5.update(sealKey + 'session key to client-to-server sealing key magic constant\x00') |
| 823 |
sealKey = md5.digest() |
| 824 |
else: |
| 825 |
md5 = hashlib.new('md5') |
| 826 |
md5.update(sealKey + 'session key to server-to-client sealing key magic constant\x00') |
| 827 |
sealKey = md5.digest() |
| 828 |
|
| 829 |
elif flags & NTLMSSP_NEGOTIATE_56: |
| 830 |
sealKey = randomSessionKey[:7] + '\xa0' |
| 831 |
else: |
| 832 |
sealKey = randomSessionKey[:5] + '\xe5\x38\xb0' |
| 833 |
|
| 834 |
return sealKey |
| 835 |
|
| 836 |
|
| 837 |
def generateEncryptedSessionKey(keyExchangeKey, exportedSessionKey): |
| 838 |
if POW: |
| 839 |
cipher = POW.Symmetric(POW.RC4) |
| 840 |
cipher.encryptInit(keyExchangeKey) |
| 841 |
cipher_encrypt = cipher.update |
| 842 |
else: |
| 843 |
cipher = ARC4.new(keyExchangeKey) |
| 844 |
cipher_encrypt = cipher.encrypt |
| 845 |
|
| 846 |
sessionKey = cipher_encrypt(exportedSessionKey) |
| 847 |
return sessionKey |
| 848 |
|
| 849 |
def KXKEY(flags, sessionBaseKey, lmChallengeResponse, serverChallenge, password, lmhash, nthash, use_ntlmv2 = USE_NTLMv2): |
| 850 |
if use_ntlmv2: |
| 851 |
return sessionBaseKey |
| 852 |
|
| 853 |
if flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY: |
| 854 |
if flags & NTLMSSP_NEGOTIATE_NTLM: |
| 855 |
keyExchangeKey = hmac_md5(sessionBaseKey, serverChallenge + lmChallengeResponse[:8]) |
| 856 |
else: |
| 857 |
keyExchangeKey = sessionBaseKey |
| 858 |
elif flags & NTLMSSP_NEGOTIATE_NTLM: |
| 859 |
if flags & NTLMSSP_NEGOTIATE_LM_KEY: |
| 860 |
keyExchangeKey = __DES_block(LMOWFv1(password,lmhash)[:7], lmChallengeResponse[:8]) + __DES_block(LMOWFv1(password,lmhash)[7] + '\xBD\xBD\xBD\xBD\xBD\xBD', lmChallengeResponse[:8]) |
| 861 |
elif flags & NTLMSSP_REQUEST_NON_NT_SESSION_KEY: |
| 862 |
keyExchangeKey = LMOWFv1(password,lmhash)[:8] + '\x00'*8 |
| 863 |
else: |
| 864 |
keyExchangeKey = sessionBaseKey |
| 865 |
else: |
| 866 |
raise "Can't create a valid KXKEY!" |
| 867 |
|
| 868 |
return keyExchangeKey |
| 869 |
|
| 870 |
def hmac_md5(key, data): |
| 871 |
if POW: |
| 872 |
h = POW.Hmac(POW.MD5_DIGEST, key) |
| 873 |
h.update(data) |
| 874 |
result = h.mac() |
| 875 |
else: |
| 876 |
import hmac |
| 877 |
h = hmac.new(key) |
| 878 |
h.update(data) |
| 879 |
result = h.digest() |
| 880 |
return result |
| 881 |
|
| 882 |
def NTOWFv2( user, password, domain, hash = ''): |
| 883 |
if hash != '': |
| 884 |
theHash = hash |
| 885 |
else: |
| 886 |
theHash = compute_nthash(password) |
| 887 |
return hmac_md5(theHash, user.upper().encode('utf-16le') + domain.encode('utf-16le')) |
| 888 |
|
| 889 |
def LMOWFv2( user, password, domain, lmhash = ''): |
| 890 |
return NTOWFv2( user, password, domain, lmhash) |
| 891 |
|
| 892 |
|
| 893 |
def computeResponseNTLMv2(flags, serverChallenge, clientChallenge, serverName, domain, user, password, lmhash = '', nthash = '', use_ntlmv2 = USE_NTLMv2): |
| 894 |
|
| 895 |
responseServerVersion = '\x01' |
| 896 |
hiResponseServerVersion = '\x01' |
| 897 |
responseKeyNT = NTOWFv2(user, password, domain, nthash) |
| 898 |
responseKeyLM = LMOWFv2(user, password, domain, lmhash) |
| 899 |
|
| 900 |
# If you're running test-ntlm, comment the following lines and uncoment the ones that are commented. Don't forget to turn it back after the tests! |
| 901 |
###################### |
| 902 |
av_pairs = AV_PAIRS(serverName) |
| 903 |
# In order to support SPN target name validation, we have to add this to the serverName av_pairs. Otherwise we will get access denied |
| 904 |
# This is set at Local Security Policy -> Local Policies -> Security Options -> Server SPN target name validation level |
| 905 |
av_pairs[NTLMSSP_AV_TARGET_NAME] = 'cifs/'.encode('utf-16le') + av_pairs[NTLMSSP_AV_HOSTNAME][1] |
| 906 |
if av_pairs[NTLMSSP_AV_TIME] is not None: |
| 907 |
aTime = av_pairs[NTLMSSP_AV_TIME][1] |
| 908 |
else: |
| 909 |
aTime = struct.pack('<q', (116444736000000000 + calendar.timegm(time.gmtime()) * 10000000) ) |
| 910 |
#aTime = '\x00'*8 |
| 911 |
av_pairs[NTLMSSP_AV_TIME] = aTime |
| 912 |
serverName = av_pairs.getData() |
| 913 |
|
| 914 |
###################### |
| 915 |
#aTime = '\x00'*8 |
| 916 |
###################### |
| 917 |
temp = responseServerVersion + hiResponseServerVersion + '\x00' * 6 + aTime + clientChallenge + '\x00' * 4 + serverName + '\x00' * 4 |
| 918 |
|
| 919 |
ntProofStr = hmac_md5(responseKeyNT, serverChallenge + temp) |
| 920 |
|
| 921 |
ntChallengeResponse = ntProofStr + temp |
| 922 |
lmChallengeResponse = hmac_md5(responseKeyNT, serverChallenge + clientChallenge) + clientChallenge |
| 923 |
sessionBaseKey = hmac_md5(responseKeyNT, ntProofStr) |
| 924 |
|
| 925 |
if (user == '' and password == ''): |
| 926 |
# Special case for anonymous authentication |
| 927 |
ntChallengeResponse = '' |
| 928 |
lmChallengeResponse = '' |
| 929 |
|
| 930 |
return ntChallengeResponse, lmChallengeResponse, sessionBaseKey |
| 931 |
|
| 932 |
class NTLM_HTTP(object): |
| 933 |
'''Parent class for NTLM HTTP classes.''' |
| 934 |
MSG_TYPE = None |
| 935 |
|
| 936 |
@classmethod |
| 937 |
def get_instace(cls,msg_64): |
| 938 |
msg = None |
| 939 |
msg_type = 0 |
| 940 |
if msg_64 != '': |
| 941 |
msg = base64.b64decode(msg_64[5:]) # Remove the 'NTLM ' |
| 942 |
msg_type = ord(msg[8]) |
| 943 |
|
| 944 |
for _cls in NTLM_HTTP.__subclasses__(): |
| 945 |
if msg_type == _cls.MSG_TYPE: |
| 946 |
instance = _cls() |
| 947 |
instance.fromString(msg) |
| 948 |
return instance |
| 949 |
|
| 950 |
|
| 951 |
class NTLM_HTTP_AuthRequired(NTLM_HTTP): |
| 952 |
commonHdr = () |
| 953 |
# Message 0 means the first HTTP request e.g. 'GET /bla.png' |
| 954 |
MSG_TYPE = 0 |
| 955 |
|
| 956 |
def fromString(self,data): |
| 957 |
pass |
| 958 |
|
| 959 |
|
| 960 |
class NTLM_HTTP_AuthNegotiate(NTLM_HTTP, NTLMAuthNegotiate): |
| 961 |
commonHdr = () |
| 962 |
MSG_TYPE = 1 |
| 963 |
|
| 964 |
def __init__(self): |
| 965 |
NTLMAuthNegotiate.__init__(self) |
| 966 |
|
| 967 |
|
| 968 |
class NTLM_HTTP_AuthChallengeResponse(NTLM_HTTP, NTLMAuthChallengeResponse): |
| 969 |
commonHdr = () |
| 970 |
MSG_TYPE = 3 |
| 971 |
|
| 972 |
def __init__(self): |
| 973 |
NTLMAuthChallengeResponse.__init__(self) |