鳩小屋

落書き帳

Falco:コンテナの動的脅威アラート

Falcoを少し触ってみたのでメモ。
Falcoはシステムコールベースの脅威アラート機能を提供するOSSです。
MITRE ATT&CKの内容も取り込まれているようです。

falco.org
Matrix - Enterprise | MITRE ATT&CK®

インストール

ドキュメントに従ってインストールします。
Install | Falco

#Trust the falcosecurity GPG key, configure the apt repository, and update the package list
curl -s https://falco.org/repo/falcosecurity-3672BA8F.asc | apt-key add -
echo "deb https://download.falco.org/packages/deb stable main" | tee -a /etc/apt/sources.list.d/falcosecurity.list
apt-get update -y

#Install kernel headers
apt-get -y install linux-headers-$(uname -r)

#Install Falco
apt-get install -y falco

#Run Falco as a service
systemctl start falco

設定ファイル

$ ls /etc/falco/
falco.yaml  falco_rules.local.yaml  falco_rules.yaml  k8s_audit_rules.yaml  rules.available  rules.d

デフォルトルールはfalco_rules.yamlに定義されていて、falco_rules.local.yamlに追記することでユーザ定義のルールを追加できるみたいです。

以下の定義は/ と /rootへの書込みを検出するルールです。
condition:のところが条件式になっていて対象のファイルパスや対象外の条件が記載されています。
output:のところがログ出力になっていて、commandやコンテナIDが含まれています。
内部的にはシステムコールベースみたいですが、ユーザルールでは抽象化されているので、システムコールが分からなくても何とかなりそうです。

$ less /etc/falco/falco_rules.yaml
...
  • rule: Write below root
desc: an attempt to write to any file directly below / or /root condition: > root_dir and evt.dir = < and open_write and proc_name_exists and not fd.name in (known_root_files) and not fd.directory pmatch (known_root_directories) and not exe_running_docker_save and not gugent_writing_guestagent_log and not dse_writing_tmp and not zap_writing_state and not airflow_writing_state and not rpm_writing_root_rpmdb and not maven_writing_groovy and not chef_writing_conf and not kubectl_writing_state and not cassandra_writing_state and not galley_writing_state and not calico_writing_state and not rancher_writing_root and not runc_writing_exec_fifo and not mysqlsh_writing_state and not known_root_conditions and not user_known_write_root_conditions and not user_known_write_below_root_activities output: "File below / or /root opened for writing (user=%user.name user_loginuid=%user.loginuid command=%proc.cmdline parent=%proc.pname file=%fd.name program=%proc.name container_id=%container.id image=%container.image.repository)"

動作確認

適当にコンテナを動かしてコマンドを実行してみます。

$ sudo docker run --rm -it ubuntu
# touch unchi
# apt-get update
# apt-get install curl

falcoの出力を確認します。

# sudo journalctl -f -u falco.service
 8月 07 15:28:23 user-VirtualBox falco[6718]: 15:28:23.754459162: Error File below / or /root opened for writing (user=root user_loginuid=1000 command=nano /etc/falco/falco.yaml parent=bash file=/root/.local/share/nano/search_history program=nano container_id=host image=)
...
 8月 07 15:36:27 user-VirtualBox falco[6718]: 15:36:27.977718775: Error File below / or /root opened for writing (user=root user_loginuid=-1 command=touch unchi parent=bash file=/unchi program=touch container_id=e5e80beb3428 image=ubuntu)
...
 8月 07 15:55:59 user-VirtualBox falco[6718]: 15:55:59.199264022: Error Package management process launched in container (user=root user_loginuid=-1 command=apt-get install curl container_id=e5e80beb3428 container_name=magical_kepler image=ubuntu:latest)

/rootへの書込みやパッケージマネージャの起動が検出されています。
また、container_id=hostとcontainer_id=コンテナIDでコンテナとホストOSの挙動も区別できています。

適当にインストールするだけでもある程度機能しそうですが、アラート機能しかないので、悪性挙動のブロック処理は独自実装が必要です。有償製品のsysdigを利用すれば、ブロック機能も使えるみたいですが。。

Falco単体では実運用は厳しいかもしれません。テスト工程のサンドボックステストあたりなら使えそうです。