Skip to content

feat: Create Debugging Pack and add Queries #66

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions python/debugging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Python - Debugging

This Pack is a collection of Python debugging tips and tricks.

## Basics

- [`Sources.ql`](./basics/Sources.ql)
- List of the sources in the Application
- [`Sinks.ql`](./basics/Sinks.ql)
- List of the sinks in the Application

## Partials

- [`PartialPathsFromSource.ql`](./partials/PartialPathsFromSource.ql)
- List of partial paths from a sources to nodes in the Application
- [`PartialPathsFromSink.ql`](./partials/PartialPathsFromSink.ql)
- List of partial paths from a sink to nodes in the Application
21 changes: 21 additions & 0 deletions python/debugging/basics/Sinks.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @name Sinks
* @kind problem
* @problem.severity warning
* @security-severity 1.0
* @sub-severity low
* @precision low
* @id py/debugging/sinks
* @tags debugging
*/

import python
import semmle.python.dataflow.new.DataFlow
// Helpers
private import ghsl.Helpers

from DataFlow::Node sinks
where
dangerousSinks(sinks) and
sinks.getScope().inSource()
select sinks, "sink"
29 changes: 29 additions & 0 deletions python/debugging/basics/Sources.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @name Sources
* @kind problem
* @problem.severity warning
* @security-severity 1.0
* @sub-severity low
* @precision low
* @id py/debugging/sources
* @tags debugging
*/

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.RemoteFlowSources
// Helpers
private import ghsl.Helpers
private import ghsl.LocalSources

class Sources extends DataFlow::Node {
Sources() {
this instanceof RemoteFlowSource
or
this instanceof LocalSources::Range
}
}

from Sources sources
where sources.getScope().inSource()
select sources, "source"
24 changes: 24 additions & 0 deletions python/debugging/codeql-pack.lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
lockVersion: 1.0.0
dependencies:
codeql/dataflow:
version: 1.0.1
codeql/mad:
version: 1.0.1
codeql/python-all:
version: 1.0.1
codeql/regex:
version: 1.0.1
codeql/ssa:
version: 1.0.1
codeql/tutorial:
version: 1.0.1
codeql/typetracking:
version: 1.0.1
codeql/util:
version: 1.0.1
codeql/xml:
version: 1.0.1
codeql/yaml:
version: 1.0.1
compiled: false
16 changes: 16 additions & 0 deletions python/debugging/diagnostics/SinksDatabases.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @name Database Sinks Diagnostic
* @id ghsl/diagnostics/database-sinks
* @description List all database sinks
* @kind diagnostic
*/

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.security.dataflow.SqlInjectionCustomizations

from SqlInjection::Sink s, Expr n
where
s.getScope().inSource() and
n = s.asExpr()
select n, ""
17 changes: 17 additions & 0 deletions python/debugging/diagnostics/SourcesLocal.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @name Local Sources Diagnostic
* @id ghsl/diagnostics/local-sources
* @description List all local sources
* @kind diagnostic
*/

import python
import semmle.python.dataflow.new.DataFlow
// Helpers
import ghsl.LocalSources

from LocalSources::Range s, Expr n
where
s.getScope().inSource() and
n = s.asExpr()
select n, ""
16 changes: 16 additions & 0 deletions python/debugging/diagnostics/SourcesRemotes.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @name Remote Sources Diagnostic
* @id ghsl/diagnostics/remote-sources
* @description List all remote sources
* @kind diagnostic
*/

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.RemoteFlowSources

from RemoteFlowSource s, Expr n
where
s.getScope().inSource() and
n = s.asExpr()
select n, ""
59 changes: 59 additions & 0 deletions python/debugging/partials/PartialPathsFromSink.ql

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @name Partial Path Query from Sink
* @kind path-problem
* @problem.severity warning
* @security-severity 1.0
* @sub-severity low
* @precision low
* @id py/debugging/partial-path-from-sink
* @tags debugging
*/

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.TaintTracking
import semmle.python.Concepts
import semmle.python.dataflow.new.RemoteFlowSources
import semmle.python.dataflow.new.BarrierGuards
import semmle.python.ApiGraphs
// Helpers
private import ghsl.Helpers

