Skip to content
KitploitKITPLOIT
ツールブログ
提出
ツールブログ
提出

ハッキング、侵入テスト、サイバーセキュリティツールをあなたのセキュリティアーセナルに!

Kitploitはハッキング、サイバーセキュリティ、ペネトレーションテストのツールディレクトリです。最新のプロジェクトアップデートを見つけて、脆弱性の発見、システム分析、テストの自動化、セキュリティの強化を行いましょう。

··フィード·お問い合わせ·プライバシー·© 2026 Kitploit

ツールディレクトリ

カテゴリ

すべてのカテゴリを見る
Loading categories
detect-secrets — コード内のシークレットを検出・防止するためのエンタープライズ向けの方法。 | Kitploit
ツール/GitHubGitHub/yelp/detect-secrets
静的分析コード分析DevSecOpsシークレット検出シークレット検出 第3位
GitHubyelp/detect-secrets

detect-secrets

コード内のシークレットを検出・防止するためのエンタープライズ向けの方法。

リポジトリを見る
4.6k564105ヶ月前Kitploit レビュー済み

人気

すべて見る →

コミュニティで最も使われているツールを見つけましょう。

すべてのツールを探索

ツールコレクションを閲覧

すべてのツールを見る →
共有

Build Status PyPI version Homebrew PRs Welcome AMF

detect-secrets

概要

detect-secrets は、コードベース内のシークレットを検出するための、適切に名付けられた(予想通り)モジュールです。

しかし、シークレットの発見のみに焦点を当てている他の同様のパッケージとは異なり、このパッケージはエンタープライズクライアントを念頭に置いて設計されています。後方互換性のある体系的な方法を提供します:

  1. 新しいシークレットがコードベースに入るのを防ぐ
  2. そのような防止策が明示的にバイパスされたかどうかを検出する
  3. ローテーションするシークレットのチェックリストを提供し、より安全なストレージに移行する

このようにして、関心の分離を実現します。大規模なリポジトリに現在シークレットが隠れている可能性があることを認め(これを_ベースライン_と呼びます)、しかし、この問題がさらに拡大するのを防ぎつつ、既存のシークレットを移動するという潜在的に膨大な作業に対処することはありません。

これは、ヒューリスティックに作成された正規表現に対して定期的な差分出力を実行することで、新しいシークレットがコミットされたかどうかを識別します。これにより、すべてのgit履歴を掘り起こすオーバーヘッドを回避し、毎回リポジトリ全体をスキャンする必要もなくなります。

最近の変更については、CHANGELOG.md をご覧ください。

コントリビューションを検討されている場合は、CONTRIBUTING.md をご覧ください。

より詳細なドキュメントについては、他のドキュメントをご確認ください。

例

クイックスタート:

現在Gitリポジトリで見つかった潜在的なシークレットのベースラインを作成します。```bash $ detect-secrets scan > .secrets.baseline

root@kitploit:~
または、別のディレクトリから実行する場合:```bash
$ detect-secrets -C /path/to/directory scan > /path/to/directory/.secrets.baseline

Git追跡対象外のファイルをスキャン:```bash $ detect-secrets scan test_data/ --all-files > .secrets.baseline

root@kitploit:~
### ベースラインへの新しいシークレットの追加:

これにより、コードベースが再スキャンされ、以下の処理が行われます:

1. 最新バージョンと互換性を持つようにベースラインを更新/アップグレードします。
2. 見つかった新しいシークレットをベースラインに追加します。
3. コードベースに存在しなくなったシークレットを削除します。

これにより、ラベル付けされたシークレットも保持されます。```bash
$ detect-secrets scan --baseline .secrets.baseline

バージョン0.9より古いベースラインの場合は、単に再作成してください。

新たに追加されたシークレットのアラートをオフにする:

ステージングされたファイルのみをスキャン:```bash $ git diff --staged --name-only -z | xargs -0 detect-secrets-hook --baseline .secrets.baseline

root@kitploit:~
**すべての追跡ファイルをスキャン中:**```bash
$ git ls-files -z | xargs -0 detect-secrets-hook --baseline .secrets.baseline

