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.

Lesson 16 of 69 · 11 min read · practice only with authorization

Diagram

A directory search filter

Follow the path. One example at a time.

Step 1 of 4

Input

A login or people-search form sends a name.

Illustration: Input
# 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.

  1. 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.
  2. 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.

1 / 3

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.

  1. MapHow web applications work: requests, sessions, and where trust is decidedA browser request from DNS to database and back, the parts of an HTTP exchange a tester reads first, and the server-side decisions that most web vulnerabilities come down to.

Practice

  1. LabControl benchA failure is described. Choose the control that changes the outcome.