sia_tp_sample / CroweCybersecurity__ad-ldap-enum.jsonl
shahp7575's picture
commit files to HF hub
3a7f06a
{"nwo":"CroweCybersecurity\/ad-ldap-enum","sha":"ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49","path":"ad-ldap-enum.py","language":"python","identifier":"ldap_queries","parameters":"(ldap_client, base_dn, explode_nested_groups)","argument_list":"","return_statement":"","docstring":"Main worker function for the script.","docstring_summary":"Main worker function for the script.","docstring_tokens":["Main","worker","function","for","the","script","."],"function":"def ldap_queries(ldap_client, base_dn, explode_nested_groups):\n \"\"\"Main worker function for the script.\"\"\"\n users_dictionary = {}\n groups_dictionary = {}\n computers_dictionary = {}\n group_id_to_dn_dictionary = {}\n\n # LDAP filters\n user_filter = '(objectcategory=user)'\n user_attributes = ['distinguishedName', 'sAMAccountName', 'userAccountControl', 'primaryGroupID', 'comment', 'description', 'homeDirectory', 'displayName', 'mail', 'pwdLastSet', 'lastLogon', 'profilePath', 'lockoutTime', 'scriptPath', 'userPassword']\n\n group_filter = '(objectcategory=group)'\n group_attributes = ['distinguishedName', 'sAMAccountName', 'member', 'primaryGroupToken']\n\n computer_filters = '(objectcategory=computer)'\n computer_attributes = ['distinguishedName', 'sAMAccountName', 'primaryGroupID', 'operatingSystem', 'operatingSystemHotfix', 'operatingSystemServicePack', 'operatingSystemVersion', 'servicePrincipalName']\n\n # LDAP queries\n logging.info('Querying users')\n users = query_ldap_with_paging(ldap_client, base_dn, user_filter, user_attributes, ADUser)\n logging.info('Querying groups')\n groups = query_ldap_with_paging(ldap_client, base_dn, group_filter, group_attributes, ADGroup)\n logging.info('Querying computers')\n computers = query_ldap_with_paging(ldap_client, base_dn, computer_filters, computer_attributes, ADComputer)\n\n # LDAP dictionaries\n logging.info('Building users dictionary')\n for element in users:\n users_dictionary[element.distinguished_name] = element\n\n logging.info('Building groups dictionary')\n for element in groups:\n group_id_to_dn_dictionary[element.primary_group_token] = element.distinguished_name\n groups_dictionary[element.distinguished_name] = element\n\n logging.info('Building computers dictionary')\n for element in computers:\n computers_dictionary[element.distinguished_name] = element\n\n # Loop through each group. If the membership is a range then query AD to get the full group membership\n logging.info('Exploding large groups')\n for group_key, group_object in groups_dictionary.items():\n if group_object.is_large_group:\n logging.debug('Getting full membership for [%s]', group_key)\n groups_dictionary[group_key].members = get_membership_with_ranges(ldap_client, base_dn, group_key)\n\n # Build group membership\n logging.info('Building group membership')\n logging.info('There is a total of [%i] groups', len(list(groups_dictionary.keys())))\n\n current_group_number = 0\n _output_dictionary = []\n for grp in list(groups_dictionary.keys()):\n current_group_number += 1\n _output_dictionary += process_group(users_dictionary, groups_dictionary, computers_dictionary, grp, explode_nested_groups, None, [])\n\n if current_group_number % 1000 == 0:\n logging.info('Processing group [%i]', current_group_number)\n\n # TODO: This could create output duplicates. It should be fixed at some point.\n # Add users if they have the group set as their primary ID as the group.\n # Additionally, add extended domain user information to a text file.\n user_information_filename = '{0} Extended Domain User Information.tsv'.format(args.filename_prepend).strip()\n with open(user_information_filename, 'w') as user_information_file:\n logging.info('Writing domain user information to [%s]', user_information_file.name)\n user_information_file.write('SAM Account Name\\tStatus\\tLocked Out\\tUser Password\\tDisplay Name\\tEmail\\tHome Directory\\tProfile Path\\tLogon Script Path\\tPassword Last Set\\tLast Logon\\tUser Comment\\tDescription\\n')\n\n for user_object in list(users_dictionary.values()):\n if user_object.primary_group_id and user_object.primary_group_id in group_id_to_dn_dictionary:\n grp_dn = group_id_to_dn_dictionary[user_object.primary_group_id]\n\n temp_list_a = []\n temp_list_b = []\n\n temp_list_a.append(groups_dictionary[grp_dn].sam_account_name)\n temp_list_b.append(groups_dictionary[grp_dn].sam_account_name)\n temp_list_a.append(user_object.sam_account_name)\n temp_list_b.append(user_object.sam_account_name)\n temp_list_a.append(user_object.get_account_flags())\n temp_list_b.append(user_object.get_account_flags())\n temp_list_a.append(user_object.locked_out)\n temp_list_a.append(user_object.user_password)\n temp_list_a.append(user_object.display_name)\n temp_list_a.append(user_object.mail)\n temp_list_a.append(user_object.home_directory)\n temp_list_a.append(user_object.profile_path)\n temp_list_a.append(user_object.logon_script)\n temp_list_a.append(user_object.get_password_last_set_date())\n temp_list_a.append(user_object.get_last_logon_date())\n temp_list_a.append(user_object.comment)\n temp_list_a.append(user_object.description)\n _output_dictionary.append(temp_list_b)\n\n tmp_element = \"\"\n for x, binary_string in enumerate(temp_list_a[1:]):\n binary_string = str(binary_string)\n if binary_string[:2] == \"b'\":\n binary_string = binary_string[2:]\n if binary_string[-1:] == \"'\":\n binary_string = binary_string[:-1]\n if x == len(temp_list_a[1:])-1 :\n tmp_element += binary_string + \"\\n\"\n else:\n tmp_element += binary_string + \"\\t\"\n \n user_information_file.write(tmp_element) #\"\\t\".join(str(temp_list_a[1:])) + \"\\n\")\n\n # Write Domain Computer Information\n computer_information_filename = '{0} Extended Domain Computer Information.tsv'.format(args.filename_prepend).strip()\n with open(computer_information_filename, 'w') as computer_information_file:\n logging.info('Writing domain computer information to [%s]', computer_information_file.name)\n computer_information_file.write('SAM Account Name\\tOS\\tOS Hotfix\\tOS Service Pack\\tOS VersiontSQL SPNs\\tRA SPNS\\tShare SPNs\\tMail SPNs\\tAuth SPNs\\tBackup SPNs\\tManagement SPNs\\tOther SPNs\\n')\n\n # TODO: This could create output duplicates. It should be fixed at some point.\n # Add computers if they have the group set as their primary ID as the group\n for computer_object in list(computers_dictionary.values()):\n if computer_object.primary_group_id:\n grp_dn = group_id_to_dn_dictionary[computer_object.primary_group_id]\n\n temp_list_a = []\n temp_list_b = []\n\n temp_list_a.append(groups_dictionary[grp_dn].sam_account_name)\n temp_list_a.append(computer_object.sam_account_name)\n\n temp_list_b.append(computer_object.sam_account_name)\n temp_list_b.append(computer_object.operating_system)\n temp_list_b.append(computer_object.operating_system_hotfix)\n temp_list_b.append(computer_object.operating_system_service_pack)\n temp_list_b.append(computer_object.operating_system_version)\n [temp_list_b.append(','.join(map(str, item))) for item in parse_spns(computer_object.service_principal_names)]\n\n tmp_element = \"\"\n for x, binary_string in enumerate(temp_list_b):\n binary_string = str(binary_string)\n if binary_string[:2] == \"b'\":\n binary_string = binary_string[2:]\n if binary_string[-1:] == \"'\":\n binary_string = binary_string[:-1]\n if x == len(temp_list_b)-1 :\n tmp_element += binary_string + \"\\n\"\n else:\n tmp_element += binary_string + \"\\t\"\n computer_information_file.write(tmp_element)\n _output_dictionary.append(temp_list_a)\n\n # Write Group Memberships\n group_membership_filename = '{0} Domain Group Membership.tsv'.format(args.filename_prepend).strip()\n with open(group_membership_filename, 'w') as group_membership_file:\n logging.info('Writing membership information to [%s]', group_membership_file.name)\n group_membership_file.write('Group Name\\tSAM Account Name\\tStatus\\n')\n\n for element in _output_dictionary:\n tmp_element = \"\"\n for x, binary_string in enumerate(element):\n binary_string = str(binary_string)\n if binary_string[:2] == \"b'\":\n binary_string = binary_string[2:]\n if binary_string[-1:] == \"'\":\n binary_string = binary_string[:-1]\n if x == len(element)-1 :\n tmp_element += binary_string + \"\\n\"\n else:\n tmp_element += binary_string + \"\\t\"\n \n group_membership_file.write(tmp_element)","function_tokens":["def","ldap_queries","(","ldap_client",",","base_dn",",","explode_nested_groups",")",":","users_dictionary","=","{","}","groups_dictionary","=","{","}","computers_dictionary","=","{","}","group_id_to_dn_dictionary","=","{","}","# LDAP filters","user_filter","=","'(objectcategory=user)'","user_attributes","=","[","'distinguishedName'",",","'sAMAccountName'",",","'userAccountControl'",",","'primaryGroupID'",",","'comment'",",","'description'",",","'homeDirectory'",",","'displayName'",",","'mail'",",","'pwdLastSet'",",","'lastLogon'",",","'profilePath'",",","'lockoutTime'",",","'scriptPath'",",","'userPassword'","]","group_filter","=","'(objectcategory=group)'","group_attributes","=","[","'distinguishedName'",",","'sAMAccountName'",",","'member'",",","'primaryGroupToken'","]","computer_filters","=","'(objectcategory=computer)'","computer_attributes","=","[","'distinguishedName'",",","'sAMAccountName'",",","'primaryGroupID'",",","'operatingSystem'",",","'operatingSystemHotfix'",",","'operatingSystemServicePack'",",","'operatingSystemVersion'",",","'servicePrincipalName'","]","# LDAP queries","logging",".","info","(","'Querying users'",")","users","=","query_ldap_with_paging","(","ldap_client",",","base_dn",",","user_filter",",","user_attributes",",","ADUser",")","logging",".","info","(","'Querying groups'",")","groups","=","query_ldap_with_paging","(","ldap_client",",","base_dn",",","group_filter",",","group_attributes",",","ADGroup",")","logging",".","info","(","'Querying computers'",")","computers","=","query_ldap_with_paging","(","ldap_client",",","base_dn",",","computer_filters",",","computer_attributes",",","ADComputer",")","# LDAP dictionaries","logging",".","info","(","'Building users dictionary'",")","for","element","in","users",":","users_dictionary","[","element",".","distinguished_name","]","=","element","logging",".","info","(","'Building groups dictionary'",")","for","element","in","groups",":","group_id_to_dn_dictionary","[","element",".","primary_group_token","]","=","element",".","distinguished_name","groups_dictionary","[","element",".","distinguished_name","]","=","element","logging",".","info","(","'Building computers dictionary'",")","for","element","in","computers",":","computers_dictionary","[","element",".","distinguished_name","]","=","element","# Loop through each group. If the membership is a range then query AD to get the full group membership","logging",".","info","(","'Exploding large groups'",")","for","group_key",",","group_object","in","groups_dictionary",".","items","(",")",":","if","group_object",".","is_large_group",":","logging",".","debug","(","'Getting full membership for [%s]'",",","group_key",")","groups_dictionary","[","group_key","]",".","members","=","get_membership_with_ranges","(","ldap_client",",","base_dn",",","group_key",")","# Build group membership","logging",".","info","(","'Building group membership'",")","logging",".","info","(","'There is a total of [%i] groups'",",","len","(","list","(","groups_dictionary",".","keys","(",")",")",")",")","current_group_number","=","0","_output_dictionary","=","[","]","for","grp","in","list","(","groups_dictionary",".","keys","(",")",")",":","current_group_number","+=","1","_output_dictionary","+=","process_group","(","users_dictionary",",","groups_dictionary",",","computers_dictionary",",","grp",",","explode_nested_groups",",","None",",","[","]",")","if","current_group_number","%","1000","==","0",":","logging",".","info","(","'Processing group [%i]'",",","current_group_number",")","# TODO: This could create output duplicates. It should be fixed at some point.","# Add users if they have the group set as their primary ID as the group.","# Additionally, add extended domain user information to a text file.","user_information_filename","=","'{0} Extended Domain User Information.tsv'",".","format","(","args",".","filename_prepend",")",".","strip","(",")","with","open","(","user_information_filename",",","'w'",")","as","user_information_file",":","logging",".","info","(","'Writing domain user information to [%s]'",",","user_information_file",".","name",")","user_information_file",".","write","(","'SAM Account Name\\tStatus\\tLocked Out\\tUser Password\\tDisplay Name\\tEmail\\tHome Directory\\tProfile Path\\tLogon Script Path\\tPassword Last Set\\tLast Logon\\tUser Comment\\tDescription\\n'",")","for","user_object","in","list","(","users_dictionary",".","values","(",")",")",":","if","user_object",".","primary_group_id","and","user_object",".","primary_group_id","in","group_id_to_dn_dictionary",":","grp_dn","=","group_id_to_dn_dictionary","[","user_object",".","primary_group_id","]","temp_list_a","=","[","]","temp_list_b","=","[","]","temp_list_a",".","append","(","groups_dictionary","[","grp_dn","]",".","sam_account_name",")","temp_list_b",".","append","(","groups_dictionary","[","grp_dn","]",".","sam_account_name",")","temp_list_a",".","append","(","user_object",".","sam_account_name",")","temp_list_b",".","append","(","user_object",".","sam_account_name",")","temp_list_a",".","append","(","user_object",".","get_account_flags","(",")",")","temp_list_b",".","append","(","user_object",".","get_account_flags","(",")",")","temp_list_a",".","append","(","user_object",".","locked_out",")","temp_list_a",".","append","(","user_object",".","user_password",")","temp_list_a",".","append","(","user_object",".","display_name",")","temp_list_a",".","append","(","user_object",".","mail",")","temp_list_a",".","append","(","user_object",".","home_directory",")","temp_list_a",".","append","(","user_object",".","profile_path",")","temp_list_a",".","append","(","user_object",".","logon_script",")","temp_list_a",".","append","(","user_object",".","get_password_last_set_date","(",")",")","temp_list_a",".","append","(","user_object",".","get_last_logon_date","(",")",")","temp_list_a",".","append","(","user_object",".","comment",")","temp_list_a",".","append","(","user_object",".","description",")","_output_dictionary",".","append","(","temp_list_b",")","tmp_element","=","\"\"","for","x",",","binary_string","in","enumerate","(","temp_list_a","[","1",":","]",")",":","binary_string","=","str","(","binary_string",")","if","binary_string","[",":","2","]","==","\"b'\"",":","binary_string","=","binary_string","[","2",":","]","if","binary_string","[","-","1",":","]","==","\"'\"",":","binary_string","=","binary_string","[",":","-","1","]","if","x","==","len","(","temp_list_a","[","1",":","]",")","-","1",":","tmp_element","+=","binary_string","+","\"\\n\"","else",":","tmp_element","+=","binary_string","+","\"\\t\"","user_information_file",".","write","(","tmp_element",")","#\"\\t\".join(str(temp_list_a[1:])) + \"\\n\")","# Write Domain Computer Information","computer_information_filename","=","'{0} Extended Domain Computer Information.tsv'",".","format","(","args",".","filename_prepend",")",".","strip","(",")","with","open","(","computer_information_filename",",","'w'",")","as","computer_information_file",":","logging",".","info","(","'Writing domain computer information to [%s]'",",","computer_information_file",".","name",")","computer_information_file",".","write","(","'SAM Account Name\\tOS\\tOS Hotfix\\tOS Service Pack\\tOS VersiontSQL SPNs\\tRA SPNS\\tShare SPNs\\tMail SPNs\\tAuth SPNs\\tBackup SPNs\\tManagement SPNs\\tOther SPNs\\n'",")","# TODO: This could create output duplicates. It should be fixed at some point.","# Add computers if they have the group set as their primary ID as the group","for","computer_object","in","list","(","computers_dictionary",".","values","(",")",")",":","if","computer_object",".","primary_group_id",":","grp_dn","=","group_id_to_dn_dictionary","[","computer_object",".","primary_group_id","]","temp_list_a","=","[","]","temp_list_b","=","[","]","temp_list_a",".","append","(","groups_dictionary","[","grp_dn","]",".","sam_account_name",")","temp_list_a",".","append","(","computer_object",".","sam_account_name",")","temp_list_b",".","append","(","computer_object",".","sam_account_name",")","temp_list_b",".","append","(","computer_object",".","operating_system",")","temp_list_b",".","append","(","computer_object",".","operating_system_hotfix",")","temp_list_b",".","append","(","computer_object",".","operating_system_service_pack",")","temp_list_b",".","append","(","computer_object",".","operating_system_version",")","[","temp_list_b",".","append","(","','",".","join","(","map","(","str",",","item",")",")",")","for","item","in","parse_spns","(","computer_object",".","service_principal_names",")","]","tmp_element","=","\"\"","for","x",",","binary_string","in","enumerate","(","temp_list_b",")",":","binary_string","=","str","(","binary_string",")","if","binary_string","[",":","2","]","==","\"b'\"",":","binary_string","=","binary_string","[","2",":","]","if","binary_string","[","-","1",":","]","==","\"'\"",":","binary_string","=","binary_string","[",":","-","1","]","if","x","==","len","(","temp_list_b",")","-","1",":","tmp_element","+=","binary_string","+","\"\\n\"","else",":","tmp_element","+=","binary_string","+","\"\\t\"","computer_information_file",".","write","(","tmp_element",")","_output_dictionary",".","append","(","temp_list_a",")","# Write Group Memberships","group_membership_filename","=","'{0} Domain Group Membership.tsv'",".","format","(","args",".","filename_prepend",")",".","strip","(",")","with","open","(","group_membership_filename",",","'w'",")","as","group_membership_file",":","logging",".","info","(","'Writing membership information to [%s]'",",","group_membership_file",".","name",")","group_membership_file",".","write","(","'Group Name\\tSAM Account Name\\tStatus\\n'",")","for","element","in","_output_dictionary",":","tmp_element","=","\"\"","for","x",",","binary_string","in","enumerate","(","element",")",":","binary_string","=","str","(","binary_string",")","if","binary_string","[",":","2","]","==","\"b'\"",":","binary_string","=","binary_string","[","2",":","]","if","binary_string","[","-","1",":","]","==","\"'\"",":","binary_string","=","binary_string","[",":","-","1","]","if","x","==","len","(","element",")","-","1",":","tmp_element","+=","binary_string","+","\"\\n\"","else",":","tmp_element","+=","binary_string","+","\"\\t\"","group_membership_file",".","write","(","tmp_element",")"],"url":"https:\/\/github.com\/CroweCybersecurity\/ad-ldap-enum\/blob\/ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49\/ad-ldap-enum.py#L176-L341"}
{"nwo":"CroweCybersecurity\/ad-ldap-enum","sha":"ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49","path":"ad-ldap-enum.py","language":"python","identifier":"process_group","parameters":"(users_dictionary, groups_dictionary, computers_dictionary, group_distinguished_name, explode_nested, base_group_name, groups_seen)","argument_list":"","return_statement":"return group_dictionary","docstring":"Builds group membership for a specified group.","docstring_summary":"Builds group membership for a specified group.","docstring_tokens":["Builds","group","membership","for","a","specified","group","."],"function":"def process_group(users_dictionary, groups_dictionary, computers_dictionary, group_distinguished_name, explode_nested, base_group_name, groups_seen):\n \"\"\"Builds group membership for a specified group.\"\"\"\n # Store assorted group information.\n group_dictionary = []\n\n # Query SAM name or used redefined SAM name if processing a nested group.\n if base_group_name is None:\n group_sam_name = groups_dictionary[group_distinguished_name].sam_account_name\n elif base_group_name is not None:\n group_sam_name = base_group_name\n\n # Add empty groups to the Domain Group Membership list for full visibility.\n if not groups_dictionary[group_distinguished_name].members:\n temp_list = [group_sam_name, '']\n group_dictionary.append(temp_list)\n\n # Add users\/groups\/computer if they are a 'memberOf' the group\n for member in groups_dictionary[group_distinguished_name].members:\n # Process users.\n if member in users_dictionary:\n user_member = users_dictionary[member]\n\n temp_list = [group_sam_name, user_member.sam_account_name, user_member.get_account_flags()]\n group_dictionary.append(temp_list)\n\n # Process computers.\n elif member in computers_dictionary:\n temp_list = [group_sam_name, computers_dictionary[member].sam_account_name]\n group_dictionary.append(temp_list)\n\n # Process groups.\n elif member in groups_dictionary:\n if not explode_nested or (explode_nested and base_group_name is None):\n temp_list = [group_sam_name, groups_dictionary[member].sam_account_name]\n group_dictionary.append(temp_list)\n\n if explode_nested:\n # Stop processing the chain if a circular reference is detected.\n if member in groups_seen:\n pass\n # Process a nested group.\n else:\n groups_seen.append(member)\n group_dictionary += process_group(users_dictionary, groups_dictionary, computers_dictionary, member, True, group_sam_name, groups_seen)\n\n return group_dictionary","function_tokens":["def","process_group","(","users_dictionary",",","groups_dictionary",",","computers_dictionary",",","group_distinguished_name",",","explode_nested",",","base_group_name",",","groups_seen",")",":","# Store assorted group information.","group_dictionary","=","[","]","# Query SAM name or used redefined SAM name if processing a nested group.","if","base_group_name","is","None",":","group_sam_name","=","groups_dictionary","[","group_distinguished_name","]",".","sam_account_name","elif","base_group_name","is","not","None",":","group_sam_name","=","base_group_name","# Add empty groups to the Domain Group Membership list for full visibility.","if","not","groups_dictionary","[","group_distinguished_name","]",".","members",":","temp_list","=","[","group_sam_name",",","''","]","group_dictionary",".","append","(","temp_list",")","# Add users\/groups\/computer if they are a 'memberOf' the group","for","member","in","groups_dictionary","[","group_distinguished_name","]",".","members",":","# Process users.","if","member","in","users_dictionary",":","user_member","=","users_dictionary","[","member","]","temp_list","=","[","group_sam_name",",","user_member",".","sam_account_name",",","user_member",".","get_account_flags","(",")","]","group_dictionary",".","append","(","temp_list",")","# Process computers.","elif","member","in","computers_dictionary",":","temp_list","=","[","group_sam_name",",","computers_dictionary","[","member","]",".","sam_account_name","]","group_dictionary",".","append","(","temp_list",")","# Process groups.","elif","member","in","groups_dictionary",":","if","not","explode_nested","or","(","explode_nested","and","base_group_name","is","None",")",":","temp_list","=","[","group_sam_name",",","groups_dictionary","[","member","]",".","sam_account_name","]","group_dictionary",".","append","(","temp_list",")","if","explode_nested",":","# Stop processing the chain if a circular reference is detected.","if","member","in","groups_seen",":","pass","# Process a nested group.","else",":","groups_seen",".","append","(","member",")","group_dictionary","+=","process_group","(","users_dictionary",",","groups_dictionary",",","computers_dictionary",",","member",",","True",",","group_sam_name",",","groups_seen",")","return","group_dictionary"],"url":"https:\/\/github.com\/CroweCybersecurity\/ad-ldap-enum\/blob\/ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49\/ad-ldap-enum.py#L343-L388"}
{"nwo":"CroweCybersecurity\/ad-ldap-enum","sha":"ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49","path":"ad-ldap-enum.py","language":"python","identifier":"query_ldap_with_paging","parameters":"(ldap_client, base_dn, search_filter, attributes, output_object=None, page_size=1000)","argument_list":"","return_statement":"return output_array","docstring":"Get all the Active Directory results from LDAP using a paging approach.\n By default Active Directory will return 1,000 results per query before it errors out.","docstring_summary":"Get all the Active Directory results from LDAP using a paging approach.\n By default Active Directory will return 1,000 results per query before it errors out.","docstring_tokens":["Get","all","the","Active","Directory","results","from","LDAP","using","a","paging","approach",".","By","default","Active","Directory","will","return","1","000","results","per","query","before","it","errors","out","."],"function":"def query_ldap_with_paging(ldap_client, base_dn, search_filter, attributes, output_object=None, page_size=1000):\n \"\"\"Get all the Active Directory results from LDAP using a paging approach.\n By default Active Directory will return 1,000 results per query before it errors out.\"\"\"\n\n # Method Variables\n cookie=''\n more_pages = True\n output_array = deque()\n\n # Paging for AD LDAP Queries\n ldap_control = ldap.controls.SimplePagedResultsControl(True, size=page_size, cookie='')\n\n while more_pages:\n # Query the LDAP Server\n msgid = ldap_client.search_ext(base_dn, ldap.SCOPE_SUBTREE, search_filter, attributes, serverctrls=[ldap_control])\n result_type, result_data, message_id, server_controls = ldap_client.result3(msgid)\n\n # Append Page to Results\n for element in result_data:\n if (output_object is None) and (element[0] is not None):\n output_array.append(element[1])\n elif (output_object is not None) and (element[0] is not None):\n output_array.append(output_object(element[1]))\n\n # Get the page control and get the cookie from the control.\n page_controls = [c for c in server_controls if c.controlType == ldap.controls.SimplePagedResultsControl.controlType]\n\n if page_controls:\n cookie = page_controls[0].cookie\n\n # If there is no cookie then all the pages have been retrieved.\n if not cookie:\n more_pages = False\n else:\n ldap_control.cookie = cookie\n\n return output_array","function_tokens":["def","query_ldap_with_paging","(","ldap_client",",","base_dn",",","search_filter",",","attributes",",","output_object","=","None",",","page_size","=","1000",")",":","# Method Variables","cookie","=","''","more_pages","=","True","output_array","=","deque","(",")","# Paging for AD LDAP Queries","ldap_control","=","ldap",".","controls",".","SimplePagedResultsControl","(","True",",","size","=","page_size",",","cookie","=","''",")","while","more_pages",":","# Query the LDAP Server","msgid","=","ldap_client",".","search_ext","(","base_dn",",","ldap",".","SCOPE_SUBTREE",",","search_filter",",","attributes",",","serverctrls","=","[","ldap_control","]",")","result_type",",","result_data",",","message_id",",","server_controls","=","ldap_client",".","result3","(","msgid",")","# Append Page to Results","for","element","in","result_data",":","if","(","output_object","is","None",")","and","(","element","[","0","]","is","not","None",")",":","output_array",".","append","(","element","[","1","]",")","elif","(","output_object","is","not","None",")","and","(","element","[","0","]","is","not","None",")",":","output_array",".","append","(","output_object","(","element","[","1","]",")",")","# Get the page control and get the cookie from the control.","page_controls","=","[","c","for","c","in","server_controls","if","c",".","controlType","==","ldap",".","controls",".","SimplePagedResultsControl",".","controlType","]","if","page_controls",":","cookie","=","page_controls","[","0","]",".","cookie","# If there is no cookie then all the pages have been retrieved.","if","not","cookie",":","more_pages","=","False","else",":","ldap_control",".","cookie","=","cookie","return","output_array"],"url":"https:\/\/github.com\/CroweCybersecurity\/ad-ldap-enum\/blob\/ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49\/ad-ldap-enum.py#L390-L426"}
{"nwo":"CroweCybersecurity\/ad-ldap-enum","sha":"ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49","path":"ad-ldap-enum.py","language":"python","identifier":"get_membership_with_ranges","parameters":"(ldap_client, base_dn, group_dn)","argument_list":"","return_statement":"return output_array","docstring":"Queries the membership of an Active Directory group. For large groups Active Directory will\n Not return the full membership by default but will instead return partial results. Additional\n processing is needed to get the full membership.","docstring_summary":"Queries the membership of an Active Directory group. For large groups Active Directory will\n Not return the full membership by default but will instead return partial results. Additional\n processing is needed to get the full membership.","docstring_tokens":["Queries","the","membership","of","an","Active","Directory","group",".","For","large","groups","Active","Directory","will","Not","return","the","full","membership","by","default","but","will","instead","return","partial","results",".","Additional","processing","is","needed","to","get","the","full","membership","."],"function":"def get_membership_with_ranges(ldap_client, base_dn, group_dn):\n \"\"\"Queries the membership of an Active Directory group. For large groups Active Directory will\n Not return the full membership by default but will instead return partial results. Additional\n processing is needed to get the full membership.\"\"\"\n output_array = []\n\n # RFC 4515 sanitation.\n sanatized_group_dn = str(group_dn).replace('(', '\\\\28').replace(')', '\\\\29').replace('*', '\\\\2a').replace('\\\\', '\\\\5c')\n\n membership_filter = '(&(|(objectcategory=user)(objectcategory=group)(objectcategory=computer))(memberof={0}))'.format(sanatized_group_dn)\n membership_results = query_ldap_with_paging(ldap_client, base_dn, membership_filter, ['distinguishedName'])\n\n for element in membership_results:\n output_array.append(element['distinguishedName'][0])\n\n return output_array","function_tokens":["def","get_membership_with_ranges","(","ldap_client",",","base_dn",",","group_dn",")",":","output_array","=","[","]","# RFC 4515 sanitation.","sanatized_group_dn","=","str","(","group_dn",")",".","replace","(","'('",",","'\\\\28'",")",".","replace","(","')'",",","'\\\\29'",")",".","replace","(","'*'",",","'\\\\2a'",")",".","replace","(","'\\\\'",",","'\\\\5c'",")","membership_filter","=","'(&(|(objectcategory=user)(objectcategory=group)(objectcategory=computer))(memberof={0}))'",".","format","(","sanatized_group_dn",")","membership_results","=","query_ldap_with_paging","(","ldap_client",",","base_dn",",","membership_filter",",","[","'distinguishedName'","]",")","for","element","in","membership_results",":","output_array",".","append","(","element","[","'distinguishedName'","]","[","0","]",")","return","output_array"],"url":"https:\/\/github.com\/CroweCybersecurity\/ad-ldap-enum\/blob\/ae2eb47d3ed0561bf9c6977f8afd86ec601e4b49\/ad-ldap-enum.py#L466-L481"}