すべての有効なプラグインの表示:```bash

$ detect-secrets scan --list-all-plugins ArtifactoryDetector AWSKeyDetector AzureStorageKeyDetector BasicAuthDetector CloudantDetector DiscordBotTokenDetector GitHubTokenDetector GitLabTokenDetector Base64HighEntropyString HexHighEntropyString IbmCloudIamDetector IbmCosHmacDetector IPPublicDetector JwtTokenDetector KeywordDetector MailchimpDetector NpmDetector OpenAIDetector PrivateKeyDetector PypiTokenDetector SendGridDetector SlackDetector SoftlayerDetector SquareOAuthDetector StripeDetector TelegramBotTokenDetector TwilioKeyDetector

root@kitploit:~
### プラグインの無効化:```bash
$ detect-secrets scan --disable-plugin KeywordDetector --disable-plugin AWSKeyDetector

特定のプラグインのみを実行したい場合は、次のようにします:```bash $ detect-secrets scan --list-all-plugins |
grep -v 'BasicAuthDetector' |
sed "s#^#--disable-plugin #g" |
xargs detect-secrets scan test_data

root@kitploit:~
### ベースラインの監査:

これは、ベースラインの結果にラベルを付けるためのオプションのステップです。これを使用して、移行するシークレットのチェックリストを絞り込んだり、プラグインをより適切に設定してシグナル対ノイズ比を向上させたりすることができます。```bash
$ detect-secrets audit .secrets.baseline

他のPythonスクリプトでの使用法

基本的な使い方:```python from detect_secrets import SecretsCollection from detect_secrets.settings import default_settings

secrets = SecretsCollection() with default_settings(): secrets.scan_file('test_data/config.ini')

import json print(json.dumps(secrets.json(), indent=2))

root@kitploit:~
**より高度な設定:**```python
from detect_secrets import SecretsCollection
from detect_secrets.settings import transient_settings

secrets = SecretsCollection()
with transient_settings({
    # Only run scans with only these plugins.
    # This format is the same as the one that is saved in the generated baseline.
    'plugins_used': [
        # Example of configuring a built-in plugin
        {
            'name': 'Base64HighEntropyString',
            'limit': 5.0,
        },

        # Example of using a custom plugin
        {
            'name': 'HippoDetector',
            'path': 'file:///Users/aaronloo/Documents/github/detect-secrets/testing/plugins.py',
        },
    ],

    # We can also specify whichever additional filters we want.
    # This is an example of using the function `is_identified_by_ML_model` within the
    # local file `./private-filters/example.py`.
    'filters_used': [
        {
            'path': 'file://private-filters/example.py::is_identified_by_ML_model',
        },
    ]
}) as settings:
    # If we want to make any further adjustments to the created settings object (e.g.
    # disabling default filters), we can do so as such.
    settings.disable_filters(
        'detect_secrets.filters.heuristic.is_prefixed_with_dollar_sign',
        'detect_secrets.filters.heuristic.is_likely_id_string',
    )

    secrets.scan_file('test_data/config.ini')

インストール```bash

$ pip install detect-secrets ✨🍰✨

root@kitploit:~
[brew](https://brew.sh/)経由でインストール:```bash
$ brew install detect-secrets

使い方

detect-secrets には3つの異なるツールが用意されており、どれを使うべきか迷うことがよくあります。以下の便利なチェックリストを参考に、適切なツールを選んでください。

  1. ベースラインにシークレットを追加したい場合: detect-secrets scan を使用します。
  2. ベースラインにない新しいシークレットを警告したい場合: detect-secrets-hook を使用します。
  3. ベースライン自体を分析したい場合: detect-secrets audit を使用します。

