Code Property Graphs
Dependencies
1 2 3 4 5 |
|
1 |
|
Code Property Graphs (CPGs) are a representation of program code that combines different code representations into a single graph. This unified representation includes abstract syntax trees (ASTs), control flow graphs (CFGs), control dependence graphs (CDGs), and data dependence graphs (DDGs). CPGs enable comprehensive analysis, which makes them a powerful tool for detecting vulnerabilities and understanding code structure. For further details, refer to this thesis.
Usage Example
In this example, we will demonstrate how to create a CPG for a vulnerable Java method and use it to identify a potential vulnerability.
Vulnerable Java Code
Let's assume we have the following vulnerable Java code in a file named VulnerableClass.java
:
1 2 3 4 5 6 7 |
|
Step 1: Obtain a SootMethod
First, we assume we have a SootMethod
for the vulnerableMethod
. For instructions on how to obtain a SootMethod
,
refer to Retrieving a Method.
Step 2: Create the CPG
We can create the CPG subgraphs using the creators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
We can create the combined CPG graph using the CpgCreator
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Step 3: Analyzing the CPG
With the CPG created, you can now analyze it for vulnerabilities. For example, you can check for potential injection vulnerabilities by analyzing data flow dependencies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
In this example, we check for data flow dependencies between the userInput
variable and any println
calls, which could indicate a potential injection vulnerability.
Similarly, we can define our own queries to detect specific patterns that identify common vulnerabilities.