From aba1c515534197cab1588ab0f3a2343be1f0a8e5 Mon Sep 17 00:00:00 2001 From: cclauss Date: Fri, 5 Apr 2019 01:32:39 +0200 Subject: tests: make Impacket (SMB server) Python 3 compatible Closes #3731 Fixes #3289 --- tests/python_dependencies/impacket/nmb.py | 46 ++--- tests/python_dependencies/impacket/ntlm.py | 78 ++++---- tests/python_dependencies/impacket/smb.py | 22 +-- tests/python_dependencies/impacket/smb3.py | 69 +++---- tests/python_dependencies/impacket/smbserver.py | 228 ++++++++++++------------ tests/python_dependencies/impacket/spnego.py | 154 ++++++++-------- tests/python_dependencies/impacket/structure.py | 69 +++---- tests/python_dependencies/impacket/uuid.py | 21 ++- 8 files changed, 352 insertions(+), 335 deletions(-) (limited to 'tests') diff --git a/tests/python_dependencies/impacket/nmb.py b/tests/python_dependencies/impacket/nmb.py index dc8777e65..791377bc2 100644 --- a/tests/python_dependencies/impacket/nmb.py +++ b/tests/python_dependencies/impacket/nmb.py @@ -1,3 +1,5 @@ +from __future__ import print_function +from __future__ import absolute_import # Copyright (c) 2003-2016 CORE Security Technologies # # This software is provided under under a slightly modified version @@ -40,7 +42,7 @@ from random import randint from struct import pack, unpack import time -from structure import Structure +from .structure import Structure CVS_REVISION = '$Revision: 526 $' @@ -454,7 +456,7 @@ class NetBIOS: except socket.error: pass if not has_bind: - raise NetBIOSError, ( 'Cannot bind to a good UDP port', ERRCLASS_OS, errno.EAGAIN ) + raise NetBIOSError( 'Cannot bind to a good UDP port', ERRCLASS_OS, errno.EAGAIN) self.__sock = s # Set the default NetBIOS domain nameserver. @@ -531,15 +533,15 @@ class NetBIOS: if res.get_rcode() == 0x03: return None else: - raise NetBIOSError, ( 'Negative name query response', ERRCLASS_QUERY, res.get_rcode() ) + raise NetBIOSError( 'Negative name query response', ERRCLASS_QUERY, res.get_rcode()) if res.get_ancount() != 1: raise NetBIOSError( 'Malformed response') return NBPositiveNameQueryResponse(res.get_answers()) - except select.error, ex: + except select.error as ex: if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN: - raise NetBIOSError, ( 'Error occurs while waiting for response', ERRCLASS_OS, ex[0] ) + raise NetBIOSError( 'Error occurs while waiting for response', ERRCLASS_OS, ex[0]) raise @@ -570,25 +572,25 @@ class NetBIOS: else: try: data, _ = self.__sock.recvfrom(65536, 0) - except Exception, e: - raise NetBIOSError, "recvfrom error: %s" % str(e) + except Exception as e: + raise NetBIOSError("recvfrom error: %s" % str(e)) self.__sock.close() res = NetBIOSPacket(data) if res.get_trn_id() == p.get_trn_id(): if res.get_rcode(): if res.get_rcode() == 0x03: # I'm just guessing here - raise NetBIOSError, "Cannot get data from server" + raise NetBIOSError("Cannot get data from server") else: - raise NetBIOSError, ( 'Negative name query response', ERRCLASS_QUERY, res.get_rcode() ) + raise NetBIOSError( 'Negative name query response', ERRCLASS_QUERY, res.get_rcode()) answ = NBNodeStatusResponse(res.get_answers()) self.mac = answ.get_mac() return answ.get_node_names() - except select.error, ex: + except select.error as ex: if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN: - raise NetBIOSError, ( 'Error occurs while waiting for response', ERRCLASS_OS, ex[0] ) - except socket.error, ex: - raise NetBIOSError, 'Connection error: %s' % str(ex) + raise NetBIOSError( 'Error occurs while waiting for response', ERRCLASS_OS, ex[0]) + except socket.error as ex: + raise NetBIOSError('Connection error: %s' % str(ex)) # Perform first and second level encoding of name as specified in RFC 1001 (Section 4) def encode_name(name, type, scope): @@ -841,7 +843,7 @@ class NetBIOSTCPSession(NetBIOSSession): af, socktype, proto, canonname, sa = socket.getaddrinfo(peer[0], peer[1], 0, socket.SOCK_STREAM)[0] sock = socket.socket(af, socktype, proto) sock.connect(sa) - except socket.error, e: + except socket.error as e: raise socket.error("Connection error (%s:%s)" % (peer[0], peer[1]), e) return sock @@ -866,7 +868,7 @@ class NetBIOSTCPSession(NetBIOSSession): while 1: p = self.recv_packet(timeout) if p.get_type() == NETBIOS_SESSION_NEGATIVE_RESPONSE: - raise NetBIOSError, ( 'Cannot request session', ERRCLASS_SESSION, ord(p.get_trailer()[0]) ) + raise NetBIOSError( 'Cannot request session', ERRCLASS_SESSION, ord(p.get_trailer()[0])) elif p.get_type() == NETBIOS_SESSION_POSITIVE_RESPONSE: break else: @@ -896,13 +898,13 @@ class NetBIOSTCPSession(NetBIOSSession): received = self._sock.recv(bytes_left) if len(received) == 0: - raise NetBIOSError, ( 'Error while reading from remote', ERRCLASS_OS, None) + raise NetBIOSError( 'Error while reading from remote', ERRCLASS_OS, None) data = data + received bytes_left = read_length - len(data) - except select.error, ex: + except select.error as ex: if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN: - raise NetBIOSError, ( 'Error occurs while reading from remote', ERRCLASS_OS, ex[0] ) + raise NetBIOSError( 'Error occurs while reading from remote', ERRCLASS_OS, ex[0]) return data @@ -919,13 +921,13 @@ class NetBIOSTCPSession(NetBIOSSession): received = self._sock.recv(bytes_left) if len(received) == 0: - raise NetBIOSError, ( 'Error while reading from remote', ERRCLASS_OS, None) + raise NetBIOSError( 'Error while reading from remote', ERRCLASS_OS, None) data = data + received bytes_left = read_length - len(data) - except select.error, ex: + except select.error as ex: if ex[0] != errno.EINTR and ex[0] != errno.EAGAIN: - raise NetBIOSError, ( 'Error occurs while reading from remote', ERRCLASS_OS, ex[0] ) + raise NetBIOSError( 'Error occurs while reading from remote', ERRCLASS_OS, ex[0]) return data @@ -974,7 +976,7 @@ def main(): n = get_netbios_host_by_name("some-host") - print n + print(n) if __name__ == '__main__': main() diff --git a/tests/python_dependencies/impacket/ntlm.py b/tests/python_dependencies/impacket/ntlm.py index 8376644bd..8845e9d50 100644 --- a/tests/python_dependencies/impacket/ntlm.py +++ b/tests/python_dependencies/impacket/ntlm.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright (c) 2003-2016 CORE Security Technologies: # # This software is provided under under a slightly modified version @@ -17,8 +18,8 @@ from impacket.structure import Structure from impacket import LOG -# This is important. NTLMv2 is not negotiated by the client or server. -# It is used if set locally on both sides. Change this item if you don't want to use +# This is important. NTLMv2 is not negotiated by the client or server. +# It is used if set locally on both sides. Change this item if you don't want to use # NTLMv2 by default and fall back to NTLMv1 (with EXTENDED_SESSION_SECURITY or not) # Check the following links: # http://davenport.sourceforge.net/ntlm.html @@ -209,7 +210,7 @@ class AV_PAIRS(): self.fields[key] = (len(value),value) def __getitem__(self, key): - if self.fields.has_key(key): + if key in self.fields: return self.fields[key] return None @@ -236,16 +237,16 @@ class AV_PAIRS(): def dump(self): for i in self.fields.keys(): - print "%s: {%r}" % (i,self[i]) + print("%s: {%r}" % (i,self[i])) def getData(self): - if self.fields.has_key(NTLMSSP_AV_EOL): + if NTLMSSP_AV_EOL in self.fields: del self.fields[NTLMSSP_AV_EOL] ans = '' for i in self.fields.keys(): ans+= struct.pack(' 0: self['flags'] |= NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED @@ -342,7 +343,7 @@ class NTLMAuthChallenge(Structure): ('TargetInfoFields_len',' 0: - # av_pairs = AV_PAIRS(self['TargetInfoFields'][:self['TargetInfoFields_len']]) + # av_pairs = AV_PAIRS(self['TargetInfoFields'][:self['TargetInfoFields_len']]) # self['TargetInfoFields'] = av_pairs return self - + class NTLMAuthChallengeResponse(Structure, NTLMAuthMixin): structure = ( @@ -394,7 +395,7 @@ class NTLMAuthChallengeResponse(Structure, NTLMAuthMixin): ('session_key_max_len','= 36: + #if len(data) >= 36: # self['os_version'] = data[32:36] #else: # self['os_version'] = '' @@ -607,11 +608,11 @@ def getNTLMSSPType3(type1, type2, user, password, domain, lmhash = '', nthash = # Let's start with the original flags sent in the type1 message responseFlags = type1['flags'] - # Token received and parsed. Depending on the authentication + # Token received and parsed. Depending on the authentication # method we will create a valid ChallengeResponse ntlmChallengeResponse = NTLMAuthChallengeResponse(user, password, ntlmChallenge['challenge']) - clientChallenge = "".join([random.choice(string.digits+string.letters) for i in xrange(8)]) + clientChallenge = "".join([random.choice(string.digits+string.letters) for i in range(8)]) serverName = ntlmChallenge['TargetInfoFields'] @@ -647,7 +648,7 @@ def getNTLMSSPType3(type1, type2, user, password, domain, lmhash = '', nthash = if ntlmChallenge['flags'] & NTLMSSP_NEGOTIATE_KEY_EXCH: # not exactly what I call random tho :\ # exportedSessionKey = this is the key we should use to sign - exportedSessionKey = "".join([random.choice(string.digits+string.letters) for i in xrange(16)]) + exportedSessionKey = "".join([random.choice(string.digits+string.letters) for i in range(16)]) #exportedSessionKey = "A"*16 #print "keyExchangeKey %r" % keyExchangeKey # Let's generate the right session key based on the challenge flags @@ -677,7 +678,7 @@ def getNTLMSSPType3(type1, type2, user, password, domain, lmhash = '', nthash = ntlmChallengeResponse['domain_name'] = domain.encode('utf-16le') ntlmChallengeResponse['lanman'] = lmResponse ntlmChallengeResponse['ntlm'] = ntResponse - if encryptedRandomSessionKey is not None: + if encryptedRandomSessionKey is not None: ntlmChallengeResponse['session_key'] = encryptedRandomSessionKey return ntlmChallengeResponse, exportedSessionKey @@ -688,13 +689,13 @@ def getNTLMSSPType3(type1, type2, user, password, domain, lmhash = '', nthash = def generateSessionKeyV1(password, lmhash, nthash): if POW: hash = POW.Digest(POW.MD4_DIGEST) - else: + else: hash = MD4.new() hash.update(NTOWFv1(password, lmhash, nthash)) return hash.digest() - + def computeResponseNTLMv1(flags, serverChallenge, clientChallenge, serverName, domain, user, password, lmhash='', nthash='', use_ntlmv2 = USE_NTLMv2): - if (user == '' and password == ''): + if (user == '' and password == ''): # Special case for anonymous authentication lmResponse = '' ntResponse = '' @@ -713,7 +714,7 @@ def computeResponseNTLMv1(flags, serverChallenge, clientChallenge, serverName, d else: ntResponse = get_ntlmv1_response(nthash,serverChallenge) lmResponse = get_ntlmv1_response(lmhash, serverChallenge) - + sessionBaseKey = generateSessionKeyV1(password, lmhash, nthash) return ntResponse, lmResponse, sessionBaseKey @@ -727,7 +728,7 @@ def compute_lmhash(password): def NTOWFv1(password, lmhash = '', nthash=''): if nthash != '': return nthash - return compute_nthash(password) + return compute_nthash(password) def LMOWFv1(password, lmhash = '', nthash=''): if lmhash != '': @@ -738,13 +739,15 @@ def compute_nthash(password): # This is done according to Samba's encryption specification (docs/html/ENCRYPTION.html) try: password = unicode(password).encode('utf_16le') + except NameError: # unicode() was removed in Python 3 + password = str(password).encode('utf_16le') except UnicodeDecodeError: import sys password = password.decode(sys.getfilesystemencoding()).encode('utf_16le') if POW: hash = POW.Digest(POW.MD4_DIGEST) - else: + else: hash = MD4.new() hash.update(password) return hash.digest() @@ -780,7 +783,7 @@ def MAC(flags, handle, signingKey, seqNum, message): messageSignature['SeqNum'] = handle('\x00\x00\x00\x00') messageSignature['SeqNum'] = struct.unpack('> 32 - y = t & 0xffffffffL + y = t & 0xffffffff geo_cal_offset = 11644473600.0 # = 369.0 * 365.25 * 24 * 60 * 60 - (3.0 * 24 * 60 * 60 + 6.0 * 60 * 60) - return (x * 4.0 * (1 << 30) + (y & 0xfff00000L)) * 1.0e-7 - geo_cal_offset + return (x * 4.0 * (1 << 30) + (y & 0xfff00000)) * 1.0e-7 - geo_cal_offset # Contain information about a SMB machine @@ -676,12 +676,12 @@ class NewSMBPacket(Structure): def __init__(self, **kargs): Structure.__init__(self, **kargs) - if self.fields.has_key('Flags2') is False: + if ('Flags2' in self.fields) is False: self['Flags2'] = 0 - if self.fields.has_key('Flags1') is False: + if ('Flags1' in self.fields) is False: self['Flags1'] = 0 - if not kargs.has_key('data'): + if 'data' not in kargs: self['Data'] = [] def addCommand(self, command): @@ -709,9 +709,9 @@ class NewSMBPacket(Structure): return 1 elif self.isMoreProcessingRequired(): return 1 - raise SessionError, ("SMB Library Error", self['ErrorClass'] + (self['_reserved'] << 8), self['ErrorCode'], self['Flags2'] & SMB.FLAGS2_NT_STATUS) + raise SessionError("SMB Library Error", self['ErrorClass'] + (self['_reserved'] << 8), self['ErrorCode'], self['Flags2'] & SMB.FLAGS2_NT_STATUS) else: - raise UnsupportedFeature, ("Unexpected answer from server: Got %d, Expected %d" % (self['Command'], cmd)) + raise UnsupportedFeature("Unexpected answer from server: Got %d, Expected %d" % (self['Command'], cmd)) class SMBCommand(Structure): @@ -2550,7 +2550,7 @@ class SMB: if s.get_error_class() == 0x00 and s.get_error_code() == 0x00: return 1 else: - raise SessionError, ( "SMB Library Error", s.get_error_class()+ (s.get_reserved() << 8), s.get_error_code() , s.get_flags2() & SMB.FLAGS2_NT_STATUS ) + raise SessionError( "SMB Library Error", s.get_error_class()+ (s.get_reserved() << 8), s.get_error_code() , s.get_flags2() & SMB.FLAGS2_NT_STATUS) else: break return 0 @@ -2583,7 +2583,7 @@ class SMB: self.__server_name = self._dialects_data['ServerName'] if self._dialects_parameters['DialectIndex'] == 0xffff: - raise UnsupportedFeature,"Remote server does not know NT LM 0.12" + raise UnsupportedFeature("Remote server does not know NT LM 0.12") return 1 else: return 0 @@ -2734,7 +2734,7 @@ class SMB: self._SigningSessionKey = key def get_encryption_key(self): - if self._dialects_data.fields.has_key('Challenge'): + if 'Challenge' in self._dialects_data.fields: return self._dialects_data['Challenge'] else: return None @@ -3241,7 +3241,7 @@ class SMB: pass # Parse Version to know the target Operating system name. Not provided elsewhere anymore - if ntlmChallenge.fields.has_key('Version'): + if 'Version' in ntlmChallenge.fields: version = ntlmChallenge['Version'] if len(version) >= 4: diff --git a/tests/python_dependencies/impacket/smb3.py b/tests/python_dependencies/impacket/smb3.py index 5548e4b0c..d81c7e117 100644 --- a/tests/python_dependencies/impacket/smb3.py +++ b/tests/python_dependencies/impacket/smb3.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright (c) 2003-2016 CORE Security Technologies # # This software is provided under under a slightly modified version @@ -231,13 +232,13 @@ class SMB3: self.negotiateSession(preferredDialect) def printStatus(self): - print "CONNECTION" + print("CONNECTION") for i in self._Connection.items(): - print "%-40s : %s" % i - print - print "SESSION" + print("%-40s : %s" % i) + print() + print("SESSION") for i in self._Session.items(): - print "%-40s : %s" % i + print("%-40s : %s" % i) def getServerName(self): return self._Session['ServerName'] @@ -308,7 +309,7 @@ class SMB3: packet['SessionID'] = self._Session['SessionID'] # Default the credit charge to 1 unless set by the caller - if packet.fields.has_key('CreditCharge') is False: + if ('CreditCharge' in packet.fields) is False: packet['CreditCharge'] = 1 # Standard credit request after negotiating protocol @@ -318,7 +319,7 @@ class SMB3: messageId = packet['MessageID'] if self._Session['SigningActivated'] is True and self._Connection['SequenceWindow'] > 2: - if packet['TreeID'] > 0 and self._Session['TreeConnectTable'].has_key(packet['TreeID']) is True: + if packet['TreeID'] > 0 and (packet['TreeID'] in self._Session['TreeConnectTable']) is True: if self._Session['TreeConnectTable'][packet['TreeID']]['EncryptData'] is False: packet['Flags'] = SMB2_FLAGS_SIGNED self.signSMB(packet) @@ -350,7 +351,7 @@ class SMB3: def recvSMB(self, packetID = None): # First, verify we don't have the packet already - if self._Connection['OutstandingResponses'].has_key(packetID): + if packetID in self._Connection['OutstandingResponses']: return self._Connection['OutstandingResponses'].pop(packetID) data = self._NetBIOSSession.recv_packet(self._timeout) @@ -727,7 +728,7 @@ class SMB3: pass # Parse Version to know the target Operating system name. Not provided elsewhere anymore - if ntlmChallenge.fields.has_key('Version'): + if 'Version' in ntlmChallenge.fields: version = ntlmChallenge['Version'] if len(version) >= 4: @@ -785,7 +786,7 @@ class SMB3: #print self._Session['TreeConnectTable'] share = share.split('\\')[-1] - if self._Session['TreeConnectTable'].has_key(share): + if share in self._Session['TreeConnectTable']: # Already connected, no need to reconnect treeEntry = self._Session['TreeConnectTable'][share] treeEntry['NumberOfUses'] += 1 @@ -837,10 +838,10 @@ class SMB3: return packet['TreeID'] def disconnectTree(self, treeId): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['TreeConnectTable'].has_key(treeId): + if treeId in self._Session['TreeConnectTable']: # More than 1 use? descrease it and return, if not, send the packet if self._Session['TreeConnectTable'][treeId]['NumberOfUses'] > 1: treeEntry = self._Session['TreeConnectTable'][treeId] @@ -862,7 +863,7 @@ class SMB3: return True def create(self, treeId, fileName, desiredAccess, shareMode, creationOptions, creationDisposition, fileAttributes, impersonationLevel = SMB2_IL_IMPERSONATION, securityFlags = 0, oplockLevel = SMB2_OPLOCK_LEVEL_NONE, createContexts = None): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) fileName = string.replace(fileName, '/', '\\') @@ -885,7 +886,7 @@ class SMB3: # Is this file NOT on the root directory? if len(fileName.split('\\')) > 2: parentDir = ntpath.dirname(pathName) - if self.GlobalFileTable.has_key(parentDir): + if parentDir in self.GlobalFileTable: LOG.critical("Don't know what to do now! :-o") raise else: @@ -957,9 +958,9 @@ class SMB3: return str(createResponse['FileID']) def close(self, treeId, fileId): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -988,9 +989,9 @@ class SMB3: # This function should NOT be used for reading files directly, but another higher # level function should be used that will break the read into smaller pieces - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1030,9 +1031,9 @@ class SMB3: # This function should NOT be used for writing directly to files, but another higher # level function should be used that will break the writes into smaller pieces - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1071,9 +1072,9 @@ class SMB3: return bytesWritten def queryDirectory(self, treeId, fileId, searchString = '*', resumeIndex = 0, informationClass = FILENAMES_INFORMATION, maxBufferSize = None, enumRestart = False, singleEntry = False): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1124,12 +1125,12 @@ class SMB3: self.sendSMB(packet) def ioctl(self, treeId, fileId = None, ctlCode = -1, flags = 0, inputBlob = '', maxInputResponse = None, maxOutputResponse = None, waitAnswer = 1): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) if fileId is None: fileId = '\xff'*16 else: - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1165,9 +1166,9 @@ class SMB3: return smbIoctlResponse['Buffer'] def flush(self,treeId, fileId): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1186,9 +1187,9 @@ class SMB3: return True def lock(self, treeId, fileId, locks, lockSequence = 0): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1248,9 +1249,9 @@ class SMB3: return True def queryInfo(self, treeId, fileId, inputBlob = '', infoType = SMB2_0_INFO_FILE, fileInfoClass = SMB2_FILE_STANDARD_INFO, additionalInformation = 0, flags = 0 ): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1280,9 +1281,9 @@ class SMB3: return queryResponse['Buffer'] def setInfo(self, treeId, fileId, inputBlob = '', infoType = SMB2_0_INFO_FILE, fileInfoClass = SMB2_FILE_STANDARD_INFO, additionalInformation = 0 ): - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) - if self._Session['OpenTable'].has_key(fileId) is False: + if (fileId in self._Session['OpenTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) packet = self.SMB_PACKET() @@ -1385,7 +1386,7 @@ class SMB3: files.append(smb.SharedFile(fileInfo['CreationTime'],fileInfo['LastAccessTime'],fileInfo['LastChangeTime'],fileInfo['EndOfFile'],fileInfo['AllocationSize'],fileInfo['ExtFileAttributes'],fileInfo['FileName'].decode('utf-16le'), fileInfo['FileName'].decode('utf-16le'))) nextOffset = fileInfo['NextEntryOffset'] res = res[nextOffset:] - except SessionError, e: + except SessionError as e: if (e.get_error_code()) != STATUS_NO_MORE_FILES: raise break @@ -1512,7 +1513,7 @@ class SMB3: def waitNamedPipe(self, treeId, pipename, timeout = 5): pipename = ntpath.basename(pipename) - if self._Session['TreeConnectTable'].has_key(treeId) is False: + if (treeId in self._Session['TreeConnectTable']) is False: raise SessionError(STATUS_INVALID_PARAMETER) if len(pipename) > 0xffff: raise SessionError(STATUS_INVALID_PARAMETER) diff --git a/tests/python_dependencies/impacket/smbserver.py b/tests/python_dependencies/impacket/smbserver.py index aa795e52e..3473c9f0c 100644 --- a/tests/python_dependencies/impacket/smbserver.py +++ b/tests/python_dependencies/impacket/smbserver.py @@ -54,6 +54,12 @@ from impacket.nt_errors import STATUS_NO_MORE_FILES, STATUS_NETWORK_NAME_DELETED STATUS_SMB_BAD_UID = 0x005B0002 STATUS_SMB_BAD_TID = 0x00050002 +try: + unicode # Python 2 +except NameError: + unicode = str # Python 3 + + # Utility functions # and general functions. # There are some common functions that can be accessed from more than one SMB @@ -80,7 +86,7 @@ def outputToJohnFormat(challenge, username, domain, lmresponse, ntresponse): else: # NTLMv1 ret_value = {'hash_string':'%s::%s:%s:%s:%s' % (username, domain, hexlify(lmresponse), hexlify(ntresponse), hexlify(challenge)), 'hash_version':'ntlm'} - except Exception, e: + except Exception as e: LOG.error("outputToJohnFormat: %s" % e) pass @@ -183,7 +189,7 @@ def openFile(path,fileName, accessMode, fileAttributes, openMode): if sys.platform == 'win32': mode |= os.O_BINARY fid = os.open(pathName, mode) - except Exception, e: + except Exception as e: LOG.error("openFile: %s,%s" % (pathName, mode) ,e) fid = 0 errorCode = STATUS_ACCESS_DENIED @@ -442,7 +448,7 @@ def queryPathInformation(path, filename, level): else: # NOT FOUND return None, STATUS_OBJECT_NAME_NOT_FOUND - except Exception, e: + except Exception as e: LOG.error('queryPathInfo: %s' % e) raise @@ -486,7 +492,7 @@ class TRANSCommands: # (beto) If offset == 0 it crashes explorer.exe on windows 7 entry['RemarkOffsetLow'] = 20 * len(shares) + len(tailData) respData += entry.getData() - if shares[i].has_key('comment'): + if 'comment' in shares[i]: tailData += shares[i]['comment'] + '\x00' else: tailData += '\x00' @@ -511,7 +517,7 @@ class TRANSCommands: shareInfo['NetworkName'] = request['ShareName'].upper() + '\x00' shareInfo['Type'] = int(share['share type']) respData = shareInfo.getData() - if share.has_key('comment'): + if 'comment' in share: shareInfo['RemarkOffsetLow'] = len(respData) respData += share['comment'] + '\x00' respParameters['TotalBytesAvailable'] = len(respData) @@ -538,7 +544,7 @@ class TRANSCommands: # Extract the FID fid = struct.unpack('= offset: @@ -1611,7 +1617,7 @@ class SMBCommands: respParameters['Count'] = writeAndX['DataLength'] respParameters['Available']= 0xff - except Exception, e: + except Exception as e: smbServer.log('smbComWriteAndx: %s' % e, logging.ERROR) errorCode = STATUS_ACCESS_DENIED else: @@ -1637,7 +1643,7 @@ class SMBCommands: comReadParameters = smb.SMBRead_Parameters(SMBCommand['Parameters']) - if connData['OpenedFiles'].has_key(comReadParameters['Fid']): + if comReadParameters['Fid'] in connData['OpenedFiles']: fileHandle = connData['OpenedFiles'][comReadParameters['Fid']]['FileHandle'] errorCode = STATUS_SUCCESS try: @@ -1651,7 +1657,7 @@ class SMBCommands: respParameters['Count'] = len(content) respData['DataLength'] = len(content) respData['Data'] = content - except Exception, e: + except Exception as e: smbServer.log('smbComRead: %s ' % e, logging.ERROR) errorCode = STATUS_ACCESS_DENIED else: @@ -1680,13 +1686,13 @@ class SMBCommands: else: readAndX = smb.SMBReadAndX_Parameters(SMBCommand['Parameters']) - if connData['OpenedFiles'].has_key(readAndX['Fid']): + if readAndX['Fid'] in connData['OpenedFiles']: fileHandle = connData['OpenedFiles'][readAndX['Fid']]['FileHandle'] errorCode = 0 try: if fileHandle != PIPE_FILE_DESCRIPTOR: offset = readAndX['Offset'] - if readAndX.fields.has_key('HighOffset'): + if 'HighOffset' in readAndX.fields: offset += (readAndX['HighOffset'] << 32) os.lseek(fileHandle,offset,0) content = os.read(fileHandle,readAndX['MaxCount']) @@ -1698,7 +1704,7 @@ class SMBCommands: respParameters['DataOffset'] = 59 respParameters['DataCount_Hi'] = 0 respData = content - except Exception, e: + except Exception as e: smbServer.log('smbComReadAndX: %s ' % e, logging.ERROR) errorCode = STATUS_ACCESS_DENIED else: @@ -1725,7 +1731,7 @@ class SMBCommands: queryInformation= smb.SMBQueryInformation_Data(flags = recvPacket['Flags2'], data = SMBCommand['Data']) # Get the Tid associated - if connData['ConnectedShares'].has_key(recvPacket['Tid']): + if recvPacket['Tid'] in connData['ConnectedShares']: fileSize, lastWriteTime, fileAttributes = queryFsInformation( connData['ConnectedShares'][recvPacket['Tid']]['path'], decodeSMBString(recvPacket['Flags2'],queryInformation['FileName'])) @@ -1755,7 +1761,7 @@ class SMBCommands: respData = '' # Get the Tid associated - if connData['ConnectedShares'].has_key(recvPacket['Tid']): + if recvPacket['Tid'] in connData['ConnectedShares']: totalUnits, freeUnits = queryDiskInformation( connData['ConnectedShares'][recvPacket['Tid']]['path']) @@ -1807,7 +1813,7 @@ class SMBCommands: respParameters = '' respData = '' - if connData['ConnectedShares'].has_key(recvPacket['Tid']): + if recvPacket['Tid'] in connData['ConnectedShares']: smbServer.log("Disconnecting Share(%d:%s)" % (recvPacket['Tid'],connData['ConnectedShares'][recvPacket['Tid']]['shareName'])) del(connData['ConnectedShares'][recvPacket['Tid']]) errorCode = STATUS_SUCCESS @@ -1854,7 +1860,7 @@ class SMBCommands: queryInformation2 = smb.SMBQueryInformation2_Parameters(SMBCommand['Parameters']) errorCode = 0xFF - if connData['OpenedFiles'].has_key(queryInformation2['Fid']): + if queryInformation2['Fid'] in connData['OpenedFiles']: errorCode = STATUS_SUCCESS pathName = connData['OpenedFiles'][queryInformation2['Fid']]['FileName'] try: @@ -1873,7 +1879,7 @@ class SMBCommands: if os.path.isfile(pathName): attribs = smb.SMB_FILE_ATTRIBUTE_NORMAL respParameters['FileAttributes'] = attribs - except Exception, e: + except Exception as e: smbServer.log('smbComQueryInformation2 %s' % e,logging.ERROR) errorCode = STATUS_ACCESS_DENIED @@ -1904,14 +1910,14 @@ class SMBCommands: # respParameters['VolumeGUID'] = '\x00' # Get the Tid associated - if connData['ConnectedShares'].has_key(recvPacket['Tid']): + if recvPacket['Tid'] in connData['ConnectedShares']: # If we have a rootFid, the path is relative to that fid errorCode = STATUS_SUCCESS if ntCreateAndXParameters['RootFid'] > 0: path = connData['OpenedFiles'][ntCreateAndXParameters['RootFid']]['FileName'] LOG.debug("RootFid present %s!" % path) else: - if connData['ConnectedShares'][recvPacket['Tid']].has_key('path'): + if 'path' in connData['ConnectedShares'][recvPacket['Tid']]: path = connData['ConnectedShares'][recvPacket['Tid']]['path'] else: path = 'NONE' @@ -1947,7 +1953,7 @@ class SMBCommands: else: mode |= os.O_CREAT elif createDisposition & smb.FILE_OPEN == smb.FILE_OPEN: - if os.path.exists(pathName) is not True and smbServer.getRegisteredNamedPipes().has_key(unicode(pathName)) is not True: + if os.path.exists(pathName) is not True and (unicode(pathName) in smbServer.getRegisteredNamedPipes()) is not True: errorCode = STATUS_NO_SUCH_FILE if errorCode == STATUS_SUCCESS: @@ -1969,7 +1975,7 @@ class SMBCommands: # Let's create the directory os.mkdir(pathName) mode = os.O_RDONLY - except Exception, e: + except Exception as e: smbServer.log("NTCreateAndX: %s,%s,%s" % (pathName,mode,e),logging.ERROR) errorCode = STATUS_ACCESS_DENIED if createOptions & smb.FILE_NON_DIRECTORY_FILE == smb.FILE_NON_DIRECTORY_FILE: @@ -1989,13 +1995,13 @@ class SMBCommands: else: if sys.platform == 'win32': mode |= os.O_BINARY - if smbServer.getRegisteredNamedPipes().has_key(unicode(pathName)): + if unicode(pathName) in smbServer.getRegisteredNamedPipes(): fid = PIPE_FILE_DESCRIPTOR sock = socket.socket() sock.connect(smbServer.getRegisteredNamedPipes()[unicode(pathName)]) else: fid = os.open(pathName, mode) - except Exception, e: + except Exception as e: smbServer.log("NTCreateAndX: %s,%s,%s" % (pathName,mode,e),logging.ERROR) #print e fid = 0 @@ -2074,7 +2080,7 @@ class SMBCommands: openAndXData = smb.SMBOpenAndX_Data( flags = recvPacket['Flags2'], data = SMBCommand['Data']) # Get the Tid associated - if connData['ConnectedShares'].has_key(recvPacket['Tid']): + if recvPacket['Tid'] in connData['ConnectedShares']: path = connData['ConnectedShares'][recvPacket['Tid']]['path'] openedFile, mode, pathName, errorCode = openFile(path, decodeSMBString(recvPacket['Flags2'],openAndXData['FileName']), @@ -2223,7 +2229,7 @@ class SMBCommands: mechType = blob['MechTypes'][0] if mechType != TypesMech['NTLMSSP - Microsoft NTLM Security Support Provider']: # Nope, do we know it? - if MechTypes.has_key(mechType): + if mechType in MechTypes: mechStr = MechTypes[mechType] else: mechStr = hexlify(mechType) @@ -2431,7 +2437,7 @@ class SMBCommands: _dialects_parameters = smb.SMBNTLMDialect_Parameters() _dialects_data= smb.SMBNTLMDialect_Data() _dialects_data['Payload'] = '' - if connData.has_key('EncryptionKey'): + if 'EncryptionKey' in connData: _dialects_data['Challenge'] = connData['EncryptionKey'] _dialects_parameters['ChallengeLength'] = len(str(_dialects_data)) else: @@ -2463,7 +2469,7 @@ class SMBCommands: connData['_dialects_data'] = _dialects_data connData['_dialects_parameters'] = _dialects_parameters - except Exception, e: + except Exception as e: # No NTLM throw an error smbServer.log('smbComNegotiate: %s' % e, logging.ERROR) respSMBCommand['Data'] = struct.pack('> 16, len(data) & 0xFFFF) + data - elif 0x1000000 <= len(data) <= 0xffffffff: - res = pack('!BL', 0x84, len(data)) + data - else: - raise Exception('Error in asn1encode') - return str(res) + #res = asn1.SEQUENCE(str).encode() + #import binascii + #print '\nalex asn1encode str: %s\n' % binascii.hexlify(str) + if 0 <= len(data) <= 0x7F: + res = pack('B', len(data)) + data + elif 0x80 <= len(data) <= 0xFF: + res = pack('BB', 0x81, len(data)) + data + elif 0x100 <= len(data) <= 0xFFFF: + res = pack('!BH', 0x82, len(data)) + data + elif 0x10000 <= len(data) <= 0xffffff: + res = pack('!BBH', 0x83, len(data) >> 16, len(data) & 0xFFFF) + data + elif 0x1000000 <= len(data) <= 0xffffffff: + res = pack('!BL', 0x84, len(data)) + data + else: + raise Exception('Error in asn1encode') + return str(res) def asn1decode(data = ''): - len1 = unpack('B', data[:1])[0] - data = data[1:] - if len1 == 0x81: - pad = calcsize('B') - len2 = unpack('B',data[:pad])[0] - data = data[pad:] - ans = data[:len2] - elif len1 == 0x82: - pad = calcsize('H') - len2 = unpack('!H', data[:pad])[0] - data = data[pad:] - ans = data[:len2] - elif len1 == 0x83: - pad = calcsize('B') + calcsize('!H') - len2, len3 = unpack('!BH', data[:pad]) - data = data[pad:] - ans = data[:len2 << 16 + len3] - elif len1 == 0x84: - pad = calcsize('!L') - len2 = unpack('!L', data[:pad])[0] - data = data[pad:] - ans = data[:len2] - # 1 byte length, string <= 0x7F - else: - pad = 0 - ans = data[:len1] - return ans, len(ans)+pad+1 + len1 = unpack('B', data[:1])[0] + data = data[1:] + if len1 == 0x81: + pad = calcsize('B') + len2 = unpack('B',data[:pad])[0] + data = data[pad:] + ans = data[:len2] + elif len1 == 0x82: + pad = calcsize('H') + len2 = unpack('!H', data[:pad])[0] + data = data[pad:] + ans = data[:len2] + elif len1 == 0x83: + pad = calcsize('B') + calcsize('!H') + len2, len3 = unpack('!BH', data[:pad]) + data = data[pad:] + ans = data[:len2 << 16 + len3] + elif len1 == 0x84: + pad = calcsize('!L') + len2 = unpack('!L', data[:pad])[0] + data = data[pad:] + ans = data[:len2] + # 1 byte length, string <= 0x7F + else: + pad = 0 + ans = data[:len1] + return ans, len(ans)+pad+1 class GSSAPI: -# Generic GSSAPI Header Format +# Generic GSSAPI Header Format def __init__(self, data = None): self.fields = {} self['UUID'] = GSS_API_SPNEGO_UUID if data: - self.fromString(data) + self.fromString(data) pass def __setitem__(self,key,value): @@ -115,27 +116,27 @@ class GSSAPI: if next_byte != ASN1_AID: raise Exception('Unknown AID=%x' % next_byte) data = data[1:] - decode_data, total_bytes = asn1decode(data) + decode_data, total_bytes = asn1decode(data) # Now we should have a OID tag - next_byte = unpack('B',decode_data[:1])[0] + next_byte = unpack('B',decode_data[:1])[0] if next_byte != ASN1_OID: raise Exception('OID tag not found %x' % next_byte) decode_data = decode_data[1:] # Now the OID contents, should be SPNEGO UUID - uuid, total_bytes = asn1decode(decode_data) + uuid, total_bytes = asn1decode(decode_data) self['OID'] = uuid # the rest should be the data self['Payload'] = decode_data[total_bytes:] #pass - + def dump(self): for i in self.fields.keys(): - print "%s: {%r}" % (i,self[i]) + print("%s: {%r}" % (i,self[i])) def getData(self): ans = pack('B',ASN1_AID) ans += asn1encode( - pack('B',ASN1_OID) + + pack('B',ASN1_OID) + asn1encode(self['UUID']) + self['Payload'] ) return ans @@ -163,7 +164,7 @@ class SPNEGO_NegTokenResp: def __init__(self, data = None): self.fields = {} if data: - self.fromString(data) + self.fromString(data) pass def __setitem__(self,key,value): @@ -198,7 +199,7 @@ class SPNEGO_NegTokenResp: if next_byte != ASN1_MECH_TYPE: # MechType not found, could be an AUTH answer if next_byte != ASN1_RESPONSE_TOKEN: - raise Exception('MechType/ResponseToken tag not found %x' % next_byte) + raise Exception('MechType/ResponseToken tag not found %x' % next_byte) else: decode_data2 = decode_data[1:] decode_data2, total_bytes = asn1decode(decode_data2) @@ -245,30 +246,30 @@ class SPNEGO_NegTokenResp: def dump(self): for i in self.fields.keys(): - print "%s: {%r}" % (i,self[i]) - + print("%s: {%r}" % (i,self[i])) + def getData(self): ans = pack('B',SPNEGO_NegTokenResp.SPNEGO_NEG_TOKEN_RESP) - if self.fields.has_key('NegResult') and self.fields.has_key('SupportedMech'): + if 'NegResult' in self.fields and 'SupportedMech' in self.fields: # Server resp ans += asn1encode( pack('B', ASN1_SEQUENCE) + asn1encode( pack('B',SPNEGO_NegTokenResp.SPNEGO_NEG_TOKEN_TARG) + asn1encode( - pack('B',ASN1_ENUMERATED) + + pack('B',ASN1_ENUMERATED) + asn1encode( self['NegResult'] )) + pack('B',ASN1_SUPPORTED_MECH) + - asn1encode( + asn1encode( pack('B',ASN1_OID) + asn1encode(self['SupportedMech'])) + pack('B',ASN1_RESPONSE_TOKEN ) + asn1encode( pack('B', ASN1_OCTET_STRING) + asn1encode(self['ResponseToken'])))) - elif self.fields.has_key('NegResult'): + elif 'NegResult' in self.fields: # Server resp ans += asn1encode( - pack('B', ASN1_SEQUENCE) + + pack('B', ASN1_SEQUENCE) + asn1encode( pack('B', SPNEGO_NegTokenResp.SPNEGO_NEG_TOKEN_TARG) + asn1encode( @@ -285,24 +286,24 @@ class SPNEGO_NegTokenResp: return ans class SPNEGO_NegTokenInit(GSSAPI): - # http://tools.ietf.org/html/rfc4178#page-8 + # http://tools.ietf.org/html/rfc4178#page-8 # NegTokeInit :: = SEQUENCE { - # mechTypes [0] MechTypeList, + # mechTypes [0] MechTypeList, # reqFlags [1] ContextFlags OPTIONAL, - # mechToken [2] OCTET STRING OPTIONAL, + # mechToken [2] OCTET STRING OPTIONAL, # mechListMIC [3] OCTET STRING OPTIONAL, # } SPNEGO_NEG_TOKEN_INIT = 0xa0 def fromString(self, data = 0): GSSAPI.fromString(self, data) payload = self['Payload'] - next_byte = unpack('B', payload[:1])[0] + next_byte = unpack('B', payload[:1])[0] if next_byte != SPNEGO_NegTokenInit.SPNEGO_NEG_TOKEN_INIT: raise Exception('NegTokenInit not found %x' % next_byte) payload = payload[1:] decode_data, total_bytes = asn1decode(payload) # Now we should have a SEQUENCE Tag - next_byte = unpack('B', decode_data[:1])[0] + next_byte = unpack('B', decode_data[:1])[0] if next_byte != ASN1_SEQUENCE: raise Exception('SEQUENCE tag not found %x' % next_byte) decode_data = decode_data[1:] @@ -321,14 +322,14 @@ class SPNEGO_NegTokenInit(GSSAPI): # And finally we should have the MechTypes self['MechTypes'] = [] while decode_data: - next_byte = unpack('B', decode_data[:1])[0] - if next_byte != ASN1_OID: - # Not a valid OID, there must be something else we won't unpack - break - decode_data = decode_data[1:] - item, total_bytes = asn1decode(decode_data) - self['MechTypes'].append(item) - decode_data = decode_data[total_bytes:] + next_byte = unpack('B', decode_data[:1])[0] + if next_byte != ASN1_OID: + # Not a valid OID, there must be something else we won't unpack + break + decode_data = decode_data[1:] + item, total_bytes = asn1decode(decode_data) + self['MechTypes'].append(item) + decode_data = decode_data[total_bytes:] # Do we have MechTokens as well? decode_data = remaining_data[total_bytes3:] @@ -352,7 +353,7 @@ class SPNEGO_NegTokenInit(GSSAPI): mechToken = '' # Do we have tokens to send? - if self.fields.has_key('MechToken'): + if 'MechToken' in self.fields: mechToken = pack('B', ASN1_MECH_TOKEN) + asn1encode( pack('B', ASN1_OCTET_STRING) + asn1encode( self['MechToken'])) @@ -363,10 +364,9 @@ class SPNEGO_NegTokenInit(GSSAPI): asn1encode( pack('B', ASN1_MECH_TYPE) + asn1encode( - pack('B', ASN1_SEQUENCE) + + pack('B', ASN1_SEQUENCE) + asn1encode(mechTypes)) + mechToken )) self['Payload'] = ans return GSSAPI.getData(self) - diff --git a/tests/python_dependencies/impacket/structure.py b/tests/python_dependencies/impacket/structure.py index 7a04117ff..68066a61f 100644 --- a/tests/python_dependencies/impacket/structure.py +++ b/tests/python_dependencies/impacket/structure.py @@ -1,3 +1,4 @@ +from __future__ import print_function # Copyright (c) 2003-2016 CORE Security Technologies # # This software is provided under under a slightly modified version @@ -97,18 +98,18 @@ class Structure: def packField(self, fieldName, format = None): if self.debug: - print "packField( %s | %s )" % (fieldName, format) + print("packField( %s | %s )" % (fieldName, format)) if format is None: format = self.formatForField(fieldName) - if self.fields.has_key(fieldName): + if fieldName in self.fields: ans = self.pack(format, self.fields[fieldName], field = fieldName) else: ans = self.pack(format, None, field = fieldName) if self.debug: - print "\tanswer %r" % ans + print("\tanswer %r" % ans) return ans @@ -119,8 +120,8 @@ class Structure: for field in self.commonHdr+self.structure: try: data += self.packField(field[0], field[1]) - except Exception, e: - if self.fields.has_key(field[0]): + except Exception as e: + if field[0] in self.fields: e.args += ("When packing field '%s | %s | %r' in %s" % (field[0], field[1], self[field[0]], self.__class__),) else: e.args += ("When packing field '%s | %s' in %s" % (field[0], field[1], self.__class__),) @@ -136,16 +137,16 @@ class Structure: self.rawData = data for field in self.commonHdr+self.structure: if self.debug: - print "fromString( %s | %s | %r )" % (field[0], field[1], data) + print("fromString( %s | %s | %r )" % (field[0], field[1], data)) size = self.calcUnpackSize(field[1], data, field[0]) if self.debug: - print " size = %d" % size + print(" size = %d" % size) dataClassOrCode = str if len(field) > 2: dataClassOrCode = field[2] try: self[field[0]] = self.unpack(field[1], data[:size], dataClassOrCode = dataClassOrCode, field = field[0]) - except Exception,e: + except Exception as e: e.args += ("When unpacking field '%s | %s | %r[:%d]'" % (field[0], field[1], data, size),) raise @@ -175,7 +176,7 @@ class Structure: def pack(self, format, data, field = None): if self.debug: - print " pack( %s | %r | %s)" % (format, data, field) + print(" pack( %s | %r | %s)" % (format, data, field)) if field: addressField = self.findAddressFieldFor(field) @@ -206,7 +207,7 @@ class Structure: try: return self.pack(two[0], data) except: - if (self.fields.has_key(two[1])) and (self[two[1]] is not None): + if (two[1] in self.fields) and (self[two[1]] is not None): return self.pack(two[0], id(self[two[1]]) & ((1<<(calcsize(two[0])*8))-1) ) else: return self.pack(two[0], 0) @@ -228,7 +229,7 @@ class Structure: if two[0]: if two[0].isdigit(): if int(two[0]) != len(data): - raise Exception, "Array field has a constant size, and it doesn't match the actual value" + raise Exception("Array field has a constant size, and it doesn't match the actual value") else: return self.pack(two[0], len(data))+answer return answer @@ -256,7 +257,7 @@ class Structure: return '%s\0\0\0\0%s%s' % (l,l,data) if data is None: - raise Exception, "Trying to pack None" + raise Exception("Trying to pack None") # literal specifier if format[:1] == ':': @@ -267,7 +268,7 @@ class Structure: def unpack(self, format, data, dataClassOrCode = str, field = None): if self.debug: - print " unpack( %s | %r )" % (format, data) + print(" unpack( %s | %r )" % (format, data)) if field: addressField = self.findAddressFieldFor(field) @@ -288,7 +289,7 @@ class Structure: if format[:1] == "'" or format[:1] == '"': answer = format[1:] if answer != data: - raise Exception, "Unpacked data doesn't match constant value '%r' should be '%r'" % (data, answer) + raise Exception("Unpacked data doesn't match constant value '%r' should be '%r'" % (data, answer)) return answer # address specifier @@ -334,13 +335,13 @@ class Structure: # asciiz specifier if format == 'z': if data[-1] != '\x00': - raise Exception, ("%s 'z' field is not NUL terminated: %r" % (field, data)) + raise Exception("%s 'z' field is not NUL terminated: %r" % (field, data)) return data[:-1] # remove trailing NUL # unicode specifier if format == 'u': if data[-2:] != '\x00\x00': - raise Exception, ("%s 'u' field is not NUL-NUL terminated: %r" % (field, data)) + raise Exception("%s 'u' field is not NUL-NUL terminated: %r" % (field, data)) return data[:-2] # remove trailing NUL # DCE-RPC/NDR string specifier @@ -392,7 +393,7 @@ class Structure: answer = 0 if two[0].isdigit(): if int(two[0]) != len(data): - raise Exception, "Array field has a constant size, and it doesn't match the actual value" + raise Exception("Array field has a constant size, and it doesn't match the actual value") elif two[0]: answer += self.calcPackSize(two[0], len(data)) @@ -428,7 +429,7 @@ class Structure: def calcUnpackSize(self, format, data, field = None): if self.debug: - print " calcUnpackSize( %s | %s | %r)" % (field, format, data) + print(" calcUnpackSize( %s | %s | %r)" % (field, format, data)) # void specifier if format[:1] == '_': @@ -487,7 +488,7 @@ class Structure: # "printf" string specifier if format[:1] == '%': - raise Exception, "Can't guess the size of a printf like specifier for unpacking" + raise Exception("Can't guess the size of a printf like specifier for unpacking") # asciiz specifier if format[:1] == 'z': @@ -520,7 +521,7 @@ class Structure: for field in self.commonHdr+self.structure: if field[0] == fieldName: return field[1] - raise Exception, ("Field %s not found" % fieldName) + raise Exception("Field %s not found" % fieldName) def findAddressFieldFor(self, fieldName): descriptor = '&%s' % fieldName @@ -558,7 +559,7 @@ class Structure: def dump(self, msg = None, indent = 0): if msg is None: msg = self.__class__.__name__ ind = ' '*indent - print "\n%s" % msg + print("\n%s" % msg) fixedFields = [] for field in self.commonHdr+self.structure: i = field[0] @@ -566,18 +567,18 @@ class Structure: fixedFields.append(i) if isinstance(self[i], Structure): self[i].dump('%s%s:{' % (ind,i), indent = indent + 4) - print "%s}" % ind + print("%s}" % ind) else: - print "%s%s: {%r}" % (ind,i,self[i]) + print("%s%s: {%r}" % (ind,i,self[i])) # Do we have remaining fields not defined in the structures? let's # print them remainingFields = list(set(self.fields) - set(fixedFields)) for i in remainingFields: if isinstance(self[i], Structure): self[i].dump('%s%s:{' % (ind,i), indent = indent + 4) - print "%s}" % ind + print("%s}" % ind) else: - print "%s%s: {%r}" % (ind,i,self[i]) + print("%s%s: {%r}" % (ind,i,self[i])) class _StructureTest: @@ -589,23 +590,23 @@ class _StructureTest: return self.theClass(alignment = self.alignment) def run(self): - print - print "-"*70 + print() + print("-"*70) testName = self.__class__.__name__ - print "starting test: %s....." % testName + print("starting test: %s....." % testName) a = self.create() self.populate(a) a.dump("packing.....") a_str = str(a) - print "packed: %r" % a_str - print "unpacking....." + print("packed: %r" % a_str) + print("unpacking.....") b = self.create(a_str) b.dump("unpacked.....") - print "repacking....." + print("repacking.....") b_str = str(b) if b_str != a_str: - print "ERROR: original packed and repacked don't match" - print "packed: %r" % b_str + print("ERROR: original packed and repacked don't match") + print("packed: %r" % b_str) class _Test_simple(_StructureTest): class theClass(Structure): @@ -732,7 +733,7 @@ if __name__ == '__main__': try: _Test_fixedLength().run() except: - print "cannot repack because length is bogus" + print("cannot repack because length is bogus") _Test_simple_aligned4().run() _Test_nested().run() diff --git a/tests/python_dependencies/impacket/uuid.py b/tests/python_dependencies/impacket/uuid.py index fb4d7b3a0..750eba459 100644 --- a/tests/python_dependencies/impacket/uuid.py +++ b/tests/python_dependencies/impacket/uuid.py @@ -17,9 +17,14 @@ import re from random import randrange from struct import pack, unpack +try: + long # Python 2 +except NameError: + long = int # Python 3 + def generate(): # UHm... crappy Python has an maximum integer of 2**31-1. - top = (1L<<31)-1 + top = (1<<31)-1 return pack("IIII", randrange(top), randrange(top), randrange(top), randrange(top)) def bin_to_string(uuid): @@ -49,16 +54,16 @@ def bin_to_uuidtup(bin): return uuidstr, "%d.%d" % (maj, min) #input: string -#output: tuple (uuid,version) +#output: tuple (uuid,version) #if version is not found in the input string "1.0" is returned -#example: -# "00000000-0000-0000-0000-000000000000 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') -# "10000000-2000-3000-4000-500000000000 version 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') -# "10000000-2000-3000-4000-500000000000 v 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') -# "10000000-2000-3000-4000-500000000000" returns ('00000000-0000-0000-0000-000000000000','1.0') +#example: +# "00000000-0000-0000-0000-000000000000 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') +# "10000000-2000-3000-4000-500000000000 version 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') +# "10000000-2000-3000-4000-500000000000 v 3.0" returns ('00000000-0000-0000-0000-000000000000','3.0') +# "10000000-2000-3000-4000-500000000000" returns ('00000000-0000-0000-0000-000000000000','1.0') def string_to_uuidtup(s): g = re.search("([A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}).*?([0-9]{1,5}\.[0-9]{1,5})",s+" 1.0") - if g: + if g: (u,v) = g.groups() return (u,v) return -- cgit v1.2.3