detecting-container-drift-at-runtime

通过监控二进制执行偏移、文件系统变更以及与原始容器镜像的配置偏差,检测对运行中容器的未授权修改。

9 stars

Best use case

detecting-container-drift-at-runtime is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

通过监控二进制执行偏移、文件系统变更以及与原始容器镜像的配置偏差,检测对运行中容器的未授权修改。

Teams using detecting-container-drift-at-runtime should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/detecting-container-drift-at-runtime/SKILL.md --create-dirs "https://raw.githubusercontent.com/killvxk/cybersecurity-skills-zh/main/skills/detecting-container-drift-at-runtime/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/detecting-container-drift-at-runtime/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How detecting-container-drift-at-runtime Compares

Feature / Agentdetecting-container-drift-at-runtimeStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

通过监控二进制执行偏移、文件系统变更以及与原始容器镜像的配置偏差,检测对运行中容器的未授权修改。

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# 检测运行时容器偏移

## 概述

容器偏移是指运行中的容器通过未授权的文件修改、意外的二进制执行、配置变更或软件包安装,偏离其原始镜像状态的现象。由于容器应被视为不可变基础设施,任何偏移都是潜在的入侵指标。检测技术利用 DIE(检测、隔离、驱逐)模型——不可变工作负载在运行期间不应发生变化,因此任何观察到的变化都可能是恶意活动的证据。

## 前提条件

- Kubernetes 集群 v1.24+,并配备运行时安全工具
- Falco 或 Sysdig 用于运行时偏移检测
- 可获取镜像清单的容器镜像仓库
- 熟悉 Linux 文件系统层和 OverlayFS

## 核心概念

### 容器偏移类型

1. **二进制偏移**:执行原始镜像中不存在的二进制文件(下载的恶意软件、编译的工具)
2. **文件偏移**:在容器文件系统中创建、修改或删除文件
3. **配置偏移**:对环境变量、挂载的 Secret 或运行时参数的更改
4. **包偏移**:在运行时通过 apt、yum、pip 或 npm 安装新软件包
5. **网络偏移**:工作负载预期之外的新监听端口或出站连接

### 检测方法

**基于镜像的比对**:将运行中容器的文件系统与源镜像进行比较,识别已添加、修改或删除的文件。

**行为监控**:使用 eBPF 或内核级监控来检测偏离预期行为的进程执行、文件访问和网络活动。

**摘要验证**:持续验证运行中的容器镜像摘要是否与已批准的部署清单匹配。

## 使用 Falco 实施

### 检测新的二进制执行

```yaml
- rule: Drift Detected (Container Image Modified Binary)
  desc: 检测执行原始容器镜像中不存在的二进制文件
  condition: >
    spawned_process and
    container and
    not proc.pname in (container_entrypoint) and
    proc.is_exe_upper_layer = true
  output: >
    检测到偏移:容器中执行了新的二进制文件
    (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: 检测应不可变容器中的交互式 shell
  condition: >
    spawned_process and
    container and
    proc.name in (bash, sh, dash, zsh, csh, ksh) and
    not proc.pname in (container_entrypoint)
  output: >
    容器中启动了 shell (user=%user.name shell=%proc.name
     container=%container.name image=%container.image.repository)
  priority: WARNING
  tags: [container, drift, shell]
```

### 检测包管理器使用

```yaml
- rule: Package Manager Execution in Container
  desc: 检测包管理器的使用,表明存在偏移
  condition: >
    spawned_process and
    container and
    proc.name in (apt, apt-get, yum, dnf, apk, pip, pip3, npm, gem, cargo)
  output: >
    容器中执行了包管理器 (user=%user.name
     command=%proc.cmdline container=%container.name
     image=%container.image.repository)
  priority: ERROR
  tags: [container, drift, package-manager]
```

### 检测文件系统修改

```yaml
- rule: Container File System Write
  desc: 检测对容器上层文件系统的写入操作
  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: >
    容器中发生文件写入 (user=%user.name file=%fd.name
     container=%container.name)
  priority: NOTICE
  tags: [container, drift, filesystem]
```

## 使用 Kubernetes 强制执行

### 只读根文件系统

通过使容器文件系统不可变来防止偏移:

```yaml
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 安全标准执行

```yaml
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
```

## 镜像摘要验证

### 持续摘要监控

```bash
#!/bin/bash
# 将运行中容器的摘要与已批准清单进行比对

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] 容器使用了可变标签: $IMAGE"
  fi
