@@ -56,7 +56,7 @@ def init_ldap_schannel_connection(domain_controller, crt, key):
5656 ldap_conn .open ()
5757 return ldap_server , ldap_conn
5858
59- def init_ldap_connection (target , tls_version , args , domain , username , password , lmhash , nthash ):
59+ def init_ldap_connection (target , tls_version , args , domain , username , password , lmhash , nthash , logger ):
6060 user = '%s\\ %s' % (domain , username )
6161 connect_to = target
6262 if args .dc_ip is not None :
@@ -73,7 +73,7 @@ def init_ldap_connection(target, tls_version, args, domain, username, password,
7373 if args .use_kerberos :
7474 ldap_session = ldap3 .Connection (ldap_server )
7575 ldap_session .bind ()
76- ldap3_kerberos_login (ldap_session , target , username , password , domain , lmhash , nthash , args .auth_key , kdcHost = args .dc_ip )
76+ ldap3_kerberos_login (ldap_session , target , username , password , logger , domain , lmhash , nthash , args .auth_key , kdcHost = args .dc_ip )
7777 elif args .auth_hashes is not None :
7878 if lmhash == "" :
7979 lmhash = "aad3b435b51404eeaad3b435b51404ee"
@@ -84,7 +84,7 @@ def init_ldap_connection(target, tls_version, args, domain, username, password,
8484 return ldap_server , ldap_session
8585
8686
87- def init_ldap_session (args , domain , username , password , lmhash , nthash ):
87+ def init_ldap_session (args , domain , username , password , lmhash , nthash , logger ):
8888 if args .use_schannel :
8989 target = args .dc_ip if args .dc_ip is not None else domain
9090 #self.logger.info("Using LDAP over Schannel (TLS) connection.")
@@ -103,16 +103,16 @@ def init_ldap_session(args, domain, username, password, lmhash, nthash):
103103
104104 if args .use_ldaps is True :
105105 try :
106- return init_ldap_connection (target , ssl .PROTOCOL_TLSv1_2 , args , domain , username , password , lmhash , nthash )
106+ return init_ldap_connection (target , ssl .PROTOCOL_TLSv1_2 , args , domain , username , password , lmhash , nthash , logger )
107107 except ldap3 .core .exceptions .LDAPSocketOpenError :
108- return init_ldap_connection (target , ssl .PROTOCOL_TLSv1 , args , domain , username , password , lmhash , nthash )
108+ return init_ldap_connection (target , ssl .PROTOCOL_TLSv1 , args , domain , username , password , lmhash , nthash , logger )
109109 else :
110- return init_ldap_connection (target , None , args , domain , username , password , lmhash , nthash )
110+ return init_ldap_connection (target , None , args , domain , username , password , lmhash , nthash , logger )
111111
112112
113- def ldap3_kerberos_login (connection , target , user , password , domain = '' , lmhash = '' , nthash = '' , aesKey = '' , kdcHost = None , TGT = None , TGS = None , useCache = True ):
114113 from pyasn1 .codec .ber import encoder , decoder
115114 from pyasn1 .type .univ import noValue
115+ def ldap3_kerberos_login (connection , target , user , password , logger , domain = '' , lmhash = '' , nthash = '' , aesKey = '' , kdcHost = None , TGT = None , TGS = None , useCache = True ):
116116 """
117117 logins into the target system explicitly using Kerberos. Hashes are used if RC4_HMAC is supported.
118118 :param string user: username
@@ -175,7 +175,7 @@ def ldap3_kerberos_login(connection, target, user, password, domain='', lmhash='
175175 TGT = creds .toTGT ()
176176 logger .debug ('Using TGT from cache' )
177177 else :
178- logger .debug ('No valid credentials found in cache' )
178+ logger .debug (f'Principal { principal } not found in cache' )
179179 else :
180180 TGS = creds .toTGS (principal )
181181 logger .debug ('Using TGS from cache' )
@@ -307,14 +307,18 @@ def info(self, device_id):
307307 try :
308308 device_id_in_current_values = False
309309 for dn_binary_value in results ['raw_attributes' ]['msDS-KeyCredentialLink' ]:
310- keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
311- if keyCredential .DeviceId is None :
312- logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
313- continue
314- if keyCredential .DeviceId .toFormatD () == device_id :
315- self .logger .success ("Found device Id" )
316- keyCredential .show ()
317- device_id_in_current_values = True
310+ try :
311+ keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
312+ if keyCredential .DeviceId is None :
313+ self .logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
314+ continue
315+ if keyCredential .DeviceId .toFormatD () == device_id :
316+ self .logger .success ("Found device Id" )
317+ keyCredential .show ()
318+ device_id_in_current_values = True
319+ except Exception as err :
320+ self .logger .warning ("Failed to parse keyCredential, error: %s, raw keyCredential: %s" % (str (err ), dn_binary_value .decode ()))
321+ self .logger .debug (traceback .format_exc ())
318322 if not device_id_in_current_values :
319323 self .logger .warning ("No value with the provided DeviceID was found for the target object" )
320324 except IndexError :
@@ -346,13 +350,16 @@ def list(self):
346350 else :
347351 self .logger .info ("Listing devices for %s" % self .target_samname )
348352 for dn_binary_value in results ['raw_attributes' ]['msDS-KeyCredentialLink' ]:
349- keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
350-
351- if keyCredential .DeviceId is None :
352- self .logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
353- self .logger .warning ("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential .DeviceId , keyCredential .CreationTime ))
354- else :
355- self .logger .info ("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential .DeviceId .toFormatD (), keyCredential .CreationTime ))
353+ try :
354+ keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
355+ if keyCredential .DeviceId is None :
356+ self .logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
357+ self .logger .warning ("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential .DeviceId , keyCredential .CreationTime ))
358+ else :
359+ self .logger .info ("DeviceID: %s | Creation Time (UTC): %s" % (keyCredential .DeviceId .toFormatD (), keyCredential .CreationTime ))
360+ except Exception as err :
361+ self .logger .warning ("Failed to parse keyCredential, error: %s, raw keyCredential: %s" % (str (err ), dn_binary_value .decode ()))
362+ self .logger .debug (traceback .format_exc ())
356363 except IndexError :
357364 self .logger .info ('Attribute msDS-KeyCredentialLink does not exist' )
358365 return
@@ -500,7 +507,7 @@ def remove(self, device_id):
500507 for dn_binary_value in results ['raw_attributes' ]['msDS-KeyCredentialLink' ]:
501508 keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
502509 if keyCredential .DeviceId is None :
503- logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
510+ self . logger .warning ("Failed to parse DeviceId for keyCredential: %s" % (str (dn_binary_value )))
504511 continue
505512 if keyCredential .DeviceId .toFormatD () == device_id :
506513 self .logger .info ("Found value to remove" )
@@ -637,11 +644,11 @@ def exportToJSON(self, filename):
637644 os .makedirs (os .path .dirname (filename ), exist_ok = True )
638645 keyCredentialsJSON = {"keyCredentials" :[]}
639646 for dn_binary_value in results ['raw_attributes' ]['msDS-KeyCredentialLink' ]:
640- keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
641647 try :
648+ keyCredential = KeyCredential .fromDNWithBinary (DNWithBinary .fromRawDNWithBinary (dn_binary_value ))
642649 keyCredentialsJSON ["keyCredentials" ].append (keyCredential .toDict ())
643650 except Exception as e :
644- logger .warning (f"Failed to serialize keyCredential, error: %s, saving the raw keyCredential instead, i.e.: %s" % (str (e ), dn_binary_value .decode ()))
651+ self . logger .warning (f"Failed to serialize keyCredential, error: %s, saving the raw keyCredential instead, i.e.: %s" % (str (e ), dn_binary_value .decode ()))
645652 keyCredentialsJSON ["keyCredentials" ].append (dn_binary_value .decode ())
646653 with open (filename , "w" ) as f :
647654 f .write (json .dumps (keyCredentialsJSON , indent = 4 ))
@@ -861,7 +868,7 @@ def main():
861868 auth_nt_hash = args .auth_hashes
862869
863870 try :
864- ldap_server , ldap_session = init_ldap_session (args = args , domain = args .auth_domain , username = args .auth_username , password = args .auth_password , lmhash = auth_lm_hash , nthash = auth_nt_hash )
871+ ldap_server , ldap_session = init_ldap_session (args = args , domain = args .auth_domain , username = args .auth_username , password = args .auth_password , lmhash = auth_lm_hash , nthash = auth_nt_hash , logger = logger )
865872 shadowcreds = ShadowCredentials (ldap_server , ldap_session , target_samname , target_domain , logger )
866873 if args .action == 'list' :
867874 shadowcreds .list ()
0 commit comments