サービスアカウントキーの漏洩検知・自動無効化を試してみた

記事タイトルとURLをコピーする

G-gen の杉村です。Google Cloud では、「サービスアカウントキーの漏洩レスポンス(Service account key exposure response)」という組織ポリシーの制約により、サービスアカウントキーがパブリックな Git レポジトリ等に漏洩した場合でも、自動的に検知され、キーが無効化されます。当記事では意図的にキーを漏洩させた場合の検証結果をご共有します。

キーの漏洩検知・無効化の自動化

サービスアカウントキーの漏洩レスポンス(Service account key exposure response)は、組織のポリシーの制約の1つであり、制約の正式な ID は constraints/iam.serviceAccountKeyExposureResponse です。

「組織のポリシー」自体の基礎については、以下の記事もご参照ください。

blog.g-gen.co.jp

「サービスアカウントキーの漏洩レスポンス」を有効化すると、サービスアカウントキー(サービスアカウントから発行された JSON 形式等の秘密鍵ファイル)が Git のパブリックレポジトリ等で公開されていることを Google が検知した際に、キーを自動で無効化してくれます。

有効化のモードとして、DISABLE_KEYWAIT_FOR_ABUSE の2種類があります。

モード 意味
DISABLE_KEY キーの漏洩が検知されると、自動で無効化する
WAIT_FOR_ABUSE キーの漏洩が検知されても、自動で無効化しない。ただしキーが Google Cloud に悪影響を及ぼす方法で使用されている場合、無効化することがある

2024-06-16 以降、この制約は Google Cloud のすべての組織で DISABLE_KEY として有効化されます(それ以前は無効化の状態です)。この自動的な有効化については、2024-03-19にGoogle Cloud から管理者に対して [Action Advised] Automatically disable publicly exposed Service Account keys という件名のメールで一斉配信されました。

詳細は、以下のドキュメントをご参照ください。

有効化

設定画面へ遷移

Google Cloud コンソールで、IAM と管理 > 組織のポリシー に遷移します。画面上部のプロジェクトセレクタで、組織レベルが選択されていることを確認します。

制約の ID である constraints/iam.serviceAccountKeyExposureResponse でフィルタし、当該制約をクリックします。

制約を有効化

デフォルトでは「親のポリシーを継承する」になっています。これは組織レベルにおいては、デフォルト値と同等ですので、「カスタマイズ」に変更します。その下部の「ポリシーの適用」は「置き換える」を選択します。

その下部ではポリシーの値を「カスタム」に、ポリシーの種類を「許可」に、カスタム値として「DISABLE_KEY」を入力します。

最後に青いボタン「ポリシーを設定」を押下します。

GitHub のパブリックレポジトリにキーを push

検証用組織の検証用プロジェクトに作成した、サービスアカウントキーを GitHub の公開レポジトリに push します。

当検証では、安全性のため、使われていない組織に新規プロジェクトを作成し、ほとんどの API を無効化したうえで、何の権限も付けていないサービスアカウントを作成しました。実際にご自身で検証される場合、ご利用中の Google Cloud プロジェクトのサービスアカウントキーを意図的に漏洩させることは大きな危険を伴いますので、ご注意ください。

なお、git push コマンドでリモートレポジトリにサービスアカウントキーファイルを push しようとした際、以下のような警告文が出て、push が中断されました。

GitHub の「シークレットスキャン」機能が Google Cloud のサービスアカウントキーを検知して、危険であるため push を拒否したものです。今回は検証のため、エラーメッセージ中の URL をクリックして、ロックを解除しました(なお、スクリーンショット中のレポジトリは現在では非公開に変更済みです)。

これで、公開レポジトリにサービスアカウントキーファイルが push され、誰でもアクセスできる状態になりました。

自動検知と無効化の確認

キーファイルを push してからわずか3分後、以下のようなログが、サービスアカウントキーの所属するプロジェクトに出力されました。

