def escape_filter_exp(filter_exp: str):
    """
    Escapes the special characters in an LDAP filter based on RFC 4515.

    :param str filter_exp: the unescaped filter expression.
    :return: the escaped filter expression.
    :rtype: str
    """
    chars_to_escape = (
        ("\\", "\\5C"),
        ("*", "\\2A"),
        ("(", "\\28"),
        (")", "\\29"),
        ("\0", "\\0"),
    )
    for char, repl in chars_to_escape:
        filter_exp = filter_exp.replace(char, repl)
    return filter_exp