def _negotiateSession(self, myName, remoteName, remoteHost, sess_port, timeout, extended_security = True): # Here we follow [MS-SMB2] negotiation handshake trying to understand what dialects # (including SMB1) is supported on the other end. if not myName: myName = socket.gethostname() i = string.find(myName, '.') if i > -1: myName = myName[:i] # If port 445 and the name sent is *SMBSERVER we're setting the name to the IP. This is to help some old applications still believing # *SMSBSERVER will work against modern OSes. If port is NETBIOS_SESSION_PORT the user better know about *SMBSERVER's limitations if sess_port == 445 and remoteName == '*SMBSERVER': remoteName = remoteHost self._nmbSession = nmb.NetBIOSTCPSession(myName, remoteName, remoteHost, nmb.TYPE_SERVER, sess_port, timeout) smbp = smb.NewSMBPacket() negSession = smb.SMBCommand(smb.SMB.SMB_COM_NEGOTIATE) if extended_security == True: smbp['Flags2']=smb.SMB.FLAGS2_EXTENDED_SECURITY negSession['Data'] = '\x02NT LM 0.12\x00\x02SMB 2.002\x00\x02SMB 2.???\x00' smbp.addCommand(negSession) self._nmbSession.send_packet(str(smbp)) r = self._nmbSession.recv_packet(timeout) return r.get_trailer()
def _negotiateSession(self, myName, remoteName, remoteHost, sess_port, timeout, extended_security=True, flags1=0, flags2=0, data=None): # Here we follow [MS-SMB2] negotiation handshake trying to understand what dialects # (including SMB1) is supported on the other end. if not myName: myName = socket.gethostname() i = string.find(myName, '.') if i > -1: myName = myName[:i] # If port 445 and the name sent is *SMBSERVER we're setting the name to the IP. This is to help some old # applications still believing # *SMSBSERVER will work against modern OSes. If port is NETBIOS_SESSION_PORT the user better know about i # *SMBSERVER's limitations if sess_port == 445 and remoteName == '*SMBSERVER': remoteName = remoteHost tries = 0 smbp = smb.NewSMBPacket() smbp['Flags1'] = flags1 smbp['Flags2'] = flags2 resp = None while tries < 2: self._nmbSession = nmb.NetBIOSTCPSession(myName, remoteName, remoteHost, nmb.TYPE_SERVER, sess_port, timeout) negSession = smb.SMBCommand(smb.SMB.SMB_COM_NEGOTIATE) if extended_security is True: smbp['Flags2'] |= smb.SMB.FLAGS2_EXTENDED_SECURITY negSession['Data'] = data smbp.addCommand(negSession) self._nmbSession.send_packet(str(smbp)) try: resp = self._nmbSession.recv_packet(timeout) break except nmb.NetBIOSError: # OSX Yosemite asks for more Flags. Let's give it a try and see what happens smbp[ 'Flags2'] |= smb.SMB.FLAGS2_NT_STATUS | smb.SMB.FLAGS2_LONG_NAMES | smb.SMB.FLAGS2_UNICODE smbp['Data'] = [] tries += 1 if resp is None: # No luck, quitting raise return resp.get_trailer()
def negotiateSessionWildcard(self, myName, remoteName, remoteHost, sess_port, timeout, extended_security=True, flags1=0, flags2=0, data=None): # Here we follow [MS-SMB2] negotiation handshake trying to understand what dialects # (including SMB1) is supported on the other end. if not myName: myName = socket.gethostname() i = string.find(myName, '.') if i > -1: myName = myName[:i] tries = 0 smbp = smb.NewSMBPacket() smbp['Flags1'] = flags1 # FLAGS2_UNICODE is required by some stacks to continue, regardless of subsequent support smbp['Flags2'] = flags2 | smb.SMB.FLAGS2_UNICODE resp = None while tries < 2: self._nmbSession = nmb.NetBIOSTCPSession(myName, remoteName, remoteHost, nmb.TYPE_SERVER, sess_port, timeout) negSession = smb.SMBCommand(smb.SMB.SMB_COM_NEGOTIATE) if extended_security is True: smbp['Flags2'] |= smb.SMB.FLAGS2_EXTENDED_SECURITY negSession['Data'] = data smbp.addCommand(negSession) self._nmbSession.send_packet(str(smbp)) try: resp = self._nmbSession.recv_packet(timeout) break except nmb.NetBIOSError: # OSX Yosemite asks for more Flags. Let's give it a try and see what happens smbp[ 'Flags2'] |= smb.SMB.FLAGS2_NT_STATUS | smb.SMB.FLAGS2_LONG_NAMES | smb.SMB.FLAGS2_UNICODE smbp['Data'] = [] tries += 1 if resp is None: # No luck, quitting raise return resp.get_trailer()