GitLab 是一个全球知名的一体化 DevOps 平台,很多人都通过私有化部署 GitLab 来进行源代码托管。极狐GitLab :https://gitlab.cn/install?channel=content&utm_source=csdn 是 GitLab 在中国的发行版,专门为中国程序员服务。可以一键式部署极狐GitLab。
更多关于极狐GitLab :https://gitlab.cn 或者 DevOps 的最佳实践,可以关注文末的极狐GitLab 公众号。
学习极狐GitLab 的相关资料:
- 极狐GitLab 官网:https://gitlab.cn
- 极狐GitLab 官网文档:https://docs.gitlab.cn
- 极狐GitLab 论坛:https://forum.gitlab.cn/
- 极狐GitLab 安装配置:https://gitlab.cn/install
- 极狐GitLab 资源中心:https://resources.gitlab.cn
搜索【极狐GitLab】公众号,后台输入加群,备注gitlab,即可加入官方微信技术交流群。
极狐GitLab 公众号后台回复新手指南,免费领取极狐GitLab 新手指南一份,从零到一快速上手极狐GitLab。
前言
在应用程序开发过程中,一个很常见的问题就是:开发人员为了本地 debug 方便,会 hardcode 一些信息,比如连接数据库的用户名、密码、连接第三方 app 的 token、certificate 等,如果在提交代码的时候没有及时删除 hardcode 的信息,则非常容易造成敏感信息泄漏,带来被拖库、撞库等风险。
因此,敏感信息管理是 DevSecOps 中一个非常重要的话题。诚然,敏感信息的管理需要软件开发中的每一个人来负责,有安全意识,但是是人就总有疏忽犯错的时候,所以最好的方式之一就是将敏感信息的检测集成到 CI/CD 中,做到持续监测,而且能在开发人员提交代码的时候就进行,真正做到安全左移。
极狐GitLab 提供开箱即用的 DevSecOps 功能,包括七大功能:容器镜像扫描、静态应用安全测试 (SAST)、动态应用安全扫描(DAST)、密钥检测、License合规、依赖项扫描以及模糊测试。
极狐GitLab 敏感信息检测
极狐GitLab 敏感信息检测(Secret Detection)功能是开箱即用的,在 11.9 版本中引入。既可以对提交代码中的敏感信息进行检测,也可以对远程仓库进行重复性的扫描来进行敏感信息检测;既可以使用默认的规则进行敏感信息检测,也可以通过自定义规则来完成敏感信息检测。极狐GitLab 敏感信息检测是与语言无关的,也就以为是“全谱”可用的。
检测原理
敏感信息检测的思路一般是:读取文件内容 --> 根据定义规则进行内容匹配 -> 出具检测报告。极狐GitLab 敏感信息检测依旧是通过特定的分析器(analyzer)来完成的,而分析器的核心组件是开源的 Gitleaks。
Gitleaks
Gitleaks 是一款开源的 SAST 工具,可以用来对 hadrcode 的密码、API key、token 等敏感信息做检测。Gitleaks 具有安装容易,使用方便的特点。
Gitleaks 的安装
Gitleaks 的安装有多种方式,本文以 macOS 为例来演示,使用下面的命令即可在 macOS 上安装 Gitleaks:
$ brew install gitleaks
可以使用 gitleaks -h 或者 gitleaks --version 来检查是否安装成功:
$ gitleaks --version
7.6.1
Gitleaks 的使用
Gitleaks 可以直接扫描本地文件,也可以直接扫描远端仓库。
- 使用 Gitleaks 来扫描本地文件
先来看看对于本地文件的扫描。新建一个包含敏感信息的文件,诸如:
$ cat > secret.txt << EOF
password="12232"
token="ADB#@DC"
EOF
添加一个 gitleaks.toml 文件,写入匹配规则:
$ cat > config.toml << EOF
title = "gitleaks config"
[[rules]]
description = "Password Type"
regex = '''[0-9]'''
tags = ["gitlab", "security"]
EOF
gitleaks 的匹配规则是用 TOML 来定义的
利用上述匹配规则,能够对全部是数字的信息进行匹配,并且将其标记为敏感信息,可以用如下命令进行扫描检测:
$ gitleaks --config-path=config.toml \--path=secret.txt --no-git \-v --report=report.json
参数说明:
- –config-path:指定写入了匹配规则的配置文件,一般是 xx.toml
- –path:指定要扫描检测的文件或者目录
- –report:指定输出报告的路径
- –no-git:将扫描目录视为普通目录去扫描检测,否则会去查找 .git 目录,找不到就提示失败
可以看到如下的扫描结果:
{"line": "password=\"12232\"","lineNumber": 1,"offender": "1","offenderEntropy": -1,"commit": "","repo": "","repoURL": "","leakURL": "","rule": "Password Type","commitMessage": "","author": "","email": "","file": ".","date": "0001-01-01T00:00:00Z","tags": "gitlab, security"
}
INFO[0000] scan time: 224 microseconds
WARN[0000] leaks found: 1
结果显示,扫描出一处匹配(leaks found: 1),扫描时长(scan time: 224 microseconds,可以看出非常快)。而这一处泄漏就是第一行 password=“12232”。这和最开始设置匹配规则时候的设想是一样的。
同时会在当前目录下看到一个 report.json 的文件:
$ls -ltr report.json
-rw-r--r-- 1 xiaomage wheel 328 Oct 26 14:24 report.json
里面记录的内容和上述标准输出的内容是一致的。
需要注意的是,如果检测出敏感信息,则扫描命令的退出结果为非 0 值:
$ gitleaks --config-path=config.toml --path=secret.txt --no-git -v --report=report.json
$ echo $?
1
如果想要指定执行命令的退出结果,则可以使用参数 --leaks-exit-code:
$ gitleaks --config-path=config.toml --path=secret.txt --no-git -v --report=report.json --leaks-exit-code=0
$ echo $?
0
- 使用 Gitleaks 扫描远端仓库
将上述的 secret.txt 文件和 gitleaks.toml 文件存放到极狐GitLab 的仓库中,如下:
执行如下命令进行远端仓库扫描:
$ gitleaks --repo-url=git@gitlab.cn:majinghe/secret-detection.git \
--username=jhma@gitla.cn --access-token=personal-token \
--ssh-key=path-to-ssh-key --repo-config-path=gitleaks.toml \
--report=report.json
参数说明:
- –repo-url:指定远端仓库的地址
- –username:扫描私有仓库时指定仓库的用户名
- –access-token:扫描私有仓库时指定的 personal access token,用来做权限验证;如果 --repo-url 是用 https 的方式,则此参数可换成 --password
- –ssh-key:指定访问私有仓库所需的 ssh-key
- –repo-config-path:指定远端仓库中的规则匹配文件
- –report:指定扫描报告的名称
扫描结果如下:
INFO[0000] cloning... git@gitlab.cn:majinghe/secret-detection.git
INFO[0000] scan time: 999 microseconds
INFO[0000] commits scanned: 9
WARN[0000] leaks found: 9
很奇怪的是看到了 9 处检测点,但是文件里面只有一处啊,到底怎么回事呢?
对于 Gitleaks 来讲,如果不做额外的参数指定,则上述扫描命令会对远端仓库的所有 commit 进行扫描,上述仓库有 9 个 commit,所以扫描了 9 次。可以通过参数来制定针对某一个 commit 或者某一个范围的 commit 信息进行扫描。若指定某个 commit 进行扫描,可看到:
$ gitleaks --repo-url=https://gitlab.cn/majinghe/secret-detection.git --username=极狐GitLab-username --password=极狐GitLab-password --repo-config-path=gitleaks.toml --report=report.json --commit=83c4c5e364bc249e410a5aa92716a35da8080111
INFO[0000] cloning... https://gitlab.cn/majinghe/secret-detection.git
INFO[0001] scan time: 204 microseconds
INFO[0001] commits scanned: 1
WARN[0001] leaks found: 2
出现了 2 处匹配检测点,是因为将 gitleaks.toml 也进行了扫描:
{"line": "regex = '''[0-9]'''","lineNumber": 4,"offender": "0","offenderEntropy": -1,"commit": "83c4c5e364bc249e410a5aa92716a35da8080111","repo": "secret-detection.git","repoURL": "https://gitlab.cn/majinghe/secret-detection.git","leakURL": "https://gitlab.cn/majinghe/secret-detection.git/blob/83c4c5e364bc249e410a5aa92716a35da8080111/config.toml#L4","rule": "Password Type","commitMessage": "add files\n","author": "xiaomage","email": "jhma@gitlab.cn","file": "config.toml","date": "2021-10-26T14:38:20+08:00","tags": "gitlab, security"}
可以通过在 gitleaks.toml 中定制化一下匹配规则来只扫描 secret.txt 文件:
title = "gitleaks config"
[[rules]]
description = "Password Type"
file = '''secret.txt'''
regex = '''[0-9]'''
tags = ["gitlab", "security"]
重新扫描检测:
$ gitleaks --repo-url=https://gitlab.cn/majinghe/secret-detection.git --username=jhma@gitlab.cn --password=GitOpsIsMyMainJobIn2021 --repo-config-path=gitleaks.toml --report=report.json --commit=83c4c5e364bc249e410a5aa92716a35da8080111
INFO[0000] cloning... https://gitlab.cn/majinghe/secret-detection.git
INFO[0000] scan time: 130 microseconds
INFO[0000] commits scanned: 1
WARN[0000] leaks found: 1
此次扫描只匹配到一处检测点,内容如下:
[{"line": "password=\"12232\"","lineNumber": 1,"offender": "1","offenderEntropy": -1,"commit": "83c4c5e364bc249e410a5aa92716a35da8080111","repo": "secret-detection.git","repoURL": "https://gitlab.cn/majinghe/secret-detection.git","leakURL": "https://gitlab.cn/majinghe/secret-detection.git/blob/83c4c5e364bc249e410a5aa92716a35da8080111/secret.txt#L1","rule": "Password Type","commitMessage": "add files\n","author": "xiaomage","email": "jhma@gitlab.cn","file": "secret.txt","date": "2021-10-26T14:38:20+08:00","tags": "gitlab, security"}
]
可以看到成功匹配到 secret.txt 里面的敏感信息。
下篇文章我们介绍 gitleaks 在极狐GitLab 中的具体使用。