
コードベースとGCPプロジェクトをスキャンして、公開されたAPI認証情報を検出します。
コードベース、GCPプロジェクト、CIパイプラインをスキャンして、公開されたGoogle API認証情報を悪用される前に発見します。
問題: Googleは、公開用に設計され、クライアントサイドコードに埋め込まれていた既存のAPIキー(Maps、Placesなど)に対して、Gemini APIアクセスを事後的に有効化しました。チームメンバーが同じGCPプロジェクトでGeminiを有効にすると、すでに公開されていたキーが静かにGemini認証情報となり、プロジェクトが不正なAI使用や8万ドル以上の課金インシデントにさらされることになります(コンテキスト)。
keyguardの機能:
keyguard scan — 正規表現とエントロピー検出を使用して、ソースファイルとgit履歴から認証情報文字列を検出keyguard audit — 稼働中のGCPプロジェクトに接続し、現在Geminiアクセス権を持つAPIキーをフラグ付けkeyguard ci — GitHub Actions、CircleCI、GitLab CIのログと変数をスキャンし、漏洩した認証情報を検出pip install keyguard-scan
または、リポジトリをクローンして開発モードでインストール:
git clone https://github.com/arzaan789/keyguard.git
cd keyguard
pip install -e ".[dev]"
# カレントディレクトリとgit履歴をスキャン
keyguard scan .
# 稼働中のGCPプロジェクトを監査
keyguard audit
# CIプラットフォームをスキャン
keyguard ci
keyguard scan正規表現とShannonエントロピーフィルタリングを使用して、ソースファイルとgit履歴から公開された認証情報をスキャンします。"REPLACE_ME"や"XXXXXXXX"のような低エントロピーのプレースホルダーは自動的に無視されます。
# ディレクトリをスキャン(ファイル+完全なgit履歴)
keyguard scan .
# ファイルのみ、git履歴はスキップ
keyguard scan . --no-git-history
# JSONとSARIFにエクスポート
keyguard scan . --output json --output sarif --out-file report
# 実際のキー値を表示(伏せ字なし)
keyguard scan . --no-redact
# カスタム設定ファイルを使用
keyguard scan . --config /path/to/.keyguard.toml
終了コード: 0 = 問題なし, 1 = 発見あり, 2 = エラー
keyguard auditCloud Resource Manager、Service Usage、API Keys APIを介して、稼働中のGCPプロジェクトに接続します。無制限のキー(サイレントGeminiアクセス)や、プロジェクトでGeminiが有効になっている状態でgenerativelanguage.googleapis.comを明示的に許可しているキーをフラグ付けします。
認証はデフォルトでアプリケーションデフォルト認証情報を使用します。最初にgcloud auth application-default loginを実行してください。
# アクセス可能なすべてのGCPプロジェクトを監査
keyguard audit
# 特定のプロジェクトを監査
keyguard audit --project my-project-id --project another-project
# サービスアカウントキーファイルを使用
keyguard audit --gcp-credentials /path/to/key.json
# JSONで結果をエクスポート
keyguard audit --output json --out-file gcp-findings.json
発見:
CRITICAL — キーにAPI制限がなく、Geminiが有効(Maps→Geminiへのサイレントアップグレードシナリオ)HIGH — キーが明示的にgenerativelanguage.googleapis.comを許可(意図的だが、クライアントコードに埋め込まれている可能性あり)keyguard ciCIプラットフォームのログと保存された変数をスキャンし、公開された認証情報を検出。GitHub Actions、CircleCI、GitLab CIをサポート。
# 設定されたすべてのプラットフォームをスキャン
keyguard ci
# 1つのプラットフォームのみスキャン
keyguard ci --platform github
# 特定のリポジトリに絞り込む
keyguard ci --repo my-org/api-service
# JSONで結果をエクスポート
keyguard ci --output json --out-file ci-findings.json
スキャン対象:
keyguard watchファイル変更時に再スキャンします。開発中に便利です。
keyguard watch .
keyguard rules listアクティブなすべての検出ルールを一覧表示します。
keyguard rules list
keyguard config check.keyguard.toml設定ファイルを検証します。
keyguard config check
keyguard config check --config /path/to/.keyguard.toml
プロジェクトルートに.keyguard.tomlを作成:
[scan]
paths = ["."]
exclude = ["tests/fixtures/", "**/*.example"]
scan_git_history = true
[output]
format = ["terminal", "json"]
redact = true
[notify]
slack_webhook = "https://hooks.slack.com/services/..."
[rules]
disabled = []
# CIプラットフォームの認証とスコープ
[ci]
github_token = "ghp_xxxxxxxxxxxxxxxxxxxx"
circleci_token = "CCIPAT_xxxxxxxxxxxxxxxx"
gitlab_token = "glpat-xxxxxxxxxxxxxxxxxxxx"
gitlab_url = "https://gitlab.com" # セルフホストGitLab用に上書き
max_runs = 10 # リポジトリあたりの最近の実行/パイプライン数
[ci.github]
orgs = ["my-org"]
repos = ["my-org/specific-repo"] # オプション: 特定のリポジトリのみスキャン
[ci.circleci]
orgs = ["my-org"]
[ci.gitlab]
groups = ["my-group"]
Keyguardは正規表現 + Shannonエントロピーアプローチを使用します。各ルールは以下を定義します:
AIza[0-9A-Za-z\-_]{35})組み込みルールで検出されるもの:
| ルールID | 検出対象 |
|---|---|
google-api-key | Google APIキー (AIza...) — 静かにGeminiアクセスを付与されたMapsキーを含む |
gcp-service-account-key | GCPサービスアカウントRSA秘密鍵 |
.keyguard.tomlでカスタムルールを追加できます:
[[rules.extra]]
id = "my-internal-token"
description = "内部サービス用トークン"
pattern = "tok-[0-9a-f]{32}"
entropy_min = 3.5
severity = "high"
tags = ["internal"]
name: keyguard scan
on: [push, pull_request]
jobs:
scan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # gitスキャン用の完全な履歴
- run: pip install keyguard-scan
- run: keyguard scan .
# .pre-commit-config.yaml
repos:
- repo: local
hooks:
- id: keyguard
name: keyguard credential scan
entry: keyguard scan --no-git-history
language: system
pass_filenames: false
docker run --rm -v $(pwd):/repo ghcr.io/arzaan789/keyguard scan /repo
端末 (デフォルト) — 重要度でグループ化された色付きテーブル。
JSON — 機械可読な検出結果配列:
keyguard scan . --output json --out-file findings.json
SARIF — GitHubのSecurityタブやその他のSASTツールと統合:
keyguard scan . --output sarif --out-file findings.sarif
Slack webhook — 検出結果が見つかったときにサマリーを投稿:
[notify]
slack_webhook = "https://hooks.slack.com/services/..."
git clone https://github.com/arzaan789/keyguard.git
cd keyguard
pip install -e ".[dev]"
pytest
143 テスト、0 失敗。
プロジェクト構造:
keyguard/
scanner/ # ファイル + git履歴スキャナー
engine/ # 正規表現 + エントロピー検出 (ルール、マッチャー)
output/ # 端末、JSON/SARIF、webhook
auditor/ # GCP APIクライアント + 監査ロジック
ci/ # GitHub Actions、CircleCI、GitLab CIスキャナー
cli.py # Click CLIエントリーポイント
config.py # .keyguard.tomlローダー
MIT
google-oauth-client-secret | Google OAuth2クライアントシークレット (GOCSPX-...) |