done
```

## Microsoft Defender for Containers 集成

对于 Azure Kubernetes 环境,Microsoft Defender 提供内置的二进制偏移检测:

```json
{
  "alertType": "K8S.NODE_ImageBinaryDrift",
  "severity": "Medium",
  "description": "执行了原始容器镜像中不存在的二进制文件",
  "remediationSteps": [
    "调查二进制文件的来源和用途",
    "检查容器是否已被入侵",
    "从干净镜像重建容器",
    "启用 readOnlyRootFilesystem"
  ]
}
```

## 偏移响应手册

1. **检测**:偏移事件触发告警(Falco、Defender、Sysdig)
2. **验证**:确认偏移不是来自已批准的进程(init 容器、配置重载)
3. **隔离**:对受影响的 Pod 应用拒绝所有流量的 NetworkPolicy
4. **调查**:捕获容器文件系统差异和进程列表
5. **驱逐**:删除偏移的 Pod(ReplicaSet 将从干净镜像重新创建)
6. **修复**:修复根本原因(修补漏洞、更新镜像、收紧 RBAC)

## 参考资料

- [使用 Falco 进行容器偏移检测 - Sysdig](https://www.sysdig.com/blog/container-drift-detection-with-falco)
- [Microsoft Defender for Containers 偏移检测](https://techcommunity.microsoft.com/blog/microsoftdefendercloudblog/detect-container-drift-with-microsoft-defender-for-containers/4232044)
- [确保运行时容器的不可变性](https://notes.kodekloud.com/docs/Certified-Kubernetes-Security-Specialist-CKS/Monitoring-Logging-and-Runtime-Security/Ensure-Immutability-of-Containers-at-Runtime/)
- [Falco 运行时安全](https://falco.org/)

Related Skills

securing-container-registry-with-harbor

9
from killvxk/cybersecurity-skills-zh

Harbor 是一个开源容器镜像仓库,提供安全功能包括漏洞扫描(集成 Trivy)、镜像签名(Notary/Cosign)、RBAC、内容信任策略(Content Trust)、复制和审计日志。配置这些功能以强制执行镜像来源验证、防止有漏洞镜像部署并维护仓库访问控制。

securing-container-registry-images

9
from killvxk/cybersecurity-skills-zh

通过使用 Trivy 和 Grype 实施漏洞扫描、使用 Cosign 和 Sigstore 强制执行镜像签名、配置镜像仓库访问控制,以及构建阻止部署未扫描或未签名镜像的 CI/CD 流水线,来保护容器仓库(Container Registry)中的镜像安全。

scanning-containers-with-trivy-in-cicd

9
from killvxk/cybersecurity-skills-zh

本技能涵盖将 Aqua Security 的 Trivy 扫描器集成到 CI/CD 流水线中,用于全面的容器镜像漏洞检测。包括扫描 Docker 镜像中的操作系统包和应用依赖 CVE、检测 Dockerfile 中的错误配置、扫描文件系统和 Git 仓库,以及建立基于严重性的质量门禁以阻止有漏洞的镜像部署。

scanning-container-images-with-grype

9
from killvxk/cybersecurity-skills-zh

使用 Anchore Grype 扫描容器镜像的已知漏洞(Vulnerability),支持基于 SBOM 的匹配和可配置的严重性阈值。

performing-container-security-scanning-with-trivy

9
from killvxk/cybersecurity-skills-zh

使用 Aqua Security Trivy 扫描容器镜像、文件系统和 Kubernetes 清单,检测漏洞(Vulnerability)、错误配置、暴露的密钥和许可证合规问题,并生成 SBOM(Software Bill of Materials,软件物料清单)及集成到 CI/CD 流水线。

performing-container-image-hardening

9
from killvxk/cybersecurity-skills-zh

本技能涵盖通过最小化攻击面、移除不必要软件包、实施多阶段构建、配置非 root 用户, 以及应用 CIS Docker 基准建议来加固容器镜像,生成安全的生产就绪镜像。

performing-container-escape-detection

9
from killvxk/cybersecurity-skills-zh

通过分析命名空间配置、特权容器检查、危险能力分配和宿主机路径挂载,使用 kubernetes Python 客户端检测容器逃逸尝试。识别通过 cgroup 滥用的 CVE-2022-0492 类型逃逸。 适用于审计容器安全态势或调查逃逸尝试。

implementing-runtime-security-with-tetragon

9
from killvxk/cybersecurity-skills-zh

使用 Cilium Tetragon 在 Kubernetes 集群中实施基于 eBPF 的运行时安全可观测性与执行机制,实现内核级别的威胁检测(Threat Detection)和策略执行。

implementing-runtime-application-self-protection

9
from killvxk/cybersecurity-skills-zh

部署运行时应用自我保护(RASP,Runtime Application Self-Protection)代理,从应用运行时内部检测并阻断攻击,涵盖 OpenRASP 集成、攻击模式检测,以及 Java 和 Python Web 应用的安全策略配置。

implementing-container-network-policies-with-calico

9
from killvxk/cybersecurity-skills-zh

使用 Calico CNI 网络策略和全局网络策略实施 Kubernetes 网络分段,控制 Pod 间流量、限制出口流量并实现零信任微分段。

implementing-container-image-minimal-base-with-distroless

9
from killvxk/cybersecurity-skills-zh

通过在 Google distroless 基础镜像上构建应用镜像来减少容器攻击面,这些镜像仅包含应用运行时,没有 shell、包管理器或不必要的 OS 工具。

implementing-aqua-security-for-container-scanning

9
from killvxk/cybersecurity-skills-zh

部署 Aqua Security 的 Trivy 扫描器,在 CI/CD 管道和镜像仓库中检测容器镜像的漏洞、配置错误、敏感信息和许可证问题。