This repository was archived by the owner on Apr 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Adding SQL Injection audit query #86
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d01bde5
Adding SQL Injection audit query
GeekMasher 0c40cb6
Merge remote-tracking branch 'origin/main' into py/audit-sql
GeekMasher 81846d3
Merge branch 'main' into py/audit-sql
GeekMasher 6e7b910
Merge branch 'main' into py/audit-sql
GeekMasher 273ed3a
Merge branch 'main' into py/audit-sql
aegilops 89d3bba
Merge branch 'main' into py/audit-sql
aegilops ec31372
Merge branch 'main' into py/audit-sql
GeekMasher eab129e
Merge branch 'main' into py/audit-sql
GeekMasher f796671
Merge branch 'main' into py/audit-sql
aegilops 7031a45
Merge branch 'main' into py/audit-sql
GeekMasher ac8c8b5
binary Add support
GeekMasher 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
There are no files selected for viewing
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,19 @@ | ||
| # Audit - SQL Injection using format strings | ||
|
|
||
| Dynamically generated SQL queries using format strings can cause SQL injection attacks. The following example shows how to use the `sql` package to execute a query with a format string: | ||
|
|
||
| ## Example | ||
|
|
||
| ```python | ||
| # Format string | ||
| query = f"SELECT * FROM users WHERE username = '{username}'" | ||
| cursor.execute(query) | ||
|
|
||
| # str.format() | ||
| query = "SELECT * FROM users WHERE username = '{}'".format(username) | ||
| cursor.execute(query) | ||
|
|
||
| # "%s" % string | ||
| query = "SELECT * FROM users WHERE username = %s" % username | ||
| cursor.execute(query) | ||
| ``` |
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,41 @@ | ||
| /** | ||
| * @name SQL query built from user-controlled sources | ||
| * @kind path-problem | ||
| * @problem.severity warning | ||
| * @security-severity 2.5 | ||
| * @sub-severity low | ||
| * @precision very-low | ||
| * @id py/audit/sql-injection | ||
| * @tags security | ||
| * external/cwe/cwe-089 | ||
| * audit | ||
| */ | ||
|
|
||
| import python | ||
| import semmle.python.dataflow.new.DataFlow | ||
| import semmle.python.dataflow.new.TaintTracking | ||
| import semmle.python.Concepts | ||
| import semmle.python.dataflow.new.BarrierGuards | ||
| import semmle.python.ApiGraphs | ||
| import DataFlow::PathGraph | ||
| private import semmle.python.security.dataflow.SqlInjectionCustomizations | ||
| // | ||
| import github.Utils | ||
|
|
||
| /** | ||
| * A taint-tracking configuration for detecting SQL injection vulnerabilities. | ||
| */ | ||
| class SqlInjectionHeuristic extends TaintTracking::Configuration { | ||
| SqlInjectionHeuristic() { this = "SqlInjectionHeuristic" } | ||
|
|
||
| override predicate isSource(DataFlow::Node source) { source instanceof DynamicStrings } | ||
|
|
||
| override predicate isSink(DataFlow::Node sink) { sink instanceof SqlInjection::Sink } | ||
|
|
||
| override predicate isSanitizer(DataFlow::Node node) { node instanceof SqlInjection::Sanitizer } | ||
| } | ||
|
|
||
| from SqlInjectionHeuristic config, DataFlow::PathNode source, DataFlow::PathNode sink | ||
| where config.hasFlowPath(source, sink) | ||
| select sink.getNode(), source, sink, "This SQL query depends on $@.", source.getNode(), | ||
| "a user-provided value" |
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,36 @@ | ||
| import python | ||
| private import semmle.python.ApiGraphs | ||
| private import semmle.python.Concepts | ||
| private import semmle.python.dataflow.new.DataFlow | ||
| private import semmle.python.dataflow.new.internal.TaintTrackingPrivate | ||
|
|
||
| // List of all the format strings | ||
| // - python/ql/lib/semmle/python/dataflow/new/internal/TaintTrackingPrivate.qll | ||
| class DynamicStrings extends DataFlow::Node { | ||
| DynamicStrings() { | ||
| ( | ||
| // s = f"WHERE name = '{input}'" | ||
| exists(Fstring fmtstr | this.asExpr() = fmtstr) | ||
| or | ||
| // "SELECT * FROM users WHERE username = '{}'".format(username) | ||
| exists(CallNode format, string methods, ControlFlowNode object | | ||
| object = format.getFunction().(AttrNode).getObject(methods) | ||
| | | ||
| methods = "format" and | ||
| this.asExpr() = format.getNode() | ||
| ) | ||
| or | ||
| exists(BinaryExpr expr | | ||
| ( | ||
| // q = "WHERE name = %s" % username | ||
| expr.getOp() instanceof Mod or | ||
| // q = "WHERE name = " + username | ||
| expr.getOp() instanceof Add | ||
| ) | ||
| and | ||
| expr.getLeft().getParent() = this.asExpr() | ||
| ) | ||
| ) and | ||
| this.getScope().inSource() | ||
| } | ||
| } |
20 changes: 20 additions & 0 deletions
20
tests/python-tests/CWE-089/audit/SqlInjectionAudit.expected
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,20 @@ | ||
| edges | ||
| | sqli.py:17:9:17:60 | ControlFlowNode for Fstring | sqli.py:18:16:18:20 | ControlFlowNode for query | | ||
| | sqli.py:21:9:21:68 | ControlFlowNode for Attribute() | sqli.py:22:16:22:20 | ControlFlowNode for query | | ||
| | sqli.py:25:9:25:60 | ControlFlowNode for BinaryExpr | sqli.py:26:16:26:20 | ControlFlowNode for query | | ||
| | sqli.py:30:9:30:58 | ControlFlowNode for BinaryExpr | sqli.py:31:16:31:20 | ControlFlowNode for query | | ||
| nodes | ||
| | sqli.py:17:9:17:60 | ControlFlowNode for Fstring | semmle.label | ControlFlowNode for Fstring | | ||
| | sqli.py:18:16:18:20 | ControlFlowNode for query | semmle.label | ControlFlowNode for query | | ||
| | sqli.py:21:9:21:68 | ControlFlowNode for Attribute() | semmle.label | ControlFlowNode for Attribute() | | ||
| | sqli.py:22:16:22:20 | ControlFlowNode for query | semmle.label | ControlFlowNode for query | | ||
| | sqli.py:25:9:25:60 | ControlFlowNode for BinaryExpr | semmle.label | ControlFlowNode for BinaryExpr | | ||
| | sqli.py:26:16:26:20 | ControlFlowNode for query | semmle.label | ControlFlowNode for query | | ||
| | sqli.py:30:9:30:58 | ControlFlowNode for BinaryExpr | semmle.label | ControlFlowNode for BinaryExpr | | ||
| | sqli.py:31:16:31:20 | ControlFlowNode for query | semmle.label | ControlFlowNode for query | | ||
| subpaths | ||
| #select | ||
| | sqli.py:18:16:18:20 | ControlFlowNode for query | sqli.py:17:9:17:60 | ControlFlowNode for Fstring | sqli.py:18:16:18:20 | ControlFlowNode for query | This SQL query depends on $@. | sqli.py:17:9:17:60 | ControlFlowNode for Fstring | a user-provided value | | ||
| | sqli.py:22:16:22:20 | ControlFlowNode for query | sqli.py:21:9:21:68 | ControlFlowNode for Attribute() | sqli.py:22:16:22:20 | ControlFlowNode for query | This SQL query depends on $@. | sqli.py:21:9:21:68 | ControlFlowNode for Attribute() | a user-provided value | | ||
| | sqli.py:26:16:26:20 | ControlFlowNode for query | sqli.py:25:9:25:60 | ControlFlowNode for BinaryExpr | sqli.py:26:16:26:20 | ControlFlowNode for query | This SQL query depends on $@. | sqli.py:25:9:25:60 | ControlFlowNode for BinaryExpr | a user-provided value | | ||
| | sqli.py:31:16:31:20 | ControlFlowNode for query | sqli.py:30:9:30:58 | ControlFlowNode for BinaryExpr | sqli.py:31:16:31:20 | ControlFlowNode for query | This SQL query depends on $@. | sqli.py:30:9:30:58 | ControlFlowNode for BinaryExpr | a user-provided value | |
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 @@ | ||
| CWE-089/SqlInjectionAudit.ql |
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 @@ | ||
| semmle-extractor-options: --max-import-depth=0 |
GeekMasher marked this conversation as resolved.
Show resolved
Hide resolved
|
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,31 @@ | ||
|
|
||
| import psycopg2 | ||
|
|
||
| # input | ||
| username = input("Username:") | ||
|
|
||
| connection = psycopg2.connect( | ||
| user="sysadmin", | ||
| password="pynative@#29", | ||
| host="127.0.0.1", | ||
| port="5432", | ||
| database="postgres_db" | ||
| ) | ||
| cursor = connection.cursor() | ||
|
|
||
| # test 1 - Format string | ||
| query = f"SELECT * FROM users WHERE username = '{username}'" | ||
| cursor.execute(query) | ||
|
|
||
| # test 2 - str.format() | ||
| query = "SELECT * FROM users WHERE username = '{}'".format(username) | ||
| cursor.execute(query) | ||
|
|
||
| # test 3 - %s | ||
| query = "SELECT * FROM users WHERE username = %s" % username | ||
| cursor.execute(query) | ||
|
|
||
|
|
||
| # test 4 - string + string | ||
| query = "SELECT * FROM users WHERE username = " + username | ||
| cursor.execute(query) |
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.