-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Python: Add LDAP Injection query #5443
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
Merged
RasmusWL
merged 35 commits into
github:main
from
jorgectf:jorgectf/python/ldapInjection
May 26, 2021
+703
−0
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
799d509
Upload LDAP Injection query, qhelp and tests
jorgectf 719b48c
Move to experimental folder
jorgectf 95a1dae
Precision warn and Remove CWE reference
jorgectf 85ec82a
Refactor in progress
jorgectf ad36bea
Refactor LDAP3 stuff (untested)
jorgectf 8223539
Add a test without attributes
jorgectf 3cda2e5
Polish up ldap3 tests
jorgectf 8faafb6
Update Sink
jorgectf 4328ff3
Remove attrs feature
jorgectf 9b43031
Improve Sanitizer calls
jorgectf 1bcb9cd
Simplify query
jorgectf 33423ea
Optimize calls
jorgectf a1850dd
Change LDAP config (qll) filename
jorgectf 8661cb0
Polish LDAP3Query
jorgectf 7296879
Polish tests
jorgectf 3c1ca72
Improve qhelp
jorgectf 1554f4f
Create qhelp examples
jorgectf 95bfdc4
Move tests to /test
jorgectf 4f85de8
Add qlref
jorgectf 7819d1a
Generate .expected
jorgectf b405c67
Add qhelp last newline
jorgectf 82f47f8
Polish metadata
jorgectf cd75433
Fix qhelp examples extension
jorgectf a2e8d88
Write documentation
jorgectf b020ea6
Polish documentation
jorgectf 1c34230
Fix documentation typo
jorgectf c2b96b3
Add documentation to main classes' functions.
jorgectf 34b8af3
Move structure to LDAP.qll
jorgectf 6159fbe
Update functions naming
jorgectf 2ad72ad
Add LDAP framework entry in Frameworks.qll
jorgectf 8665747
Update sink and sanitizer to match new naming
jorgectf 9e9678b
Apply documentation suggestions
jorgectf 37d6ff7
Update tests and .expected
jorgectf d5f2846
Merge branch 'main' into jorgectf/python/ldapInjection
RasmusWL f807c2f
Python: autoformat
RasmusWL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move structure to LDAP.qll
- Loading branch information
commit 34b8af30acd8d28b5f5980169ed0d0d4ec826455
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
python/ql/src/experimental/semmle/python/frameworks/LDAP.qll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of the LDAP libraries. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.dataflow.new.TaintTracking | ||
private import semmle.python.dataflow.new.RemoteFlowSources | ||
private import experimental.semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for Python's ldap-related libraries. | ||
*/ | ||
private module LDAP { | ||
/** | ||
* Provides models for Python's `ldap` library. | ||
* | ||
* See https://www.python-ldap.org/en/python-ldap-3.3.0/index.html | ||
*/ | ||
private module LDAP2 { | ||
/** | ||
* List of `ldap` methods used to execute a query. | ||
* | ||
* See https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap.html#functions | ||
*/ | ||
private class LDAP2QueryMethods extends string { | ||
LDAP2QueryMethods() { | ||
this in ["search", "search_s", "search_st", "search_ext", "search_ext_s"] | ||
} | ||
} | ||
|
||
/** | ||
* A class to find `ldap` methods executing a query. | ||
* | ||
* See `LDAP2QueryMethods` | ||
*/ | ||
private class LDAP2Query extends DataFlow::CallCfgNode, LDAPQuery::Range { | ||
DataFlow::Node ldapNode; | ||
|
||
LDAP2Query() { | ||
exists(DataFlow::AttrRead searchMethod | | ||
this.getFunction() = searchMethod and | ||
API::moduleImport("ldap").getMember("initialize").getACall() = | ||
searchMethod.getObject().getALocalSource() and | ||
searchMethod.getAttributeName() instanceof LDAP2QueryMethods and | ||
( | ||
ldapNode = this.getArg(0) | ||
or | ||
( | ||
ldapNode = this.getArg(2) or | ||
ldapNode = this.getArgByName("filterstr") | ||
) | ||
) | ||
) | ||
} | ||
|
||
override DataFlow::Node getLDAPNode() { result = ldapNode } | ||
} | ||
|
||
/** | ||
* A class to find calls to `ldap.dn.escape_dn_chars`. | ||
* | ||
* See https://github.com/python-ldap/python-ldap/blob/7ce471e238cdd9a4dd8d17baccd1c9e05e6f894a/Lib/ldap/dn.py#L17 | ||
*/ | ||
private class LDAP2EscapeDNCall extends DataFlow::CallCfgNode, LDAPEscape::Range { | ||
LDAP2EscapeDNCall() { | ||
this = API::moduleImport("ldap").getMember("dn").getMember("escape_dn_chars").getACall() | ||
} | ||
|
||
override DataFlow::Node getEscapeNode() { result = this.getArg(0) } | ||
} | ||
|
||
/** | ||
* A class to find calls to `ldap.filter.escape_filter_chars`. | ||
* | ||
* See https://www.python-ldap.org/en/python-ldap-3.3.0/reference/ldap-filter.html#ldap.filter.escape_filter_chars | ||
*/ | ||
private class LDAP2EscapeFilterCall extends DataFlow::CallCfgNode, LDAPEscape::Range { | ||
LDAP2EscapeFilterCall() { | ||
this = | ||
API::moduleImport("ldap").getMember("filter").getMember("escape_filter_chars").getACall() | ||
} | ||
|
||
override DataFlow::Node getEscapeNode() { result = this.getArg(0) } | ||
} | ||
} | ||
|
||
/** | ||
* Provides models for Python's `ldap3` library. | ||
jorgectf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* See https://pypi.org/project/ldap3/ | ||
*/ | ||
private module LDAP3 { | ||
/** | ||
* A class to find `ldap3` methods executing a query. | ||
*/ | ||
private class LDAP3Query extends DataFlow::CallCfgNode, LDAPQuery::Range { | ||
DataFlow::Node ldapNode; | ||
|
||
LDAP3Query() { | ||
exists(DataFlow::AttrRead searchMethod | | ||
this.getFunction() = searchMethod and | ||
API::moduleImport("ldap3").getMember("Connection").getACall() = | ||
searchMethod.getObject().getALocalSource() and | ||
searchMethod.getAttributeName() = "search" and | ||
( | ||
ldapNode = this.getArg(0) or | ||
ldapNode = this.getArg(1) | ||
) | ||
) | ||
} | ||
|
||
override DataFlow::Node getLDAPNode() { result = ldapNode } | ||
} | ||
|
||
/** | ||
* A class to find calls to `ldap3.utils.dn.escape_rdn`. | ||
* | ||
* See https://github.com/cannatag/ldap3/blob/4d33166f0869b929f59c6e6825a1b9505eb99967/ldap3/utils/dn.py#L390 | ||
*/ | ||
private class LDAP3EscapeDNCall extends DataFlow::CallCfgNode, LDAPEscape::Range { | ||
LDAP3EscapeDNCall() { | ||
this = | ||
API::moduleImport("ldap3") | ||
.getMember("utils") | ||
.getMember("dn") | ||
.getMember("escape_rdn") | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getEscapeNode() { result = this.getArg(0) } | ||
} | ||
|
||
/** | ||
* A class to find calls to `ldap3.utils.conv.escape_filter_chars`. | ||
* | ||
* See https://github.com/cannatag/ldap3/blob/4d33166f0869b929f59c6e6825a1b9505eb99967/ldap3/utils/conv.py#L91 | ||
*/ | ||
private class LDAP3EscapeFilterCall extends DataFlow::CallCfgNode, LDAPEscape::Range { | ||
LDAP3EscapeFilterCall() { | ||
this = | ||
API::moduleImport("ldap3") | ||
.getMember("utils") | ||
.getMember("conv") | ||
.getMember("escape_filter_chars") | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getEscapeNode() { result = this.getArg(0) } | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.