2

使用 Falco 监听运行时安全

 1 year ago
source link: https://www.51cto.com/article/746264.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

​1. Falco 是什么

Falco 是由 Sysdig 贡献给 CNCF 的云原生运行时安全相关项目。

Falco 实现了一套可扩展的事件规则过滤引擎,通过获取事件、匹配安全规则、产生告警通知系列操作,能够发现系统中的安全问题。其中的事件来自系统调用,同时也支持 ebpf 探针,规则是开源的[1],可以自行定义扩展[2]。下图为其架构图:

图片

Falco 可以检测到的典型事件包括:

  • 在容器中运行 shell
  • 以特权形式运行的容器
  • 读取敏感数据,比如 /etc/shadow
  • 容器挂载主机的敏感路径
  • 出站网络连接

2. 生成证书

Falco 的 gRPC 需要双向的 TLS 认证 [3]。Falco exporter 通过 gRPC 暴露相关事件,自研的系统也可以通过 gRPC 直接集成 Falco。下面的步骤用来生成交互所需的证书,Falco 官方的文档有些陈旧,有些操作会报错。

  • 创建证书目录
mkdir /root/falco
cd /root/falco
  • 创建 CA 证书
openssl genrsa -out ca.key 4096

openssl req -x509 -new -nodes -sha512 -days 3650 \
        -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=dev.chenshaowen.com" \
        -key ca.key \
        -out ca.crt
  • 创建服务端证书
openssl genrsa -out server.key 4096

openssl req -sha512 -new                  \
              -key server.key       \
              -out server.csr       \
              -subj  "/C=SP/ST=Italy/L=Ornavasso/O=Test/OU=Server/CN=localhost"

openssl x509 -req -sha512               \
               -days 3650            \
               -CA ca.crt           \
               -CAkey ca.key        \
               -in server.csr       \
               -out server.crt      \
               -set_serial 01
  • 创建客户端证书
openssl genrsa -out client.key 4096

openssl req -sha512 -new                  \
              -key client.key       \
              -out client.csr       \
              -subj  "/C=SP/ST=Italy/L=Ornavasso/O=Test/OU=client/CN=localhost"

openssl x509 -req -sha512               \
               -days 3650            \
               -CA ca.crt           \
               -CAkey ca.key        \
               -in client.csr       \
               -out client.crt      \
               -set_serial 01
  • 查看全部生成的证书
ls /root/falco

ca.crt  ca.key  client.crt  client.csr  client.key  server.crt  server.csr  server.key

3. 在 Kubernetes 上安装 Falco

  • 添加 Helm 源
helm repo add falcosecurity https://falcosecurity.github.io/charts
helm repo update
  • 安装 falco [4]
helm install falco falcosecurity/falco \
      --namespace falco --create-namespace \
      --version 3.0.0 \
      --set-file certs.ca.crt=/root/falco/ca.crt,certs.server.key=/root/falco/server.key,certs.server.crt=/root/falco/server.crt \
      --set ebpf.enabled=true \
      --set falco.grpc.enabled=true \
      --set falco.grpc_output.enabled=true \
      --set falcosidekick.enabled=true \
      --set falcosidekick.webui.enabled=true \
      --set falcosidekick.webui.user="admin:admin" \
      --set falco.grpc.unixSocketPath=""
  • 安装 falco-exporter
helm install falco-exporter falcosecurity/falco-exporter \
     --namespace falco --create-namespace \
     --version 0.9.1 \
     --set falco.grpcTimeout=3m
     --set-file certs.ca.crt=/root/falco/ca.crt,certs.client.key=/root/falco/client.key,certs.client.crt=/root/falco/client.crt

在运行过程中会请求 ghcr.io 下载默认规则 falco_rules.yaml.tar.gz,在网络受限环境下,可能会下载失败。

kubectl -n falco get pod -w

NAME                                      READY   STATUS    RESTARTS   AGE
falco-5bbl6                               2/2     Running   0          152s
falco-exporter-26gfz                      1/1     Running   0          124s
falco-falcosidekick-5c8bf5d7fb-kx778      1/1     Running   0          111s
falco-falcosidekick-ui-5b56bbd7cb-5wdwl   1/1     Running   3          111s
...
  • [可选]卸载 Falco
helm uninstall falco --namespace falco
helm uninstall falco-exporter --namespace falco

4. 使用 Grafana 面板查看 Falco 事件数据

通过查看 falco-exporter svc 可以看到其已经将 metrics 暴露给了 Prometheus。

kubectl -n falco get svc falco-exporter  -o yaml

apiVersion: v1
kind: Service
metadata:
  annotations:
    prometheus.io/port: "9376"
    prometheus.io/scrape: "true"
  name: falco-exporter
  namespace: falco
spec:
  ports:
  - name: metrics
    port: 9376
    protocol: TCP
    targetPort: 9376
  selector:
    app.kubernetes.io/instance: falco-exporter
    app.kubernetes.io/name: falco-exporter

接下来,只需要添加 Grafana 面板即可。导入 11914,即 https://grafana.com/grafana/dashboards/11914-falco-dashboard/。查看数据如下图:

图片

但 Metrics 中暴露的信息比较有限,在 Prometheus 中查询 falco_events{rule="Read sensitive file untrusted"}​,得到结果 falco_events{app_kubernetes_io_instance="falco-exporter", app_kubernetes_io_managed_by="Helm", app_kubernetes_io_name="falco-exporter", app_kubernetes_io_versinotallow="0.8.0", helm_sh_chart="falco-exporter-0.9.1", hostname="falco-h57xg", instance="1.1.1.1:9376", job="kubernetes-service-endpoints", k8s_ns_name="<NA>", k8s_pod_name="<NA>", namespace="falco", node="node1", priority="4", rule="Read sensitive file untrusted", service="falco-exporter", source="syscall", tags=",T1020,T1083,T1212,T1552,T1555,container,filesystem,host,mitre_credential_access,mitre_discovery,"} ,并不会展示执行的用户、执行的命令等详情信息,只能看到事件的优先级、触发的规则等有限的信息。

5. 使用 falcosidekick-ui 查看事件

falcosidekick 主要是实现了对事件的集中管理,并提供丰富的告警通道能力,能够将告警发送给 slack、rocketchat、elasticsearch 等 [5]。

而 falco-falcosidekick-ui 提供了对 falco 事件的查看能力。

  • 安装 falco-falcosidekick-ui

刚才在安装 falco 时,已经添加如下参数,因此 falcosidekick 及 falcosidekick-ui 已经安装。

--set falcosidekick.enabled=true \
      --set falcosidekick.webui.enabled=true \
      --set falcosidekick.webui.user="admin:admin"
  • 暴服务端口
kubectl -n falco patch svc falco-falcosidekick-ui  --patch \
  '{"spec": { "type": "NodePort", "ports": [ { "nodePort": 32000, "port": 2802, "protocol": "TCP", "targetPort": 2802 } ] } }'
  • 登录页面查看 UI

通过主机 IP:32000 端口,即可打开 falcosidekick ui 的页面。默认账户是 admin,默认密码是 admin。

在主机上,读取敏感文件 cat /etc/shadow 之后,在 falcosidekick ui 页面就可以查看到相关的事件,如下图:

图片

Output 内容,由于是主机上的操作,会缺失集群相关字段,但文件、命令等信息会丰富不少:

Warning Sensitive file opened for reading by non-trusted program (user=root user_loginuid=1001 program=cat command=cat /etc/shadow pid=54909 file=/etc/shadow parent=bash gparent=sudo ggparent=bash gggparent=sshd container_id=host image=<NA>) k8s.ns=<NA> k8s.pod=<NA> cnotallow=host

Dashboard 页面提供的是全局统计的视图,可以从整体上对集群、主机的安全进行评估,视图如下:

图片
  1. https://github.com/falcosecurity/rules/blob/main/rules/falco_rules.yaml
  2. https://falco.org/docs/rules/basic-elements/
  3. https://falco.org/docs/grpc/grpc-config/
  4. https://github.com/falcosecurity/charts/tree/master/falco#enabling-grpc
  5. https://github.com/falcosecurity/falcosidekick

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK