Ref: https://evilpan.com/2024/09/16/using-joern/

组成部分

joern-parse

源代码 → 代码属性图 (CPG) 的转换器。 用法:

./joern-parse /home/harson/CGraph/12.c -o /home/harson/CGraph/12cpg.bin

其中生成的12cpg.bin文件为代码属性图(CPG)数据文件。

joern-scan

自动扫描,调用joern-parse生成cpg,执行预定义的安全查询规则。

mkdir foo
echo "int foo(int a, int b, int c, int d, int e, int f) {}" > foo/foo.c
./joern-scan foo

joern-flow

数据流分析工具。

joern-silce

joern-export

图导出工具

查看帮助

只有使用--help参数才是传入sc的程序

./joern-parse --help

创建数据库

使用joern-parse

./joern-parse /home/harson/CGraph/12.c -o /home/harson/CGraph/12cpg.bin

导入代码

importCode("~/exp.jar")

按照提示创建cpg

/home/harson/Desktop/JoernStudy/joern-cli/jimple2cpg -J-Xmx1974m /home/harson/Desktop/JoernStudy/postgresql-42.3.1.jar --output /home/harson/Desktop/JoernStudy/joern-cli/workspace/postgresql/cpg.bin.zip
# 然后在Joern控制台导入
importCpg("path/to/cpg")

查询

Doc: https://docs.joern.io/cpgql/node-type-steps/

节点获取

// 根据名字查找
cpg.method.name("getRequestBody").l
cpg.method("getRequestBody")
 
// 根据调用点查找
cpg.call("getRequestBody").take(1).l // 取第一个调用点
cpg.call.name("getRequestBody")
cpg.method.where(_.name("getRequestBody")).l
 
// 根据注解查找
cpg.annotation.name(".*Mapping").take(1).l
 
// 谁调用了他
cpg.method.name("getRequestBody").caller
// 他调用了谁
cpg.method.name("getRequestBody").callee
cpg.method.name("getRequestBody").callIn
 

返回结果处理

cpg.method.name("getRequestBody").map(n=>List(n.filename, n.lineNumber, n.fullName, n.code)).l

.l 输出列表格式 .toJson 输出JSON格式 .p 输出可读性格式 .size 节点大小 .dump 输出节点代码

正向搜索,从每一个RequestMapping的节点向下搜索寻找有没有调用getRequestBody方法的节点

cpg.method.where(_.annotation.name(".*Mapping")).repeat(_.callee)(_.until(_.name("getRequestBody"))).l

反向搜索,从调用getRequestBody方法的节点向上搜索寻找@RequestMapping的注解

cpg.method.name("getRequestBody").repeat(_.caller)(_.until(_.annotation.name(".*Mapping"))).l

数据流分析

污点分析

def source = cpg.method.where(_.annotation.name(".*Mapping")).parameter
def sink = cpg.call.name("getConnection")
sink.reachableByFlows(source).p