Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions impacket/ldap/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,10 +1110,13 @@ def _compileSimpleFilter(attribute, operator, value):
@classmethod
def _processLdapString(cls, ldapstr):
def replace_escaped_chars(match):
return chr(int(match.group(1), 16)) # group(1) == "XX" (valid hex)
return bytes([int(match.group(1), 16)]) # group(1) == "XX" (valid hex)

escaped_chars = re.compile(r'\\([0-9a-fA-F]{2})') # Capture any sequence of "\XX" (where XX is a valid hex)
return re.sub(escaped_chars, replace_escaped_chars, ldapstr)
# RFC 4515 escapes are octets, not Unicode code points. Encode unescaped
# text as UTF-8 first, then substitute each \XX with its raw byte so that
# binary values such as objectGUID and escaped UTF-8 sequences survive.
escaped_chars = re.compile(rb'\\([0-9a-fA-F]{2})') # Capture any sequence of "\XX" (where XX is a valid hex)
return re.sub(escaped_chars, replace_escaped_chars, ldapstr.encode('utf-8'))


class LDAPFilterSyntaxError(SyntaxError):
Expand Down
52 changes: 52 additions & 0 deletions tests/misc/test_ldap_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pytest

from impacket.ldap.ldap import LDAPConnection


def parse_value(filter_str: str) -> bytes:
connection = LDAPConnection.__new__(LDAPConnection)
search_filter = connection._parseFilter(filter_str)
return bytes(search_filter["equalityMatch"]["assertionValue"])


def test_binary_escapes():
"""RFC 4515 escapes are octets, not Unicode code points."""
assert parse_value(r"(objectGUID=\a1\00\ff)") == b"\xa1\x00\xff"


def test_escaped_utf8():
"""Escaped UTF-8 bytes must reach the wire unchanged."""
assert parse_value(r"(cn=Jos\c3\a9)") == b"Jos\xc3\xa9"


def test_unescaped_utf8():
"""Unescaped non-ASCII text is still encoded as UTF-8."""
assert parse_value("(cn=José)") == b"Jos\xc3\xa9"


@pytest.mark.parametrize(
"filter_str,expected",
[
(r"(cn=\28paren\29)", b"(paren)"),
(r"(cn=a\2ab)", b"a*b"),
(r"(cn=a\5cb)", b"a\\b"),
],
)
def test_reserved_characters(filter_str, expected):
assert parse_value(filter_str) == expected


def test_substring_escapes():
connection = LDAPConnection.__new__(LDAPConnection)
search_filter = connection._parseFilter(r"(cn=Jos\c3\a9*admin)")
substrings = search_filter["substrings"]["substrings"]
assert [
(component.getName(), bytes(component.getComponent()))
for component in substrings
] == [("initial", b"Jos\xc3\xa9"), ("final", b"admin")]


def test_extensible_match_escapes():
connection = LDAPConnection.__new__(LDAPConnection)
search_filter = connection._parseFilter(r"(cn:caseExactMatch:=Jos\c3\a9)")
assert bytes(search_filter["extensibleMatch"]["matchValue"]) == b"Jos\xc3\xa9"