LDAP Injection

User input is placed into an LDAP search filter or distinguished name without escaping, so it can change which directory entries a query matches.

SeverityHigh
CVSS 3.18.2 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:L/A:N Open in the calculator
CVSS 4.08.8 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator
CWECWE-90: Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection')
OWASP Top 10A05:2025 Injection
MITRE ATT&CKT1190 Exploit Public-Facing Application
CategoryInjection

Description

The application queries an LDAP directory, such as Active Directory or OpenLDAP, and builds the search filter or distinguished name by inserting values supplied by the user. Characters with special meaning in a filter, such as *, (, ), \ and the NUL character, or in a distinguished name, such as ,, +, = and ;, are not escaped.

Input can therefore add conditions, wildcards or extra clauses to the query, which lets an attacker read directory entries and attributes beyond those the query was meant to return, such as other users' details, group memberships and descriptive attributes that sometimes hold sensitive information. Where the application decides whether a login succeeded from the result of a search, the attacker may also be able to bypass authentication. The data exposed is limited by the permissions of the account the application uses to query the directory, which are often broader than necessary.

LDAP injection typically shows itself on a test when a * in a search or login field returns every entry or succeeds where it should not, when an unbalanced parenthesis causes a directory error, or when injected conditions that are always true or always false give consistently different responses. The last form allows attribute values to be inferred one character at a time even when results are not displayed. Common places are login forms that look the user up with a filter, staff directories, and address book or group membership searches.

Remediation

Treat every value placed in an LDAP query as data:

  • Escape user input with the encoding for the context it is used in, filter encoding for values in search filters and distinguished name encoding for values in DNs, using the directory library's own functions, for example ldap_escape() in PHP or escape_filter_chars() in the Python ldap3 library.
  • Where the library offers a filter builder that encodes values, use it rather than building filter strings by concatenation.
  • Validate input against the expected format before use, such as a username pattern, and reject wildcards where searching by pattern is not a feature.
  • Authenticate users by binding to the directory with their own credentials, rejecting empty passwords before the bind, rather than deciding the outcome from a search result.
  • Give the account the application uses for directory queries read-only access to only the entries and attributes it needs.

Confirm the fix by repeating the original tests with wildcards, parentheses and always-true conditions, and checking that they are treated as literal characters.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Web Applications Findings

Every Web Applications finding