LDAP injection: filters built from user input against a directory
How applications that search Active Directory or OpenLDAP build filters, how special characters change a filter’s logic, and how escaping, bind design, and least-privilege service accounts prevent it.
Diagram
A directory search filter
Step 1 of 4
Input
A login or people-search form sends a name.
# Input # A login or people-search form sends a name.
The example on the open step is one illustration, or the fix you can paste into an application you own.
What this page is made of
3 pieces. Each one says what fails, who is affected, what you would notice, and what you change.
Filters are a language
- What fails
- LDAP filters have operators and wildcards, so unescaped input changes the query.
- Who is affected
- Directory users whose attributes become searchable.
- What you would notice
- String concatenation building (&(...)) filters.
- What you change
- Use the library’s escape function or a query builder.
Authenticate with bind, not search
- What fails
- Logins that search for a user with a password attribute in the filter are fragile.
- Who is affected
- Every account in the directory.
- What you would notice
- A filter that includes userPassword.
- What you change
- Search for the user’s DN, then bind as that DN with the supplied password.
The service account sees too much
- What fails
- Application bind accounts often have read access to the whole directory.
- Who is affected
- Every user whose attributes the account can read.
- What you would notice
- A web app bound as a domain admin or an account with broad read rights.
- What you change
- Use a dedicated account restricted to the OU and attributes the app needs.
What needs escaping
RFC 4515 defines the characters to escape in filter values: asterisk, opening and closing parentheses, backslash, and NUL, each written as a backslash and two hex digits. Distinguished names have their own escaping rules (RFC 4514), so use separate functions for each.
input: ana*)(objectClass=* escaped: ana\2a\29\28objectClass=\2a filter: (&(objectClass=user)(sAMAccountName=ana\2a\29\28objectClass=\2a)) -> matches nothing
Confirming in a lab
Against a lab directory, enter a single asterisk in a people search. If results for every user come back, the wildcard was not escaped. That is enough to report.
Directory-side signals
On Active Directory, enable diagnostic logging for expensive and inefficient LDAP searches (event 1644) to see unusual filters from application accounts.
Check yourself
Recognition and the fix. Nothing here is a procedure.
A people search returns every user when you enter a single asterisk. What does that show?
Sibling techniques
Pages at the same depth, plus a lab and a glossary card for this one.