掘金 后端 ( ) • 2024-04-28 17:42

highlight: xcode

CodeQL 是什么

CodeQL 是一个强大的代码分析引擎,在 CodeQL 中,代码被视为数据,可以被转换成一个可查询的数据库形式。这个数据库包含了源代码的抽象语法树(AST)、语义信息、控制流和数据流等关键信息。利用 CodeQL,用户可以编写查询来检测代码中的安全漏洞、Bug 以及其他潜在的问题。

CodeQL 的核心是一个不开源的解析引擎,它帮助将代码转换成 CodeQL 可以识别的数据库。同时,CodeQL 提供了一个完全开源的 SDK,其中包含了大部分现成的漏洞规则,用户也可以利用它来参考编写自定义规则。

CodeQL 引擎地址:https://github.com/github/codeql-cli-binaries

CodeQL SDK 地址:https://github.com/github/codeql

CodeQL 的使用包括创建 CodeQL 数据库、运行查询以及解析查询结果几个步骤。它支持多种编程语言: image.png

此外,CodeQL 还提供了一套自己的查询语言(QL),用户可以通过编写 QL 规则来获取想要的数据。CodeQL 的处理对象并不是源码本身,而是中间生成的 AST 结构数据库。因此,在使用 CodeQL 之前,需要将项目源码转换成 CodeQL 能够识别的 CodeDatabase。

下载安装

下载 CodeQL 的 CLI 引擎和 SDK 模版:

CodeQL CLI 引擎地址:https://github.com/github/codeql-cli-binaries

CodeQL SDK 地址:https://github.com/github/codeql

然后放到你的工具文件夹目录里去,然后解压,添加 Code CLI 引擎的 PATH 环境变量:

% vim ~/.zshrc # 编辑内容,添加下面的内容
% cat ~/.zshrc
# CodeQL 工具
export PATH="$PATH:/Users/{你的用户名}/Code_Space/CodeQL/codeql/codeql"  # 你的路径

简单实验一下是否安装成功:

% codeql --version
CodeQL command-line toolchain release 2.17.1.
Copyright (C) 2019-2024 GitHub, Inc.
Unpacked in: /Users/sanqiushu/Code_Space/CodeQL/codeql
   Analysis results depend critically on separately distributed query and
   extractor modules. To list modules that are visible to the toolchain,
   use 'codeql resolve qlpacks' and 'codeql resolve languages'.

% codeql pack ls
Running on packs: codeql/csharp-all, codeql/threat-models, codeql/csharp-queries, codeql/mad, codeql/swift-queries, codeql/cpp-examples, codeql/ruby-queries, codeql/ssa, codeql/xml, codeql/controlflow, codeql/ruby-examples, codeql/java-queries, codeql/regex, codeql/yaml, codeql/csharp-examples, codeql/go-queries, codeql/swift-all, codeql/python-queries, codeql/go-all, codeql/javascript-all, codeql/typos, codeql/suite-helpers, codeql/tutorial, codeql/typeflow, codeql/rangeanalysis, codeql/go-examples, codeql/python-all, codeql/java-examples, codeql/cpp-queries, codeql/python-examples, codeql/javascript-examples, legacy-upgrades, codeql/javascript-queries, codeql/cpp-all, codeql/util, codeql/dataflow, codeql/typetracking, codeql/java-all, codeql/ruby-all.
Found [email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]
Found codeql/[email protected]

这样基本上就可以了

然后我们还需要准备一下需要审计的代码,我这里直接就用漏洞靶场代替了

% cd /Users/{你的用户名}/Code_Space/IntelliJ_IEDA_CE_Space
% git clone https://github.com/WebGoat/WebGoat.git
% cd WebGoat 
% ls
CODE_OF_CONDUCT.md		Dockerfile_desktop		README_I18N.md			mvnw
CONTRIBUTING.md			FAQ.md				RELEASE_NOTES.md		mvnw.cmd
COPYRIGHT.txt			LICENSE.txt			config				pom.xml
CREATE_RELEASE.md		PULL_REQUEST_TEMPLATE.md	docs				robot
Dockerfile			README.md			mvn-debug			src

然后我们还需要准备一下代码编辑器环境,这里使用 VSCode,直接插件市场搜索 CodeQL 安装即可

image.png

然后把我们安装的 CodeQL 引擎路径放入 CodeQL 插件的设置中去(直接放可执行文件的路径)

image.png

简单使用

好的,环境基本准备完毕了,接下来就可以开始进行学习了。 首先我们需要为这个项目构建 CodeQL 数据库,只有这样 CodeQL 才能在数据库的基础上进行查询。

请注意,WebGoat 默认 JAVA 版本是 JDK 17,请注意修改自己的 JAVA_HOME 环境变量。

% cd /Users/{你的用户名}/Code_Space/IntelliJ_IEDA_CE_Space/CMS/WebGoat
% codeql database create WebGoat_CodeQL_database -l java  # 只需要指定数据库名字和语言就行了

运行之后 CodeQL 会运行 mvn 进行下载依赖、编译程序,然后使用 CodeQL 引擎生成数据库等等操作。

这就是生成的数据库文件夹。 image.png

然后点击 CodeQL 插件,在界面中完成语言选择、数据库选择

image.png

然后我们来到编辑器页面,新建一个查询,路径如下 /codeql-codeql-cli-v2.17.1 15.09.14/java/ql/src/myWebgoat.ql 编写查询代码

image.png

import java

from Method m
select m

直接右键运行

image.png

右侧即可查询到内容

我们再编写一个查询对 executeQuery 方法调用的例子:

import java

from MethodCall mm
where mm.getMethod().getName() = "executeQuery"
select mm

点击右侧的结果,还可以直接看到源码。

image.png

下一篇我们将介绍如何实战查找漏洞。


文中如果有任何错误欢迎随时沟通和指出。

如果觉得有用,请点赞支持一下吧。