Beispiel #1
0
    def get_creds(self):
        if self.secret_type == KerberosSecretType.KEYTAB:
            return KerberosCredential.from_keytab(self.secret, self.username,
                                                  self.domain)
        if self.secret_type == KerberosSecretType.KIRBI:
            return KerberosCredential.from_kirbi(self.secret)

        res = KerberosCredential()
        res.username = self.username
        res.domain = self.domain

        if self.secret_type in [
                KerberosSecretType.PASSWORD, KerberosSecretType.PW,
                KerberosSecretType.PASS
        ]:
            res.password = self.secret
        elif self.secret_type in [
                KerberosSecretType.NT, KerberosSecretType.RC4
        ]:
            if len(self.secret) != 32:
                raise Exception('Incorrect RC4/NT key! %s' % self.secret)
            res.nt_hash = self.secret
            res.kerberos_key_rc4 = self.secret
        elif self.secret_type in [
                KerberosSecretType.AES128, KerberosSecretType.AES256,
                KerberosSecretType.AES
        ]:
            if self.secret_type == KerberosSecretType.AES:
                if len(self.secret) == 32:
                    res.kerberos_key_aes_128 = self.secret
                elif len(self.secret) == 64:
                    res.kerberos_key_aes_256 = self.secret
                else:
                    raise Exception('Incorrect AES key! %s' % self.secret)
            elif self.secret_type == KerberosSecretType.AES128:
                if len(self.secret) != 32:
                    raise Exception('Incorrect AES128 key! %s' % self.secret)
                res.kerberos_key_aes_128 = self.secret
            else:
                if len(self.secret) != 64:
                    raise Exception('Incorrect AES256 key! %s' % self.secret)
                res.kerberos_key_aes_256 = self.secret
        elif self.secret_type == KerberosSecretType.DES:
            if len(self.secret) != 16:
                raise Exception('Incorrect DES key! %s' % self.secret)
            res.kerberos_key_des = self.secret
        elif self.secret_type in [
                KerberosSecretType.DES3, KerberosSecretType.TDES
        ]:
            if len(self.secret) != 24:
                raise Exception('Incorrect DES3 key! %s' % self.secret)
            res.kerberos_key_des3 = self.secret
        elif self.secret_type == KerberosSecretType.CCACHE:
            res.ccache = self.secret
        else:
            raise Exception('Missing/unknown secret_type!')

        return res
