Skip to content

Cheatsheet · OSINT · Web pentesting

Detecting secrets in a Git repository with Gitleaks and TruffleHog

Search a client’s Git repositories for secrets with Gitleaks, TruffleHog and git, then handle a finding: rotation first, history rewrite second.

By Steven Carrier Published

Official website: Gitleaks

A committed secret (API key, password, token, private key) remains readable in the history even after the file is deleted. This cheatsheet covers detection on repositories the client owns or has explicitly entrusted to you: Gitleaks, TruffleHog and a few basic git commands. Scope the search (repositories, organisations, branches) with the penetration test scoping checklist; on the OSINT side, the topic complements the exposure assessment methodology. Examples (target.example, alice, example-repo) are fictitious; replace CLIENT_ORG and CLIENT_REPO with the client’s GitHub organisation and repository.

Principle and precautions

These tools read repository contents, so anything they find may be a valid credential. Three rules for the engagement:

  • work on a local clone, never writing to the client’s repository;
  • never use a discovered secret to log in unless the agreement expressly says so;
  • do not copy secrets in clear text into the report: cite the file, the commit and a masked fragment.

Tool versions change: confirm options with gitleaks --help and trufflehog --help before automating anything.

Gitleaks: scan a repository

Recent Gitleaks versions split the modes into subcommands: git (a repository’s history) and dir (files in a directory, no history). The older gitleaks detect command is deprecated in recent versions; check gitleaks --help for what your version offers.

# Full history of a cloned repository (every commit)
gitleaks git --verbose --redact example-repo

# Repository in the current directory
gitleaks git --redact .

# Files in a directory or a single file, without reading Git history
gitleaks dir --redact ./example-repo

# JSON report for follow-up, secrets masked
gitleaks git --redact --report-format json --report-path gitleaks-report.json example-repo

--redact masks secrets in the output and report: enable it by default. --verbose prints every finding (rule, file, commit, author, date). Gitleaks returns a non-zero exit code (1 by default) when it finds secrets, which is useful in continuous integration; --exit-code changes it.

# Restrict to a range of commits (options passed to git log)
gitleaks git --redact --log-opts="--all --since=2026-01-01" example-repo

# Skip findings already triaged (baseline from a previous report)
gitleaks git --redact --baseline-path previous-report.json example-repo

# Custom configuration (added rules, allowlists)
gitleaks git --redact --config gitleaks.toml example-repo

Gitleaks detects using patterns and entropy: expect false positives (documentation examples, test tokens, public identifiers) and false negatives (secrets without a recognisable format).

TruffleHog: repositories, organisations and files

TruffleHog scans a Git repository, a GitHub organisation or a filesystem, and can check whether a detected secret is still valid.

# Local repository (full history)
trufflehog git file://./example-repo

# A client remote repository, reachable with the access supplied
trufflehog git https://git.target.example/team/example-repo.git

# A client GitHub organisation or repository (read-only token supplied by the client)
export GITHUB_TOKEN=...
trufflehog github --org=CLIENT_ORG
trufflehog github --repo=https://github.com/CLIENT_ORG/CLIENT_REPO

# Files in a directory
trufflehog filesystem ./example-repo

# JSON output
trufflehog git file://./example-repo --json > trufflehog-report.json

Use a GitHub token with minimal scope (repository read) and revoke it at the end of the engagement.

By default, TruffleHog tries to verify each detected secret by sending it to the service concerned (for example a cloud provider or messaging API) to learn whether it is valid. That changes the nature of the action:

  • you send authentication requests to third-party services, from your IP address, using a credential that is not yours;
  • these attempts show up in the provider’s logs and may trigger alerts on the client’s side;
  • the audit agreement and the third-party service’s terms may not allow this use.
# No verification: the secret is not sent to third parties (purely local analysis)
trufflehog git file://./example-repo --no-verification

# Keep only secrets verified as valid (calls third-party APIs)
trufflehog git file://./example-repo --only-verified

The choice is a point for the agreement: settle it in writing. Without the client’s explicit authorisation for verification, run with --no-verification and let the client validate or revoke the secrets themselves. Note that --only-verified also hides secrets that cannot be verified: an empty result therefore does not prove there are no secrets.

Complement with git

A few commands are enough to check a finding or search for a specific pattern.

# Search for a pattern across the whole history (all commits)
git log --all -p -S'AKIA' --oneline

# Find when a sensitive file was added or deleted
git log --all --diff-filter=AD --name-status -- '*.env' '*.pem' '*id_rsa*'

# Search for a pattern in the tree of every commit
git grep -n -i 'password' $(git rev-list --all) -- '*.yml' | head -50

# Who introduced the line, and when
git blame -L 12,12 path/to/file.conf

git log -S returns commits that add or remove the string; git grep over git rev-list --all can be very slow on a large repository, so limit it by path. Also look for risky file names: .env, *.pem, id_rsa, terraform.tfstate, *.kdbx, credentials.

What to do with a finding

  1. Qualify: is the secret real, still active, and what does it allow (scope of its permissions)? Have the client check, rather than doing so yourself.
  2. Revoke and replace: rotating the secret is the measure that matters. Deleting the file or the last commit is not enough.
  3. Look for use: ask the client to review the relevant service’s logs since the commit date to detect illegitimate use.
  4. Rewrite history if needed: with git filter-repo (recommended today over git filter-branch), followed by a force push and invalidation of clones and forks. It is a heavy operation and never sufficient on its own: existing copies and the hosting provider’s caches may keep the old content. See your host’s documentation.
  5. Prevent recurrence: pre-commit hook, continuous-integration scanning, a secrets manager, ignored configuration files (.gitignore).

In the report

  • State the rule triggered, repository, file, commit, author and date, without the secret in clear text.
  • Say whether the secret was verified, by whom and how; otherwise record it as "unverified".
  • Rate severity by what the secret allows, not by its mere presence: a permissionless test token does not carry the weight of a production administration key.
  • Recommend rotation first, then history clean-up and prevention.

Sources

Related resources

  • AI prompt · Web pentesting

    API test plan prompt

    Prompt that builds an API test plan from an OpenAPI excerpt: endpoints by risk, authorisation matrix and tests per OWASP API Top 10 category.

    Published

  • AI prompt · Infrastructure and AD pentesting · OSINT · Cloud

    Attack surface prompt

    Prompt that analyses an asset inventory from the authorised scope (domains, IPs, services, cloud) to prioritise testing: exposure, criticality, checks.

    Published

  • Cheatsheet · Web pentesting

    Burp Suite cheatsheet

    Burp Suite for an authorised web test: proxy and CA certificate, scope, HTTP history, Repeater, careful Intruder use, Decoder and evidence export.

    Published

BEAROPS

Need an OSINT investigation?

BEAROPS carries out OSINT investigations lasting from a few days to several weeks, using open sources within a legal framework.