Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit abc2882

Browse files
author
mpgn
committed
Fix ldap with null binding thx @juliourena
1 parent 65724d4 commit abc2882

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

cme/protocols/ldap.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -535,33 +535,34 @@ def check_if_admin(self):
535535
attributes= ["objectSid"]
536536
resp = self.search(searchFilter, attributes, sizeLimit=0)
537537
answers = []
538-
for attribute in resp[0][1]:
539-
if str(attribute['type']) == 'objectSid':
540-
sid = self.sid_to_str(attribute['vals'][0])
541-
sid_domaine = '-'.join(sid.split('-')[:-1])
542-
543-
# 2. get all group cn name
544-
searchFilter = "(|(objectSid="+sid_domaine+"-512)(objectSid="+sid_domaine+"-544)(objectSid="+sid_domaine+"-519)(objectSid=S-1-5-32-549)(objectSid=S-1-5-32-551))"
545-
attributes= ["distinguishedName"]
546-
resp = self.search(searchFilter, attributes, sizeLimit=0)
547-
answers = []
548-
for item in resp:
549-
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
550-
continue
551-
for attribute in item['attributes']:
552-
if str(attribute['type']) == 'distinguishedName':
553-
answers.append(str("(memberOf:1.2.840.113556.1.4.1941:=" + attribute['vals'][0] + ")"))
538+
if resp and self.password != '' and self.username != '':
539+
for attribute in resp[0][1]:
540+
if str(attribute['type']) == 'objectSid':
541+
sid = self.sid_to_str(attribute['vals'][0])
542+
sid_domaine = '-'.join(sid.split('-')[:-1])
543+
544+
# 2. get all group cn name
545+
searchFilter = "(|(objectSid="+sid_domaine+"-512)(objectSid="+sid_domaine+"-544)(objectSid="+sid_domaine+"-519)(objectSid=S-1-5-32-549)(objectSid=S-1-5-32-551))"
546+
attributes= ["distinguishedName"]
547+
resp = self.search(searchFilter, attributes, sizeLimit=0)
548+
answers = []
549+
for item in resp:
550+
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
551+
continue
552+
for attribute in item['attributes']:
553+
if str(attribute['type']) == 'distinguishedName':
554+
answers.append(str("(memberOf:1.2.840.113556.1.4.1941:=" + attribute['vals'][0] + ")"))
554555

555-
# 3. get memeber of these groups
556-
searchFilter = "(&(objectCategory=user)(sAMAccountName=" + self.username + ")(|" + ''.join(answers) + "))"
557-
attributes= [""]
558-
resp = self.search(searchFilter, attributes, sizeLimit=0)
559-
answers = []
560-
for item in resp:
561-
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
562-
continue
563-
if item:
564-
self.admin_privs = True
556+
# 3. get memeber of these groups
557+
searchFilter = "(&(objectCategory=user)(sAMAccountName=" + self.username + ")(|" + ''.join(answers) + "))"
558+
attributes= [""]
559+
resp = self.search(searchFilter, attributes, sizeLimit=0)
560+
answers = []
561+
for item in resp:
562+
if isinstance(item, ldapasn1_impacket.SearchResultEntry) is not True:
563+
continue
564+
if item:
565+
self.admin_privs = True
565566

566567
def getUnixTime(self, t):
567568
t -= 116444736000000000
@@ -570,10 +571,12 @@ def getUnixTime(self, t):
570571

571572
def search(self, searchFilter, attributes, sizeLimit=0):
572573
try:
573-
logging.debug('Search Filter=%s' % searchFilter)
574-
resp = self.ldapConnection.search(searchFilter=searchFilter,
575-
attributes=attributes,
576-
sizeLimit=sizeLimit)
574+
if self.ldapConnection:
575+
logging.debug('Search Filter=%s' % searchFilter)
576+
resp = self.ldapConnection.search(searchFilter=searchFilter,
577+
attributes=attributes,
578+
sizeLimit=sizeLimit)
579+
return resp
577580
except ldap_impacket.LDAPSearchError as e:
578581
if e.getErrorString().find('sizeLimitExceeded') >= 0:
579582
self.logger.error('sizeLimitExceeded exception caught, giving up and processing the data received')
@@ -584,7 +587,7 @@ def search(self, searchFilter, attributes, sizeLimit=0):
584587
else:
585588
self.logger.error(e)
586589
return False
587-
return resp
590+
return False
588591

589592
def users(self):
590593
# Building the search filter

0 commit comments

Comments
 (0)