Cheat sheet / Active Directory
ldapsearch
Reference rapide pour interroger LDAP et Active Directory en ligne de commande.
Utilisation
Ces notes reprennent mes commandes de reference pour les labs Active Directory et CTF. Les variables $IP, lab.local, user et password sont a adapter a chaque environnement.
ldapsearch is a standard command-line LDAP client available on most Linux distributions. It queries LDAP directories directly and is the universal fallback when specialized AD tools are unavailable or misbehave. In AD pentesting, ldapsearch is useful for raw LDAP queries, extracting attributes that other tools don't expose by default, and situations where installing Python tools is not possible.
Installation
Already installed on most pentesting distributions (Kali, Parrot, etc.).
sudo apt install ldap-utilsAuthentication
Simple Bind (cleartext password)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '<FILTER>' <ATTRIBUTES>Simple Bind (LDAPS)
ldapsearch -x -H ldaps://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '<FILTER>' <ATTRIBUTES>NTLM-style Bind (domain\user format)
ldapsearch -x -H ldap://$IP -D 'LAB\user' -w 'password' -b 'DC=lab,DC=local' '<FILTER>' <ATTRIBUTES>Anonymous Bind (if allowed)
ldapsearch -x -H ldap://$IP -b 'DC=lab,DC=local' '<FILTER>' <ATTRIBUTES>Password from file (avoid shell history)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -y /path/to/passfile -b 'DC=lab,DC=local' '<FILTER>'Through SOCKS proxy
proxychains4 -q ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '<FILTER>'Common flags:
-x= simple authentication,-H= LDAP URI,-D= bind DN,-w= password,-b= search base,-s= scope (base/one/sub). Default scope issub(subtree).
Enumeration
RootDSE (no auth required)
Get domain information without credentials
ldapsearch -x -H ldap://$IP -s base '(objectClass=*)' namingContexts defaultNamingContextGet domain functional level
ldapsearch -x -H ldap://$IP -s base '(objectClass=*)' domainControllerFunctionality domainFunctionality forestFunctionalityDomain Enumeration
Get all users
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user))' sAMAccountNameGet a specific user's information
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(sAMAccountName=TARGET_USER)'Search for passwords in description fields
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user)(description=*))' sAMAccountName descriptionSearch for passwords in info fields
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user)(info=*))' sAMAccountName infoGet all groups
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectCategory=group)' cn memberGet members of a specific group (e.g. Domain Admins)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(cn=Domain Admins)' memberGet all computers
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectCategory=computer)' sAMAccountName operatingSystemGet all OUs
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectCategory=organizationalUnit)' ouGet domain trusts
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectClass=trustedDomain)' cn trustDirection trustType trustAttributesGet users inside a specific OU
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'OU=TARGET_OU,DC=lab,DC=local' '(objectClass=user)' sAMAccountNameKerberos-Related Queries
Get Kerberoastable users (SPN set)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(samAccountType=805306368)(servicePrincipalName=*))' sAMAccountName servicePrincipalNameGet AS-REP Roastable users (DONT_REQ_PREAUTH)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(userAccountControl:1.2.840.113556.1.4.803:=4194304)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))' sAMAccountNameGet users with Constrained Delegation
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(msDS-AllowedToDelegateTo=*)' sAMAccountName msDS-AllowedToDelegateToGet computers with Unconstrained Delegation (excluding DCs)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=computer)(userAccountControl:1.2.840.113556.1.4.803:=524288)(!(userAccountControl:1.2.840.113556.1.4.803:=8192)))' sAMAccountNameGet computers with RBCD configured
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(msDS-AllowedToActOnBehalfOfOtherIdentity=*)' sAMAccountName msDS-AllowedToActOnBehalfOfOtherIdentityPassword Policy
Get domain password policy
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectClass=domain)' minPwdLength maxPwdAge minPwdAge pwdHistoryLength lockoutThreshold lockoutDurationGet Fine-Grained Password Policies
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'CN=Password Settings Container,CN=System,DC=lab,DC=local' '(objectClass=msDS-PasswordSettings)' cn msDS-MinimumPasswordLength msDS-LockoutThresholdSensitive Attributes
Get Machine Account Quota
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectClass=domain)' ms-DS-MachineAccountQuotaGet LAPS passwords (requires read rights)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(ms-mcs-admpwdexpirationtime=*)' sAMAccountName ms-mcs-admpwd ms-mcs-admpwdexpirationtimeGet gMSA accounts
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectClass=msDS-GroupManagedServiceAccount)' sAMAccountName msDS-ManagedPassword msDS-GroupMSAMembershipGet users with adminCount=1
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user)(adminCount=1))' sAMAccountNameGet disabled accounts
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=2))' sAMAccountNameGet users with password never expires
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(&(objectCategory=person)(objectClass=user)(userAccountControl:1.2.840.113556.1.4.803:=65536))' sAMAccountNameGet users with Shadow Credentials (msDS-KeyCredentialLink)
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(msDS-KeyCredentialLink=*)' sAMAccountName msDS-KeyCredentialLinkGPO Enumeration
Get all GPOs
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'DC=lab,DC=local' '(objectCategory=groupPolicyContainer)' displayName gPCFileSysPathAD CS Enumeration
Get Certificate Authorities
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'CN=Enrollment Services,CN=Public Key Services,CN=Services,CN=Configuration,DC=lab,DC=local' '(objectClass=pKIEnrollmentService)' cn dNSHostName certificateTemplatesGet Certificate Templates
ldapsearch -x -H ldap://$IP -D 'user@lab.local' -w 'password' -b 'CN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,DC=lab,DC=local' '(objectClass=pKICertificateTemplate)' cn msPKI-Certificate-Name-Flag msPKI-Enrollment-Flag pKIExtendedKeyUsageUseful Tips
Limit results
ldapsearch ... -z 10 # Return max 10 entriesPage through large result sets
ldapsearch ... -E pr=1000/nopromptOutput to LDIF file
ldapsearch ... > output.ldifCount results only
ldapsearch ... | grep -c "^dn:"Get all attributes for an object
ldapsearch ... '(sAMAccountName=TARGET)' '*'Get operational attributes (e.g. timestamps)
ldapsearch ... '(sAMAccountName=TARGET)' '*' '+'