Code Property Graph: specification, query language, and utilities
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 年前 | ||
| 3 个月前 | ||
| 1 个月前 | ||
| 5 年前 | ||
| 3 个月前 | ||
| 4 个月前 | ||
| 1 年前 | ||
| 2 年前 | ||
| 1 个月前 | ||
| 1 年前 | ||
| 6 年前 | ||
| 1 年前 | ||
| 4 年前 | ||
| 4 年前 | ||
| 1 年前 | ||
| 6 年前 | ||
| 2 年前 | ||
| 1 个月前 | ||
| 7 年前 | ||
| 5 年前 | ||
| 7 个月前 |
程序属性图 - 规范与工具集
可点击规范链接如下:
注意:对于初次使用者,我们建议从https://github.com/joernio/joern/构建“Joern”。它结合了本存储库和C/C++语言前端,以构建一个完整的代码分析平台。
程序属性图(CPG)是一种可扩展且语言无关的代码表示法,专为增量及分布式代码分析设计。此仓库托管了基础规范以及生成过程,用于产生能用不同编程语言访问图表的数据结构定义。
我们将程序属性图规范公布为开放标准的一种提议,以交换中间表达形式下的代码及分析结果。基于这一目标,规范由最小的基础模式组成,可通过扩展模式增强,以实现应用特定数据的储存。
依赖使用
在build.sbt文件中添加以下依赖项:
libraryDependencies += "io.shiftleft" %% "codepropertygraph" % "x.y.z"
编译代码
已验证编译流程适用于Linux系统,并且应该同样可以在OS X及BSD系统上执行。编译流程需满足以下先决条件:
部分测试所需二进制文件通过git-lfs管理。如尚未克隆此仓库,请运行git lfs install。若已经完成克隆,则额外运行git lfs pull(位于仓库内)。
编译过程中会自动下载额外构建时间依赖项。要编译并安装到本地Maven缓存中,执行sbt clean test publishM2命令。
代码格式
代码格式由scalafmt自动验证。
如果您的PR构建因代码格式检查而失败,只需运行sbt scalafmt并提交更改,同时附带其余代码。该命令按正确顺序执行必要的格式化操作。
创建多种语言的协议缓冲区绑定
artifact codepropertygraph-VERSION.jar 包含协议缓冲区定义文件cpg.proto,可用于生成您自己的语言特定位绑定。例如,为了创建C++和Python绑定,可执行下列指令序列:
sbt package
cd codepropertygraph/target
mkdir cpp python
protoc --cpp_out=cpp --python_out=python cpg.proto
加载程序属性图
以下是您如何在ShiftLeft Tinkergraph控制台加载cpg的方式——下一节将列出您可以在此交互性环境中运行的一些查询。
此仓库中的resources/testcode/cpgs目录包含一些示例cpg。
您可以通过运行./regenerate-test-cpgs.sh更新它们,但这需要本地安装专有的java2cpg。
内存数据库参考:Tinkergraph
sbt semanticcpg/console
import io.shiftleft.codepropertygraph.generated.Cpg
import io.shiftleft.semanticcpg.language._
val cpg = io.shiftleft.codepropertygraph.cpgloading.CpgLoader.load("./resources/testcode/cpgs/hello-shiftleft-0.0.5/cpg.bin.zip")
查询cpg
一旦加载了cpg,便可以运行查询,这些查询由子项目query-primitives提供。请注意,在sbt Shell中,您可以直接进行互动尝试:TAB补全功能为您效劳。否则,IDE会辅助您。
下面是一些获取所有基节点的简单遍历示例。无错误地运行所有这些操作是确保cpg有效性的良好检验:
cpg.literal.toList
cpg.file.toList
cpg.namespace.toList
cpg.types.toList
cpg.methodReturn.toList
cpg.parameter.toList
cpg.member.toList
cpg.call.toList
cpg.local.toList
cpg.identifier.toList
cpg.argument.toList
cpg.typeDecl.toList
cpg.method.toList
自此之后,您就可以遍历cpg。查询原语DSL确保仅可采用有效步骤 —— 否则会导致编译错误:
cpg.method.name("getAccountList").parameter.toList
/* List(
* MethodParameterIn(Some(v[7054781587948444580]),this,0,this,BY_SHARING,io.shiftleft.controller.AccountController,Some(28),None,None,None),
* MethodParameterIn(Some(v[7054781587948444584]),request,2,request,BY_SHARING,javax.servlet.http.HttpServletRequest,Some(28),None,None,None),
* MethodParameterIn(Some(v[7054781587948444582]),response,1,response,BY_SHARING,javax.servlet.http.HttpServletResponse,Some(28),None,None,None)
* )
**/
cpg.method.name("getAccountList").definingTypeDecl.toList.head
// TypeDecl(Some(v[464]),AccountController,io.shiftleft.controller.AccountController,false,List(java.lang.Object))
脚本
输出CPG架构
利用schema2json.sh bash脚本来输出当前程序属性图架构。
./schema2json.sh
Schema 已写入至: /tmp/schema.json
进一步阅读
-
Rodriguez和Neubauer - 图遍历模式:https://pdfs.semanticscholar.org/ae6d/dcba8c848dd0a30a30c5a895cbb491c9e445.pdf
-
Yamaguchi等人 - 利用程序属性图建模并发现漏洞:https://www.sec.cs.tu-bs.de/pubs/2014-ieeesp.pdf
-
ShiftLeft Tinkergraph:https://github.com/ShiftLeftSecurity/tinkergraph-gremlin
-
ShiftLeft OverflowDB:https://github.com/ShiftLeftSecurity/overflowdb