|
| 1 | +/** |
| 2 | + * Provides classes modeling security-relevant aspects of the LDAP libraries. |
| 3 | + */ |
| 4 | + |
| 5 | +private import python |
| 6 | +private import semmle.python.dataflow.new.DataFlow |
| 7 | +private import semmle.python.dataflow.new.TaintTracking |
| 8 | +private import semmle.python.dataflow.new.RemoteFlowSources |
| 9 | +private import experimental.semmle.python.Concepts |
| 10 | +private import semmle.python.ApiGraphs |
| 11 | + |
| 12 | +/** |
| 13 | + * Provides models for Python's ldap-related libraries. |
| 14 | + */ |
| 15 | +private module LDAP { |
| 16 | + /** |
| 17 | + * Provides models for Python's `ldap` library. |
| 18 | + * |
| 19 | + * See https://www.python-ldap.org/en/python-ldap-3.3.0/index.html |
| 20 | + */ |
| 21 | + private module LDAP2 { |
| 22 | + /** |
| 23 | + * List of `ldap` methods used to execute a query. |
| 24 | + * |
| 25 | + * See https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap.html#functions |
| 26 | + */ |
| 27 | + private class LDAP2QueryMethods extends string { |
| 28 | + LDAP2QueryMethods() { |
| 29 | + this in ["search", "search_s", "search_st", "search_ext", "search_ext_s"] |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * A class to find `ldap` methods executing a query. |
| 35 | + * |
| 36 | + * See `LDAP2QueryMethods` |
| 37 | + */ |
| 38 | + private class LDAP2Query extends DataFlow::CallCfgNode, LDAPQuery::Range { |
| 39 | + DataFlow::Node ldapNode; |
| 40 | + |
| 41 | + LDAP2Query() { |
| 42 | + exists(DataFlow::AttrRead searchMethod | |
| 43 | + this.getFunction() = searchMethod and |
| 44 | + API::moduleImport("ldap").getMember("initialize").getACall() = |
| 45 | + searchMethod.getObject().getALocalSource() and |
| 46 | + searchMethod.getAttributeName() instanceof LDAP2QueryMethods and |
| 47 | + ( |
| 48 | + ldapNode = this.getArg(0) |
| 49 | + or |
| 50 | + ( |
| 51 | + ldapNode = this.getArg(2) or |
| 52 | + ldapNode = this.getArgByName("filterstr") |
| 53 | + ) |
| 54 | + ) |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + override DataFlow::Node getLDAPNode() { result = ldapNode } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * A class to find calls to `ldap.dn.escape_dn_chars`. |
| 63 | + * |
| 64 | + * See https://github.com/python-ldap/python-ldap/blob/7ce471e238cdd9a4dd8d17baccd1c9e05e6f894a/Lib/ldap/dn.py#L17 |
| 65 | + */ |
| 66 | + private class LDAP2EscapeDNCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 67 | + LDAP2EscapeDNCall() { |
| 68 | + this = API::moduleImport("ldap").getMember("dn").getMember("escape_dn_chars").getACall() |
| 69 | + } |
| 70 | + |
| 71 | + override DataFlow::Node getEscapeNode() { result = this.getArg(0) } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * A class to find calls to `ldap.filter.escape_filter_chars`. |
| 76 | + * |
| 77 | + * See https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap-filter.html#ldap.filter.escape_filter_chars |
| 78 | + */ |
| 79 | + private class LDAP2EscapeFilterCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 80 | + LDAP2EscapeFilterCall() { |
| 81 | + this = |
| 82 | + API::moduleImport("ldap").getMember("filter").getMember("escape_filter_chars").getACall() |
| 83 | + } |
| 84 | + |
| 85 | + override DataFlow::Node getEscapeNode() { result = this.getArg(0) } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Provides models for Python's `ldap3` library. |
| 91 | + * |
| 92 | + * See https://pypi.org/project/ldap3/ |
| 93 | + */ |
| 94 | + private module LDAP3 { |
| 95 | + /** |
| 96 | + * A class to find `ldap3` methods executing a query. |
| 97 | + */ |
| 98 | + private class LDAP3Query extends DataFlow::CallCfgNode, LDAPQuery::Range { |
| 99 | + DataFlow::Node ldapNode; |
| 100 | + |
| 101 | + LDAP3Query() { |
| 102 | + exists(DataFlow::AttrRead searchMethod | |
| 103 | + this.getFunction() = searchMethod and |
| 104 | + API::moduleImport("ldap3").getMember("Connection").getACall() = |
| 105 | + searchMethod.getObject().getALocalSource() and |
| 106 | + searchMethod.getAttributeName() = "search" and |
| 107 | + ( |
| 108 | + ldapNode = this.getArg(0) or |
| 109 | + ldapNode = this.getArg(1) |
| 110 | + ) |
| 111 | + ) |
| 112 | + } |
| 113 | + |
| 114 | + override DataFlow::Node getLDAPNode() { result = ldapNode } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * A class to find calls to `ldap3.utils.dn.escape_rdn`. |
| 119 | + * |
| 120 | + * See https://github.com/cannatag/ldap3/blob/4d33166f0869b929f59c6e6825a1b9505eb99967/ldap3/utils/dn.py#L390 |
| 121 | + */ |
| 122 | + private class LDAP3EscapeDNCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 123 | + LDAP3EscapeDNCall() { |
| 124 | + this = |
| 125 | + API::moduleImport("ldap3") |
| 126 | + .getMember("utils") |
| 127 | + .getMember("dn") |
| 128 | + .getMember("escape_rdn") |
| 129 | + .getACall() |
| 130 | + } |
| 131 | + |
| 132 | + override DataFlow::Node getEscapeNode() { result = this.getArg(0) } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * A class to find calls to `ldap3.utils.conv.escape_filter_chars`. |
| 137 | + * |
| 138 | + * See https://github.com/cannatag/ldap3/blob/4d33166f0869b929f59c6e6825a1b9505eb99967/ldap3/utils/conv.py#L91 |
| 139 | + */ |
| 140 | + private class LDAP3EscapeFilterCall extends DataFlow::CallCfgNode, LDAPEscape::Range { |
| 141 | + LDAP3EscapeFilterCall() { |
| 142 | + this = |
| 143 | + API::moduleImport("ldap3") |
| 144 | + .getMember("utils") |
| 145 | + .getMember("conv") |
| 146 | + .getMember("escape_filter_chars") |
| 147 | + .getACall() |
| 148 | + } |
| 149 | + |
| 150 | + override DataFlow::Node getEscapeNode() { result = this.getArg(0) } |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments