Course contentsShow
← Software Engineering
Lesson 1058 of 1,93332. Security EngineeringFree lesson

LDAP and XML Injection

How injection attacks apply to LDAP queries and XML parsers, and techniques for safe data handling.

LDAP and XML Injection

You'll learn how injection attacks target LDAP directories and XML parsers, and how to defend against them using safe data handling practices.

The Problem

Just like SQL injection, LDAP injection and XML injection exploit user input that gets directly embedded into queries or documents without proper validation. These attacks manipulate the structure of LDAP filters or XML data to access unauthorized information or execute unintended operations.

LDAP Injection

LDAP (Lightweight Directory Access Protocol) is used for querying directory services like Active Directory. A typical LDAP query filter might look like:

(&(username={userInput})(objectClass=person))

If an attacker enters *)(objectClass=*) as their username, the filter becomes:

(&(username=*)(objectClass=*))(objectClass=person))

This returns all users instead of a specific one—similar to SQL's OR 1=1 trick.

Defense: Use LDAP escaping functions to neutralize special characters like *, (, ), and \ before inserting user input into filters.

XML Injection

XML parsers can be exploited through malicious input that breaks document structure or triggers dangerous features like XML External Entity (XXE) attacks. An attacker might inject:

<!DOCTYPE foo [<!ENTITY xxe SYSTEM "file:///etc/passwd">]>

This could expose sensitive files on the server.

Defense: Disable external entity processing in your XML parser configuration, validate and escape user input before embedding it in XML documents, and use schema validation to enforce expected structure.

Safe Data Handling Pattern

The principle is identical across all injection types: treat all user input as untrusted data, never as code or query syntax. Use context-appropriate escaping or parameterization for the target system.

Key Takeaway: LDAP and XML injection follow the same pattern as SQL injection—attackers manipulate query/document structure through unescaped input. Always escape special characters specific to LDAP or disable dangerous XML features like external entities.