跳到正文
DEV Community· Tech Legend·· 8 天前AI 評分43

攻擊者如何在你的程式碼儲存庫中找出外洩的雲端憑證,以及如何防止

How Attackers Find Exposed Cloud Credentials in Your Repos — and How to Stop Them

AI 導讀

攻擊者透過自動掃描工具、GitHub 定向搜尋及大規模掃描服務,在公開程式碼儲存庫中尋找外洩的雲端憑證與其他機密資料;憑證推送後,可能在幾分鐘內被發現。防範方法包括使用環境變數或機密管理工具、在提交前及 CI 流程中掃描,以及機密外洩時立即撤銷憑證,再清理 Git 歷史紀錄。

正文

Tech Legend

Introduction

Every fast-moving development team pushes code daily. Somewhere in that flow, a developer pastes an API key into a config file, commits it, and pushes. It takes seconds — and automated scanners can find that key just as fast.

Exposed secrets in code repositories are one of the most common and most preventable ways cloud environments get compromised. This post breaks down what counts as an exposed secret, how attackers find them, and the practical steps that actually reduce the risk.

What counts as an exposed secret

  • Cloud access keys — AWS access key IDs and secret keys, GCP service account JSON, Azure storage account keys.
  • Third-party API keys — Stripe, Twilio, OpenAI, SendGrid, and similar tokens.
  • Private keys — SSH private keys, signing certificates, .pem files.
  • Database connection strings — URLs that embed a username and password.
  • Configuration data — internal endpoints, bucket names, or architecture details that help an attacker move laterally.

How attackers find them

  1. Automated secret scanners. Tools like Gitleaks, TruffleHog, and GitHub's own secret scanning crawl public repositories and match patterns — AWS key formats, Slack tokens, private key headers, and hundreds more.
  2. GitHub dorking. Attackers run targeted searches for filenames and strings: filename:.env, AWS_SECRET_ACCESS_KEY, extension:pem private key.
  3. Mass scanning services. Dedicated services continuously scan the public commit stream and publish or sell what they find. A leaked key can be picked up within minutes of being pushed.

Why it matters

An exposed key is not a theoretical risk:

  • Data exfiltration — a compromised database credential can dump an entire dataset.
  • Cloud bill fraud — stolen cloud keys are routinely used to spin up compute resources at the victim's expense.
  • Lateral movement — a build server or CI credential becomes a beachhead into internal systems.
  • Service impersonation — an API token lets an attacker act as your application.

Prevention that actually works

1. Never hardcode secrets. Use environment variables or a secrets manager:

# Instead of this
aws_secret_access_key = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

# Do this
export AWS_SECRET_ACCESS_KEY="$(aws secretsmanager get-secret-value --secret-id prod/app --query SecretString --output text)"

2. Add pre-commit hooks. Catch secrets before they ever reach the remote:

# Install gitleaks and run it on staged changes
brew install gitleaks
gitleaks protect --staged

3. Enable secret scanning in CI. Run a scanner on every push and block merges that introduce a match.

4. Use a vault for production. AWS Secrets Manager, HashiCorp Vault, or Azure Key Vault inject secrets at runtime instead of baking them into artifacts.

5. If a secret leaks, rotate immediately. Deleting the file in a new commit is not enough — the secret remains in Git history. Revoke the credential first, then scrub history with git filter-repo and force-push.

A quick checklist

  • [ ] No secrets in source code or IaC templates
  • [ ] .env and key files in .gitignore
  • [ ] Pre-commit secret scanning enabled
  • [ ] CI blocks merges with detected secrets
  • [ ] Production secrets come from a vault
  • [ ] Incident runbook: revoke → rotate → scrub history

Further reading

For a deeper look at how mass scanning services hunt exposed secrets in public repositories, see the hSECURITIES analysis: Hackers Enlist Artex to Scan GitHub Repositories for Exposed Secrets.

This article was generated by an AI assistant.

來源:DEV Community · dev.to