{
  "protoPayload": {
    "@type": "type.googleapis.com/google.cloud.audit.AuditLog",
    "status": {},
    "authenticationInfo": {
      "principalEmail": "gcp-compromised-key-response@system.gserviceaccount.com"
    },
    "requestMetadata": {
      "callerIp": "private",
      "requestAttributes": {
        "time": "2024-03-18T09:08:11.967994734Z",
        "auth": {}
      },
      "destinationAttributes": {}
    },
    "serviceName": "iam.googleapis.com",
    "methodName": "google.iam.admin.v1.DisableServiceAccountKey",
    "authorizationInfo": [
      {
        "resource": "projects/-/serviceAccounts/123456789012345678901",
        "permission": "iam.serviceAccountKeys.disable",
        "granted": true,
        "resourceAttributes": {
          "name": "projects/-/serviceAccounts/123456789012345678901"
        }
      }
    ],
    "resourceName": "projects/-/serviceAccounts/123456789012345678901/keys/xxxxx",
    "request": {
      "name": "projects/-/serviceAccounts/sample-sa-exposure@sample-sa-exposure.iam.gserviceaccount.com/keys/xxxxx",
      "@type": "type.googleapis.com/google.iam.admin.v1.DisableServiceAccountKeyRequest"
    }
  },
  "insertId": "ope2zuep3w6x",
  "resource": {
    "type": "service_account",
    "labels": {
      "unique_id": "123456789012345678901",
      "email_id": "sample-sa-exposure@sample-sa-exposure.iam.gserviceaccount.com",
      "project_id": "sample-sa-exposure"
    }
  },
  "timestamp": "2024-03-18T09:08:11.930390275Z",
  "severity": "NOTICE",
  "logName": "projects/sample-sa-exposure/logs/cloudaudit.googleapis.com%2Factivity",
  "receiveTimestamp": "2024-03-18T09:08:12.357022232Z"
}

検知と無効化が行われた際は、 gcp-compromised-key-response@system.gserviceaccount.com という Google Cloud 管理のサービスエージェントを principalEmail とするログが出力されます。

サービスアカウントキーのファイルがインターネットに公開されてから、検知・自動無効化まで3分足らずであることは、特筆すべき点でした。

メールでの通知

管理者に対して、以下のような E メールが通知されます。通知対象のメールアドレスは、エッセンシャルコンタクト(Essential Contacts)で管理が可能です。未設定の場合、組織の管理者ロール(roles/resourcemanager.organizationAdmin)を持つ Google アカウント等に対して通知されます。

WAIT_FOR_ABUSE の場合の挙動

サービスアカウントキーの漏洩レスポンス(constraints/iam.serviceAccountKeyExposureResponse)を WAIT_FOR_ABUSE モードにした場合の挙動は、以下のようになっています。

  • 漏洩したサービスアカウントキーが自動的に無効になるとは限らない
  • ただし Google Cloud に悪影響を及ぼす方法でキーが使用されている場合、無効にされることがある
  • 漏洩が検知されても、Cloud Logging へ出力はされない
  • 漏洩が検知されても、メール等の通知があるとは限らない

もしキー漏洩に対する対策を確実にしたい場合は、WAIT_FOR_ABUSE モードではなく、DISABLE_KEY モードとすることが推奨されます。

なお、キーの漏洩が検知された場合、WAIT_FOR_ABUSE モードであっても、以下のようなメールが Google Cloud から届く場合があります。ただしこれは当社の2024年3月の検証にもとづいた結果であり、今後もこの仕様が維持されるとは限りません。また、どの環境でも同じ挙動が保証されるわけではありませんため、ご留意ください。

Hello , Your Case #(ケース番号) for Google Cloud Platform has been updated.

Case Subject: Leaked Credentials from Google Cloud Project (プロジェクト名)

Case Comment : We have detected potentially compromised Service Account authentication credentials associated with the following Google Cloud Platform account: (サービスアカウント名)@(プロジェクトID).iam.gserviceaccount.com with key ID (Key ID) This key was found at the following URL: (GitHub のファイルリンク)

Based on our investigation of the issue, we believe that you or your organization may have inadvertently published the affected Service Account credentials in public sources or websites (for instance, if credentials were mistakenly uploaded to a service like GitHub). Immediate action is required to secure your account(s). We strongly recommend that you take the following steps: --- Log in to the Google Cloud Console (cloud.google.com/console) and review the activity on your account. -- Revoke all (or listed) credentials for compromised Service Accounts. As every resource accessible to the Service Account may have been affected, it is best to revoke and reissue all credentials on potentially affected projects. For more details, review the instructions available at https://cloud.google.com/security/compromised-credentials ---Delete all unauthorized VMs or resources. ---Take immediate steps to ensure that your Service Account credentials are not embedded in public source code systems, stored in download directories, or unintentionally shared in other ways. The security of your Google Cloud Platform account(s) is important to us. You can find more information on securely using IAM at https://cloud.google.com/iam/docs/using-iam-securely and also recommend best practices for keeping service accounts keys safe at https://cloudplatform.googleblog.com/2017/07/help-keep-your-Google-Cloud-service-account-keys-safe.html. Please let us know if you have additional questions by responding to this message.

Please review your case and take any actions as requested. If you have any questions or require immediate assistance, please reply to this email to contact Google Cloud Support.

Thanks for choosing Google Cloud Platform. —The Google Team

杉村 勇馬 (記事一覧)

執行役員 CTO / クラウドソリューション部 部長

元警察官という経歴を持つ現 IT エンジニア。クラウド管理・運用やネットワークに知見。AWS 12資格、Google Cloud認定資格11資格。X (旧 Twitter) では Google Cloud や AWS のアップデート情報をつぶやいています。