Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

LDAP query built from user-controlled sources

ID: cs/ldap-injectionKind: path-problemSecurity severity: 9.8Severity: errorPrecision: highTags:   - security   - external/cwe/cwe-090Query suites:   - csharp-code-scanning.qls   - csharp-security-extended.qls   - csharp-security-and-quality.qls

Click to see the query in the CodeQL repository

If an LDAP query is built using string concatenation, and the components of the concatenation include user input, a user is likely to be able to run malicious LDAP queries.

Recommendation

If user input must be included in an LDAP query, it should be escaped to avoid a malicious user providing special characters that change the meaning of the query. If possible, use an existing library, such as the AntiXSS library. One may also make their own encoder filter`LdapEncode` following RFC 4515 standards.

Example

In the following examples, the code accepts an “organization name” and a “username” from the user, which it uses to query LDAP to access a “type” property.

The first example concatenates the unvalidated and unencoded user input directly into both the DN (Distinguished Name) and the search filter used for the LDAP query. A malicious user could provide special characters to change the meaning of these queries, and search for a completely different set of values.

The second example uses the Microsoft AntiXSS library to encode the user values before they are included in the DN and search filters. This ensures the meaning of the query cannot be changed by a malicious user.

usingMicrosoft.Security.Application.EncoderusingSystem;usingSystem.DirectoryServices;usingSystem.Web;publicclassLDAPInjectionHandler:IHttpHandler{publicvoidProcessRequest(HttpContextctx){stringuserName=ctx.Request.QueryString["username"];stringorganizationName=ctx.Request.QueryString["organization_name"];// BAD: User input used in DN (Distinguished Name) without encodingstringldapQuery="LDAP://myserver/OU=People,O="+organizationName;using(DirectoryEntryroot=newDirectoryEntry(ldapQuery)){// BAD: User input used in search filter without encodingDirectorySearcherds=newDirectorySearcher(root,"username="+userName);SearchResultresult=ds.FindOne();if(result!=null){using(DirectoryEntryuser=result.getDirectoryEntry()){ctx.Response.Write(user.Properties["type"].Value)}}}// GOOD: Organization name is encoded before being used in DNstringsafeOrganizationName=Encoder.LdapDistinguishedNameEncode(organizationName);stringsafeLDAPQuery="LDAP://myserver/OU=People,O="+safeOrganizationName;using(DirectoryEntryroot=newDirectoryEntry(safeLDAPQuery)){// GOOD: User input is encoded before being used in search filterstringsafeUserName=Encoder.LdapFilterEncode(userName);DirectorySearcherds=newDirectorySearcher(root,"username="+safeUserName);SearchResultresult=ds.FindOne();if(result!=null){using(DirectoryEntryuser=result.getDirectoryEntry()){ctx.Response.Write(user.Properties["type"].Value)}}}}}

References


[8]ページ先頭

©2009-2025 Movatter.jp