[CKS] K8S Dockerfile和yaml文件安全检测

最近准备花一周的时间准备CKS考试,在准备考试中发现有一个题目关于Dockerfile和yaml文件安全检测的题目。

​ 专栏其他文章:

Question 1

  • Check the Dockerfile located at /home/cloud_user/Dockerfile on the CLI server. This Dockerfile is based upon alpine:3.13.5.
  • Correct the two security issues within the file.

Note: If you need an OS user, you can use the user nobody with id 65534.

这个题目是要求你检查Dockerfile,并修改在Dockerfile中存在的两个问题,这个题目中给我们了一个提示就是,如果没有指定系统用户,就使用id为65534的nobody用户。

Practice

首先,我们查看这个Dockerfile文件可以发现内容如下:

Dockerfile 复制代码
FROM alpine:latest

USER root
COPY sunnydale.sh .

USER root
CMD ["./sunnydale.sh"]

我们从Dockerfile中可以看到问题如下:

  • 基础镜像在题目中指明的是alpine:3.13.5,而在提供的Dockerfile中为alpine:latest
  • 没有指定系统用户,就使用id为65534的nobody用户,而在Dockerfile中使用的root user,这也是不安全的,所以需要对Dockerfile修改成以下内容:

Question 2

  • Check the YAML file located at /home/cloud_user/scooby-gang-deploy.yml on the CLI server.
  • Correct the two security issues within the file.

这个题目是要求我们对yaml文件进行分析,纠正其中存在的两个系统问题。

Practice

首先,我们查看这个yaml文件可以发现内容如下:

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: scooby-gang
  namespace: sunnydale
spec:
  replicas: 3
  selector:
    matchLabels:
      app: scooby
  template:
    metadata:
      labels:
        app: scooby
    spec:
      containers:
      - name: scoobygang
        image: scoobygang:1.4.0
        securityContext:
          privileged: true
          runAsUser: 0

我们从yaml中可以看到问题如下:

  • privileged: true这将会使得容器在运行时,运行用户拥有container的全部权限,这样是不安全的
  • runAsUser: 0同样的,这将会使得容器运行过程中会拥有root权限

所以我们需要对yaml文件作出以下更改

  • 设置privileged: false
  • 设置runAsUser: 65534

Question 3

Check all of the Pods in the sunnydale namespace and determine whether their containers are static and immutable.

Note: In this scenario, static/immutable means that:

  • The container does not have access to any elevated privileges.
  • The container process cannot write to the container file system.
  • Delete any Pods in the namespace that are not static and immutable.

这个题目比较麻烦就是需要一个个的对每个pod进行分析,分析出哪些pod是not static 和 immutable的,这里也对not static 和 immutable做出了定义:

  • 容器无法访问特权权限。
  • 容器进程无法写入容器文件系统。

Practice

首先我们需要查看在sunnydale下的所有的pod,然后一一对pod的yaml文件进行检查。

  1. 查看sunnydale下面的所有的pod
shell 复制代码
kubectl get pods -n sunnydale

在这里包含了以下的pod: buffy,cordelia,willow,xander

  1. 然后依次对每个pod的yaml文件进行检查,检查内容如下
  • allowPrivilegeEscalation设置必须是false ,或者没有进行设置
  • runAsUser不能设置为0,最好是65534,或者没有进行设置
  • readOnlyRootFilesystem需要设置为true,或者没有进行设置

通过kubectl get pod -n sunnydale xxx -o yaml进行检查即可,对于不满足上述条件的pod,使用kubectl delete pod -n sunnydale进行删除即可

相关推荐
小此方2 分钟前
Re:从零开始的 C++ 进阶篇(四)工业级 C++ 编程:如何构建异常安全的健壮系统?(含案例分析)
运维·开发语言·c++·安全
TechWayfarer8 分钟前
IP归属地API接入实战指南:3天内安全上线的评估与落地方法
网络·tcp/ip·安全
星幻元宇VR9 分钟前
VR消防安全学习机,数字化消防培训新选择
科技·学习·安全·vr
Chengbei1116 分钟前
FOFA高级会员、DayDaymap、360Quake、Hunter测绘搜索引擎高级会员免费使用最大1W条查询
网络·安全·web安全·搜索引擎·网络安全·金融·系统安全
小夏子_riotous24 分钟前
Docker学习路径——7、Docker搭建MySQL 主从复制
linux·运维·mysql·docker·容器·centos·云计算
liyinchi198824 分钟前
Windows Server 部署Docker Engine
运维·docker·容器
郝开30 分钟前
Docker Compose 本地环境搭建:.env 统一配置模板
运维·docker·容器
jiejiejiejie_1 小时前
Flutter for OpenHarmony 登录认证小指南:用 Flutter 给鸿蒙 App 安上 “安全小锁”✨
安全·flutter·华为·harmonyos
白豆五1 小时前
使用 Docker 搭建 Maven 私服
docker·容器·maven
@insist1231 小时前
信息安全工程师-核心考点:网络攻击模型与一般过程全解析
网络·安全·网络攻击模型·软考·信息安全工程师·软件水平考试