Ejemplo n.º 1
0
    def create_smb_connection(self, force=False):
        if self._smb_conn and not force:
            return self._smb_conn

        key = None

        if self._use_cache and not force:
            key = self._cache_key_entry()
            if key in SMB_SESSIONS_CACHE:
                self._smb_conn = SMB_SESSIONS_CACHE[key]
                self._cached = True
                return self._smb_conn

        try:
            smb = SMBConnection(
                self.host, self.host, None, self.port, timeout=self.timeout
            )

            if self.kerberos:
                smb.kerberos_login(
                    self.user, self.password,
                    self.domain, self.lm, self.nt,
                    self.aes, self.KDC, self.TGT, self.TGS)
            else:
                smb.login(self.user, self.password, self.domain, self.lm, self.nt)

            self.valid = True

            add_cred(
                self.user, self.password, self.domain, 'smb',
                self.host, None, self.port,
                ntlm=self.ntlm, aes=self.aes,
                tgt=self.TGT, tgs=self.TGS, kdc=self.KDC
            )

            if not force:
                self._smb_conn = smb

            if key is not None and not force:
                SMB_SESSIONS_CACHE[key] = self._smb_conn

            return smb

        except SessionError, e:
            raise PsExecException(e.getErrorString()[0])
Ejemplo n.º 2
0
class SMBTransport(DCERPCTransport):
    """Implementation of ncacn_np protocol sequence"""

    def __init__(self, target, credential = SMBCredential(), filename='', smb_connection=None):
        DCERPCTransport.__init__(self, target)
        self.__socket = None
        self.__tid = 0
        self.__filename = filename
        self.__handle = 0
        self.__pending_recv = 0
        self.__credential = credential
        self.set_credentials(credential)

        if target.hostname is not None:
            self.setRemoteHost(target.hostname)

        if smb_connection is None:
            self.__existing_smb = False
        else:
            self.__existing_smb = True
            self.set_credentials(smb_connection.getCredentials())

        self.__prefDialect = None
        self.__smb_connection = smb_connection

    def preferred_dialect(self, dialect):
        self.__prefDialect = dialect

    def setup_smb_connection(self):
        if not self.__smb_connection:
            self.__smb_connection = SMBConnection(self.getRemoteName(), self.getRemoteHost(), sess_port=self.get_dport(),
                                                  preferredDialect=self.__prefDialect)

    def connect(self):
        # Check if we have a smb connection already setup
        if self.__smb_connection is None:
            self.setup_smb_connection()
            if self._doKerberos is False:
                self.__smb_connection.login(self.__credential)
            else:
                self.__smb_connection.kerberos_login(self.__credential)
        self.__tid = self.__smb_connection.connectTree('IPC$')
        self.__handle = self.__smb_connection.openFile(self.__tid, self.__filename)
        self.__socket = self.__smb_connection.getSMBServer().get_socket()
        return 1

    def disconnect(self):
        self.__smb_connection.disconnectTree(self.__tid)
        # If we created the SMB connection, we close it, otherwise
        # that's up for the caller
        if self.__existing_smb is False:
            self.__smb_connection.logoff()
            self.__smb_connection.close()
            self.__smb_connection = None

    def send(self,data, forceWriteAndx = 0, forceRecv = 0):
        if self._max_send_frag:
            offset = 0
            while 1:
                toSend = data[offset:offset+self._max_send_frag]
                if not toSend:
                    break
                self.__smb_connection.writeFile(self.__tid, self.__handle, toSend, offset = offset)
                offset += len(toSend)
        else:
            self.__smb_connection.writeFile(self.__tid, self.__handle, data)
        if forceRecv:
            self.__pending_recv += 1

    def recv(self, forceRecv = 0, count = 0 ):
        if self._max_send_frag or self.__pending_recv:
            # _max_send_frag is checked because it's the same condition we checked
            # to decide whether to use write_andx() or send_trans() in send() above.
            if self.__pending_recv:
                self.__pending_recv -= 1
            return self.__smb_connection.readFile(self.__tid, self.__handle, bytesToRead = self._max_recv_frag)
        else:
            return self.__smb_connection.readFile(self.__tid, self.__handle)

    def get_smb_connection(self):
        return self.__smb_connection

    def set_smb_connection(self, smb_connection):
        self.__smb_connection = smb_connection
        self.set_credentials(smb_connection.getCredentials())
        self.__existing_smb = True

    def get_smb_server(self):
        # Raw Access to the SMBServer (whatever type it is)
        return self.__smb_connection.getSMBServer()

    def get_socket(self):
        return self.__socket

    def doesSupportNTLMv2(self):
        return self.__smb_connection.doesSupportNTLMv2()