Skip to content

Commit 128848e

Browse files
Add challenges part 3
1 parent a77bce7 commit 128848e

File tree

15 files changed

+128
-0
lines changed

15 files changed

+128
-0
lines changed

3/1/instructions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
You will need to set up CodeQL using one of the methods presented in [challenge 2](https://github.com/GitHubSecurityLab/codeql-zero-to-hero/blob/main/2/challenge-2/instructions.md) from CodeQL zero to hero part 2 to run the queries. Remember also to download and [select a CodeQL database](https://github.com/GitHubSecurityLab/codeql-zero-to-hero/blob/main/2/challenge-2/instructions.md#select-codeql-database) - it can be the GitHubSecurityLab/codeql-zero-to-hero database, but you may also choose another project.
2+
3+
Run the above query to find all method calls that are called ‘execute’ and come from the `django.db` library.

3/1/query.ql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import python
2+
import semmle.python.ApiGraphs
3+
4+
from API::CallNode node
5+
where node =
6+
API::moduleImport("django").getMember("db").getMember("connection").getMember("cursor").getReturn().getMember("execute").getACall()
7+
and
8+
node.getLocation().getFile().getRelativePath().regexpMatch("2/challenge-1/.*")
9+
10+
select node

3/2/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Now you know how to query for calls to functions from specific libraries. If os.system executes input coming from a user, it could lead to a command injection. Write a query to find calls to os.system and run it on the database you selected in the previous challenge.

3/2/query.ql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import python
2+
import semmle.python.ApiGraphs
3+
4+
from API::CallNode node
5+
where node = API::moduleImport("os").getMember("system").getACall()
6+
and node.getLocation().getFile().getRelativePath().regexpMatch("2/challenge-1/.*")
7+
select node

3/3/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Flask is a popular Python web framework. Frameworks very often introduce potential sources for untrusted data, [Flask request](https://flask.palletsprojects.com/en/3.0.x/api/#incoming-request-data) being one of them. Write a query to find Flask requests.

3/3/query.ql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import python
2+
import semmle.python.ApiGraphs
3+
4+
select API::moduleImport("flask").getMember("request").asSource()

3/4/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Run the query with `getAQlClass` predicate

3/4/query.ql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import python
2+
import semmle.python.ApiGraphs
3+
4+
from API::CallNode node
5+
where node = API::moduleImport("django").getMember("db").getMember("connection").getMember("cursor").getReturn().getMember("execute").getACall()
6+
select node, node.getAQlClass()

3/5/instructions.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Run the local data flow query to find execute calls that do not take a string literal

3/5/query.ql

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import python
2+
import semmle.python.ApiGraphs
3+
4+
class ExecuteCall extends DataFlow::CallCfgNode {
5+
ExecuteCall() {
6+
this = API::moduleImport("django").getMember("db").getMember("connection").getMember("cursor").getReturn().getMember("execute").getACall()
7+
}
8+
}
9+
10+
predicate executeNotLiteral(DataFlow::CallCfgNode call) {
11+
exists(DataFlow::ExprNode expr |
12+
call instanceof ExecuteCall
13+
and DataFlow::localFlow(expr, call.getArg(0))
14+
and expr instanceof DataFlow::LocalSourceNode
15+
and not expr.getNode().isLiteral()
16+
)
17+
}
18+
19+
from DataFlow::CallCfgNode call
20+
where executeNotLiteral(call)
21+
select call

0 commit comments

Comments
 (0)