0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Falco ~クラウドネイティブなセキュリティツール~

Posted at

目的

  • Falco を理解する

手段

killercodaで手を動かす

環境

killercoda

falco とは

Falcoは、クラウドネイティブなセキュリティツールであり、ホスト、コンテナ、Kubernetes、およびクラウド環境全体にわたる実行時セキュリティを提供します。Linuxカーネルイベントやその他のデータソースに基づくカスタムルールを活用し、プラグインを通じてイベントデータをコンテキストメタデータで拡張し、リアルタイムのアラートを提供します。Falcoは、異常な挙動、潜在的なセキュリティ脅威、およびコンプライアンス違反の検出を可能にします。
出典

つまり、コンテナや Kubernetes 実行環境の追加セキュリティツールですね。

ざっくりどんな動きなのか

設定値は /etc/falco 配下に存在します。

controlplane:~$ ls -la /etc/falco
total 148
drwxr-xr-x   4 root root  4096 Sep 18 09:17 .
drwxr-xr-x 152 root root 12288 Sep 18 09:17 ..
drwxr-xr-x   2 root root  4096 Aug 19  2024 config.d
-rw-r--r--   1 root root 53941 Sep 18 09:17 falco.yaml # 大元の設定ファイル
-rw-r--r--   1 root root    21 Aug 19  2024 falco_rules.local.yaml # カスタムルール記述用設定ファイル。後ほど説明します
-rw-r--r--   1 root root 63735 Jan  1  1970 falco_rules.yaml # デフォルトのルールファイル
drwxr-xr-x   2 root root  4096 Aug 19  2024 rules.d

falco はサービスとして動作しています。

controlplane:~$ cat /var/log/syslog |grep -a falco|more
2025-09-18T09:17:53.024141+00:00 controlplane systemd[1]: Started falco-modern-bpf.service - Falco: Container Native Runtime Security with modern ebp
f.
2025-09-18T09:17:53.030983+00:00 controlplane systemd[1]: Started falcoctl-artifact-follow.service - Falcoctl Artifact Follow: automatic artifacts up
date service.
2025-09-18T09:17:53.210702+00:00 controlplane falco: Falco version: 0.38.2 (x86_64)
2025-09-18T09:17:53.211523+00:00 controlplane falco: Falco initialized with configuration files:
2025-09-18T09:17:53.211971+00:00 controlplane falco:    /etc/falco/falco.yaml
# サービスとしては `falco-modern-bpf` と `falcoctl-artifact-follow` が起動しています

次に各サービスの起動状態を確認します。

controlplane:~$ service falco-custom status
○ falco-custom.service - Falco: Container Native Runtime Security with custom configuration
     Loaded: loaded (/usr/lib/systemd/system/falco-custom.service; disabled; preset: enabled)
     Active: inactive (dead)
       Docs: https://falco.org/docs/
