从分析一个简单的程序入门joern的用法

官网给了一个简单的教程,这篇文章就是按github上的文档来实践的。

网址:https://github.com/ShiftLeftSecurity/joern/blob/master/docs2/docs/quickstart.mdx
这个工具的作者最近更新频率好高。太强了。

获取样本程序

这里用joern作者提供的一个样本程序叫x42

$ git clone git@github.com:ShiftLeftSecurity/x42.git

如果遇到下面这个问题:permission denied (publickey)

在这里插入图片描述
可以使用这个链接的方法来解决:https://baijiahao.baidu.com/s?id=1628034638876029219&wfr=spider&for=pc

打开joern的shell

在joern的文件夹下,运行./joern即可打开shell。这个joern shell是基于scala的REPL,REPL类似于python的IDLE。作者提到,如果没用过scala也没关系,仍然可以用joern的命令做到狠多事情。如果很熟悉scala的话,你会感叹它的灵活性。

导入代码

这里就不像上次那篇博客用joern-parse来导入代码,shell里也可以直接使用importCode命令导入代码。

importCode(inputPath="./x42/c", projectName="x42-c")
  • inputPath是代码路径
  • projectName是工程名称,下次用joern就可以直接使用open("x42-c")打开

在这里插入图片描述

查询代码属性图

马上就要用joern和代码属性图来分析程序了。joern的程序分析的实现是基于查询语言的,一种专门为代码属性图设计的语言。它包含了代码属性图各种各样节点的有用表示,以及有用的函数来查询它们的属性及它们之间的关系。代码属性图中最顶层的入口点加载在内存中。然后查询语言中的根对象叫cpg,如果我们在shell里输入cpg,只会返回一个id,并没有什么卵用。

在这里插入图片描述

我们首先学习一个有用的技巧,输入cpg.然后按TAB键,会列出cpg可用的方法。

在这里插入图片描述
查看关于cpg的描述信息可以输入help.cpg查看描述

res12: String = """
Upon importing code, a project is created that holds an intermediate
representation called `Code Property Graph`. This graph is a composition of
low-level program representations such as abstract syntax trees and control flow
graphs, but it can be arbitrarily extended to hold any information relevant in
your audit, information about HTTP entry points, IO routines, information flows,
or locations of vulnerable code. Think of Ocular and Joern as a CPG editors.

In practice, `cpg` is the root object of the query language, that is, all query
language constructs can be invoked starting from `cpg`. For example,
`cpg.method.l` lists all methods, while `cpg.finding.l` lists all findings of
potentially vulnerable code."""

输入cpg.help可以查看cpg下面各个方法的描述

res13: String = """Available starter steps:
____________________________________________________________________________
 step             | description                                            |
===========================================================================|
 .all             | All nodes of the graph                                 |
 .argument        | All arguments (actual parameters)                      |
 .arithmetic      | All arithmetic operations                              |
 .assignment      | All assignments                                        |
 .call            | All call sites                                         |
 .comment         | All comments in source-based CPGs                      |
 .controlStructure| All control structures (source-based frontends)        |
 .file            | All source files                                       |
 .identifier      | All identifier usages                                  |
 .jumpTarget      | All jump targets, i.e., labels                         |
 .literal         | All literals, e.g., numbers or strings                 |
 .local           | All local variables                                    |
 .member          | All members of complex types (e.g., classes/structures)|
 .metaData        | Meta data blocks for graph                             |
 .method          | All methods                                            |
 .methodRef       | All method references                                  |
 .methodReturn    | All formal return parameters                           |
 .namespace       | All namespaces                                         |
 .parameter       | All parameters                                         |
 .returns         | All actual return parameters                           |
 .tag             | All tags                                               |
 .typeDecl        | All declarations of types                              |
 .types           | All used types                                         |

"""

首先来看一下x42程序:

// X42.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[]) {
  if (argc > 1 && strcmp(argv[1], "42") == 0) {
    fprintf(stderr, "It depends!\n");
    exit(42);
  }
  printf("What is the meaning of life?\n");
  exit(0);
}

这个程序主要分为两个部分:

  1. 程序向STDERR写了什么东西吗?
  2. 如果有个调用写了STDERR,那这个传入x42程序的参数的值是否是有条件限制的呢?

Joern使我们很容易回答这两个问题。回答第一个问题,我们只需要搜索图里属性为CALL的节点,然后使用filter直接选择那些和类型为ARGUMENT且值为stderr的节点有关系的CALL节点。

cpg.call.filter(_.argument.code("stderr")).l 

结果如下:

在这里插入图片描述

这个查询告诉我们确实有个地方向STDERR写了点啥玩意。接下来,我们看第二个问题。由于我们分析的程序是用C语言写的,所以我们在代码属性图里搜索main函数里的argcargv参数,这些参数可能会触发写STDERR的调用。基于上一步的查询,现在我们可以使用astParent来移到代码属性图的抽象语法树层面来,而找到更多的关于fprintf调用的信息。移到AST层,只给了我们一个block,并不是很有用:

cpg.call.filter(_.argument.code("stderr")).astParent.l

在这里插入图片描述

另外一层给了我们一个if语句,更有用:

cpg.call.filter(_.argument.code("stderr")).astParent.astParent.l

在这里插入图片描述

这个ControlStructure里的code属性告诉我们,写STDERR的调用是基于argcargv的。

关闭工程

现在我们完成了分析,让我们关闭工程。不需要担心会丢失数据,因为x42-c工程在importCode的时候就创建完毕了。

close

在这里插入图片描述
最后,离开joern,按ctrl-D或者输入exit.

在这里插入图片描述

管理工程

输入命令workspace可以看到你之前建立的所有工程,如下图:
在这里插入图片描述
这样我们可以查询到各个工程的状态,有时候忘记自己建立的工程的名字,也可以在这里查看到。
接下来你可以使用open("simple")打开这个simple的工程。

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
Joern一个开源的代码静态分析工具,可以帮助开发人员快速进行代码安全性评估和漏洞检测。在Eclipse中使用Joern,可以进一步方便地集成Joern的功能。 下面是在Eclipse中使用Joern的步骤: 1. 首先,需要安装Eclipse IDE(集成开发环境)和Joern插件。可以从Eclipse官网下载Eclipse IDE,并从Joern的官方网站获取Joern插件的jar文件。 2. 打开Eclipse IDE,选择“Help”菜单,然后点击“Eclipse Marketplace”。在搜索栏中输入“Joern”,点击搜索按钮。 3. 在搜索结果中,找到Joern插件,点击“Go”按钮进行安装。安装完成后,需要重启Eclipse IDE。 4. 安装完毕后,在Eclipse的菜单栏中出现“Joern”选项,点击它可以进入Joern插件界面。 5. 在Joern插件界面中,可以选择分析的代码项目。点击“Add”按钮,选择要分析的代码项目所在的文件夹,并设置分析语言类型。 6. 设置完项目后,可以在Joern插件界面的“Analyse”部分选择不同的分析方法,如漏洞检测、规则匹配等。根据需要进行适当的配置。 7. 完成配置后,点击“Analyze”按钮开始分析代码。分析结果将在Eclipse的Console视图中显示出来。 8. 可以根据分析结果进行代码改进或漏洞修复。可以使用Joern插件中的其他功能和工具,如可视化图形展示、查询分析结果等。 通过以上步骤,就可以在Eclipse中使用Joern进行代码静态分析和漏洞检测。使用Joern可以帮助开发人员快速发现和修复代码中的安全问题,提高代码质量和安全性。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

破落之实

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

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

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

打赏作者

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

抵扣说明:

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

余额充值