关于 detecting-container-drift-at-runtime
面向安全分析人员和 Kubernetes 运维人员的技能,用于发现容器运行时偏离镜像状态的变化,并指导调查与响应。内容涵盖 Falco 规则、Kubernetes 强制措施和镜像摘要检查。
主要功能
- 01分类识别二进制、文件、配置、软件包和网络漂移
- 02比较运行文件系统与源镜像
- 03监控行为和镜像摘要
- 04提供 Falco 检测规则示例
- 05说明隔离与驱逐响应流程
适用场景
- 01调查疑似容器失陷
- 02编写运行时漂移检测规则
- 03检查容器变更监控覆盖情况
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ mukul975/ anthropic-cybersecurity-skills --skill detecting-container-drift-at-runtime安装后在 Claude Code 中输入/detecting-container-drift-at-runtime确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
LICENSEassets/template.mdreferences/api-reference.mdstandards.mdworkflows.mdscripts/agent.pyprocess.py详细介绍
在运行时检测容器漂移
概述
容器漂移是指运行中的容器因未授权的文件修改、意外的二进制文件执行、配置更改或软件包安装而偏离其原始镜像状态。容器应被视为不可变基础设施,因此任何漂移都可能是遭到入侵的迹象。检测技术采用 DIE(检测、隔离、清除)模型——不可变工作负载在运行时不应发生变化,因此观察到的任何变化都可能是恶意活动的证据。
适用场景
- 调查需要在运行时检测容器漂移的安全事件时
- 为此领域构建检测规则或威胁狩猎查询时
- SOC 分析师需要针对这类分析制定结构化流程时
- 验证针对相关攻击技术的安全监控覆盖范围时
前提条件
- 运行时安全工具版本为 v1.24+ 的 Kubernetes 集群
- 用于运行时漂移检测的 Falco 或 Sysdig
- 可获取镜像清单的容器镜像注册表
- 熟悉 Linux 文件系统层和 OverlayFS
核心概念
容器漂移的类型
- 二进制文件漂移:执行原始镜像中不存在的二进制文件(下载的恶意软件、编译的工具)
- 文件漂移:在容器文件系统中创建、修改或删除文件
- 配置漂移:环境变量、挂载的密钥或运行时参数发生变化
- 软件包漂移:在运行时通过 apt、yum、pip 或 npm 安装新软件包
- 网络漂移:出现工作负载未预期的新监听端口或出站连接
检测方法
基于镜像的比较:将运行中容器的文件系统与其源镜像进行比较,以识别新增、修改或删除的文件。
行为监控:使用 eBPF 或内核级监控,检测偏离预期行为的进程执行、文件访问和网络活动。
摘要验证:持续验证运行中容器的镜像摘要是否与已批准的部署清单一致。
使用 Falco 实现
检测新二进制文件的执行
- rule: Drift Detected (Container Image Modified Binary)
desc: Detect execution of a binary not present in the original container image
condition: >
spawned_process and
container and
not proc.pname in (container_entrypoint) and
proc.is_exe_upper_layer = true
output: >
Drift detected: new binary executed in container
(user=%user.name command=%proc.cmdline container=%container.name
image=%container.image.repository:%container.image.tag
exe_path=%proc.exepath)
priority: WARNING
tags: [container, drift]
- rule: Container Shell Spawned
desc: Detect interactive shell in a container that should be immutable
condition: >
spawned_process and
container and
proc.name in (bash, sh, dash, zsh, csh, ksh) and
not proc.pname in (container_entrypoint)
output: >
Shell spawned in container (user=%user.name shell=%proc.name
container=%container.name image=%container.image.repository)
priority: WARNING
tags: [container, drift, shell]
检测软件包管理器的使用
- rule: Package Manager Execution in Container
desc: Detect use of package managers indicating drift
condition: >
spawned_process and
container and
proc.name in (apt, apt-get, yum, dnf, apk, pip, pip3, npm, gem, cargo)
output: >
Package manager executed in container (user=%user.name
command=%proc.cmdline container=%container.name
image=%container.image.repository)
priority: ERROR
tags: [container, drift, package-manager]
检测文件系统修改
- rule: Container File System Write
desc: Detect writes to container upper layer filesystem
condition: >
open_write and
container and
fd.typechar = 'f' and
not fd.name startswith /tmp and
not fd.name startswith /var/log and
not fd.name startswith /proc
output: >
File write in container (user=%user.name file=%fd.name
container=%container.name)
priority: NOTICE
tags: [container, drift, filesystem]
使用 Kubernetes 强制执行策略
只读根文件系统
通过将容器文件系统设为不可变来防止漂移:
apiVersion: apps/v1
kind: Deployment
metadata:
name: immutable-app
spec:
template:
spec:
containers:
- name: app
image: app:v1.0@sha256:abc123...
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
runAsNonRoot: true
volumeMounts:
- name: tmp
mountPath: /tmp
- name: cache
mountPath: /var/cache
volumes:
- name: tmp
emptyDir:
sizeLimit: 100Mi
- name: cache
emptyDir:
sizeLimit: 50Mi
强制执行 Pod 安全标准
apiVersion: v1
kind: Namespace
metadata:
name: production
labels:
pod-security.kubernetes.io/enforce: restricted
pod-security.kubernetes.io/audit: restricted
pod-security.kubernetes.io/warn: restricted
镜像摘要验证
持续监控摘要
#!/bin/bash
# Compare running container digests against approved manifest
NAMESPACE="production"
kubectl get pods -n "$NAMESPACE" -o json | jq -r '
.items[] |
.spec.containers[] |
"\(.image) \(.imageID)"
' | while read IMAGE IMAGE_ID; do
APPROVED_DIGEST=$(kubectl get deploy -n "$NAMESPACE" -o json | \
jq -r ".items[].spec.template.spec.containers[] | select(.image==\"$IMAGE\") | .image")
if [[ "$IMAGE" != *"@sha256:"* ]]; then
echo "[WARN] Container using mutable tag: $IMAGE"
fi
done
与 Microsoft Defender for Containers 集成
对于 Azure Kubernetes 环境,Microsoft Defender 提供内置的二进制文件漂移检测:
{
"alertType": "K8S.NODE_ImageBinaryDrift",
"severity": "Medium",
"description": "Binary executed that was not part of the original container image",
"remediationSteps": [
"Investigate the binary origin and purpose",
"Check if the container was compromised",
"Rebuild the container from a clean image",
"Enable readOnlyRootFilesystem"
]
}
漂移响应手册
- 检测:漂移事件触发警报(Falco、Defender、Sysdig)
- 验证:确认漂移并非来自已批准的流程(初始化容器、配置重新加载)
- 隔离:对受影响的 Pod 应用拒绝所有流量的 NetworkPolicy
- 调查:捕获容器文件系统差异和进程列表
- 清除:删除发生漂移的 Pod(ReplicaSet 将使用干净的镜像重新创建 Pod)
- 修复:解决根本原因(修补漏洞、更新镜像、收紧 RBAC)
参考资料
评论
支持 Markdown