Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ldap search with binary values #52

Merged
merged 1 commit into from
Sep 5, 2024
Merged

Conversation

CravateRouge
Copy link
Contributor

\\XX (XX being an hex value) in ldap query string value was re-decoded in the query string using chr(\xXX) by calling _read_EscapedCharacter -> _read_ASCII_VALUE -> _read_HEX_VALUE.

However chr() interprets \xXX as a unicode code point U+00XX but for example U+00a0 has a binary representation of \xC2\xA0. Hence, the current parser doesn't correctly handle the encoding of the \\XX bytes provided by the user and the binary search cannot be used because it sends a wrong binary representation to the server.

To comply with RFC 4515 specifying that the notation \\XX can be used to search for binary values, I disabled the re-encoding part detected by if chunk0 == '\\' l.1263 in msldap/protocol/ldap_filter/parser.py and instead called a special encoding function called RFC4515_encode() when encoding the string into the asn1 structure. The encoding function cannot be called before (e.g. instead of _read_HEX_VALUE) because not all bytes have a string representation in UTF8 and for example b'\xa0'.decode() will fail and as stated previously chr(\xa0) will not give the right decoding of our binary value.

With this PR, you'll be able to perform queries such as this one:

guid_str = "\\" + "\\".join([
                "{:02x}".format(b)
                for b in dtyp.guid.GUID().from_string('2628A46A-A6AD-4AE0-B854-2B12D9FE6F9E').to_bytes()
            ])
    filter = f"(schemaIDGUID={guid_str})"
connection.pagedsearch(query= f"(schemaIDGUID={guid_str})", attributes=attributes, tree=dn)

'2628A46A-A6AD-4AE0-B854-2B12D9FE6F9E' being the schemaIDGUID for CN=account,CN=Schema,CN=Configuration,DC=your,DC=domain

@skelsec
Copy link
Owner

skelsec commented Sep 5, 2024

Awesome! Thank you!

@skelsec skelsec merged commit d57c404 into skelsec:main Sep 5, 2024
@CravateRouge CravateRouge deleted the patch-2 branch January 2, 2025 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants