# Improper Neutralization of Special Elements used in an LDAP Query ('LDAP Injection') (CWE-90) The product constructs all or part of an LDAP query using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the intended LDAP query. - Prevalence: 높음 자주 악용됨 - Impact: 높음 3개의 높은 심각도 규칙 - Prevention: 문서화됨 3개의 수정 예시 **OWASP:** Injection (A03:2021-Injection) - #3 ## Description If user input is incorporated into an LDAP query without proper sanitization, an attacker can inject LDAP commands that could read or modify sensitive directory information. ## Prevention 3개의 Shoulder 탐지 규칙을 기반으로 한 LDAP Injection 예방 전략. ### Go Use ldap.EscapeFilter to sanitize user input in LDAP queries ### JavaScript Escape LDAP special characters in user input before constructing LDAP queries ### Python Escape LDAP special characters using escape_filter_chars() before constructing filters ## Warning Signs - [HIGH] user input flowing to LDAP queries without proper escaping - [HIGH] LDAP queries constructed with unsanitized user input ## Consequences - 애플리케이션 데이터 읽기 - 애플리케이션 데이터 수정 - 보호 메커니즘 우회 ## Mitigations - 가능한 경우 파라미터화된 LDAP 쿼리를 사용하세요 - LDAP 쿼리에 사용되는 모든 사용자 입력을 이스케이프하거나 인코딩하세요 - 허용 가능한 문자의 허용 목록을 기준으로 입력을 검증하세요 ## Detection - Total rules: 3 - Languages: go, javascript, typescript, python ## Rules by Language ### Go (1 rules) - **LDAP Injection** [HIGH]: Detects user input flowing to LDAP queries without proper escaping. - Remediation: Use ldap.EscapeFilter to sanitize user input before including in LDAP filters. ```go escaped := ldap.EscapeFilter(username) filter := fmt.Sprintf("(uid=%s)", escaped) ``` Learn more: https://shoulder.dev/learn/go/cwe-90/ldap-injection ### Javascript (1 rules) - **LDAP Injection** [HIGH]: Detects user input flowing to LDAP queries without escaping special characters. - Remediation: Escape LDAP special characters before including user input in queries. ```javascript const safe = input.replace(/[\\*()]/g, c => '\\' + c.charCodeAt(0).toString(16)); ldap.search(`cn=${safe},dc=example,dc=com`, opts); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-90/ldap-injection ### Typescript (1 rules) - **LDAP Injection** [HIGH]: Detects user input flowing to LDAP queries without escaping special characters. - Remediation: Escape LDAP special characters before including user input in queries. ```javascript const safe = input.replace(/[\\*()]/g, c => '\\' + c.charCodeAt(0).toString(16)); ldap.search(`cn=${safe},dc=example,dc=com`, opts); ``` Learn more: https://shoulder.dev/learn/javascript/cwe-90/ldap-injection ### Python (1 rules) - **LDAP Injection** [HIGH]: Detects LDAP queries constructed with unsanitized user input. - Remediation: Escape LDAP special characters using escape_filter_chars(). ```python from ldap3.utils.conv import escape_filter_chars safe_username = escape_filter_chars(username) ``` Learn more: https://shoulder.dev/learn/python/cwe-90/ldap-injection