Dfate
Dfate

Reputation: 13

Search AD groups with square brackets in name

I want to query Active Directory for group memberships of groups with square brackets in their names (e.g test[group]data). I am using Python 3.6.5 with the ldap3 libraries. Finding other groups and resolving group memberships works totally fine.

Setting the group_name e.g. to * and then scrolling through the results I do find the group I am searching for:

DN: CN=test[group]data,OU=Groups,DC=awesome,DC=local - STATUS: Read - READ TIME: 2019-09-24T10:19:17.536473

Searching for this one group directly fails to find anything and conn.entries remains empty.

group_name = "test[group]data"
group_name = escape_filter_chars(group_name)
searchfilter= '(&(objectClass=group)(sAMAccountName={group_name}))'.replace('{group_name}', group_name)
conn.search(search_base=AD_BASE,search_filter=searchfilter,search_scope=SUBTREE)
conn.entries[0].entry_dn

Upvotes: 0

Views: 340

Answers (1)

Dfate
Dfate

Reputation: 13

I was searching for the wrong attribute. Instead of sAMAccountname the correct attribute is CN

This code now works!

group_name = 'test[group]data'
group_name = escape_filter_chars(group_name)
searchfilter= '(&(objectClass=group)(CN={group_name}))'.replace('{group_name}', group_name)
conn.search(search_base=AD_BASE,search_filter=searchfilter,search_scope=SUBTREE)
conn.entries[0].entry_dn

Upvotes: 1

Related Questions