广州英文建站公司,晋中公司做网站,企业做网站一般要多少钱,企业培训网站前言 在应用程序开发过程中#xff0c;一个很常见的问题就是#xff1a;开发人员为了本地 debug 方便#xff0c;会 hardcode 一些信息#xff0c;比如连接数据库的用户名、密码、连接第三方 app 的 token、certificate 等#xff0c;如果在提交代码的时候没有及时删除 ha…前言 在应用程序开发过程中一个很常见的问题就是开发人员为了本地 debug 方便会 hardcode 一些信息比如连接数据库的用户名、密码、连接第三方 app 的 token、certificate 等如果在提交代码的时候没有及时删除 hardcode 的信息则非常容易造成敏感信息泄漏带来被拖库、撞库等风险。
因此敏感信息管理是 DevSecOps 中一个非常重要的话题。诚然敏感信息的管理需要软件开发中的每一个人来负责有安全意识但是是人就总有疏忽犯错的时候所以最好的方式之一就是将敏感信息的检测集成到 CI/CD 中做到持续监测而且能在开发人员提交代码的时候就进行真正做到安全左移。
极狐GitLab 提供开箱即用的 DevSecOps 功能包括七大功能容器镜像扫描、静态应用安全测试 (SAST)、动态应用安全扫描DAST、密钥检测、License合规、依赖项扫描以及模糊测试。关于镜像安全扫描可以查看文章极狐GitLab DevSecOps 七剑下天山之容器镜像安全扫描。这篇文章将介绍极狐GitLab DevSecOps 中关于敏感信息检测的功能。 极狐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.1Gitleaks 的使用
Gitleaks 可以直接扫描本地文件也可以直接扫描远端仓库。 使用 Gitleaks 来扫描本地文件
先来看看对于本地文件的扫描。新建一个包含敏感信息的文件诸如
$ cat secret.txt EOF
password12232
tokenADB#DC
EOF添加一个 gitleaks.toml 文件写入匹配规则
$ cat config.toml EOF
title gitleaks config
[[rules]]
description Password Type
regex [0-9]
tags [gitlab, security]
EOFgitleaks 的匹配规则是用 TOML 来定义的。
利用上述匹配规则能够对全部是数字的信息进行匹配并且将其标记为敏感信息可以用如下命令进行扫描检测
$ gitleaks --config-pathconfig.toml \--pathsecret.txt --no-git \-v --reportreport.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可以看出非常快。而这一处泄漏就是第一行 password12232。这和最开始设置匹配规则时候的设想是一样的。
同时会在当前目录下看到一个 report.json 的文件
$ls -ltr report.json
-rw-r--r-- 1 xiaomage wheel 328 Oct 26 14:24 report.json里面记录的内容和上述标准输出的内容是一致的。
需要注意的是如果检测出敏感信息则扫描命令的退出结果为非 0 值
$ gitleaks --config-pathconfig.toml --pathsecret.txt --no-git -v --reportreport.json
$ echo $?
1如果想要指定执行命令的退出结果则可以使用参数 --leaks-exit-code
$ gitleaks --config-pathconfig.toml --pathsecret.txt --no-git -v --reportreport.json --leaks-exit-code0
$ echo $?
0使用 Gitleaks 扫描远端仓库
将上述的 secret.txt 文件和 gitleaks.toml 文件存放到极狐GitLab 的仓库中如下 执行如下命令进行远端仓库扫描
$ gitleaks --repo-urlgitgitlab.cn:majinghe/secret-detection.git \
--usernamejhmagitla.cn --access-tokenpersonal-token \
--ssh-keypath-to-ssh-key --repo-config-pathgitleaks.toml \
--reportreport.json参数说明 --repo-url指定远端仓库的地址 --username扫描私有仓库时指定仓库的用户名 --access-token扫描私有仓库时指定的 personal access token用来做权限验证如果 --repo-url 是用 https 的方式则此参数可换成 --password --ssh-key指定访问私有仓库所需的 ssh-key --repo-config-path指定远端仓库中的规则匹配文件 --report指定扫描报告的名称
扫描结果如下
INFO[0000] cloning... gitgitlab.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-urlhttps://gitlab.cn/majinghe/secret-detection.git --username极狐GitLab-username --password极狐GitLab-password --repo-config-pathgitleaks.toml --reportreport.json --commit83c4c5e364bc249e410a5aa92716a35da8080111
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: jhmagitlab.cn,file: config.toml,date: 2021-10-26T14:38:2008: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-urlhttps://gitlab.cn/majinghe/secret-detection.git --usernamejhmagitlab.cn --passwordGitOpsIsMyMainJobIn2021 --repo-config-pathgitleaks.toml --reportreport.json --commit83c4c5e364bc249e410a5aa92716a35da8080111
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: jhmagitlab.cn,file: secret.txt,date: 2021-10-26T14:38:2008:00,tags: gitlab, security}
]可以看到成功匹配到 secret.txt 里面的敏感信息。
当然Gitleaks 还有很多其他的参数来进行多种细粒度的扫描。 极狐GitLab 敏感信息检测实践 单独使用镜像做扫描
极狐GitLab 敏感信息检测是通过分析器analyzer来实现的而 analyzer 的核心就是 Gitleaks。analyzer 的镜像为
registry.gitlab.com/security-products/secret-detection:3可以直接使用此镜像来完成敏感信息扫描。将上面测试用的包含 secret.txt 和 gitleaks.toml 文件的仓库 clone 到本地然后执行下面的命令启动一个容器
$ docker run --rm -d -it -v $(pwd):/tmp/ registry.gitlab.com/security-products/secret-detection:3 sh可以用 docker exec -it 进入容器在里面可以看到容器里面已经安装了 Gitleaks 和 anaylyzer
$ gitleaks --version
v7.5.0
$ ./analyzer --version
[INFO] [secrets] [2021-10-27T01:27:43Z] ▶ GitLab secrets analyzer v3.22.0
analyzer version 3.22.0analyzer 就是极狐GitLab 执行敏感信息检测的法宝其本身是对 gitleaks 做了封装。大体思路就是读取 analyzer 命令后面的参数然后拼装成 gitleaks 命令再执行命令。
将 /tmp/gitleaks.tmol 文件拷贝至根目录执行如下命令即可完成敏感信息扫描
$ ./analyzer run --target-dir /tmp/secret.txt
[INFO] [secrets] [2021-10-27T01:25:46Z] ▶ GitLab secrets analyzer v3.22.0
[INFO] [secrets] [2021-10-27T01:25:46Z] ▶ Detecting project
[INFO] [secrets] [2021-10-27T01:25:46Z] ▶ Found project in /tmp[INFO] [secrets] [2021-10-27T01:25:46Z] ▶ Running analyzer
[INFO] [secrets] [2021-10-27T01:25:47Z] ▶ Creating report拷贝的原因是 analyzer 默认读取根目录下自带的 gitleaks.toml 文件。为了匹配到测试使用的敏感信息对匹配规则做了一些定制化改动。
查看扫描报告
$ cat tmp/gitleaks-100398148.json
[{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}
]可以看到结果和用 gitleaks 命令扫描是一样的。关于 analyzer 更多的用法可以使用 analyzer h 查看
$ ./analyzer h
[INFO] [secrets] [2021-10-27T01:32:42Z] ▶ GitLab secrets analyzer v3.22.0
NAME:analyzer - GitLab secrets analyzer v3.22.0USAGE:analyzer [global options] command [command options] [arguments...]VERSION:3.22.0AUTHOR:GitLabCOMMANDS:run, r Run the analyzer on detected project and generate a compatible artifactsearch, s Search for compatible projects and return project directoryanalyze, a Analyze detected project and generate reportconvert, c Convert analyzer output to a compatible artifacthelp, h Shows a list of commands or help for one commandGLOBAL OPTIONS:--help, -h show help (default: false)--version, -v print the version (default: false)当然analyzer 很灵活的一点就是可以自己来根据极狐GitLab 提供的 Dockerfile 来构建自己所需的景象内容如下
FROM golang:1.15-alpine AS buildENV CGO_ENABLED0 GOOSlinux
WORKDIR /go/src/app
COPY . .
# build the analyzer binary and automatically set the AnalyzerVersion
# variable to the most recent version from the CHANGELOG.md file
RUN CHANGELOG_VERSION$(grep -m 1 ^## v.*$ CHANGELOG.md | sed s/## v//) \PATH_TO_MODULEgo list -m \go build -ldflags-X $PATH_TO_MODULE/metadata.AnalyzerVersion$CHANGELOG_VERSION -o analyzerFROM alpine:latestARG SCANNER_VERSION
ENV SCANNER_VERSION ${SCANNER_VERSION:-v7.5.0}RUN wget -O /usr/local/bin/gitleaks https://github.com/zricethezav/gitleaks/releases/download/${SCANNER_VERSION}/gitleaks-linux-amd64 \chmod ax /usr/local/bin/gitleaks \apk add --no-cache gitCOPY --frombuild --chownroot:root /go/src/app/analyzer /
COPY /gitleaks.toml /gitleaks.tomlENTRYPOINT []
CMD [/analyzer, run]和极狐GitLab CI 集成使用
只需要简单配置极狐GitLab CI即可将敏感信息检测集成到极狐GitLab CI/CD 中
variables:SECURE_ANALYZERS_PREFIX: registry.gitlab.com/security-productsSECRETS_ANALYZER_VERSION: 3services:- docker:20.10.7-dindstages: - testsecret_detection:stage: testimage: $SECURE_ANALYZERS_PREFIX/secret-detection:$SECRETS_ANALYZER_VERSIONservices: []allow_failure: trueartifacts:reports:secret_detection: gl-secret-detection-report.jsonpaths: [gl-secret-detection-report.json]script:- cp config/gitleaks.toml /- ../../../analyzer run --target-dir . - cat gl-secret-detection-report.json可以查看构建日志 内容过长只截取了部分可以从红色方框看到扫描的过程以及扫描报告。因为在极狐GitLab CI 中加了 artifacts 关键字所以也可以直接下载报告到本地进行查阅下载方式可以参考公众号文章极狐GitLab DevSecOps 七剑下天山之容器镜像安全扫描。
极狐GitLab DevSecOps CI/CD
可以将容器镜像扫描、敏感信息检测加入极狐GitLab CI 中打造 DevSecOps CI/CD
variables:CS_ANALYZER_IMAGE: registry.gitlab.com/security-products/container-scanning/trivy:4KUBECONFIG: /tmp/.kube/configSECURE_ANALYZERS_PREFIX: registry.gitlab.com/security-productsSECRETS_ANALYZER_VERSION: 3services:- docker:20.10.7-dindstages: - build- test- deploybuild:image: docker:lateststage: buildservices:- docker:20.10.7-dindscript:- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY- docker build -t $CI_REGISTRY_IMAGE:3.0.0 .- docker push $CI_REGISTRY_IMAGE:3.0.0container_scanning:image: $CS_ANALYZER_IMAGEstage: testvariables:GIT_STRATEGY: fetchDOCKER_USER: $CI_REGISTRY_USERDOCKER_PASSWORD: $CI_REGISTRY_PASSWORDDOCKER_IMAGE: $CI_REGISTRY_IMAGE:3.0.0allow_failure: falseartifacts:reports:container_scanning: gl-container-scanning-report.jsonpaths: [gl-container-scanning-report.json]dependencies: []script:- gtcs scansecret_detection:stage: testimage: $SECURE_ANALYZERS_PREFIX/secret-detection:$SECRETS_ANALYZER_VERSIONservices: []allow_failure: trueartifacts:reports:secret_detection: gl-secret-detection-report.jsonpaths: [gl-secret-detection-report.json]script:- cp gitleaks.toml /- ../../../analyzer run --target-dir . - cat gl-secret-detection-report.jsondeploy:stage: deployimage:name: bitnami/kubectl:latestentrypoint: []script:- mkdir -p /tmp/.kube- echo $kube_config | base64 -d $KUBECONFIG- kubectl -n gitlab-k8s-demo apply -f deployment.yaml可以查看 Pipeline 的构建流程 上述流程是极狐GitLab DevSecOps 中关于敏感信息检测的原理及演示但是实际使用中需要用户根据自身的需求完成更复杂的配置、集成而对于这些极狐GitLab 都有很好的支持。 写在最后 敏感信息检测其实是一个相对而言比较繁琐的安全工作需要根据特定的敏感信息不同厂商、不同产品的 API tokenkeys 等都有所不同编写特定的匹配工作。可以将敏感信息检测集成到极狐GitLab CI/CD 中实现安全左移、安全持续自动化利用极狐GitLab 构建真正的 DevSecOps 体系。从而来保证软件供应链的安全。