基于K8s日志审计实现攻击行为检测

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))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bypass--

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值