controlplane:~$ service falco-modern-bpf status
● falco-modern-bpf.service - Falco: Container Native Runtime Security with modern ebpf
     Loaded: loaded (/usr/lib/systemd/system/falco-modern-bpf.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-09-18 09:17:53 UTC; 6min ago
       Docs: https://falco.org/docs/

controlplane:~$ service falcoctl-artifact-follow status
● falcoctl-artifact-follow.service - Falcoctl Artifact Follow: automatic artifacts update service
     Loaded: loaded (/usr/lib/systemd/system/falcoctl-artifact-follow.service; disabled; preset: enabled)
     Active: active (running) since Thu 2025-09-18 09:17:53 UTC; 6min ago
       Docs: https://falco.org/docs/
#両サービス共に起動しています

falco のログを確認します。

controlplane:~$ cat /var/log/syslog |grep -a falco
--- snip ---
# falco サービス起動関連以外のログは見受けられません。

コンテナ作成後、shell コマンド実行で falco のログ出力を確認します。

controlplane:~$ k run app1 --image=nginx:alpine
pod/app1 created
controlplane:~$ k get po
NAME   READY   STATUS    RESTARTS   AGE
app1   1/1     Running   0          9s
controlplane:~$ k exec -it app1 -- sh
/ # exit
# シェル実行まで

controlplane:~$ cat /var/log/syslog |grep -a falco
2025-09-18T09:28:03.493707+00:00 controlplane falco: 09:28:03.490996211: Notice A shell was spawned in a container with an attached terminal (evt_type=execve user=root user_uid=0 user_loginuid=-1 process=sh proc_exepath=/bin/busybox parent=containerd-shim command=sh terminal=34816 exe_flags=EXE_WRITABLE container_id=c1bffaf9f5db container_name=app1)
# falco のログ出力があります。基本的に /var/log/syslog を確認する形です

ここまでで falco の動作を確認しました。ここからはカスタムルールを作成したいと思います。カスタムルールは falco_rules.local.yaml ファイルに記述します。

また、既存の falco_rules.yaml 内の設定を変更する場合は、falco_rules.yaml の内容を falco_rules.local.yaml ファイルに転記し、内容を修正します。

設定変更後は service を再起動して設定を反映します。

controlplane:/etc/falco$ grep -ri "Terminal shell in container"
falco_rules.yaml:    unique to your environment. The rule "Terminal shell in container" that fires when using "kubectl exec" is more Kubernetes 
falco_rules.yaml:- rule: Terminal shell in container
# 設定変更対象のルール名やキーワードが明らかな場合、grep で該当ルール名を確認します

controlplane:/etc/falco$ cat falco_rules.yaml |grep -A30
--- snip ---

- rule: Terminal shell in container
  desc: >
    A shell was used as the entrypoint/exec point into a container with an attached terminal. Parent process may have 
    legitimately already exited and be null (read container_entrypoint macro). Common when using "kubectl exec" in Kubernetes. 
    Correlate with k8saudit exec logs if possible to find user or serviceaccount token used (fuzzy correlation by namespace and pod name). 
    Rather than considering it a standalone rule, it may be best used as generic auditing rule while examining other triggered 
    rules in this container/tty.
  condition: >
    spawned_process 
    and container
    and shell_procs 
    and proc.tty != 0
    and container_entrypoint
    and not user_expected_terminal_shell_in_container_conditions
  output: A shell was spawned in a container with an attached terminal (evt_type=%evt.type user=%user.name user_uid=%user.uid user_loginuid=%user.loginuid process=%proc.name proc_exepath=%proc.exepath parent=%proc.pname command=%proc.cmdline terminal=%proc.tty exe_flags=%evt.arg.flags %container.info)
  priority: NOTICE
  tags: [maturity_stable, container, shell, mitre_execution, T1059]
--- snip ---
# 対象設定を grep で確認します。

次に編集したいルール全体を falco_rules.local.yaml ファイルに追記します。

controlplane:/etc/falco$ cat falco_rules.local.yaml 
# Your custom rules!
- rule: Terminal shell in container
  desc: >
    A shell was used as the entrypoint/exec point into a container with an attached terminal. Parent process may have 
    legitimately already exited and be null (read container_entrypoint macro). Common when using "kubectl exec" in Kubernetes. 
    Correlate with k8saudit exec logs if possible to find user or serviceaccount token used (fuzzy correlation by namespace and pod name). 
    Rather than considering it a standalone rule, it may be best used as generic auditing rule while examining other triggered 
    rules in this container/tty.
  condition: >
    spawned_process 
    and container
    and shell_procs 
    and proc.tty != 0
    and container_entrypoint
    and not user_expected_terminal_shell_in_container_conditions
  output: NEW SHELL!!! A shell was spawned in a container with an attached terminal (user_id=%user.uid repo=%container.image.repository evt_type=%evt.type user=%user.name user_uid=%user.uid user_loginuid=%user.loginuid process=%proc.name proc_exepath=%proc.exepath parent=%proc.pname command=%proc.cmdline terminal=%proc.tty exe_flags=%evt.arg.flags %container.info)
  priority: NOTICE
  tags: [maturity_stable, container, shell, mitre_execution, T1059]
# 基本的には `falco_rules.yaml` ファイルの一部転記です。変更したい動作(主にログ出力箇所:`output` を指定します)

最後に falco_rules.local.yaml ファイルを変更して falco サービスを再起動します。

controlplane:/etc/falco$ service falco-modern-bpf restart

続いて、falo のルールの書き方を確認します。基本には、どのような内容をログに記述するのか output の行を修正します。
また、どのような項目をロギングできるかは公式サイトで確認できます。

よく目にする下記条件で output を変更します。

  • 条件
    • evt_time=%evt.time
    • evt_time_s=%evt.time.s
    • evt_datetime=%evt.datetime
    • proc_duration=%proc.duration
    • container_id=%container.id
    • container_name=%container.name
    • k8s_pod_name=%k8s.pod.name
    • k8s_pod_ip=%k8s.pod.ip
controlplane:/etc/falco$ cat falco_rules.local.yaml |grep output
  output: NEW SHELL!!! A shell was spawned in a container with an attached terminal (evt_time=%evt.time evt_time_s=%evt.time.s evt_datetime=%evt.datetime proc_duration=%proc.duration container_id=%container.id container_name=%container.name k8s_pod_name=%k8s.pod.name k8s_pod_ip=%k8s.pod.ip)

controlplane:/etc/falco$ cat /var/log/syslog |grep -a "NEW SHELL"
2025-09-20T16:08:41.528768+00:00 controlplane falco: 16:08:41.523598707: Notice NEW SHELL!!! A shell was spawned in a container with an attached terminal (evt_time=16:08:41.523598707 evt_time_s=16:08:41 evt_datetime=2025-09-20 16:08:41.523598707 proc_duration=6406909 container_id=5662388b9a9c container_name=nginx k8s_pod_name=nginx k8s_pod_ip=192.168.0.6)

falco 公式サイト -> Reference -> Fields for Conditions and Outputs で列挙された項目を k8s_pod_ip=%k8s.pod.ip の形式で記載すればOKですね。左側のログ項目名は ._ が分かり易いかと思います。

あとがき

アウトプットは良いですね!

ソース

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?