From d17108897fd7a90d6719adb4c6a2e018068ee10e Mon Sep 17 00:00:00 2001 From: Ramzeth Date: Fri, 7 Dec 2018 21:20:40 +0300 Subject: [PATCH 1/3] added support for using Referral TGT in smbclient.py, atexec.py --- impacket/krb5/kerberosv5.py | 23 +++++++++++++++-------- impacket/smb3.py | 10 +++++----- impacket/smbconnection.py | 21 ++++++++++++++------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/impacket/krb5/kerberosv5.py b/impacket/krb5/kerberosv5.py index 6bd08f037f..1ffcb12fd4 100644 --- a/impacket/krb5/kerberosv5.py +++ b/impacket/krb5/kerberosv5.py @@ -491,7 +491,7 @@ def getKerberosType3(cipher, sessionKey, auth_data): return cipher, sessionKey2, resp.getData() -def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT = None, TGS = None, targetName='', kdcHost = None, useCache = True): +def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', TGT = None, TGS = None, targetName='', kdcHost = None, useCache = True): if TGT is None and TGS is None: if useCache is True: try: @@ -501,16 +501,23 @@ def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT pass else: # retrieve domain information from CCache file if needed - if domain == '': - domain = ccache.principal.realm['data'] - LOG.debug('Domain retrieved from CCache: %s' % domain) + if userDomain == '': + userDomain = ccache.principal.realm['data'] + LOG.debug('User domain retrieved from CCache: %s' % userDomain) + + # retreive target domain from host FQDN + targetDomain = '.'.join(targetName.upper().split('.')[1:]) + if targetDomain == '': + targetDomain = userDomain + else: + LOG.debug('Target domain retrieved from host FQDN: %s' % targetDomain) LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) - principal = 'host/%s@%s' % (targetName.upper(), domain.upper()) + principal = 'host/%s@%s' % (targetName.upper(), userDomain.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()) + principal = 'krbtgt/%s@%s' % (targetDomain.upper(),userDomain.upper()) creds = ccache.getCredential(principal) if creds is not None: TGT = creds.toTGT() @@ -562,7 +569,7 @@ def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT 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, targetDomain, 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 @@ -610,7 +617,7 @@ def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT authenticator = Authenticator() authenticator['authenticator-vno'] = 5 - authenticator['crealm'] = domain + authenticator['crealm'] = userDomain seq_set(authenticator, 'cname', userName.components_to_asn1) now = datetime.datetime.utcnow() diff --git a/impacket/smb3.py b/impacket/smb3.py index 8219155292..fe0d9dae5a 100644 --- a/impacket/smb3.py +++ b/impacket/smb3.py @@ -502,7 +502,7 @@ def getCredentials(self): self.__TGT, self.__TGS) - def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', aesKey='', kdcHost = '', TGT=None, TGS=None): + def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmhash = '', nthash = '', aesKey='', kdcHost = '', TGT=None, TGS=None): # If TGT or TGS are specified, they are in the form of: # TGS['KDC_REP'] = the response from the server # TGS['cipher'] = the cipher used @@ -519,7 +519,7 @@ def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', a self.__userName = user self.__password = password - self.__domain = domain + self.__domain = targetDomain self.__lmhash = lmhash self.__nthash = nthash self.__kdc = kdcHost @@ -573,7 +573,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, targetDomain, kdcHost, tgt, cipher, sessionKey) else: tgs = TGS['KDC_REP'] cipher = TGS['cipher'] @@ -602,7 +602,7 @@ def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', a authenticator = Authenticator() authenticator['authenticator-vno'] = 5 - authenticator['crealm'] = domain + authenticator['crealm'] = userDomain seq_set(authenticator, 'cname', userName.components_to_asn1) now = datetime.datetime.utcnow() @@ -636,7 +636,7 @@ def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', a if ans.isValidAnswer(STATUS_SUCCESS): self._Session['SessionID'] = ans['SessionID'] self._Session['SigningRequired'] = self._Connection['RequireSigning'] - self._Session['UserCredentials'] = (user, password, domain, lmhash, nthash) + self._Session['UserCredentials'] = (user, password, targetDomain, lmhash, nthash) self._Session['Connection'] = self._NetBIOSSession.get_socket() self._Session['SessionKey'] = sessionKey.contents[:16] diff --git a/impacket/smbconnection.py b/impacket/smbconnection.py index 645e6c6737..b1a02b8383 100644 --- a/impacket/smbconnection.py +++ b/impacket/smbconnection.py @@ -264,7 +264,7 @@ def login(self, user, password, domain = '', lmhash = '', nthash = '', ntlmFallb except (smb.SessionError, smb3.SessionError), e: raise SessionError(e.get_error_code(), e.get_error_packet()) - def kerberosLogin(self, user, password, domain='', lmhash='', nthash='', aesKey='', kdcHost=None, TGT=None, + def kerberosLogin(self, user, password, userDomain='', lmhash='', nthash='', aesKey='', kdcHost=None, TGT=None, TGS=None, useCache=True): """ logins into the target system explicitly using Kerberos. Hashes are used if RC4_HMAC is supported. @@ -303,15 +303,22 @@ def kerberosLogin(self, user, password, domain='', lmhash='', nthash='', aesKey= 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) + if userDomain == '': + userDomain = ccache.principal.realm['data'] + LOG.debug('User domain retrieved from CCache: %s' % userDomain) + + # retreive target domain from host FQDN + targetDomain = '.'.join(self.getRemoteName().upper().split('.')[1:]) + if targetDomain == '': + targetDomain = userDomain + else: + LOG.debug('Target domain retrieved from host FQDN: %s' % targetDomain) - principal = 'cifs/%s@%s' % (self.getRemoteName().upper(), domain.upper()) + 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()) + principal = 'krbtgt/%s@%s' % (targetDomain.upper(),userDomain.upper()) creds = ccache.getCredential(principal) if creds is not None: TGT = creds.toTGT() @@ -335,7 +342,7 @@ def kerberosLogin(self, user, password, domain='', lmhash='', nthash='', aesKey= if self.getDialect() == smb.SMB_DIALECT: return self._SMBConnection.kerberos_login(user, password, domain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) - return self._SMBConnection.kerberosLogin(user, password, domain, lmhash, nthash, aesKey, kdcHost, TGT, + return self._SMBConnection.kerberosLogin(user, password, userDomain, targetDomain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) except (smb.SessionError, smb3.SessionError), e: raise SessionError(e.get_error_code(), e.get_error_packet()) From 37709ab6b0dc399d1d8e9d06a3b4ab0ca153e0e6 Mon Sep 17 00:00:00 2001 From: Ramzeth Date: Sat, 8 Dec 2018 00:59:36 +0300 Subject: [PATCH 2/3] fix build errors --- impacket/krb5/kerberosv5.py | 2 +- impacket/smb3.py | 2 +- impacket/smbconnection.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/impacket/krb5/kerberosv5.py b/impacket/krb5/kerberosv5.py index 1ffcb12fd4..324de6a3ef 100644 --- a/impacket/krb5/kerberosv5.py +++ b/impacket/krb5/kerberosv5.py @@ -541,7 +541,7 @@ def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', if TGT is None: if TGS is None: try: - tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, domain, lmhash, nthash, aesKey, kdcHost) + tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, targetDomain, lmhash, nthash, aesKey, kdcHost) 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 fe0d9dae5a..d1083e074c 100644 --- a/impacket/smb3.py +++ b/impacket/smb3.py @@ -549,7 +549,7 @@ def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmha 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) + tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, targetDomain, lmhash, nthash, aesKey, kdcHost) else: tgt = TGT['KDC_REP'] cipher = TGT['cipher'] diff --git a/impacket/smbconnection.py b/impacket/smbconnection.py index b1a02b8383..8642e6f216 100644 --- a/impacket/smbconnection.py +++ b/impacket/smbconnection.py @@ -340,7 +340,7 @@ def kerberosLogin(self, user, password, userDomain='', lmhash='', nthash='', aes while True: try: if self.getDialect() == smb.SMB_DIALECT: - return self._SMBConnection.kerberos_login(user, password, domain, lmhash, nthash, aesKey, kdcHost, + return self._SMBConnection.kerberos_login(user, password, targetDomain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) return self._SMBConnection.kerberosLogin(user, password, userDomain, targetDomain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) From 8b685fb18eed5db84162bbb799d07c6c8c1409a2 Mon Sep 17 00:00:00 2001 From: Ramzeth Date: Wed, 12 Dec 2018 22:49:40 +0300 Subject: [PATCH 3/3] Added correct parsing of ccache file for all types of tickets --- impacket/krb5/kerberosv5.py | 46 +++++++++++++++++++++++++------------ impacket/smb3.py | 21 ++++++++++------- impacket/smbconnection.py | 30 +++++++++++++++--------- 3 files changed, 63 insertions(+), 34 deletions(-) diff --git a/impacket/krb5/kerberosv5.py b/impacket/krb5/kerberosv5.py index 324de6a3ef..685baf1f61 100644 --- a/impacket/krb5/kerberosv5.py +++ b/impacket/krb5/kerberosv5.py @@ -491,7 +491,7 @@ def getKerberosType3(cipher, sessionKey, auth_data): return cipher, sessionKey2, resp.getData() -def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', TGT = None, TGS = None, targetName='', kdcHost = None, useCache = True): +def getKerberosType1(username, password, domain, lmhash, nthash, aesKey='', TGT = None, TGS = None, targetName='', kdcHost = None, useCache = True): if TGT is None and TGS is None: if useCache is True: try: @@ -500,32 +500,42 @@ def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', # No cache present pass else: + LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) + # retrieve domain information from CCache file if needed - if userDomain == '': - userDomain = ccache.principal.realm['data'] - LOG.debug('User domain retrieved from CCache: %s' % userDomain) + if domain == '': + domain = ccache.principal.realm['data'] + LOG.debug('User domain retrieved from CCache: %s' % domain) # retreive target domain from host FQDN targetDomain = '.'.join(targetName.upper().split('.')[1:]) if targetDomain == '': - targetDomain = userDomain + targetDomain = domain else: LOG.debug('Target domain retrieved from host FQDN: %s' % targetDomain) - LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) - principal = 'host/%s@%s' % (targetName.upper(), userDomain.upper()) + # 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' % (targetDomain.upper(),userDomain.upper()) + 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: @@ -541,7 +551,7 @@ def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', if TGT is None: if TGS is None: try: - tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, targetDomain, lmhash, nthash, aesKey, kdcHost) + tgt, cipher, oldSessionKey, sessionKey = getKerberosTGT(userName, password, domain, lmhash, nthash, aesKey, kdcHost) except KerberosError, e: if e.getErrorCode() == constants.ErrorCodes.KDC_ERR_ETYPE_NOSUPP.value: # We might face this if the target does not support AES @@ -564,12 +574,18 @@ def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', 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, targetDomain, 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 @@ -617,7 +633,7 @@ def getKerberosType1(username, password, userDomain, lmhash, nthash, aesKey='', authenticator = Authenticator() authenticator['authenticator-vno'] = 5 - authenticator['crealm'] = userDomain + authenticator['crealm'] = domain seq_set(authenticator, 'cname', userName.components_to_asn1) now = datetime.datetime.utcnow() diff --git a/impacket/smb3.py b/impacket/smb3.py index d1083e074c..f1ac0b28ae 100644 --- a/impacket/smb3.py +++ b/impacket/smb3.py @@ -502,7 +502,7 @@ def getCredentials(self): self.__TGT, self.__TGS) - def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmhash = '', nthash = '', aesKey='', kdcHost = '', TGT=None, TGS=None): + def kerberosLogin(self, user, password, domain = '', lmhash = '', nthash = '', aesKey='', kdcHost = '', TGT=None, TGS=None): # If TGT or TGS are specified, they are in the form of: # TGS['KDC_REP'] = the response from the server # TGS['cipher'] = the cipher used @@ -519,7 +519,7 @@ def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmha self.__userName = user self.__password = password - self.__domain = targetDomain + self.__domain = domain self.__lmhash = lmhash self.__nthash = nthash self.__kdc = kdcHost @@ -547,14 +547,19 @@ def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmha # 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, targetDomain, 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, userDomain = '', targetDomain = '', lmha if TGS is None: serverName = Principal('cifs/%s' % (self._Connection['ServerName']), type=constants.PrincipalNameType.NT_SRV_INST.value) - tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, targetDomain, kdcHost, tgt, cipher, sessionKey) + tgs, cipher, oldSessionKey, sessionKey = getKerberosTGS(serverName, kdcDomain, kdcHost, tgt, cipher, sessionKey) else: tgs = TGS['KDC_REP'] cipher = TGS['cipher'] @@ -602,7 +607,7 @@ def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmha authenticator = Authenticator() authenticator['authenticator-vno'] = 5 - authenticator['crealm'] = userDomain + authenticator['crealm'] = domain seq_set(authenticator, 'cname', userName.components_to_asn1) now = datetime.datetime.utcnow() @@ -636,7 +641,7 @@ def kerberosLogin(self, user, password, userDomain = '', targetDomain = '', lmha if ans.isValidAnswer(STATUS_SUCCESS): self._Session['SessionID'] = ans['SessionID'] self._Session['SigningRequired'] = self._Connection['RequireSigning'] - self._Session['UserCredentials'] = (user, password, targetDomain, lmhash, nthash) + self._Session['UserCredentials'] = (user, password, domain, lmhash, nthash) self._Session['Connection'] = self._NetBIOSSession.get_socket() self._Session['SessionKey'] = sessionKey.contents[:16] diff --git a/impacket/smbconnection.py b/impacket/smbconnection.py index 8642e6f216..e75ca089ee 100644 --- a/impacket/smbconnection.py +++ b/impacket/smbconnection.py @@ -264,7 +264,7 @@ def login(self, user, password, domain = '', lmhash = '', nthash = '', ntlmFallb except (smb.SessionError, smb3.SessionError), e: raise SessionError(e.get_error_code(), e.get_error_packet()) - def kerberosLogin(self, user, password, userDomain='', lmhash='', nthash='', aesKey='', kdcHost=None, TGT=None, + def kerberosLogin(self, user, password, domain='', lmhash='', nthash='', aesKey='', kdcHost=None, TGT=None, TGS=None, useCache=True): """ logins into the target system explicitly using Kerberos. Hashes are used if RC4_HMAC is supported. @@ -303,28 +303,36 @@ def kerberosLogin(self, user, password, userDomain='', lmhash='', nthash='', aes else: LOG.debug("Using Kerberos Cache: %s" % os.getenv('KRB5CCNAME')) # retrieve domain information from CCache file if needed - if userDomain == '': - userDomain = ccache.principal.realm['data'] - LOG.debug('User domain retrieved from CCache: %s' % userDomain) + if domain == '': + domain = ccache.principal.realm['data'] + LOG.debug('User domain retrieved from CCache: %s' % domain) # retreive target domain from host FQDN targetDomain = '.'.join(self.getRemoteName().upper().split('.')[1:]) if targetDomain == '': - targetDomain = userDomain + 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' % (targetDomain.upper(),userDomain.upper()) + 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') @@ -340,9 +348,9 @@ def kerberosLogin(self, user, password, userDomain='', lmhash='', nthash='', aes while True: try: if self.getDialect() == smb.SMB_DIALECT: - return self._SMBConnection.kerberos_login(user, password, targetDomain, lmhash, nthash, aesKey, kdcHost, + return self._SMBConnection.kerberos_login(user, password, domain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) - return self._SMBConnection.kerberosLogin(user, password, userDomain, targetDomain, lmhash, nthash, aesKey, kdcHost, TGT, + return self._SMBConnection.kerberosLogin(user, password, domain, lmhash, nthash, aesKey, kdcHost, TGT, TGS) except (smb.SessionError, smb3.SessionError), e: raise SessionError(e.get_error_code(), e.get_error_packet())