C# LDAP Injection in Searcher
Description
Detects LDAP injection vulnerabilities in C# code where untrusted input is used directly in LDAP directory searcher queries. This could allow attackers to manipulate LDAP queries and potentially access or modify unauthorized directory data.
Detection Strategy
• Identifies calls to the DirectorySearcher methods 'FindOne' and 'FindAll' in files that import 'System.DirectoryServices'
• Resolves the searcher to a 'new DirectorySearcher(...)' creation and takes its LDAP filter from the constructor arguments or from a 'Filter' assignment made between the creation and the search
• Splits concatenated filters into their parts and follows each variable through its definitions until it reaches a request or stream read such as 'Params', 'QueryString', 'Cookies', 'ReadLine' or 'GetString'
• Confirms the read comes from an 'HttpRequest' parameter or a connection object ('TcpClient', 'TcpListener', 'SqlCommand', 'WebClient')
• Reports the search call when that untrusted data reaches the LDAP filter
Vulnerable code example
using System.DirectoryServices;
public class UserSearch {
public void SearchUser(string userName) {
// Vulnerable: Direct concatenation of user input in LDAP filter
string filter = "(uid=" + userName + ")";
DirectorySearcher searcher = new DirectorySearcher(filter);
searcher.FindAll();...✅ Secure code example
using System.DirectoryServices;
using System.Text.RegularExpressions;
public class UserSearch {
public void SearchUser(string userName) {
// Validate input with regex to allow only safe characters
if (!Regex.IsMatch(userName, @"^[a-zA-Z0-9_.-]{1,64}$")) {
return;...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' pentesting team, fill out this contact form.