|
| 1 | +from flask import request, Flask |
| 2 | +import ldap |
| 3 | +import ldap.filter |
| 4 | +import ldap.dn |
| 5 | + |
| 6 | +app = Flask(__name__) |
| 7 | + |
| 8 | + |
| 9 | +@app.route("/normal") |
| 10 | +def normal(): |
| 11 | + """ |
| 12 | + A RemoteFlowSource is sanitized and used as DN and search filter |
| 13 | + """ |
| 14 | + |
| 15 | + unsafe_dc = request.args['dc'] |
| 16 | + unsafe_filter = request.args['username'] |
| 17 | + |
| 18 | + safe_dc = ldap.dn.escape_dn_chars(unsafe_dc) |
| 19 | + safe_filter = ldap.filter.escape_filter_chars(unsafe_filter) |
| 20 | + |
| 21 | + dn = "dc={}".format(safe_dc) |
| 22 | + search_filter = "(user={})".format(safe_filter) |
| 23 | + |
| 24 | + ldap_connection = ldap.initialize("ldap://127.0.0.1") |
| 25 | + user = ldap_connection.search_s( |
| 26 | + dn, ldap.SCOPE_SUBTREE, search_filter) |
| 27 | + |
| 28 | + |
| 29 | +@app.route("/direct") |
| 30 | +def direct(): |
| 31 | + """ |
| 32 | + A RemoteFlowSource is sanitized and used as DN and search filter using a oneline call to .search_s |
| 33 | + """ |
| 34 | + |
| 35 | + unsafe_dc = request.args['dc'] |
| 36 | + unsafe_filter = request.args['username'] |
| 37 | + |
| 38 | + safe_dc = ldap.dn.escape_dn_chars(unsafe_dc) |
| 39 | + safe_filter = ldap.filter.escape_filter_chars(unsafe_filter) |
| 40 | + |
| 41 | + dn = "dc={}".format(safe_dc) |
| 42 | + search_filter = "(user={})".format(safe_filter) |
| 43 | + |
| 44 | + user = ldap.initialize("ldap://127.0.0.1").search_s( |
| 45 | + dn, ldap.SCOPE_SUBTREE, search_filter, ["testAttr1", "testAttr2"]) |
| 46 | + |
| 47 | + |
| 48 | +@app.route("/normal_argbyname") |
| 49 | +def normal_argbyname(): |
| 50 | + """ |
| 51 | + A RemoteFlowSource is sanitized and used as DN and search filter, while the search filter is specified as |
| 52 | + an argument by name |
| 53 | + """ |
| 54 | + |
| 55 | + unsafe_dc = request.args['dc'] |
| 56 | + unsafe_filter = request.args['username'] |
| 57 | + |
| 58 | + safe_dc = ldap.dn.escape_dn_chars(unsafe_dc) |
| 59 | + safe_filter = ldap.filter.escape_filter_chars(unsafe_filter) |
| 60 | + |
| 61 | + dn = "dc={}".format(safe_dc) |
| 62 | + search_filter = "(user={})".format(safe_filter) |
| 63 | + |
| 64 | + ldap_connection = ldap.initialize("ldap://127.0.0.1") |
| 65 | + user = ldap_connection.search_s( |
| 66 | + dn, ldap.SCOPE_SUBTREE, filterstr=search_filter) |
| 67 | + |
| 68 | + |
| 69 | +# if __name__ == "__main__": |
| 70 | +# app.run(debug=True) |
0 commit comments