How can you serialize an object in java




















Only objects that support the java. Serializable interface can be written to streams. In this example, we are going to serialize the object of Student class from above code.

The writeObject method of ObjectOutputStream class provides the functionality to serialize the object. We are saving the state of the object in the file named f. Deserialization is the process of reconstructing the object from the serialized state.

It is the reverse operation of serialization. Let's see an example where we are reading the data from a deserialized object. If a class implements Serializable interface then all its sub classes will also be serializable. Let's see the example given below:. Parent class properties are inherited to subclasses so if parent class is Serializable, subclass would also be.

If a class has a reference to another class, all the references must be Serializable otherwise serialization process will not be performed. In such case, NotSerializableException is thrown at runtime. If there is any static data member in a class, it will not be serialized because static is the part of class not object. Rule: In case of array or collection, all the objects of array or collection must be serializable.

If any object is not serialiizable, serialization will be failed. The Externalizable interface provides the facility of writing the state of an object into a byte stream in compress format. It is not a marker interface. Now, id will not be serialized, so when you deserialize the object after serialization, you will not get the value of id.

It will return default value always. In such case, it will return 0 because the data type of id is an integer. The serialization process at runtime associates an id with each Serializable class which is known as SerialVersionUID. It is used to verify the sender and receiver of the serialized object.

The sender and receiver must be the same. It must be of the long type with static and final. It is suggested to explicitly declare the serialVersionUID field in the class and have it private also. For example, a Java application might include in its classpath a JAR file that contains the class files of the serialized object s or load the class definitions by using information stored in the directory, as explained later in this lesson.

You can store a serializable object in the directory if the underlying service provider supports that action, as does Oracle's LDAP service provider. The following example invokes Context. To associate attributes with the new binding, you use DirContext.

To overwrite an existing binding, use Context. You can then read the object back using Context. Running this example produces the following output. Note: The procedures described here are for binding a serializable object in a directory service that follows the schema defined in RFC These procedures might not be generally applicable to other naming and directory services that support binding a serializable object with a specified codebase.

When a serialized object is bound in the directory as shown in the previous example, applications that read the serialized object from the directory must have access to the class definitions necessary to deserialize the object. Alternatively, you can record a codebase with the serialized object in the directory, either when you bind the object or subsequently by adding an attribute by using DirContext.

You can use any attribute to record this codebase and have your application read that attribute from the directory and use it appropriately. Or you can use the "javaCodebase" attribute specified in. So, if an application accepts serialized objects, it is relatively easy to temper with the values. Tampering with the data in an object is harmful already. However, this can also lead to code execution if the correct set of objects is deserialized.

To explain this I first have to explain gadgets and chains. This existing executable code can be reused for malicious purposes. If we look at Java serializable objects, some magic methods—like the private readObject method—are reflectively called when deserializing. This gadget class overrides the default readObject method.

As a result, every time an Object of class Gadget gets deserialized, the Runnable object command is executed. When a command class looks something like the example below, it is easy to manipulate this serialized object and perform code injection. Also, note that if an application accepts serialized objects, the object is deserialized first before it is cast to the desired type. This means that even if casting fails, deserialization is already completed and the readObject method is executed.

A typical deserialization attack consists of a cleverly crafted chain of gadgets. An attacker searches for a gadget that is usable for launching an attack and chains several executions that end with arbitrary code execution, for instance. In our example:.

For a more real-life example, take a look at the implementation of java. It is good to know that whatever gadget chains are available in your application, is not related to your code. Although creating such a malicious gadget chain is very hard and labor-intensive, Java deserialization vulnerabilities are a genuine and dangerous security risk. The best way to prevent a Java deserialize vulnerability is to prevent Java serialization overall.

If your application does not accept serialized objects at all, it cannot harm you. However, if you do need to implement the Serializable interface due to inheritance, you can override the readObject , as seen below, to prevent actual deserialization. If your application relies on serialized objects, you can consider inspecting your ObjectInputStream before deserializing. A library that can help you with this is the Apache Commons IO library. This library provides a ValidatedObjectInputStream where you can explicitly allow the objects you want to deserialize.

Now you prevent that unexpected types are deserialized at all.



0コメント

  • 1000 / 1000