ベースラインへのシークレットの追加```

$ detect-secrets scan --help usage: detect-secrets scan [-h] [--string [STRING]] [--only-allowlisted] [--all-files] [--baseline FILENAME] [--force-use-all-plugins] [--slim] [--list-all-plugins] [-p PLUGIN] [--base64-limit [BASE64_LIMIT]] [--hex-limit [HEX_LIMIT]] [--disable-plugin DISABLE_PLUGIN] [-n | --only-verified] [--exclude-lines EXCLUDE_LINES] [--exclude-files EXCLUDE_FILES] [--exclude-secrets EXCLUDE_SECRETS] [--word-list WORD_LIST_FILE] [-f FILTER] [--disable-filter DISABLE_FILTER] [path [path ...]]

Scans a repository for secrets in code. The generated output is compatible with detect-secrets-hook --baseline.

positional arguments: path Scans the entire codebase and outputs a snapshot of currently identified secrets.

optional arguments: -h, --help show this help message and exit --string [STRING] Scans an individual string, and displays configured plugins' verdict. --only-allowlisted Only scans the lines that are flagged with allowlist secret. This helps verify that individual exceptions are indeed non-secrets.

scan options: --all-files Scan all files recursively (as compared to only scanning git tracked files). --baseline FILENAME If provided, will update existing baseline by importing settings from it. --force-use-all-plugins If a baseline is provided, detect-secrets will default to loading the plugins specified by that baseline. However, this may also mean it doesn't perform the scan with the latest plugins. If this flag is provided, it will always use the latest plugins --slim Slim baselines are created with the intention of minimizing differences between commits. However, they are not compatible with the audit functionality, and slim baselines will need to be remade to be audited.

plugin options: Configure settings for each secret scanning ruleset. By default, all plugins are enabled unless explicitly disabled.

--list-all-plugins Lists all plugins that will be used for the scan. -p PLUGIN, --plugin PLUGIN Specify path to custom secret detector plugin. --base64-limit [BASE64_LIMIT] Sets the entropy limit for high entropy strings. Value must be between 0.0 and 8.0, defaults to 4.5. --hex-limit [HEX_LIMIT] Sets the entropy limit for high entropy strings. Value must be between 0.0 and 8.0, defaults to 3.0. --disable-plugin DISABLE_PLUGIN Plugin class names to disable. e.g. Base64HighEntropyString

filter options: Configure settings for filtering out secrets after they are flagged by the engine.

-n, --no-verify Disables additional verification of secrets via network call. --only-verified Only flags secrets that can be verified. --exclude-lines EXCLUDE_LINES If lines match this regex, it will be ignored. --exclude-files EXCLUDE_FILES If filenames match this regex, it will be ignored. --exclude-secrets EXCLUDE_SECRETS If secrets match this regex, it will be ignored. --word-list WORD_LIST_FILE Text file with a list of words, if a secret contains a word in the list we ignore it. -f FILTER, --filter FILTER Specify path to custom filter. May be a python module path (e.g. detect_secrets.filters.common.is_invalid_file) or a local file path (e.g. file://path/to/file.py::function_name). --disable-filter DISABLE_FILTER Specify filter to disable. e.g. detect_secrets.filters.common.is_invalid_file

root@kitploit:~
### ベースラインにないシークレットのブロック```
$ detect-secrets-hook --help
usage: detect-secrets-hook [-h] [-v] [--version] [--baseline FILENAME]
                           [--list-all-plugins] [-p PLUGIN]
                           [--base64-limit [BASE64_LIMIT]]
                           [--hex-limit [HEX_LIMIT]]
                           [--disable-plugin DISABLE_PLUGIN]
                           [-n | --only-verified]
                           [--exclude-lines EXCLUDE_LINES]
                           [--exclude-files EXCLUDE_FILES]
                           [--exclude-secrets EXCLUDE_SECRETS]
                           [--word-list WORD_LIST_FILE] [-f FILTER]
                           [--disable-filter DISABLE_FILTER]
                           [filenames [filenames ...]]

positional arguments:
  filenames             Filenames to check.

optional arguments:
  -h, --help            show this help message and exit
  -v, --verbose         Verbose mode.
  --version             Display version information.
  --json                Print detect-secrets-hook output as JSON
  --baseline FILENAME   Explicitly ignore secrets through a baseline generated
                        by `detect-secrets scan`

plugin options:
  Configure settings for each secret scanning ruleset. By default, all
  plugins are enabled unless explicitly disabled.

  --list-all-plugins    Lists all plugins that will be used for the scan.
  -p PLUGIN, --plugin PLUGIN
                        Specify path to custom secret detector plugin.
  --base64-limit [BASE64_LIMIT]
                        Sets the entropy limit for high entropy strings. Value
                        must be between 0.0 and 8.0, defaults to 4.5.
  --hex-limit [HEX_LIMIT]
                        Sets the entropy limit for high entropy strings. Value
                        must be between 0.0 and 8.0, defaults to 3.0.
  --disable-plugin DISABLE_PLUGIN
                        Plugin class names to disable. e.g.
                        Base64HighEntropyString

