Kubernetes 容器安全 最小特权原则POLP AppArmor限制容器对资源访问

最小特权原则(POLP)


最小特权原则 (Principle of least privilege,POLP) :是一种信息安全概念,即为用户提供执行其工作职责所需的最小权限等级或许可。

最小特权原则被广泛认为是网络安全的最佳实践,也是保护高价值数据和资产的特权访问的基本方式。

最小特权原则 (POLP) 重要性


减少网络攻击面: 当今,大多数高级攻击都依赖于利用特权凭证(提权)。通过限制超级用户和管理员权限,最小权限执行有助于减少总体网络攻击面。
阻止恶意软件的传播: 通过在服务器或者在应用系统上执行最小权限,恶意软件攻击(例如SQL注入攻击)将很难提权来增加访问权限并横向移动破坏其他软件、设备。
有助于简化合规性和审核 :许多内部政策和法规要求都要求组织对特权帐户实施最小权限原则,以防止对关键业务系统的恶意破坏。最小权限执行可以帮助组织证明对特权活动的完整审核跟踪的合规性。

在团队中实施最小特权原则 (POLP) 


• 在所有服务器、业务系统中,审核整个环境以查找特权帐户(例如SSH账号、管理后台账号、跳板机账号);

• 减少不必要的管理员权限,并确保所有用户和工具执行工作时所需的权限;
• 定期更改管理员账号密码;
• 监控管理员账号操作行为,告警通知异常活动。

AppArmor限制容器对资源访问


AppArmor(Application Armor) 是一个 Linux 内核安全模块,可用于限制主机操作系统上运行的进程的功能。每个进程都可以拥有自己的安全配置文件。安全配置文件用来允许或禁止特定功能,例如网络访问、文件读/写/执行权限等。

Linux发行版内置:Ubuntu、Debian

Apparmor两种工作模式


• Enforcement(强制模式) :在这种模式下,配置文件里列出的限制条件都会得到执行,并且对于违反这些限制条件的程序会进行日志记录。

• Complain(投诉模式):在这种模式下,配置文件里的限制条件不会得到执行,Apparmor只是对程序的行为进行记录。一般用于调试。

AppArmor限制容器对资源访问


安全策略是基于文件去设置的,配置了配置文件之后还需要去加载一下
常用命令:
• apparmor_status:查看AppArmor配置文件的当前状态的
• apparmor_parser:将AppArmor配置文件加载到内核中
• apparmor_parser <profile># 加载到内核中
• apparmor_parser -r <profile># 重新加载配置
• apparmor_parser -R <profile># 删除配置
• aa-complain:将AppArmor配置文件设置为投诉模式,需要安装apparmor-utils软件包
• aa-enforce:将AppArmor配置文件设置为强制模式,需要安装apparmor-utils软件包

K8s使用AppArmor的先决条件:
• K8s版本v1.4+,检查是否支持:kubectl describe node |grep AppArmor
• Linux内核已启用AppArmor,查看 cat /sys/module/apparmor/parameters/enabled
• 容器运行时需要支持AppArmor,目前Docker已支持
root@k8s-master:~# kubectl describe node | grep AppAr
  Ready                True    Sat, 03 Jul 2021 12:31:43 +0800   Sat, 05 Jun 2021 23:41:25 +0800   KubeletReady                 kubelet is posting ready status. AppArmor enabled


root@k8s-master:~# cat /sys/module/apparmor/parameters/enabled
Y

 AppArmor 目前处于测试阶段,因此在注解中指定AppArmor策略配置文件。

示例:
apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
  annotations:
    container.apparmor.security.beta.kubernetes.io/<container_name>: localhost/<profile_ref>
...
• <container_name> Pod中容器名称,也就是对哪个容器生效
• <profile_ref> Pod所在宿主机上策略名,默认目录/etc/apparmor.d,策略名字是想要手动去创建配置文件的

如果想要pod使用 apparmor,限制对容器的哪些资源操作,就需要去声明一下让容器使用apparmor,否则默认情况下是不具有这种能力的。

An introduction to AppArmor Profiles


An apparmor profile defines what resources (like network, system capabilities, or files) on the system can be accessed by the target confined application.

Here’s an example of a simple AppArmor profile:

