Ldap Injection in Aspnet
How Ldap Injection Manifests in Aspnet
LDAP injection in ASP.NET applications typically occurs when user input is concatenated directly into LDAP queries without proper sanitization. This vulnerability is particularly dangerous in ASP.NET because many applications use Active Directory for authentication and authorization, creating a direct attack surface.
The most common attack pattern involves an attacker manipulating LDAP filters to bypass authentication. For example, consider this vulnerable ASP.NET code:
protected void Login_Click(object sender, EventArgs e) {
string username = txtUsername.Text;
string password = txtPassword.Text;
string filter = "(&(objectClass=user)(samAccountName=" + username + ")(userPassword=" + password + "))";
DirectorySearcher searcher = new DirectorySearcher(filter);
SearchResult result = searcher.FindOne();
if (result != null) {
// Authentication successful
}
}An attacker could submit a username like admin)(userPassword=* which would create a filter that always returns true, effectively bypassing authentication. The resulting LDAP query would be:
(&(objectClass=user)(samAccountName=admin)(userPassword=*)(userPassword=*))Another common pattern is attribute manipulation where attackers modify LDAP queries to extract sensitive directory information. Consider this vulnerable code that searches for users:
public DataTable SearchUsers(string department) {
string filter = "(department=" + department + ")";
DirectorySearcher searcher = new DirectorySearcher(filter);
searcher.PropertiesToLoad.Add("cn");
searcher.PropertiesToLoad.Add("mail");
searcher.PropertiesToLoad.Add("telephoneNumber");
SearchResultCollection results = searcher.FindAll();
// Process results...
}An attacker could submit Sales*)(mail=* as the department parameter, causing the filter to become:
(department=Sales*)(mail=*)This would return all users in the Sales department along with their email addresses, potentially exposing more data than intended. The vulnerability becomes even more severe when combined with wildcard characters and logical operators that LDAP supports, allowing attackers to perform information disclosure, authentication bypass, and even modify directory entries if the application has write permissions.
Aspnet-Specific Detection
Detecting LDAP injection in ASP.NET applications requires both static code analysis and dynamic testing. From a code perspective, look for these patterns:
DirectorySearcher searcher = new DirectorySearcher(
"(&(objectClass=user)(samAccountName=" + username + "))"
);Any string concatenation with user input in LDAP filters is a red flag. Use tools like SonarQube or Roslyn analyzers to scan your codebase for these patterns.
For runtime detection, middleBrick's black-box scanning can identify LDAP injection vulnerabilities without requiring source code access. The scanner tests unauthenticated endpoints by sending payloads designed to trigger LDAP-specific syntax errors or unexpected behaviors. For ASP.NET applications, middleBrick specifically checks:
- Authentication endpoints that might construct LDAP filters from username/password inputs
- Directory search APIs that accept filter parameters
- Active Directory integration points
- LDAP-based authorization mechanisms
middleBrick's LDAP injection tests include payloads like:
*)(objectClass=*
admin)(userPassword=*
)(cn=*))(|(cn=*
userPassword=*))(|(userPassword=*These payloads test whether the application properly escapes special LDAP characters like (, ), *,
,
, and