Beispiel #2
0
    def build(self):
        if self.creds.auth_method == LDAPAuthProtocol.SICILY:
            ntlmcred = MSLDAPNTLMCredential()
            ntlmcred.username = self.creds.username
            ntlmcred.domain = self.creds.domain if self.creds.domain is not None else ''
            ntlmcred.workstation = None
            ntlmcred.is_guest = False
            ntlmcred.encrypt = self.creds.encrypt

            if self.creds.password is None:
                raise Exception(
                    'NTLM authentication requres password/NT hash!')

            if len(self.creds.password) == 32:
                try:
                    bytes.fromhex(self.creds.password)
                except:
                    ntlmcred.password = self.creds.password
                else:
                    ntlmcred.nt_hash = self.creds.password

            else:
                ntlmcred.password = self.creds.password

            settings = NTLMHandlerSettings(ntlmcred)
            return NTLMAUTHHandler(settings)

        elif self.creds.auth_method == LDAPAuthProtocol.SIMPLE:
            cred = MSLDAPPLAINCredential()
            cred.username = self.creds.username
            cred.domain = self.creds.domain
            cred.password = self.creds.password
            return cred

        elif self.creds.auth_method == LDAPAuthProtocol.PLAIN:
            cred = MSLDAPSIMPLECredential()
            cred.username = self.creds.username
            cred.domain = self.creds.domain
            cred.password = self.creds.password
            return cred

        elif self.creds.auth_method in [
                LDAPAuthProtocol.NTLM_PASSWORD, LDAPAuthProtocol.NTLM_NT
        ]:
            ntlmcred = MSLDAPNTLMCredential()
            ntlmcred.username = self.creds.username
            ntlmcred.domain = self.creds.domain if self.creds.domain is not None else ''
            ntlmcred.workstation = None
            ntlmcred.is_guest = False
            ntlmcred.encrypt = self.creds.encrypt

            if self.creds.password is None:
                raise Exception('NTLM authentication requres password!')

            if self.creds.auth_method == LDAPAuthProtocol.NTLM_PASSWORD:
                ntlmcred.password = self.creds.password
            elif self.creds.auth_method == LDAPAuthProtocol.NTLM_NT:
                ntlmcred.nt_hash = self.creds.password
            else:
                raise Exception('Unknown NTLM auth method!')

            settings = NTLMHandlerSettings(ntlmcred)
            handler = NTLMAUTHHandler(settings)

            ##setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context(
                'NTLMSSP - Microsoft NTLM Security Support Provider', handler)

            return spneg

        elif self.creds.auth_method in [
                LDAPAuthProtocol.KERBEROS_RC4, LDAPAuthProtocol.KERBEROS_NT,
                LDAPAuthProtocol.KERBEROS_AES,
                LDAPAuthProtocol.KERBEROS_PASSWORD,
                LDAPAuthProtocol.KERBEROS_CCACHE,
                LDAPAuthProtocol.KERBEROS_KEYTAB
        ]:

            if self.target is None:
                raise Exception('Target must be specified with Kerberos!')

            if self.target.host is None:
                raise Exception(
                    'target must have a domain name or hostname for kerberos!')

            if self.target.dc_ip is None:
                raise Exception('target must have a dc_ip for kerberos!')

            kcred = MSLDAPKerberosCredential()
            kc = KerberosCredential()
            kc.username = self.creds.username
            kc.domain = self.creds.domain
            kcred.enctypes = []
            if self.creds.auth_method == LDAPAuthProtocol.KERBEROS_PASSWORD:
                kc.password = self.creds.password
                kcred.enctypes = [23, 17, 18]
            elif self.creds.auth_method == LDAPAuthProtocol.KERBEROS_NT:
                kc.nt_hash = self.creds.password
                kcred.enctypes = [23]

            elif self.creds.auth_method == LDAPAuthProtocol.KERBEROS_AES:
                if len(self.creds.password) == 32:
                    kc.kerberos_key_aes_128 = self.creds.password
                    kcred.enctypes = [17]
                elif len(self.creds.password) == 64:
                    kc.kerberos_key_aes_256 = self.creds.password
                    kcred.enctypes = [18]

            elif self.creds.auth_method == LDAPAuthProtocol.KERBEROS_RC4:
                kc.kerberos_key_rc4 = self.creds.password
                kcred.enctypes = [23]

            elif self.creds.auth_method == LDAPAuthProtocol.KERBEROS_CCACHE:
                kc.ccache = self.creds.password
                kcred.enctypes = [23, 17, 18]
            elif self.creds.auth_method == LDAPAuthProtocol.KERBEROS_KEYTAB:
                kc.keytab = self.creds.password
                kcred.enctypes = [23, 17, 18]
            else:
                raise Exception(
                    'No suitable secret type found to set up kerberos!')

            if self.creds.etypes is not None:
                kcred.enctypes = list(
                    set(self.creds.etypes).intersection(set(kcred.enctypes)))

            kcred.ccred = kc
            kcred.spn = KerberosSPN.from_target_string(
                self.target.to_target_string())
            kcred.target = KerberosTarget(self.target.dc_ip)
            kcred.encrypt = self.creds.encrypt

            if self.target.proxy is not None:
                kcred.target.proxy = KerberosProxy()
                kcred.target.proxy.type = self.target.proxy.type
                kcred.target.proxy.target = copy.deepcopy(
                    self.target.proxy.target)
                kcred.target.proxy.target.endpoint_ip = self.target.dc_ip
                kcred.target.proxy.target.endpoint_port = 88
                kcred.target.proxy.creds = copy.deepcopy(
                    self.target.proxy.auth)

            handler = MSLDAPKerberos(kcred)

            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5', handler)
            return spneg

        elif self.creds.auth_method == LDAPAuthProtocol.SSPI_KERBEROS:
            if self.target is None:
                raise Exception('Target must be specified with Kerberos SSPI!')

            kerbcred = MSLDAPKerberosSSPICredential()
            kerbcred.username = self.creds.domain if self.creds.domain is not None else '<CURRENT>'
            kerbcred.username = self.creds.username if self.creds.username is not None else '<CURRENT>'
            kerbcred.password = self.creds.password if self.creds.password is not None else '<CURRENT>'
            kerbcred.spn = self.target.to_target_string()
            kerbcred.encrypt = self.creds.encrypt

            handler = MSLDAPKerberosSSPI(kerbcred)
            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5', handler)
            return spneg

        elif self.creds.auth_method == LDAPAuthProtocol.SSPI_NTLM:
            ntlmcred = MSLDAPNTLMSSPICredential()
            ntlmcred.username = self.creds.domain if self.creds.domain is not None else '<CURRENT>'
            ntlmcred.username = self.creds.username if self.creds.username is not None else '<CURRENT>'
            ntlmcred.password = self.creds.password if self.creds.password is not None else '<CURRENT>'
            ntlmcred.encrypt = self.creds.encrypt

            handler = MSLDAPNTLMSSPI(ntlmcred)
            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context(
                'NTLMSSP - Microsoft NTLM Security Support Provider', handler)
            return spneg

        elif self.creds.auth_method.value.startswith('MULTIPLEXOR'):
            if self.creds.auth_method in [
                    LDAPAuthProtocol.MULTIPLEXOR_SSL_NTLM,
                    LDAPAuthProtocol.MULTIPLEXOR_NTLM
            ]:
                from msldap.authentication.ntlm.multiplexor import MSLDAPNTLMMultiplexor
                ntlmcred = MSLDAPMultiplexorCredential()
                ntlmcred.type = 'NTLM'
                if self.creds.username is not None:
                    ntlmcred.username = '******'
                if self.creds.domain is not None:
                    ntlmcred.domain = '<CURRENT>'
                if self.creds.password is not None:
                    ntlmcred.password = '******'
                ntlmcred.is_guest = False
                ntlmcred.is_ssl = True if self.creds.auth_method == LDAPAuthProtocol.MULTIPLEXOR_SSL_NTLM else False
                ntlmcred.parse_settings(self.creds.settings)
                ntlmcred.encrypt = self.creds.encrypt

                handler = MSLDAPNTLMMultiplexor(ntlmcred)
                #setting up SPNEGO
                spneg = SPNEGO()
                spneg.add_auth_context(
                    'NTLMSSP - Microsoft NTLM Security Support Provider',
                    handler)
                return spneg

            elif self.creds.auth_method in [
                    LDAPAuthProtocol.MULTIPLEXOR_SSL_KERBEROS,
                    LDAPAuthProtocol.MULTIPLEXOR_KERBEROS
            ]:
                from msldap.authentication.kerberos.multiplexor import MSLDAPKerberosMultiplexor

                ntlmcred = MSLDAPMultiplexorCredential()
                ntlmcred.type = 'KERBEROS'
                ntlmcred.target = self.target
                if self.creds.username is not None:
                    ntlmcred.username = '******'
                if self.creds.domain is not None:
                    ntlmcred.domain = '<CURRENT>'
                if self.creds.password is not None:
                    ntlmcred.password = '******'
                ntlmcred.is_guest = False
                ntlmcred.is_ssl = True if self.creds.auth_method == LDAPAuthProtocol.MULTIPLEXOR_SSL_NTLM else False
                ntlmcred.parse_settings(self.creds.settings)
                ntlmcred.encrypt = self.creds.encrypt

                handler = MSLDAPKerberosMultiplexor(ntlmcred)
                #setting up SPNEGO
                spneg = SPNEGO()
                spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5',
                                       handler)
                return spneg
