Gitleaks是一个一个开源SAST(静态应用安全测试)命令行工具,用于检测Git 仓库以防止把密码、API 密钥和访问令牌等敏感信息硬编码到代码中。它既可以检测Git历史提交记录中的Secret,也可以用于Git 的pre-commit hook 和CI/CD 流水线。
一、安装方式
1.1 MacOs 安装
brew install gitleaks
1.2 Docker 安装
docker pull zricethezav/gitleaks:latest
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]
1.3 源码编译安装
git clone https://github.com/gitleaks/gitleaks.git
cd gitleaks
make build
二、使用方式
Gitleaks 主要使用如下两个子命令,detect和protect
admin@admindeMacBook-Pro go-gin-api % gitleaks
Gitleaks scans code, past or present, for secrets
Usage:
gitleaks [command]
Available Commands:
completion generate the autocompletion script for the specified shell
detect detect secrets in code
help Help about any command
protect protect secrets in code
version display gitleaks version
Flags:
-b, --baseline-path string path to baseline with issues that can be ignored
-c, --config string config file path
order of precedence:
1. --config/-c
2. env var GITLEAKS_CONFIG
3. (--source/-s)/.gitleaks.toml
If none of the three options are used, then gitleaks will use the default config
--exit-code int exit code when leaks have been encountered (default 1)
-h, --help help for gitleaks
--ignore-gitleaks-allow ignore gitleaks:allow comments
-l, --log-level string log level (trace, debug, info, warn, error, fatal) (default "info")
--max-target-megabytes int files larger than this will be skipped
--no-banner suppress banner
--no-color turn off color for verbose output
--redact uint[=100] redact secrets from logs and stdout. To redact only parts of the secret just apply a percent value from 0..100. For example --redact=20 (default 100%)
-f, --report-format string output format (json, csv, junit, sarif) (default "json")
-r, --report-path string report file
-s, --source string path to source (default ".")
-v, --verbose show verbose output from scan
Use "gitleaks [command] --help" for more information about a command.
目前Gitleaks最新版本是8.18版本,根据作者在Github上发起的9.0版本特性的讨论,https://github.com/gitleaks/gitleaks/issues/1274,很大可能会在9.0版本把protect 命令合并到 detect
命令中
2.1 detect命令
detect 命令用于扫描存储库、目录和文件。该命令可以在开发者机器和 CI 环境中使用。在 git 存储库上运行 detect 时,gitleaks 将解析 git log -p 命令,如果检测的目录不是git 存储库,需要加上–no-git 参数,否则将会报错如果检测的是git存储库,将会检测历史commit 是否包含敏感信息,以开源的go-gin-api项目为例,如下图所示检测所示,共检测了140个commit,发现了32处风险点
可以通过-f 指定生成的文件格式,如json、csv、junit等,默认是json格式, -r指定输出文件地址
gitleaks detect --source . -f json -r result.json
以开源的go-gin-api项目为例,检测输出结果如下,将会输出命中的文件路径、行号、Commit、规则ID、描述、提交的作者、以及Fingerprint等值如果不检测历史commit,只检测当前文件夹,可以使用如下命令,输出结果就相应的缺少Commit、Author等信息
gitleaks detect --source . -f json -r result.json --no-git

2.2 protect命令
protect 命令用于扫描 git 存储库中未提交的更改,protect 命令只能在 git 存储库上使用,在文件或目录上运行 protect 将会报错。当在 git 存储库上运行 protect 时,gitleaks 将解析 git diff 命令的输出,添加–staged参数,可以检查git add已编辑的提交中的更改。如下会检测 git diff 变更的文件内容是否含有敏感信息
gitleaks protect -r result.json -f json
如下会检测 git add 添加的文件内容是否含有敏感信息
gitleaks protect --staged -r result.json -f json
三、创建基线
扫描大型存储库或历史悠久的存储库时,使用基线会比较方便。当使用基线时, gitleaks 将忽略基线中存在的历史问题。基线可以是任何 格式的gitleaks 报告。要创建 gitleaks 报告,可以使用 –report-path 参数运行 gitleaks。
3.1 生成基线
第一次运行生成敏感信息检测报告,把该报告当做基线
gitleaks detect --report-path gitleaks-report.json
3.2 再次检测
以 gitleaks-report.json 当做基线,再次检测该项目,查看是否有新的风险,findings.json 将仅展示新问题
gitleaks detect --baseline-path gitleaks-report.json --report-path findings.json
3.3 验证结果
检测的结果可以使用 git log 命令验证 gitleaks 发现的是否正确。输出结果如下图:
{
"Description": "HashiCorp Terraform password field",
"StartLine": 12,
"EndLine": 13,
"StartColumn": 7,
"EndColumn": 1,
"Match": "Password = "qkhPAGA13HocW3GAEWwb"",
"Secret": ""qkhPAGA13HocW3GAEWwb"",
"File": "internal/pkg/password/password.go",
"SymlinkFile": "",
"Commit": "6d9a07f5ddefd883c260efe9cc1ff916e40797a8",
"Entropy": 3.879664,
"Author": "新亮",
"Email": "xinliangnote@163.com",
"Date": "2021-05-22T08:05:51Z",
"Message": "upgrade",
"Tags": [],
"RuleID": "hashicorp-tf-password",
"Fingerprint": "6d9a07f5ddefd883c260efe9cc1ff916e40797a8:internal/pkg/password/password.go:hashicorp-tf-password:12"
},
可以执行如下命令验证
git log -L {StartLine,EndLine}:{File} {Commit}
git log -L 12,13:internal/pkg/password/password.go 6d9a07f5ddefd883c260efe9cc1ff916e40797a8

四、防止提交到Git 仓库
4.1 方式一
将pre-commit.py文件https://github.com/gitleaks/gitleaks/blob/master/scripts/pre-commit.py 拷贝到 .git/hooks/ 目录,并修改名字为pre-commit
-
为该文件添加可执行权限
chmod +x .git/hooks/pre-commit
-
启用gitleaks钩子
git config --bool hooks.gitleaks true
当执行git commit 命令后,会检测Git变动文件是否含有敏感信息
-
可以通过执行如下命令禁止gitleaks检测
4.2 方式二
针对pre-commit 使用方式可参考 Pre-commit: Git hooks管理工具
-
安装pre-commit
git config hooks.gitleaks false
pip install pre-commit
-
项目根目录创建**.pre-commit-config.yaml**
touch .pre-commit-config.yaml
-
将以下内容拷贝到 .pre-commit-config.yaml文件
repos:
- repo: https://github.com/zricethezav/gitleaks
rev: v8.18.1
hooks:
- id: gitleaks
-
更新gitleaks版本
pre-commit autoupdate
-
安装pre-commit hook
pre-commit install
只需要确保每个开发人员都执行了以下两个命令,即可确保Git 仓库不会出现敏感信息
pip install pre-commit
pre-commit install
-
可以通过如下命令跳过检测
SKIP=gitleaks git commit -m "skip gitleaks check"
五、总结
总之,Gitleaks是一个强大的开源工具,用于帮助开发者在Git版本控制系统中发现和防止敏感信息的泄漏。它支持多种配置选项,可以根据不同的需求进行定制,想了解更多其检测原理和检测细节后续可以期待后续分析源码的文章。
原文始发于微信公众号(洋洋自语):敏感信息检测之Gitleaks
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/273048.html