LDAP injection: when input becomes part of a directory filter
LDAP injection happens when an application builds an LDAP search filter or distinguished name from user input without neutralising the characters that have meaning in that syntax. The directory then runs a query the developer did not write. It is most often found in sign-in forms and in the search box of an employee directory, and it is prevented by escaping values for the exact place they land, not by a blocklist.
Quick facts
- Weakness
- CWE-90, improper neutralization of special elements used in an LDAP query
- OWASP category
- A05:2025 Injection
- Test guide
- WSTG-INJT-06, LDAP Injection
- Filter syntax
- RFC 4515, the string form of search filters
- DN syntax
- RFC 4514, the string form of distinguished names
- Main fix
- Escape every untrusted value with the encoding for its context, filter or DN, and run the application's bind account with the least privilege that works
Prevention guidance follows the OWASP LDAP Injection Prevention Cheat Sheet. The impact list comes from the WSTG page above.
How an LDAP filter is built
LDAP is a protocol for querying and changing a directory, a tree of entries such as users, groups and devices. A search carries a base, a scope and a filter. RFC 4515 defines how a filter is written as a string: every condition sits in parentheses, and conditions combine in prefix notation. & means and, | means or, ! means not. A condition compares an attribute with a value using =, ~=, >= or <=, and * inside a value is a wildcard.
So a lookup of a person by login name might be written with the user's input in one spot inside a pair of parentheses. Everything the application intends sits around that spot as fixed text. The trouble starts because parentheses, the asterisk and the ampersand are not data to the parser. A value that contains them is read as structure: an extra condition, a condition that matches everything, or a different boolean shape. The same idea applies to XML queries, covered under XPath injection.
A filter is sent over the wire as a structured value (the search request in RFC 4511), and the protocol has no counterpart to the bind variables of SQL. The string form is parsed by the client library, which means the injection happens inside your application, before a request reaches the directory. That is why the directory server cannot defend you.
The same family as SQL injection
The mechanism is the one behind SQL injection: data and instructions travel in one string. The difference is the toolbox. SQL has parameterised queries in mainstream drivers. LDAP libraries more often give you an escaping function and a template, so the discipline sits with the developer. Document stores have their own variant, covered in NoSQL injection, and the shell has another in command injection.
Where input reaches the filter
LDAP shows up wherever an organisation keeps identities in a directory such as Active Directory or OpenLDAP. The places input is spliced into a query are few, which helps when you audit.
- Sign-in. The application searches for the entry that matches the typed login name, then tries a bind with the entry's DN and the typed password. The login name sits in the filter.
- Directory search. An address book, an "find a colleague" box or a group picker puts the search term into a name or mail filter. Wildcards are sometimes a feature here, which complicates blocking.
- Membership and role checks. A page asks whether a user belongs to a group by building a filter from the user name or a request parameter.
- Account recovery. A reset form looks up an entry by mail address or employee number.
- DN construction. Some code builds the bind DN directly from the login name instead of searching for it. That is a separate context with separate rules, covered below.
Input can also arrive second-hand. A display name stored by one page and used in a filter by another carries the same risk, because the neutralisation is owed at the point of use.
What goes wrong
The WSTG page for LDAP injection lists four outcomes: access to unauthorised content, evasion of application restrictions including authentication checks, gathering of unauthorised information, and the adding or modifying of objects in the directory tree. Which of them is possible depends on what the application does with the result and on what the bind account may read or write.
In a sign-in flow the typical failure is a lookup that returns an entry it should not, or returns any entry at all. In a search box it is a result set broader than the user is meant to see, with attributes such as mail addresses or group names that were never meant for the page. If the application also performs modifications through the same account, injected structure in a modify or add operation is a larger problem than a read.
A second, quieter failure belongs to the same sign-in code. RFC 4513, section 5.1.2, describes the unauthenticated bind: a non-empty name with a zero-length password. Clients should refuse an empty password in a login form, and servers should by default fail such a request. An application that treats any bind that does not raise an error as success can let someone in without a password. The OWASP cheat sheet makes the same point and says to reject empty passwords. It is not injection, but it is checked in the same review.
The attack also pairs with credential abuse. A directory-backed login is a target for credential stuffing and brute force attacks, and those are controlled by limits on attempts rather than by escaping.
Escaping by context
There are two escaping rules, and using one in the other's place is a common error. The filter and the DN are different grammars.
| Context | Characters | Rule |
|---|---|---|
| Search filter value | NUL, (, ), *, \ | RFC 4515, section 3: backslash plus two hex digits, giving \00, \28, \29, \2a, \5c |
| DN attribute value | " + , ; < > \ and NUL anywhere; a space or # at the start; a space at the end | RFC 4514, section 2.4: a backslash before the character, or backslash plus hex |
As an illustration, a search term typed as j*(x) must reach the filter as j\2a\28x\29. The directory then compares against a literal value and nothing in it is read as a wildcard or a boundary. The OWASP cheat sheet lists a slightly longer set of characters for DNs than RFC 4514 requires, and escaping more than the minimum does no harm.
Use the library, and escape late
Write no escaping routine of your own. Use the function that ships with your LDAP library or framework; the OWASP cheat sheet points to encoder functions for both filters and DNs in ESAPI and .NET and recommends a framework that encodes automatically. Escape at the point where the value joins the filter, not when it first arrives, so every path to that line is covered.
Allow-list validation is a second layer. If a login name is meant to be letters, digits and a few separators, reject the rest before the directory sees it. Treat it as an addition to escaping and not a substitute: real names include apostrophes, hyphens and non-Latin letters that a strict pattern would reject.
Prevention beyond escaping
- Give the bind account least privilege. The account the application uses to search should read only the subtree and attributes the feature needs, and should not be able to write. The cheat sheet lists this as a way to limit the damage when injection does occur.
- Use separate accounts for separate jobs. A sign-in lookup and a directory-search page rarely need the same rights.
- Reject empty passwords before binding. Check for a zero-length password in your own code, as RFC 4513 advises clients to do.
- Return the same message for every failed sign-in. Do not tell the caller whether the name or the password was wrong, and do not print directory errors in the response.
- Do not rely on authentication to stop it. An authenticated bind does not prevent filter injection, so keep the escaping in place for every query.
- Test the code path. Follow the WSTG chapter against a staging copy of the directory, with test accounts.
Where a WAF helps and where it does not
A request filter sees the HTTP request: the query string, the form fields, a JSON body, the headers. Filter metacharacters sent in a login field or a search parameter have a recognisable shape in that traffic, and an injection rule can flag them before they reach the application. The OWASP Top 10 page on injection describes this general case.
It has limits that matter more here than for SQL. A WAF sits between the client and your application. It does not see the LDAP query the application sends to the directory, so it cannot tell whether the escaping was done. Parentheses and asterisks are also ordinary characters in some legitimate input, and a directory search box may accept wildcards on purpose, so a rule that is too eager will flag real users. Values stored earlier and used later never pass the filter at the moment of use, and encodings the filter does not decode can carry the same characters through. The empty-password bind problem involves no unusual characters at all.
The three modes fit this uncertainty. In off nothing is inspected. In monitor a match is recorded and the request is still served, so the activity log shows which sign-in and search parameters carry filter characters and from which clients. Read it for a while, add exceptions for the search feature that legitimately takes wildcards, and sort out false positives. Then switch to block, where matching requests are refused and still recorded. The route is described in monitor mode.
Treat that as a net under the code and not a replacement for it. The fix for the flaw itself, escaping and a restricted bind account, lives in the application, and the WAF is how you find out in the meantime who is probing it.
Questions
What is LDAP injection?
LDAP injection is an attack in which user input changes the meaning of an LDAP search filter or distinguished name because the application did not escape characters such as parentheses and the asterisk. The directory then runs a query the developer did not intend. It is tracked as CWE-90.
How do you prevent LDAP injection?
Escape every untrusted value with the encoding for its context: RFC 4515 rules for search filter values and RFC 4514 rules for distinguished names. Add allow-list validation where the format is known, and run the application's bind account with the least privilege the feature needs.
Which characters must be escaped in an LDAP filter?
RFC 4515 requires escaping NUL, the opening and closing parentheses, the asterisk and the backslash in filter values. Each is written as a backslash followed by two hexadecimal digits, for example \2a for the asterisk.
Is LDAP injection the same as SQL injection?
It is the same kind of flaw aimed at a different interpreter. Both mix data and instructions in one string. SQL drivers offer parameterised queries, while LDAP defences rest mainly on escaping values for the filter or the distinguished name. See SQL injection for the comparison.
Can a WAF stop LDAP injection?
A WAF can flag filter metacharacters in request parameters, and monitor mode shows what would be blocked before block is enabled. It cannot see the query the application sends to the directory, so the fix remains escaping and a restricted bind account in the code.