trailofbits/codeql-queries
GitHub: trailofbits/codeql-queries
由知名安全公司 Trail of Bits 开发的 CodeQL 自定义查询库,专注于密码学实现缺陷和底层安全漏洞的静态检测
Stars: 147 | Forks: 7
# Trail of Bits 公开的 CodeQL 查询
此仓库包含由 Trail of Bits 开发并向公众公开的 CodeQL 查询。它们是我们持续开发工作的一部分,并被用于我们的安全审计、漏洞研究和内部项目。随着我们识别出新技术,这些查询也将不断演进。
## 使用自定义 CodeQL 查询
最简单的方法是从 GitHub registry [下载所有包](https://docs.github.com/en/code-security/codeql-cli/using-the-advanced-functionality-of-the-codeql-cli/publishing-and-using-codeql-packs#running-codeql-pack-download-scopepack):
```
codeql pack download trailofbits/cpp-queries trailofbits/go-queries
```
然后验证新查询是否已安装:
```
codeql resolve qlpacks | grep trailofbits
```
并使用这些查询进行分析:
```
codeql database analyze database.db --format=sarif-latest --output=./tob.sarif -- trailofbits/cpp-queries
# 或
codeql database analyze database.db --format=sarif-latest --output=./tob.sarif -- trailofbits/go-queries
```
## 查询
### C 和 C++
#### 密码学
| 名称 | 描述 | 严重程度 | 精确度 |
| --- | ----------- | :----: | :--------: |
|[BN_CTX_free called before BN_CTX_end](./cpp/src/docs/crypto/BnCtxFreeBeforeEnd.md)|检测在 BN_CTX_end 之前调用的 BN_CTX_free,这违反了必要的生命周期|error|medium|
|[Unbalanced BN_CTX_start and BN_CTX_end pair](./cpp/src/docs/crypto/UnbalancedBnCtx.md)|检测 BN_CTX_start/BN_CTX_end 对中是否缺少一个调用|warning|medium|
|[Crypto variable initialized using static key](./cpp/src/docs/crypto/StaticKeyFlow.md)|查找使用静态密钥初始化的加密变量|error|high|
|[Crypto variable initialized using static password](./cpp/src/docs/crypto/StaticPasswordFlow.md)|查找使用静态密码初始化的加密变量|error|high|
|[Crypto variable initialized using weak randomness](./cpp/src/docs/crypto/WeakRandomnessTaint.md)|查找使用弱随机性初始化的加密变量|error|high|
|[Invalid key size](./cpp/src/docs/crypto/InvalidKeySize.md)|测试传递给 EVP_EncryptInit 和 EVP_EncryptInit_ex 的密钥大小是否与所用加密算法的密钥大小相同|warning|medium|
|[Memory leak related to custom allocator](./cpp/src/docs/crypto/CustomAllocatorLeak.md)|查找来自自定义分配内存的内存泄漏|warning|medium|
|[Memory use after free related to custom allocator](./cpp/src/docs/crypto/CustomAllocatorUseAfterFree.md)|查找与自定义分配器(如 `BN_new`)相关的释放后使用|warning|medium|
|[Missing OpenSSL engine initialization](./cpp/src/docs/crypto/MissingEngineInit.md)|查找可能未正确初始化的已创建 OpenSSL 引擎|warning|medium|
|[Missing error handling](./cpp/src/docs/crypto/MissingErrorHandling.md)|检查返回的错误代码是否被正确检查|warning|high|
|[Missing zeroization of potentially sensitive random BIGNUM](./cpp/src/docs/crypto/MissingZeroization.md)|确定随机大数是否被正确归零|warning|medium|
|[Random buffer too small](./cpp/src/docs/crypto/RandomBufferTooSmall.md)|查找调用 CSPRNGs 时的缓冲区溢出|warning|high|
|[Use of legacy cryptographic algorithm](./cpp/src/docs/crypto/UseOfLegacyAlgorithm.md)|检测遗留加密算法的潜在实例化|warning|medium|
#### 安全
| 名称 | 描述 | 严重程度 | 精确度 |
| --- | ----------- | :----: | :--------: |
|[Async unsafe signal handler](./cpp/src/docs/security/AsyncUnsafeSignalHandler/AsyncUnsafeSignalHandler.md)|异步不安全的信号处理程序(如 CVE-2024-6387 中使用的那个)|warning|high|
|[Decrementation overflow when comparing](./cpp/src/docs/security/DecOverflowWhenComparing/DecOverflowWhenComparing.md)|此查询查找在比较期间因未经检查的递减而导致的无符号整数溢出。|error|high|
|[Find all problematic implicit casts](./cpp/src/docs/security/UnsafeImplicitConversions/UnsafeImplicitConversions.md)|查找所有可能有问题的隐式转换。即可能导致意外截断、重新解释或值扩展的转换。|error|high|
|[Inconsistent handling of return values from a specific function](./cpp/src/docs/security/InconsistentReturnValueHandling/InconsistentReturnValueHandling.md)|检测返回值在不同调用点比较不一致的函数,这可能表示存在 bug|warning|medium|
|[Invalid string size passed to string manipulation function](./cpp/src/docs/security/CStrnFinder/CStrnFinder.md)|查找接受字符串及其大小作为单独输入参数的函数调用(例如 `strncmp`、`strncat` 等),且大小参数错误|error|low|
|[Iterator invalidation](./cpp/src/docs/security/IteratorInvalidation/IteratorInvalidation.md)|在迭代容器时修改它可能会使迭代器失效,从而导致未定义的行为。|warning|medium|
|[Missing null terminator](./cpp/src/docs/security/NoNullTerminator/NoNullTerminator.md)|此查询查找传递给期望以空字节结尾的字符串函数的错误初始化字符串|error|high|
### Go
#### 密码学
| 名称 | 描述 | 严重程度 | 精确度 |
| --- | ----------- | :----: | :--------: |
|[Message not hashed before signature verification](./go/src/docs/crypto/MsgNotHashedBeforeSigVerfication/MsgNotHashedBeforeSigVerfication.md)|检测使用未哈希的消息调用 (EC)DSA API。如果消息长于预期的哈希摘要大小,它会被静默截断|error|medium|
#### 安全
| 名称 | 描述 | 严重程度 | 精确度 |
| --- | ----------- | :----: | :--------: |
|[Invalid file permission parameter](./go/src/docs/security/FilePermsFlaws/FilePermsFlaws.md)|查找用作文件系统权限参数(`FileMode`)的非八进制(例如 `755` 对 `0o755`)和不支持(例如 `04666`)的字面量|error|medium|
|[Missing MinVersion in tls.Config](./go/src/docs/security/MissingMinVersionTLS/MissingMinVersionTLS.md)|查找未设置 MinVersion 的 tls.Config 用法,且项目的最低 Go 版本(来自 go.mod)指示了不安全的默认值:客户端 Go < 1.18 或服务端 Go < 1.22。不标记显式设置的版本(包括显式设置为不安全的版本)。|error|medium|
|[Trim functions misuse](./go/src/docs/security/TrimMisuse/TrimMisuse.md)|查找对 `string.{Trim,TrimLeft,TrimRight}` 的调用,其第二个参数不是字符集而是要修剪的连续子字符串|error|low|
### Java-kotlin
#### 安全
| 名称 | 描述 | 严重程度 | 精确度 |
| --- | ----------- | :----: | :--------: |
|[Recursive functions](./java-kotlin/src/docs/security/Recursion/Recursion.md)|检测可能无界的递归调用|warning|low|
## 查询套件
CodeQL 查询被分组为“套件”。要执行来自特定套件的查询,请在其名称后添加冒号:`trailofbits/cpp-queries:codeql-suites/tob-cpp-full.qls`。
推荐的套件 —— `tob-cpp-code-scanning.qls` —— 会在您未显式指定任何套件时被选择并执行。此仓库中的其他套件包括:
* `tob--crypto.qls` - 针对密码学漏洞的查询
* `tob--security.qls` - 针对标准安全问题的查询
* `tob--full.qls` - 所有查询,包括实验性的
## 开发
#### 准备环境
克隆此仓库并配置全局 CodeQL 的搜索路径:
```
git clone git@github.com:trailofbits/codeql-queries.git
mkdir -p "${HOME}/.config/codeql/"
echo "--search-path '$PWD/codeql-queries'" > "${HOME}/.config/codeql/config"
```
检查 CodeQL CLI 是否检测到新的 qlpacks:
```
codeql resolve packs | grep trailofbits
```
#### 提交前
运行测试:
```
make test
```
格式化查询:
```
make format
```
安装依赖:
```
make install
```
生成查询表并将其复制粘贴到 README.md 文件中
```
python ./scripts/queries_table_generator.py 2>/dev/null
```
生成 markdown 查询帮助文件
```
codeql generate query-help ./cpp/src/ --format=markdown --output ./cpp/src/docs
codeql generate query-help ./go/src/ --format=markdown --output ./go/src/docs
codeql generate query-help ./java/src/ --format=markdown --output ./java/src/docs
```
标签:C++, CodeQL, DevSecOps, DNS 反向解析, Go语言, JS文件枚举, OpenSSL, SARIF, SAST, Trail of Bits, 上游代理, 云安全监控, 代码安全, 加密安全, 安全专业人员, 安全测试工具, 安全评估工具, 弱随机数, 数据擦除, 日志审计, 服务器监控, 漏洞枚举, 盲注攻击, 硬编码密钥, 程序破解, 自定义规则库, 软件供应链安全, 远程方法调用, 防御机制, 防御机制, 静态分析