用joern画AST、CFG、CDG、DDG、PDG、CPG

之前写了一点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文件了。

image.png

输入命令:

dot -Tpng -o test.png [dot-file-name]

image.png

同理也可以画出别的图。

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

image.png

CFG

image.png

CPG

image.png

暂时不知道终端怎么画这CDG、PDG、DDG。

  • 14
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 23
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

破落之实

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值