Beispiel #3
0
    def to_spnego_cred(creds, target=None):
        if creds.authentication_type == SMBAuthProtocol.NTLM:
            ntlmcred = SMBNTLMCredential()
            ntlmcred.username = creds.username
            ntlmcred.domain = creds.domain if creds.domain is not None else ''
            ntlmcred.workstation = None
            ntlmcred.is_guest = False

            if creds.secret is None:
                raise Exception('NTLM authentication requres password!')
            if creds.secret_type == SMBCredentialsSecretType.NT:
                ntlmcred.nt_hash = creds.secret
            elif creds.secret_type == SMBCredentialsSecretType.PASSWORD:
                ntlmcred.password = creds.secret

            settings = NTLMHandlerSettings(ntlmcred)
            handler = NTLMAUTHHandler(settings)

            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context(
                'NTLMSSP - Microsoft NTLM Security Support Provider', handler)

            return spneg

        elif creds.authentication_type == SMBAuthProtocol.KERBEROS:
            if target is None:
                raise Exception('Target must be specified with Kerberos!')

            if target.hostname is None:
                raise Exception(
                    'target must have a domain name or hostname for kerberos!')

            if target.dc_ip is None:
                raise Exception('target must have a dc_ip for kerberos!')

            kc = KerberosCredential()
            kc.username = creds.username
            kc.domain = creds.domain
            if creds.secret_type == SMBCredentialsSecretType.PASSWORD:
                kc.password = creds.secret
            elif creds.secret_type == SMBCredentialsSecretType.NT:
                kc.nt_hash = creds.secret

            elif creds.secret_type == SMBCredentialsSecretType.AES:
                if len(creds.secret) == 32:
                    kc.kerberos_key_aes_128 = creds.secret
                elif len(creds.secret) == 64:
                    kc.kerberos_key_aes_256 = creds.secret

            elif creds.secret_type == SMBCredentialsSecretType.RC4:
                kc.kerberos_key_rc4 = creds.secret

            elif creds.secret_type == SMBCredentialsSecretType.RC4:
                kc.ccache = creds.secret
            else:
                raise Exception(
                    'No suitable secret type found to set up kerberos!')

            kcred = SMBKerberosCredential()
            kcred.ccred = kc
            kcred.spn = KerberosSPN.from_target_string(
                target.to_target_string())
            kcred.target = KerberosTarget(target.dc_ip)
            if target.proxy is not None:
                kcred.target.proxy = KerberosProxy()
                kcred.target.proxy.target = copy.deepcopy(target.proxy.target)
                kcred.target.proxy.target.endpoint_ip = target.dc_ip
                kcred.target.proxy.target.endpoint_port = 88
                kcred.target.proxy.creds = copy.deepcopy(target.proxy.auth)

            handler = SMBKerberos(kcred)

            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5', handler)
            return spneg

        elif creds.authentication_type == SMBAuthProtocol.SSPI_KERBEROS:
            if target is None:
                raise Exception('Target must be specified with Kerberos SSPI!')

            kerbcred = SMBKerberosSSPICredential()
            kerbcred.client = None  #creds.username #here we could submit the domain as well for impersonation? TODO!
            kerbcred.password = creds.secret
            kerbcred.target = target.to_target_string()

            handler = SMBKerberosSSPI(kerbcred)
            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5', handler)
            return spneg

        elif creds.authentication_type == SMBAuthProtocol.SSPI_NTLM:
            ntlmcred = SMBNTLMSSPICredential()
            ntlmcred.client = creds.username  #here we could submit the domain as well for impersonation? TODO!
            ntlmcred.password = creds.secret

            handler = SMBNTLMSSPI(ntlmcred)
            #setting up SPNEGO
            spneg = SPNEGO()
            spneg.add_auth_context(
                'NTLMSSP - Microsoft NTLM Security Support Provider', handler)
            return spneg

        elif creds.authentication_type.value.startswith('MULTIPLEXOR'):
            if creds.authentication_type in [
                    SMBAuthProtocol.MULTIPLEXOR_SSL_NTLM,
                    SMBAuthProtocol.MULTIPLEXOR_NTLM
            ]:
                from aiosmb.authentication.ntlm.multiplexor import SMBNTLMMultiplexor

                ntlmcred = SMBMultiplexorCredential()
                ntlmcred.type = 'NTLM'
                if creds.username is not None:
                    ntlmcred.username = '******'
                if creds.domain is not None:
                    ntlmcred.domain = '<CURRENT>'
                if creds.secret is not None:
                    ntlmcred.password = '******'
                ntlmcred.is_guest = False
                ntlmcred.is_ssl = True if creds.authentication_type == SMBAuthProtocol.MULTIPLEXOR_SSL_NTLM else False
                ntlmcred.parse_settings(creds.settings)

                handler = SMBNTLMMultiplexor(ntlmcred)
                #setting up SPNEGO
                spneg = SPNEGO()
                spneg.add_auth_context(
                    'NTLMSSP - Microsoft NTLM Security Support Provider',
                    handler)
                return spneg

            elif creds.authentication_type in [
                    SMBAuthProtocol.MULTIPLEXOR_SSL_KERBEROS,
                    SMBAuthProtocol.MULTIPLEXOR_KERBEROS
            ]:
                from aiosmb.authentication.kerberos.multiplexor import SMBKerberosMultiplexor

                ntlmcred = SMBMultiplexorCredential()
                ntlmcred.type = 'KERBEROS'
                ntlmcred.target = creds.target
                if creds.username is not None:
                    ntlmcred.username = '******'
                if creds.domain is not None:
                    ntlmcred.domain = '<CURRENT>'
                if creds.secret is not None:
                    ntlmcred.password = '******'
                ntlmcred.is_guest = False
                ntlmcred.is_ssl = True if creds.authentication_type == SMBAuthProtocol.MULTIPLEXOR_SSL_NTLM else False
                ntlmcred.parse_settings(creds.settings)

                handler = SMBKerberosMultiplexor(ntlmcred)
                #setting up SPNEGO
                spneg = SPNEGO()
                spneg.add_auth_context('MS KRB5 - Microsoft Kerberos 5',
                                       handler)
                return spneg