filter options:
  Configure settings for filtering out secrets after they are flagged by the
  engine.

  -n, --no-verify       Disables additional verification of secrets via
                        network call.
  --only-verified       Only flags secrets that can be verified.
  --exclude-lines EXCLUDE_LINES
                        If lines match this regex, it will be ignored.
  --exclude-files EXCLUDE_FILES
                        If filenames match this regex, it will be ignored.
  --exclude-secrets EXCLUDE_SECRETS
                        If secrets match this regex, it will be ignored.
  -f FILTER, --filter FILTER
                        Specify path to custom filter. May be a python module
                        path (e.g.
                        detect_secrets.filters.common.is_invalid_file) or a
                        local file path (e.g.
                        file://path/to/file.py::function_name).
  --disable-filter DISABLE_FILTER
                        Specify filter to disable. e.g.
                        detect_secrets.filters.common.is_invalid_file

これをpre-commitフックとして設定することをお勧めします。その方法の1つとして、pre-commitフレームワークを利用することができます:```yaml

.pre-commit-config.yaml

repos:

  • repo: https://github.com/Yelp/detect-secrets rev: v1.5.0 hooks:
    • id: detect-secrets args: ['--baseline', '.secrets.baseline'] exclude: package.lock.json
root@kitploit:~
#### インライン許可リスト

偽陽性(false positive)によってコミットがブロックされるのを除外したい場合があり、そのためにベースラインを作成せずに
行うことができます。次のようにコメントを追加することで実現できます:```python
secret = "hunter2"      # pragma: allowlist secret

または```javascript // pragma: allowlist nextline secret const secret = "hunter2";

root@kitploit:~
### ベースラインにおけるシークレット監査```bash
$ detect-secrets audit --help
usage: detect-secrets audit [-h] [--diff] [--stats]
                      [--report] [--only-real | --only-false]
                      [--json]
                      filename [filename ...]

Auditing a baseline allows analysts to label results, and optimize plugins for
the highest signal-to-noise ratio for their environment.

positional arguments:
  filename      Audit a given baseline file to distinguish the difference
                between false and true positives.

optional arguments:
  -h, --help    show this help message and exit
  --diff        Allows the comparison of two baseline files, in order to
                effectively distinguish the difference between various plugin
                configurations.
  --stats       Displays the results of an interactive auditing session which
                have been saved to a baseline file.
  --report      Displays a report with the secrets detected

reporting:
  Display a summary with all the findings and the made decisions. To be used with the report mode (--report).

  --only-real   Only includes real secrets in the report
  --only-false  Only includes false positives in the report

analytics:
  Quantify the success of your plugins based on the labelled results in your
  baseline. To be used with the statistics mode (--stats).

  --json        Outputs results in a machine-readable format.

設定

このツールは、プラグインとフィルターのシステムで動作します。

  • プラグインはコード内のシークレットを検出します
  • フィルターは誤検知を無視してスキャンの精度を高めます

必要に応じて、どちらも調整して精度/再現率のニーズに合わせることができます。

プラグイン

コード内でシークレットを見つけるために、3つの異なる戦略を採用しています。

  1. 正規表現ベースのルール

    これは最も一般的なプラグインのタイプであり、構造化されたシークレットに対してうまく機能します。 これらのシークレットはオプションで検証でき、スキャンの精度が向上します。ただし、これだけに依存するとスキャンの再現率に悪影響を及ぼす可能性があります。

  2. エントロピー検出器

    これはさまざまなヒューリスティックなアプローチを通じて「シークレットらしい」文字列を検索します。非構造化シークレットには最適ですが、スキャン精度を調整するためにチューニングが必要になる場合があります。

  3. キーワード検出器

    これはシークレットの値を無視し、ハードコードされた値でシークレットを割り当てることに関連付けられることが多い変数名を検索します。これは「シークレットに見えない」文字列(例:le3tc0deパスワード)に最適ですが、スキャン精度を調整するためにフィルターのチューニングが必要になる場合があります。

現在検出できないシークレットを見つけたいですか?独自のプラグインを(簡単に)開発して、エンジンで使用することもできます!詳細については、プラグインのドキュメントを参照してください。

フィルター

detect-secrets には、ニーズに合う可能性のあるいくつかの組み込みフィルターが用意されています。

--exclude-lines

特定のパターンに一致する行を、スキャンでグローバルに許可したい場合があります。次のように正規表現ルールを指定できます。```bash $ detect-secrets scan --exclude-lines 'password = (blah|fake)'

root@kitploit:~
または、次のように複数の正規表現ルールを指定することもできます:```bash
$ detect-secrets scan --exclude-lines 'password = blah' --exclude-lines 'password = fake'

--exclude-files

場合によっては、スキャンで特定のファイルを無視したいことがあります。そのためには正規表現パターンを指定でき、ファイル名がこの正規表現パターンに一致すると、そのファイルはスキャンされません。```bash $ detect-secrets scan --exclude-files '.*.signature$'

root@kitploit:~
または、次のように複数の正規表現パターンを指定することもできます:```bash
$ detect-secrets scan --exclude-files '.*\.signature$' --exclude-files '.*/i18n/.*'

--exclude-secrets

スキャンにおいて、特定のシークレット値を無視したい場合があります。次のように正規表現ルールを指定できます:```bash $ detect-secrets scan --exclude-secrets '(fakesecret|${.*})'

root@kitploit:~
または、複数の正規表現ルールを次のように指定することもできます:```bash
$ detect-secrets scan --exclude-secrets 'fakesecret' --exclude-secrets '\${.*})'

インライン許可リスト

特定の行にのみ除外を適用したい場合があり、それをグローバルに除外するのではなく、インライン許可リストを使用して行うことができます。```python API_KEY = 'this-will-ordinarily-be-detected-by-a-plugin' # pragma: allowlist secret

root@kitploit:~
これらのコメントは複数の言語でサポートされています。例:```java
const GoogleCredentialPassword = "something-secret-here";     //  pragma: allowlist secret

以下も使用できます:```python

pragma: allowlist nextline secret

API_KEY = 'WillAlsoBeIgnored'

root@kitploit:~
これは、ベースライン全体を再生成する必要なく、機密情報を無視する便利な方法かもしれません。これらの許可リストに登録された機密情報を明示的に検索する必要がある場合は、次のようにすることもできます:```bash
$ detect-secrets scan --only-allowlisted

より多くのカスタムロジックを記述して誤検知をフィルタリングしたいですか? 詳細はフィルターのドキュメントをご参照ください。

拡張機能

wordlist

--exclude-secretsフラグを使用すると、シークレット値を除外するための正規表現ルールを指定できます。ただし、代わりに多数の単語リストを指定したい場合は、--word-listフラグを使用できます。

この機能を使用するには、pyahocorasickパッケージをインストールしてください。または、単に次のように使用します:```bash $ pip install detect-secrets[word_list]

root@kitploit:~
その後、次のように使用できます:```bash
$ cat wordlist.txt
not-a-real-secret
$ cat sample.ini
password = not-a-real-secret

# Will show results
$ detect-secrets scan sample.ini

# No results found
$ detect-secrets scan --word-list wordlist.txt

Gibberish Detector

Gibberish Detectorは、シークレット値が実際にgibberishであるかどうかを判断しようとするシンプルなMLモデルです。本当のシークレット値は単語のようにはならないという仮定に基づいています。

この機能を使用するには、gibberish-detectorパッケージをインストールしてください。または使用:```bash $ pip install detect-secrets[gibberish]

root@kitploit:~
モデルの訓練方法の詳細については、[gibberish-detector](https://github.com/domanchi/gibberish-detector) パッケージを参照してください。事前に訓練されたモデル(RFCを処理してシードされたもの)が簡単に使用できるように含まれています。

また、以下のように独自のモデルを指定することもできます。```bash
$ detect-secrets scan --gibberish-model custom.model

これはデフォルトのプラグインではありません。password のようなシークレットを無視するためです。

注意事項

これは、シークレットがコードベースに入るのを防ぐ確実な解決策を意図したものではありません。本当にそれを実現できるのは、適切な開発者教育だけです。このpre-commitフックは、シークレットをコミットする明らかなケースを防ぐためのいくつかのヒューリスティックを実装しているに過ぎません。

防止できないもの:

  • 複数行のシークレット
  • KeywordDetector をトリガーしないデフォルトパスワード(例: login = "hunter2")

FAQ

一般的な質問

  • 「gitリポジトリを検出できませんでした。」という警告が表示されるが、私はgitリポジトリの中にいる。

    git のバージョンが 1.8.5 以上であるか確認してください。そうでない場合はアップグレードしてから再度試してください。 詳細はこちら。

Windows

  • ベースラインファイルを作成した後、detect-secrets audit が "Not a valid baseline file!" と表示する。

    ベースラインファイルのファイルエンコーディングがUTF-8であることを確認してください。 詳細はこちら。

ツールをダウンロード