CodeQL是一款很知名的源码静态分析工具。本文主要介绍CodeQL的安装以及如何用它自带的查询去检测漏洞。
直接下载预编译的二进制程序
下载链接:https://github.com/github/codeql-cli-binaries/releases
然后进入codeql目录,运行`./codeql -h`,看是否有以下帮助信息。
Usage: codeql <command> <argument>...
Create and query CodeQL databases, or work with the QL language.
GitHub makes this program freely available for the analysis of open-source
software and certain other uses, but it is not itself free software. Type
codeql --license to see the license terms.
--license Show the license terms for the CodeQL toolchain.
Common options:
-h, --help Show this help text.
-v, --verbose Incrementally increase the number of progress
messages printed.
-q, --quiet Incrementally decrease the number of progress
messages printed.
Some advanced options have been hidden; try --help -v for a fuller view.
Commands:
query Compile and execute QL code.
bqrs Get information from .bqrs files.
database Create, analyze and process CodeQL databases.
dataset [Plumbing] Work with raw QL datasets.
test Execute QL unit tests.
resolve [Deep plumbing] Helper commands to resolve disk locations etc.
execute [Deep plumbing] Low-level commands that need special JVM options.
version Show the version of the CodeQL toolchain.
generate Commands that generate useful output.
github Commands useful for interacting with the GitHub API through CodeQL.
pack [Experimental] Commands to manage QL packages.
使用部分在文档中的该部分可以查阅到:https://codeql.github.com/docs/codeql-cli/using-the-codeql-cli/
使用codeql扫描程序主要分为创建数据库和分析两部分。
还是以libpng这一常见的库为例来介绍如何使用,首先使用codeql编译代码并创建数据库。
关于更多创建数据库的信息可以看官方文档的这一部分:https://codeql.github.com/docs/codeql-cli/creating-codeql-databases/
codeql database create --language=cpp -c "make" ./libpng_codedb
上述命令中各选项的含义:
language:指定语言
-c:编译程序的命令
libpng_codedb:codeql会将代码信息转换后,存储在数据库中。libpng_codedb即为数据库的路径。
如果万事ok,应该会提示:
Finalizing database at /home/iskindar/Project/StaticReportAnalyzer/testbench/libpng/libpng_codedb.
Successfully created database at /home/iskindar/Project/StaticReportAnalyzer/testbench/libpng/libpng_codedb.
接下来的分析部分,可以参考官方文档,这里做个简单介绍:https://codeql.github.com/docs/codeql-cli/analyzing-databases-with-the-codeql-cli/
使用codeql自带的queries去分析libpng。首先需要去github clone下codeql的库。
git clone https://github.com/github/codeql
由于我们是检查c++代码,所以自带的queries位于codeql/cpp/ql/src
中,其中有很多queries,安全相关的位于Security中。
PS: queries可以理解为是某种检测漏洞的规则。
这里我们选择使用codeql自带的cpp code scanning的查询套件,具体命令如下:
codeql database analyze ../libpnb_codedb/ --output=../codeql_results.csv --format=csv /home/iskindar/Software/codeql/cpp/ql/src/codeql-suites/cpp-code-scanning.qls
libpng_codedb:上条命令创建的数据库
-o …/codeql_results.csv : 指定输出结果的文件名为codeql_results.csv
—format=csv:指定输出格式为csv
/home/iskindar/Software/codeql/cpp/ql/src/codeql-suites/cpp-code-scanning.qls :要用的queries所在的目录
然后可以看到跑出来的结果的csv(只跑出了一条报告。)
Multiplication result converted to larger type A multiplication result that is converted to a larger type can be a sign that the result can overflow the type converted from. warning Multiplication result may overflow 'unsigned int' before it is converted to 'long'. /contrib/libtests/pngstest.c 669 18 669 67
从左到右依次代表的意思是: