Java deserialization principle-demo (i)

Source: Internet
Author: User
Tags serialization

Java deserialization principle-demo (a) 0x00 What is Java serialization and deserialization?

Java serialization refers to the process of converting a Java object into a sequence of bytes that is easy to store in memory, files, and databases, and the WriteObject () method of the ObjectOutputStream class can be serialized.
Java deserialization refers to the process of reverting a sequence of bytes to a Java object, and the ReadObject () method of the ObjectInputStream class is used for deserialization.

0x01 Java Anti-Sequence Vulnerability principle analysis

First define a user class to inherit serializable

package test;import java.io.IOException;import java.io.Serializable;public class user implements Serializable {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}   

Write a test class, generate a user object, save its serialized bytes on the hard disk, and then read the serialized bytes, and then deserialize it into the name of user familiar

Package Test;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import java.io.FileOutputStream;    Import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;public class Test1 {            public static void Main (string[] args) {try {fileoutputstream out =new fileoutputstream ("D:/1.bin");            ObjectOutputStream obj_out = new ObjectOutputStream (out);            User U = new user ();            U.setname ("test");            Obj_out.writeobject (U);            Use the ReadObject method to restore the user object FileInputStream in = new FileInputStream ("D:/1.bin");            ObjectInputStream ins = new ObjectInputStream (in);             User U1 = (user) ins.readobject ();        System.err.println (U1.getname ());        } catch (FileNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace();        } catch (ClassNotFoundException e) {//TODO auto-generated catch block E.printstacktrace (); }    }}

Post-run Output Name property: Test

In order to construct a deserialization vulnerability, the user's Readobjec method needs to be rewritten, and the calculator will be popped in the change method:
Rewrite the user class after Readobjec:

package test;import java.io.IOException;import java.io.Serializable;public class user implements Serializable {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    private void readObject(java.io.ObjectInputStream in) throws ClassNotFoundException, IOException {        in.defaultReadObject();        Runtime.getRuntime().exec("calc.exe");    }}   

Run the test class again and find that the calculator has popped up:

You only need to modify Runtime.getruntime (). EXEC ("Calc.exe"), and calc.exe in can execute arbitrary commands

0x02 Summary

The prerequisite for a deserialization vulnerability is that the Readobjec method inheriting the serializable class must be overridden

Reference connection:
Http://www.freebuf.com/vuls/170344.html

Java deserialization principle-demo (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.