LDAP: discover and query a directory
Read RootDSE, choose a search base and perform a bounded authenticated query over TLS.
LDAP commonly uses TCP 389; LDAPS uses TLS from connection establishment, commonly on 636. LDAP on 389 can negotiate StartTLS. A directory is not necessarily Active Directory, and an open port does not establish anonymous access to directory entries.
Read RootDSE
RootDSE describes the server itself. Query the empty DN with base scope, rather than searching the whole directory:
ldapsearch -x -H ldap://dc.example.org:389 -b '' -s base \
-o nettimeout=5 -l 5 '(objectClass=*)' \
namingContexts defaultNamingContext supportedLDAPVersion supportedSASLMechanismsThis attempts an anonymous bind and metadata query. Some servers permit it
while requiring authentication for every other search. namingContexts lists
directory partitions; AD commonly supplies defaultNamingContext for its
domain partition. Do not manufacture a base DN from the hostname if the server
or administrator can provide it.
Query a selected entry
For a known AD account and a confirmed base DN:
ldapsearch -x -H ldap://dc.example.org:389 -ZZ \
-D 'reader@example.org' -W -b 'DC=example,DC=org' -s sub \
-o nettimeout=5 -l 5 -z 10 \
'(&(objectClass=user)(sAMAccountName=example-user))' \
distinguishedName sAMAccountName memberOf-W prompts for the password; -ZZ requires StartTLS to succeed before the
bind. Use the server’s certificate hostname and configure the trusted CA,
for example with LDAPTLS_CACERT=/path/to/ca.pem. For LDAPS, replace the URI
with ldaps://dc.example.org:636 and omit -ZZ.
UPN binding and sAMAccountName are AD-specific here. Other directories may
require a bind DN and filters using attributes such as uid. -s base
queries one object, -s one its immediate children, and -s sub a subtree.
Request only the attributes needed.
Interpret limits and errors
- A successful bind establishes authentication, not unrestricted read access.
- No matching entry can mean a wrong base/filter or restricted visibility.
- Size/time-limit errors can accompany partial results; do not present them as a complete inventory.
memberOfis not a complete effective-privilege calculation: nested and primary-group membership need separate treatment.- TLS failure precedes password validation; diagnose the certificate and trust chain before changing credentials.
Filters and pagination
LDAP filters use RFC 4515 escaping. Shell quotes protect the shell, not the
LDAP filter: literal *, (, ), backslash and NUL in values require
LDAP escaping. Do not interpolate arbitrary input into a filter.
The paged-results control -E pr=100/prompt requests pages of up to 100 entries
and prompts between pages. A page size is not a total collection limit;
server limits and ACLs still apply.
Continue to AD enumeration or ADCS when the directory context and required access are established.
Sources
OpenLDAP ldapsearch manual, reviewed 2026-11-24. No directory authentication performed.
Provenance and confidence
- Version
- 1
- Sources reviewed
- 2026-11-24
- Confidence
- medium — OpenLDAP ldapsearch options reviewed on 2026-11-24; examples are documentary and were not run against a directory.
Also searched as: ldap, ldaps, 389/tcp, 636/tcp, rootdse, base dn, annuaire, énumération LDAP