Kubernetes Falco运行时安全 电脑版发表于:2022/6/28 21:29 ![](https://img.tnblog.net/arcimg/hb/614d34a8a895477d940476f4298690f9.png) >#Kubernetes Falco运行时安全 [TOC] 简介 ------------ tn2>Falco,云原生运行时安全项目,是事实上是 Kubernetes 威胁检测引擎。 通过观察应用程序和容器的行为在运行时检测威胁。 使用 Falco 插件跨云环境扩展威胁检测。 ![](https://img.tnblog.net/arcimg/hb/25922c63734c42ac9342843124c9a2fb.png) 安装与启动 ------------ >### Debian/Ubuntu ```bash # 信任密钥 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 # 安装内核头文件 apt-get -y install linux-headers-$(uname -r) # 安装 Falco apt-get install -y falco # 卸载也很简单 apt-get remove falco ``` >### Centos7 ```bash # 信任密钥 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 # 安装内核头文件 apt-get -y install linux-headers-$(uname -r) # 安装 Falco apt-get install -y falco # 卸载也很简单 apt-get remove falco ``` >### 启动 ```bash systemctl enable falco systemctl start falco # 查看falco状态 service falco status ``` tn2>还可通过使用以下命令,查看Falco 日志 ```bash journalctl -fu falco ``` tn2>也会将日志记录到`/var/log/syslog`中 ```bash cat /var/log/syslog | grep falco ``` ![](https://img.tnblog.net/arcimg/hb/5ac65dea1df542149f39ac5d2a4380d6.png) >### Demo tn2>打开单独的一个窗口,我们可以通过创建pod,并通过`exec`连接到容器里面,将触发Falco的`shell in a container`规则,并且会记录下来。 然后在以前的端口进行日志shell的检查。 ```bash # 第二个窗口 kuberctl run pod --image=nginx:alpine kuberctl exec -it pod -- sh # 第一个窗口 cat /var/log/syslog | grep falco | grep shell ``` ![](https://img.tnblog.net/arcimg/hb/5311f0459e5e453ab73d98c21ea3e762.png) ![](https://img.tnblog.net/arcimg/hb/abceecf6171c4339b07651b7faabecb6.png) >### 自定义日志输出格式 tn2>这个格式可能看着没那么好看,我们可以自定义一个自己的格式。 首先规则在`/etc/falco/`目录下,我们先备份,在修改日志记录的规则。 ```bash cd /etc/falco/ cp falco_rules.yaml falco_rules.local.yaml # 修改日志记录 vim falco_rules.local.yaml ``` tn2>首先我们找到`Terminal shell in container`规则,更改`output`输出格式。 ```bash - rule: Terminal shell in container desc: A shell was used as the entrypoint/exec point into a container with an attached terminal. 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: > Bob NEW SHELL!!! (user_id=%user.uid repo=%container.image.repository %user.uiduser=%user.name user_loginuid=%user.loginuid %container.info shell=%proc.name parent=%proc.pname cmdline=%proc.cmdline terminal=%proc.tty container_id=%container.id image=%container.image.repository) priority: NOTICE tags: [container, shell, mitre_execution] ``` tn2>然后我们重启falco,然后再次打开一个pod的shell。 ```bash # 重启falco service falco restart # 远程连接 k exec -it pod -- sh # 再次查询日志 cat /var/log/syslog | grep Bob ``` ![](https://img.tnblog.net/arcimg/hb/d51ce8956ea045d58678b0430655d41f.png) tn2>关于更多字段的设置请参考: https://falco.org/docs/rules/supported-fields/ 官网请参考: https://falco.org/