diff --git a/impacket/krb5/kerberosv5.py b/impacket/krb5/kerberosv5.py index 6bd08f037f..685baf1f61 100644 --- a/impacket/krb5/kerberosv5.py +++ b/impacket/krb5/kerberosv5.py @@ -500,25 +500,42 @@ def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT # No cache present pass else: + LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) + # retrieve domain information from CCache file if needed if domain == '': domain = ccache.principal.realm['data'] - LOG.debug('Domain retrieved from CCache: %s' % domain) + LOG.debug('User domain retrieved from CCache: %s' % domain) - LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) + # retreive target domain from host FQDN + targetDomain = '.'.join(targetName.upper().split('.')[1:]) + if targetDomain == '': + targetDomain = domain + else: + LOG.debug('Target domain retrieved from host FQDN: %s' % targetDomain) + + # check if there a service ticket already in ccache principal = 'host/%s@%s' % (targetName.upper(), domain.upper()) creds = ccache.getCredential(principal) if creds is None: # Let's try for the TGT and go from there principal = 'krbtgt/%s@%s' % (domain.upper(),domain.upper()) creds = ccache.getCredential(principal) - if creds is not None: + if creds is None: + # Let's try for the refferal TGT and go from there + principal = 'krbtgt/%s@%s' % (targetDomain.upper(),domain.upper()) + creds = ccache.getCredential(principal) + if creds is not None: + TGT = creds.toTGT() + LOG.debug('Using Refferal TGT from cache') + else: + LOG.debug("No valid credentials found in cache. ") + else: TGT = creds.toTGT() LOG.debug('Using TGT from cache') - else: - LOG.debug("No valid credentials found in cache. ") else: TGS = creds.toTGS(principal) + LOG.debug('Using TGS from cache') # retrieve user information from CCache file if needed if username == '' and creds is not None: @@ -557,12 +574,18 @@ def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT cipher = TGT['cipher'] sessionKey = TGT['sessionKey'] - # Now that we have the TGT, we should ask for a TGS for cifs + #now we need to get target KDC from TGT + from impacket.krb5.ccache import decoder, AS_REP + decodedTGT = decoder.decode(tgt, asn1Spec = AS_REP())[0] + kdcDomain = str(decodedTGT['ticket']['sname']['name-string'][1]) + LOG.debug('KDC domain obtained from TGT: %s' % kdcDomain) + + # Now that we have the TGT, we should ask for a TGS for host if TGS is None: serverName = Principal('host/%s' % targetName, type=constants.PrincipalNameType.NT_SRV_INST.value) try: - tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, domain, kdcHost, tgt, cipher, sessionKey) + tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, kdcDomain, kdcHost, tgt, cipher, sessionKey) except KerberosError, e: if e.getErrorCode() == constants.ErrorCodes.KDC_ERR_ETYPE_NOSUPP.value: # We might face this if the target does not support AES diff --git a/impacket/smb3.py b/impacket/smb3.py index 8219155292..f1ac0b28ae 100644 --- a/impacket/smb3.py +++ b/impacket/smb3.py @@ -547,14 +547,19 @@ def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', a # First of all, we need to get a TGT for the user userName = Principal(user, type=constants.PrincipalNameType.NT_PRINCIPAL.value) - if TGT is None: - if TGS is None: - tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, domain, lmhash, nthash, aesKey, kdcHost) + if (TGT is None) and (TGS is None): + tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, domain, lmhash, nthash, aesKey, kdcHost) else: tgt = TGT['KDC_REP'] cipher = TGT['cipher'] sessionKey = TGT['sessionKey'] + #now we need to get target KDC from TGT + from impacket.krb5.ccache import decoder, AS_REP + decodedTGT = decoder.decode(tgt, asn1Spec = AS_REP())[0] + kdcDomain = str(decodedTGT['ticket']['sname']['name-string'][1]) + LOG.debug('KDC domain obtained from TGT: %s' % kdcDomain) + # Save the ticket # If you want, for debugging purposes # from impacket.krb5.ccache import CCache @@ -573,7 +578,7 @@ def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', a if TGS is None: serverName = Principal('cifs/%s' % (self._Connection['ServerName']), type=constants.PrincipalNameType.NT_SRV_INST.value) - tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, domain, kdcHost, tgt, cipher, sessionKey) + tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, kdcDomain, kdcHost, tgt, cipher, sessionKey) else: tgs = TGS['KDC_REP'] cipher = TGS['cipher'] diff --git a/impacket/smbconnection.py b/impacket/smbconnection.py index 645e6c6737..e75ca089ee 100644 --- a/impacket/smbconnection.py +++ b/impacket/smbconnection.py @@ -305,19 +305,34 @@ def kerberosLogin(self, user, password, domain='', lmhash='', nthash='', aesKey= # retrieve domain information from CCache file if needed if domain == '': domain = ccache.principal.realm['data'] - LOG.debug('Domain retrieved from CCache: %s' % domain) + LOG.debug('User domain retrieved from CCache: %s' % domain) - principal = 'cifs/%s@%s' % (self.getRemoteName().upper(), domain.upper()) + # retreive target domain from host FQDN + targetDomain = '.'.join(self.getRemoteName().upper().split('.')[1:]) + if targetDomain == '': + targetDomain = domain + else: + LOG.debug('Target domain retrieved from host FQDN: %s' % targetDomain) + + # check if there a service ticket already in ccache + principal = 'cifs/%s@%s' % (self.getRemoteName().upper(), targetDomain.upper()) creds = ccache.getCredential(principal) if creds is None: # Let's try for the TGT and go from there principal = 'krbtgt/%s@%s' % (domain.upper(),domain.upper()) creds = ccache.getCredential(principal) - if creds is not None: + if creds is None: + # Let's try for the refferal TGT and go from there + principal = 'krbtgt/%s@%s' % (targetDomain.upper(),domain.upper()) + creds = ccache.getCredential(principal) + if creds is not None: + TGT = creds.toTGT() + LOG.debug('Using Refferal TGT from cache') + else: + LOG.debug("No valid credentials found in cache. ") + else: TGT = creds.toTGT() LOG.debug('Using TGT from cache') - else: - LOG.debug("No valid credentials found in cache. ") else: TGS = creds.toTGS(principal) LOG.debug('Using TGS from cache')