之前写了一点joern的博客,发现很多人在问我怎么画图。确实,用joern这个工具的话,画代码属性图是这个工具的亮点了。
这篇文章怎么说呢,也可以看作是对官方文档的翻译和实现吧。
能搜到这篇文章,想必应该是搜到上面的一个图的名字进来的。就不详细介绍每个图了,就简单介绍下缩写的含义。
- AST:abstract syntax tree
- CFG:control flow graph
- CDG:Control Dependence Graphs
- DDG:Data Dependence Graphs
- PDG:Program Dependence graphs
- CPG:Code Property Graphs
命令行操作
首先解析源码目录(下面代码的/src/directory)的代码,然后用export命令将图导出到outdir文件夹下,默认的cpg的名字是当前目录下的cpg.bin。
- –repr:指定画什么图ast|cfg|ddg|cdg|pdg|cpg14
- –out:指定输出图dot文件目录名字(自动创建)
#生成cpg.bin
joern-parse /src/directory
#基于cpg.bin文件生成别的图
joern-export [cpg-name] --repr pdg --out outdir
此外需要注意outdir文件夹不需要你自己建,如果你自己建的话,可能会报下面的错误:
Output directory outdir already exists. Bailing out
成功运行后,会发现你的outdir文件夹都是图的dot文件了。
输入命令:
dot -Tpng -o test.png [dot-file-name]
同理也可以画出别的图。
joern终端操作
首先,导入一段测试代码
joern> importCode.c.fromString( """
int myfunc(int b) {
int a = 42;
if (b > 10) {
foo(a)
}
bar(a);
}
"""
)
画各种图的命令:
cpg.method("myfunc").plotDotAst
cpg.method("myfunc").plotDotCfg
cpg.method("myfunc").plotDotCdg
cpg.method("myfunc").plotDotDdg
cpg.method("myfunc").plotDotPdg
cpg.method("myfunc").plotDotCpg14
AST
CFG
CPG
暂时不知道终端怎么画这CDG、PDG、DDG。