Table des matières

Introduction Gitleaks

Ses fonctionnalités donnent une rude concurrence à son prédécesseur trufflehog.

Quelques-unes de ses caractéristiques sont :

Il y a un article sur comment ajouter Gitleaks au pipeline Gitlab par Cyber Defence Lab. Mais le pipeline CI n'est pas tout à fait optimisé. Dans cette expérience, je vais vous montrer comment implémenter Gitleaks dans le pipeline CI de GitLab.

Installation

Gitleaks peut être installé en utilisant Homebrew, Docker, ou Go.

Gitleaks est également disponible sous forme binaire pour de nombreuses plateformes et systèmes d'exploitation populaires sur la page des versions. En outre, Gitleaks peut être implémenté en tant que hook pre-commit directement dans votre repo ou en tant qu'action GitHub en utilisant Gitleaks-Action.

Linux

Téléchargement de Gitleaks

$ curl -s  https://api.github.com/repos/zricethezav/gitleaks/releases/latest | grep browser_download_url  |  cut -d '"' -f 4  | grep '\linux_x64.tar.gz'| wget -i -

On change le nom du binaire, on le rend exécutable et on le déplace dans '/usr/local/bin/'

$ tar xvzf gitleaks_8.10.3_linux_x64.tar.gz 
$ sudo mv gitleaks /usr/local/bin/

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]

ghcr.io

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

Usage

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:
  -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 string       exit code when leaks have been encountered (default: 1)
  -h, --help                   help for gitleaks
  -l, --log-level string       log level (debug, info, warn, error, fatal) (default "info")
      --redact                 redact secrets from logs and stdout
  -f, --report-format string   output format (json, csv, sarif)
  -r, --report-path string     report file
  -s, --source string          path to source (git repo, directory, file)
  -v, --verbose                show verbose output from scan
 
Use "gitleaks [command] --help" for more information about a command.

Configuration de gitleaks sur Gitlab CI

Gitleaks est disponible sous forme d'image docker.

Nous pouvons directement les télécharger et les utiliser sur notre configuration Gitlab CI. Gitleaks scanne tous les commits.

Il peut être optimisé pour scanner les nouveaux commits qui ont été récemment poussés vers une branche particulière.

stages:
  - secrets-detection
 
gitleaks:
  stage: secrets-detection
  image: 
    name: "zricethezav/gitleaks"
    entrypoint: [""]
  script: gitleaks detect -l debug -v .

Exemple pipeline

Voici un output de gitlab-ci pipeline. Le pipeline sort en erreur lors de la détection de credentials AWS.

...
$ gitleaks detect . -v
    ○
    │╲
    │ ○
    ○ ░
    ░    gitleaks
{
	"Description": "AWS",
	"StartLine": 20,
	"EndLine": 20,
	"StartColumn": 29,
	"EndColumn": 48,
	"Match": "AKIAIOSFODNN7EXAMPLE",
	"Secret": "AKIAIOSFODNN7EXAMPLE",
	"File": ".gitlab-ci.yml",
	"Commit": "af16491c9be4f24adf1195275dcd0c145601cc03",
	"Entropy": 3.6841838,
	"Author": "Rachid BOUIKILA",
	"Email": "[MASKED]@gmail.com",
	"Date": "2022-08-09T19:06:31Z",
	"Message": "Update .gitlab-ci.yml file",
	"Tags": [],
	"RuleID": "aws-access-token",
	"Fingerprint": "af16491c9be4f24adf1195275dcd0c145601cc03:.gitlab-ci.yml:aws-access-token:20"
}
7:07PM INF scan completed in 71.998774ms
7:07PM WRN leaks found: 1
Cleaning up project directory and file based variables
00:01
ERROR: Job failed: exit code 1

Annexe

https://gitleaks.io/

https://github.com/zricethezav/gitleaks