// Manual Sinks
class ManualSinks extends DataFlow::Node {
ManualSinks() { this = API::moduleImport("any").getMember("any").getACall() }
}

/**
* Partial Graph module interface
*/
module RemoteFlowsConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) { none() }

predicate isSink(DataFlow::Node sink) {
// List of dangerous sinks (SQL Injection, Command Injection, etc.)
dangerousSinks(sink)
or
// List of manually added sinks (above)
sink instanceof ManualSinks
}
}

// Set the limit of the exloration depth
int explorationLimit() { result = 10 }

module RemoteFlows = DataFlow::Global<RemoteFlowsConfig>;

module RemoteFlowsPartial = RemoteFlows::FlowExplorationRev<explorationLimit/0>;

import RemoteFlowsPartial::PartialPathGraph

from RemoteFlowsPartial::PartialPathNode source, RemoteFlowsPartial::PartialPathNode sink
where RemoteFlowsPartial::partialFlow(source, sink, _)
/// Filter by location
// and findByLocation(source.getNode(), "app.py", 20)
//
/// Filter by Function Parameters
// and functionParameters(source.getNode())
//
select sink.getNode(), source, sink, "Partial Graph $@.", source.getNode(), "user-provided value"
50 changes: 50 additions & 0 deletions python/debugging/partials/PartialPathsFromSource.ql

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @name Partial Path Query from Source
* @kind path-problem
* @problem.severity warning
* @security-severity 1.0
* @sub-severity low
* @precision low
* @id py/debugging/partial-path-from-source
* @tags debugging
*/

import python
import semmle.python.dataflow.new.DataFlow
import semmle.python.dataflow.new.TaintTracking
import semmle.python.Concepts
import semmle.python.dataflow.new.RemoteFlowSources
import semmle.python.dataflow.new.BarrierGuards
import semmle.python.ApiGraphs
// Helpers
private import ghsl.Helpers
private import ghsl.LocalSources

// Partial Graph
module RemoteFlowsConfig implements DataFlow::ConfigSig {
predicate isSource(DataFlow::Node source) {
source instanceof RemoteFlowSource
or
// Local Sources
source instanceof LocalSources::Range
}

predicate isSink(DataFlow::Node sink) { none() }
}

int explorationLimit() { result = 10 }

module RemoteFlows = DataFlow::Global<RemoteFlowsConfig>;

module RemoteFlowsPartial = RemoteFlows::FlowExplorationFwd<explorationLimit/0>;

import RemoteFlowsPartial::PartialPathGraph

from RemoteFlowsPartial::PartialPathNode source, RemoteFlowsPartial::PartialPathNode sink
where RemoteFlowsPartial::partialFlow(source, sink, _)
/// Filter by location
// and findByLocation(source.getNode(), "app.py", 50)
/// Filter by Function Parameters
// and functionParameters(sink.getNode())
//
select sink.getNode(), source, sink, "Partial Graph $@.", source.getNode(), "user-provided value"
8 changes: 8 additions & 0 deletions python/debugging/qlpack.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
library: false
name: githubsecuritylab/codeql-python-debugging
version: 0.1.0
suites: suites
defaultSuiteFile: suites/default.qls
dependencies:
codeql/python-all: '^1.0.0'
githubsecuritylab/codeql-python-libs: "${workspace}"
12 changes: 12 additions & 0 deletions python/debugging/suites/default.qls
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- description: "GitHub's Community Packs Python Debugging Suite"

# Field query pack with some audit queries
- queries: "."
from: githubsecuritylab/codeql-python-debugging

- include:
kind:
- problem
- path-problem
- metric
- diagnostic
Loading