profile k8s-apparmor-example-deny-write flags=(attach_disconnected) {
  file,
  # Deny all file writes.
  deny /** w,
}

In this example, the profile grants the application all kinds of access, except write to the entire file system. It contains two rules:

  • file: Allows all kinds of access to the entire file system.
  • deny /** w: Denies any file write under the root / directory. The expression /** translates to any file under the root directory, as well as those under its subdirectories.

 Setting up a Kubernetes cluster so containers can use apparmor profiles is done with the following steps:

  1. Install and enable AppArmor on all of the cluster nodes.
  2. Copy the apparmor profile you want to use to every node, and parse it into either enforce mode or complain mode.
  3. Annotate the container workload with the AppArmor profile name.

Here is how you would use a profile in a Pod:

apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
  annotations:
    # Tell Kubernetes to apply the AppArmor profile "k8s-apparmor-example-deny-write".
    container.apparmor.security.beta.kubernetes.io/hello: localhost/k8s-apparmor-example-deny-write
spec:
  containers:
  - name: hello
    image: busybox
    command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]

In the pod yaml above, the container named hello is using the AppArmor profile named k8s-apparmor-example-deny-write. If the AppArmor profile does not exist, the pod will fail to be created.

Each profile can be run in either enforce mode, which blocks access to disallowed resources, or complain mode, which only reports violations. After building an AppArmor profile, it is good practice to apply it with the complain mode first and let the workload run for a while. By analyzing the AppArmor log, you can detect and fix any false positive activities. Once you are confident enough, you can turn the profile to enforce mode.

If the previous profile runs in enforce mode, it will block any file write activities:

$ kubectl exec hello-apparmor touch /tmp/test
touch: /tmp/test: Permission denied
error: error executing remote command: command terminated with non-zero exit code: Error executing in Docker Container:

This was a simplified example. Now, think of the challenges of implementing AppArmor in production.

First, you will have to build robust profiles for each of your containers to prevent attacks without blocking daily tasks.

Then, you will have to manage several profiles across all the nodes in your cluster.

案例:容器文件系统访问限制


容器当中跑着我们的应用程序,想要对文件系统最小权限的访问,默认情况下对容器环境下的文件系统有任何的访问权限。如果黑客进入到你的容器当中,可能想方设法的去提取权限。提权到宿主机上面去。最小权限的目的是为了即使黑客进入到容器他的权限也是非常有限的。可能是很多文件都不能访问,很多的工具都不能使用。

步骤:

1、将自定义策略配置文件保存到/etc/apparmor.d/
2、加载配置文件到内核:apparmor_parser <profile>
3、Pod注解指定策略配置名
在pod所在节点上将策略给创建,因为不确定pod会被分配在哪些节点,所以所有的节点都需要创建策略文件,可以使用ansible工具去创建。
#include <tunables/global>
profile k8s-deny-write flags=(attach_disconnected) {
  include <abstractions/base>
  file, 
  deny /bin/** w, 
  deny /data/www/** w,
}


#include <tunables/global>   导入依赖,是linux的内核模块,遵循c语言的语法,格式固定
profile k8s-deny-write flags=(attach_disconnected)  策略名字为k8s-deny-write,这个是需要自己写
include <abstractions/base>   c语言导入的依赖,格式固定


注意,如果策略里面什么都不写就是拒绝一切行为,你在策略里面添加的东西相对于白名单,就是放行

测试,默认策略,里面什么都不写

root@k8s-master:~# cd /etc/apparmor.d/
root@k8s-master:/etc/apparmor.d# cat k8s-deny-write 
#include <tunables/global>
profile k8s-deny-write flags=(attach_disconnected) {
  #include <abstractions/base>
  #file,  
  #deny /bin/** w, 
  #deny /data/www/** w,
}


#让这个文件生效一下
root@k8s-master:/etc/apparmor.d# apparmor_parser -r k8s-deny-write 

#显示加载成功
root@k8s-master:/etc/apparmor.d# apparmor_status | grep k8s
   k8s-deny-write
container.apparmor.security.beta.kubernetes.io/hello   hello是指定了容器的名称

apiVersion: v1
kind: Pod
metadata:
  name: hello-apparmor
  annotations:
    container.apparmor.security.beta.kubernetes.io/hello: localhost/k8s-deny-write
spec:
  containers:
  - name: hello
    image: busybox
    command: [ "sh", "-c", "echo 'Hello AppArmor!' && sleep 1h" ]


使用默认的策略创建后的pod,进入里面会发现使用touch会提示没有权限,在使用默认策略的时候都是拒绝的行为,这个就相对于白名单,往里面加什么就是要放行什么
root@k8s-master:/etc/apparmor.d# cat k8s-deny-write 
#include <tunables/global>
profile k8s-deny-write flags=(attach_disconnected) {
  #include <abstractions/base>
  file,    #允许所有文件读写,创建和查看文件都有
  deny /bin/** w, #控制某个目录进行读写,这边一般都放可执行的程序
}

这里都是先放行所有的文件,然后针对具体的目录去限制

修改之后不需要重启容器,只需要使用-r生效一下即可

工作流程


当去写pod的yaml的时候,要去注解里面去指定你要使用的AppArmor的容器名称和策略名称,pod会被调度到某个节点上面,这个节点就会去读取AppArmor的配置文件,然后对pod当中容器应用上。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值