linux/ubuntu国内镜像安装gitleaks敏感信息扫描工具教程及避坑点

发布于:2024-09-17 ⋅ 阅读:(135) ⋅ 点赞:(0)

1、背景

利用gitleaks扫描git仓库或者文件

GitHub上有比较详细的教程,但是由于每个人的安装环境不同,坑很多,网上能查到的有效信息也比较少。这里就以我坑很多的环境为例,捋一下步骤。

GitHub - gitleaks/gitleaks: Protect and discover secrets using Gitleaks 🔑

2、步骤

# MacOS
brew install gitleaks

# Docker (DockerHub)
docker pull zricethezav/gitleaks:latest
docker run -v ${path_to_host_folder_to_scan}:/path zricethezav/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]

# Docker (ghcr.io)
docker pull ghcr.io/gitleaks/gitleaks:latest
docker run -v ${path_to_host_folder_to_scan}:/path ghcr.io/gitleaks/gitleaks:latest [COMMAND] --source="/path" [OPTIONS]

# From Source (make sure `go` is installed)
git clone https://github.com/gitleaks/gitleaks.git
cd gitleaks
make build

(1)安装brew

没有用docker,因为本来linux环境里之前就装好brew了。这个装起来也挺烦的,但是考虑到我用过其他很多扫描工具,install的时候也常用brew,所以就装了。一举多得。

首先,手动克隆 Homebrew 的代码库:

bash

复制代码

git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git ~/.linuxbrew/Homebrew

然后,创建一些必要的目录并将 Homebrew 添加到你的 PATH 中:

bash

复制代码

mkdir ~/.linuxbrew 
mkdir ~/.linuxbrew/bin 
ln -s ~/.linuxbrew/Homebrew/bin/brew ~/.linuxbrew/bin 
echo 'export PATH="$HOME/.linuxbrew/bin:$PATH"' >> ~/.bashrc 
source ~/.bashrc
brew update
brew --version  # 确认是否安装好了

可能会有的报错:

①Updating Homebrew... Error: Failed to download https://formulae.brew.sh/api/cask.jws.json!

这可能与网络连接或访问权限有关。

# 切换 Homebrew 主仓库到清华大学镜像
git -C "$(brew --repo)" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git

# 切换 Homebrew 核心库到清华大学镜像
git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-core" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

# 切换 Homebrew Cask 库到清华大学镜像
git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-cask" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-cask.git

# 重新更新 Homebrew
brew update

②fatal: cannot change to '/home/iot/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-core': No such file or directory

这个错误表明 Homebrew 的核心库 (homebrew-core) 还没有被正确克隆或安装。通常情况下,Homebrew 会在第一次安装时自动下载和配置这些库,但由于你的安装过程遇到了一些问题,所以这个目录可能还没有被创建。

可以手动克隆 homebrew-core 仓库到正确的位置,克隆完成后,重新设置远程镜像地址:

mkdir -p "$(brew --repo)/Library/Taps/homebrew/"
git clone https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
git -C "$(brew --repo)/Library/Taps/homebrew/homebrew-core" remote set-url origin https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git

更新

brew update
brew install gitleaks

(2)brew install gitleaks

可能会有的报错:

①Error: gitleaks: Failed to download resource "glibc--bootstrap-binutils" Download failed: https://github.com/Homebrew/glibc-bootstrap/releases/download/1.0.0/bootstrap-binutils-2.38.tar.gz或者Error: gitleaks: Failed to download resource "glibc--bootstrap-gcc" Download failed: https://github.com/Homebrew/glibc-bootstrap/releases/download/1.0.0/bootstrap-gcc-9.5.0.tar.gz,。还是网络权限问题,如果镜像多次连接不上,有网络问题报错比如尝试直接从源代码编译安装:

brew install gitleaks --build-from-source

(3)git clone https://github.com/gitleaks/gitleaks.git

如果还是装不上,尝试手动(官网教程的第四条):

这里因为又要连github,网络不通畅就多试几次,我大概试了8次左右吧。

(4)make build

打开进入安装好的gitleaks目录,输入指令make build

可能会有的报错:

①go fmt ./... go: github.com/BobuSumisu/aho-corasick@v1.0.3: Get https://proxy.golang.org/github.com/%21bobu%21sumisu/aho-corasick/@v/v1.0.3.mod: dial tcp 142.250.217.113:443: connect: connection refused make: *** [Makefile:13: format] Error 1

这个错误表明 go fmt 过程中尝试从 proxy.golang.org 下载依赖包时,因网络问题(例如连接被拒绝)而失败。通常这是由于网络限制、代理设置或防火墙问题导致无法访问外部资源。

1. 设置 Go Modules 代理
可以尝试设置 Go 的代理为国内的代理服务,这通常可以解决网络访问问题。你可以通过以下命令设置 Go Modules 的代理:
export GOPROXY=https://goproxy.cn,direct

再次 make build

②build github.com/zricethezav/gitleaks/v8: cannot load embed: malformed module path "embed": missing dot in first path element make: *** [Makefile:13: format] Error 1

这个错误提示 embed 包无法加载,原因是 Go 的版本过低,因为 embed 包是在 Go 1.16 中引入的。如果你正在使用较低版本的 Go,这个包会导致构建失败。

解决方案:升级GO版本

首先,确认你当前使用的 Go 版本是否为 1.16 及以上。

go version

如果 Go 版本低于 1.16,可以使用以下命令更新 Go

sudo apt update
sudo apt install golang-go

如果已经装了brew也可以尝试

brew update
brew upgrade go

③更新的时候返回golang-go is already the newest version

在这种情况下,你可以手动下载并安装最新版本的 Go。需要先卸载旧版本,再下载新的:

sudo apt remove golang-go
sudo apt remove --auto-remove golang-go
wget https://go.dev/dl/go1.21.1.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz

# 将 Go 的二进制文件路径添加到系统环境变量 PATH 中
export PATH=$PATH:/usr/local/go/bin

④如果用brew upgrade go,要确保之前在brew已经装过了go,否则可能报错Error: go not installed

执行:

brew install go

成功的话会显示Summary……

⑤如果安装完成go version还是没有更新,可能是环境变脸没有配置

编辑你的 .bashrc.zshrc 文件,添加以下内容:

export PATH=$PATH:/home/linuxbrew/.linuxbrew/bin

保存更改

source ~/.bashrc  # 或 `source ~/.zshrc`,取决于你使用的 shell

⑥go fmt ./... go: updates to go.mod needed; to update it: go mod tidy make: *** [Makefile:13: format] Error 1

go.mod 需要更新。

gitleaks 项目根目录下运行以下命令:

go mod tidy

# 之后再运行make build 

⑥出现 gitleaks: command not found 错误,可能是因为 gitleaks 可执行文件没有添加到系统的 PATH 中。

  • 编辑 ~/.bashrc~/.zshrc 文件:
nano ~/.bashrc # 或者 nano ~/.zshrc
  • 在文件末尾添加以下行,将 gitleaks 目录添加到 PATH 中:

    export PATH=$PATH:/path/to/gitleaks   # 这里改成自己的安装路径
  • 保存并重新加载 shell 配置:

source ~/.bashrc # 或者 source ~/.zshrc

(5)开始扫描

具体的指令官方也给出了

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
  -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-color                   turn off color for verbose output
      --no-banner                  suppress banner
      --redact                     redact secrets from logs and stdout
  -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.

我这里以扫描一个本地文件为例:

如果要在终端里面展示扫描结果,--no-git后面加上 --verbose

如果要把结果保存到本地加上--report-path


网站公告

今日签到

点亮在社区的每一天
去签到