ちょっと話題の記事

【Kube-hunter】Dockerワンライナーで30項目のkubernetes環境脆弱性テストができるOSSを試してみた

常に頻繁なアップデートが要求されるkubernetes環境の運用において必要となる、定期的な脆弱性試験ツールとしてのkube-hunterの紹介です。
2019.07.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

kubernetes環境、マスターノードまで含めると、構成要素は膨大かつkubernetes自体の進化も非常に早く(3ヶ月に1回)、運用コストは決して低いとは言えません。

今日はそんなkubernetes環境に対して、お手軽簡単に脆弱性試験を実施できるKube-hunterというOSSがあったので、それを使ってみた様子をお届けします。

脆弱性試験という性質上実施には厳重な注意が必要ですが、OSSかつDockerワンライナーでの実行が可能なので、実際の実行には5分もかかりません。一度あなたのk8s環境、これを機にチェックしてみてはいかがでしょうか。

(祭) ∧ ∧
 Y  ( ゚Д゚)
 Φ[_ソ__y_l〉     脆弱性試験 マツリダワッショイ
    |_|_|
    し'´J

kube-hunterとは

コンテナセキュリティソリューションで有名なAqua - Container Securityが開発したOSS。

概要は、Aquaの下記ブログが詳しいです。

一言でいうと、任意のKubernetesクラスターに脆弱性試験を実施できるツールです。

Aquaのサイトにも重要事項として記載がありますが、kube-hunterの利用は、あくまで自分自身が所有しているクラスターに対して実行するようにしてください。

kube-hunterの実行方法

kube-hunterのコードはオープンソースで、誰でもそのツールを使うことができます。また、Aquaには専用のWebサイトが設置されているので、簡単に結果を確認できます。

Kube-hunter で、メールアドレスを入力すると、専用のDocker runコマンド(トークン付き)が表示されるので、それを実行するだけです。簡単!後は、トークンに紐付けられたURLのWebサイトで、実行結果を確認することができます。このURLを知っている人はだれでも、結果を共有できます。

軽くkube-hunterで脆弱性試験をやってみた

というわけで、まずは実行してみます。

Kubernetes環境の用意

脆弱性試験対象のKubernetesクラスターとして、 今回は、Macに構築したDocker for MacのKubernetes環境を利用して検証します。

パブリッククラウド(AWSのEKSなど)のクラスタエンドポイントに対してこのツールを実行するのは、確実に脆弱性試験の範疇に入ります。もし、パブリッククラウドなどのエンドポイントに対してこのツールを実行する場合は、事前に必ず各クラウドベンダーの脆弱性試験ポリシーに準拠してください。

最初に、Kubernetesを起動します。

作成されたクラスターエンドポイントを確認します。ローカルなら、PCのIPアドレスですが、念の為。

$ kubectl describe endpoints
Name:         kubernetes
Namespace:    default
Labels:       <none>
Annotations:  <none>
Subsets:
  Addresses:          192.168.65.3
  NotReadyAddresses:  <none>
  Ports:
    Name   Port  Protocol
    ----   ----  --------
    https  6443  TCP

Events:  <none>

kube-hunter起動用Docker runコマンドの入手

Aqua社がホストしているKube-hunterサイトにアクセスします。

テキストボックス欄にメールアドレスを入力し、「Get Started」ボタンをクリックすると、瞬時に専用のトークンがセットされたDocker runコマンドが表示されます。

事前準備はこれだけ。

kube-hunterによる脆弱性試験の実行

表示されたdocker runコマンドを、ペネトレーションテストする端末で実行します。

$ docker run -it --rm --network host aquasec/kube-hunter --token XXXXXXXX
Unable to find image 'aquasec/kube-hunter:latest' locally
latest: Pulling from aquasec/kube-hunter
a5a6f2f73cd8: Pull complete
8da2a74f37b1: Pull complete
09b6f498cfd0: Pull complete
f0afb4f0a079: Pull complete
7053893cb5fe: Pull complete
5184fdd04ff2: Pull complete
05ae3034a5ea: Pull complete
1f5732b3331e: Pull complete
ca22b7022f20: Pull complete
Digest: sha256:e01214f1a6f15ee41279a51d563335d06738eccfca0c6ea72bd7d2e79d6b3abb
Status: Downloaded newer image for aquasec/kube-hunter:latest
Choose one of the options below:
1. Remote scanning      (scans one or more specific IPs or DNS names)
2. Subnet scanning      (scans subnets on all local network interfaces)
3. IP range scanning    (scans a given IP range)

一通りイメージのダウンロードが終わると、プロンプトが表示されます。ここで、脆弱性試験対象のクラスターエンドポイントを指定します。

今回は、1を選んで、IPアドレスを指定すると、対象のエンドポイントに対して脆弱性試験が開始されます。

1. Remote scanning      (scans one or more specific IPs or DNS names)
2. Subnet scanning      (scans subnets on all local network interfaces)
3. IP range scanning    (scans a given IP range)
Your choice: 1
Remotes (separated by a ','): 192.168.65.3
~ Started
~ Discovering Open Kubernetes Services...

Report will be available at:
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| https://kube-hunter.aquasec.com/report.html?token=XXXXXXXXXX |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

--- 以下、試験結果諸々 ---

試験結果は、Docker run時のトークンと紐づけたWebサイトも用意されているので、アクセスすることで、試験結果を画面で共有することも可能です。こういう細かい使い勝手の良さは素敵ですね。

自分のMacでのKubernetesバージョンはこちら。

$ kubectl version --short
Client Version: v1.10.3
Server Version: v1.10.11

この場合、14個脆弱性が発見されています。

実行結果はこちらを参照。

kube-hunter-result-for-hamako9999-mac

ワンライナーでの利用

kube-hunterですが、Webサイトで試験結果を共有しないのならトークン指定は不要です。そのまま、docker runのワンライナーで実行できるので、そこらへん活用すると便利。

このあたりは、公式GitHUbのこちらに詳しく説明されているので、合わせて確認ください。

aquasecurity/kube-hunter: Hunt for security weaknesses in Kubernetes clusters

スキャン方法は3種類。

1. リモートスキャン

パブリッククラウド(AWSのEKSなど)のクラスタエンドポイントに対してこのツールを実行するのは、確実に脆弱性試験の範疇に入ります。もし、パブリッククラウドなどのエンドポイントに対してこのツールを実行する場合は、事前に必ず各クラウドベンダーの脆弱性試験ポリシーに準拠してください。

リモートマシンへの脆弱性試験は、--remoteを使います。

docker run -it --rm --network host aquasec/kube-hunter --remote yourcluster.hamako.com

2. インターナルスキャン

インターナルスキャンを実施する場合、--internalを使います。リモートマシンへの脆弱性試験は、--remoteを使います。

docker run -it --rm --network host aquasec/kube-hunter --internal

3. ネットワークスキャン

一定のネットワーク範囲をネットワークスキャンには、--cidrを利用できます。

docker run -it --rm --network host aquasec/kube-hunter --cidr 192.168.0.0/16

脆弱性試験対象一覧

試験対象は大きく分けて、passiveスキャンとactiveスキャンの2種類あります。デフォルトでは、passiveテストのみ実行されます。

passiveスキャン(21項目)

passiveスキャンでは、クラスターの状態は決して変更されません。いろいろありますが、概ね以下の内容がスキャン対象です。

  • ローカル環境のkubectlクライアントの有無
  • proxy背後のdasyuboard
  • オープンProxyサービスの確認
  • Etcdのリモートアクセスチェック
  • podへの機密情報のアクセス可否
  • kubectlのCVE確認
  • ダッシュボードの有無
  • API Serverへのアクセス可否
  • kubernetes、SSL認証のためのメールアドレスチェック

スキャン項目の詳細は、--listオプションで確認できます。

$ docker run -it --rm aquasec/kube-hunter --list
Passive Hunters:
----------------
* Etcd service
  check for the existence of etcd service

* API Server Hunter
  Accessing the API server using the service account token obtained from a compromised pod

* Kubelet Discovery
  Checks for the existence of a Kubelet service, and its open ports

* Kubectl Client Discovery
  Checks for the existence of a local kubectl client

* Proxy Hunting
  Hunts for a dashboard behind the proxy

* Proxy Discovery
  Checks for the existence of a an open Proxy service

* Dashboard Hunting
  Hunts open Dashboards, gets the type of nodes in the cluster

* Kubelet Readonly Ports Hunter
  Hunts specific endpoints on open ports in the readonly Kubelet server

* Etcd Remote Access
  Checks for remote availability of etcd, its version, and read access to the DB

* Access Secrets
  Accessing the secrets accessible to the pod

* Kubelet Secure Ports Hunter
  Hunts specific endpoints on an open secured Kubelet

* Host Discovery
  Generates ip adresses to scan, based on cluster/scan type

* AKS Hunting
  Hunting Azure cluster deployments using specific known configurations

* Port Scanning
  Scans Kubernetes known ports to determine open endpoints for discovery

* Kubectl CVE Hunter
  Compares version of the kubectl binary to known CVE affected versions

* K8s Dashboard Discovery
  Checks for the existence of a Dashboard

* API Server Discovery
  Checks for the existence of a an API Server

* API Server Hunter
  Checks if API server is accessible

* Host Discovery when running as pod
  Generates ip adresses to scan, based on cluster/scan type

* CVE hunter
  Checks if Node is running a Kubernetes version vulnerable to critical CVEs

* Certificate Email Hunting
  Checks for email addresses in kubernetes ssl certificates

activeスキャン(7項目)

activeスキャンは、クラスターの状態を変更する可能性があり、クラスターに害を及ぼす可能性があるので利用は注意してください。主に以下の内容がチェック対象です。

  • APIサーバーに対して、攻撃者がフル権限をもつことができるかチェック
  • Proxyが公開されている場合の、バージョン情報や関連情報の抜き出し
  • etcdへのリモートアクセス、およびデータ挿入の試行
  • コンテナ無いのunameの実行、ログの取得

デフォルトでは、activeスキャンは無効となっているため、必要な場合は、--activeオプションを付与します。

スキャン項目の詳細は、--list --activeオプションで確認できます。

$ docker run -it --rm aquasec/kube-hunter --list --active
Active Hunters:
---------------
* API server hunter
  Accessing the api server might grant an attacker full control over the cluster

* K8s Version Hunter
  Hunts Proxy when exposed, extracts the version

* Azure SPN Hunter
  Gets the azure subscription file on the host by executing inside a container

* Build Date Hunter
  Hunts when proxy is exposed, extracts the build date of kubernetes

* Etcd Remote Access
  Checks for remote write access to etcd- will attempt to add a new key to the etcd DB

* Kubelet Run Hunter
  Executes uname inside of a random container

* Kubelet Container Logs Hunter
  Retrieves logs from a random container

kube-hunterの実行場所

kube-hunterの実行場所は3種類あります。

クライアントマシンでの実行

上では、docker runで実行していましたが、クライアントにpython実行環境があれば、Git Cloneした後直接、kube-hunterを動かせます。

  • python 2.7 or phtyon 3.x
  • pip

Gitリポジトリのクローン。

$ git clone https://github.com/aquasecurity/kube-hunter.git

モジュールインストール。

cd ./kube-hunter
pip install -r requirements.txt

kube-hunterの実行。

./kube-hunter.py

コンテナでの実行

これは、ここまでの利用例で書いていた内容です。基本的に、DockerHubのaquasec/kube-hunterは常にメンテナンスされていますが、現在、GitHubリポジトリからのオートビルドは動いていないようなので、利用するときは最新バージョンか都度確認する必要があります。

トークンを利用した、Kube-hunter by Aquaでの、試験結果確認は、Dockerバージョンでのみ提供されています。

Podでの実行

クラスター内のPodとして、kube-hunterを定期事項できます。PodのJobとして登録するためのマニフェストファイルが、job.yamlにて提供されているので、それを使いましょう。

お手軽簡単にk8s環境の脆弱性試験やってみそ

以下、kube-hunter利用上の注意点を再度挙げます。

  • 必ず自分が所有しているクラスターに対して実行すること
  • リモートスキャンする場合は、必ずホスティング環境の脆弱性試験ポリシーに準拠すること
    • 特にAWSの場合、一部例外はありますが脆弱性試験は基本的に事前申請が必須です

kube-hunter、OSSかつ、Dockerワンライナーで利用できるため、試験自体は速攻実行できます。リモートでの実行は上述したとおり注意が必要ですが、ワーカーノードの中や、プライベート環境からの実行はすぐにできると思うので、一度、運用しているkubernetes環境のチェックに使ってみてはいかがでしょうか。

それでは、今日はこのへんで。濱田(@hamako9999)でした。