K8s日志审计以一种事件溯源的方式完整记录了所有API Server的交互请求。当K8s集群遭受入侵时,安全管理员可以通过审计日志进行攻击溯源,通过分析攻击痕迹,找到攻击者的入侵行为并还原攻击者的攻击路径,修复安全问题。
在本篇文章中,我们将介绍如何配置K8s审计日志,并利用这些审计日志作为数据源,实现对攻击行为的检测。
01、配置K8s审计日志
K8配置审计日志仅需要三步:1、准备审计策略文件;2、修改APIServer配置; 3、验证审计日志。
(1)准备一个审计策略文件,这里使用官网审计策略文件示例。
wget https://raw.githubusercontent.com/kubernetes/website/main/content/zh-cn/examples/audit/audit-policy.yaml -o /etc/kubernetes/audit-policy.yaml
(2)修改APIServer配置,保存yaml文件,kube-apiserver 会重新启动。
vi /etc/kubernetes/manifests/kube-apiserver.yaml
spec:
containers:
- command:
- kube-apiserver
- --audit-policy-file=/etc/kubernetes/audit-policy.yaml
- --audit-log-path=/var/log/k8s/audit.log
- --audit-log-format=json
- --audit-log-maxsize=50
- --audit-log-maxage=7
- --audit-log-maxbackup=2
volumeMounts:
- mountPath: /etc/kubernetes
name: audit-config
readOnly: true
- mountPath: /var/log/k8s
name: audit-log
volumes:
- hostPath:
path: /etc/kubernetes
type: Directory
name: audit-config
- hostPath:
path: /var/log/k8s
type: DirectoryOrCreate
name: audit-log
(3)验证审计日志
tail -f /var/log/k8s/audit.log
02、攻击行为检测
Falco提供了k8saudit插件,可以将K8s审计日志作为数据源,通过监测K8s审计日志,我们可以了解集群中的异常情况,及时发现潜在的入侵行为。
(1)Falco启用k8saudit插件,修改falco.yaml文件
load_plugins: [k8saudit, json]
plugins:
- name: k8saudit
library_path: libk8saudit.so
init_config: ""
open_params: "http://:9765/k8s-audit"
- name: json
library_path: libjson.so
使用falcoctl 安装k8saudit插件和规则
falcoctl artifact install k8saudit
falcoctl artifact install k8saudit-rules
启动Falco,监听k8s审计日志的事件数据:
(2)使用webhook将K8s审计日志转发到Falco
使用k8saudit插件定制的audit-policy.yaml
wget https://raw.githubusercontent.com/falcosecurity/plugins/refs/heads/main/plugins/k8saudit/configs/audit-policy.yaml -o /etc/kubernetes/audit-policy.yaml
audit-webhook.yaml配置
vi /etc/kubernetes/audit-webhook.yaml
apiVersion: v1
kind: Config
clusters:
- cluster:
server: http://<ip_of_falco>:9765/k8s_audit
name: falco
contexts:
- context:
cluster: falco
user: ""
name: default-context
current-context: default-context
preferences: {}
users: []
修改APIServer配置,保存yaml,等待重启。
spec:
containers:
- command:
- kube-apiserver
- --audit-policy-file=/etc/kubernetes/audit/audit-policy.yaml
- --audit-webhook-config-file=/etc/kubernetes/audit-webhook.yaml
- --audit-webhook-batch-max-wait=5s # 批量发送等待时间
- --audit-webhook-mode=batch # 批量模式(可选:blocking,blocking-strict)
volumes:
- name: audit-config
hostPath:
path: /etc/kubernetes
type: Directory
volumeMounts:
- name: audit-config
mountPath: /etc/kubernetes
readOnly: true
(3)验证k8saudit告警
创建特权容器,触发k8saudit规则
kubectl run my-pod --image=nginx --privileged=true
falco输出告警信息:
08:47:57.783746000: Warning Pod started with privileged container (user=kubernetes-admin pod=my-pod resource=pods ns=default images=(nginx))