> For the complete documentation index, see [llms.txt](https://duc193.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://duc193.gitbook.io/notes/web-security/java-deserialize/serialization-va-deserialization-trong-java.md).

# Serialization và Deserialization trong Java

**Serialization** trong Java là quá trình chuyển đổi Object thành 1 dạng dữ liệu byte để có thể lưu lại dưới dạng file, gửi đi nơi khác ,...&#x20;

Object sau khi bị serialize sẽ trở thành 1 chuỗi byte. Mảng byte này đại diện cho Class của Object, phiên bản của Object, và trạng thái của Object.

Deserialization là quá trình ngược lại, nó sẽ chuyển đổi lại dữ liệu byte thành Object.

Ex :

```java
// Users.java
import java.io.Serializable;

public class Users implements Serializable {
    public String name;
    public int age;

    public Users(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
```

```java
// Test.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class Test {
    public static void main(String[] args) throws IOException {
        String filename = "user.ser";
        Users userToSave = new Users("duc", 19);
        System.out.println("User Name: " + userToSave.name);
        System.out.println("User Age: " + userToSave.age);
        try (FileOutputStream fileOut = new FileOutputStream(filename);
                ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {
            objectOut.writeObject(userToSave);
            System.out.println("\nDa serialize vao file : " + filename);
        }

        // Deserialize
        try (FileInputStream fileIn = new FileInputStream(filename);
                ObjectInputStream objectIn = new ObjectInputStream(fileIn)) {
            Users userFromFile = (Users) objectIn.readObject();
            System.out.println("\nDa deserialize tu file : " + filename);
            System.out.println("Name: " + userFromFile.name);
            System.out.println("Age: " + userFromFile.age);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
```

Trong đoạn code trên thì nó dùng `writeObject()` để serialize và ghi data vào file. Còn `readObject()` là để Deserialize. KQ:&#x20;

<figure><img src="/files/e5xNedlkFXJ4wYkOfIS6" alt=""><figcaption></figcaption></figure>

File `data.ser` :&#x20;

<figure><img src="/files/dO6p5GFSO9zgUnHkaDPJ" alt=""><figcaption></figcaption></figure>

## Lỗ hổng Java Deserialize&#x20;

Lỗ hổng Java Deserialize xảy ra khi server deserialize untrusted data từ users. Khiến cho attacker có thể chain các gadgets để trigger RCE hoặc thay đổi logic web.

## Cách khai thác&#x20;

Để khai thác Java Deserialize chúng ta cần:

* Tìm nơi input chuỗi đã serialize (untrusted data).
* Tìm xem ứng dụng có deserialize chuỗi đó ra ko.
* Chain các gadget lại với nhau để có thể lead to rce hoặc làm những thứ khác.

## Referrence&#x20;

* <https://viblo.asia/p/serialization-va-deserialization-trong-java-tai-sao-lai-can-thiet-0gdJzYKj4z5>
* <https://viblo.asia/p/lo-hong-java-deserialization-va-nhung-dieu-co-the-ban-chua-biet-djeZ1wR85Wz>
* <https://sec.vnpt.vn/2020/02/co-gi-ben-trong-cac-gadgetchain>
* <https://hackmd.io/@endy/r1sYTUYOh>
* [https://hackmd.io/@janlele91/](https://hackmd.io/@janlele91/H1epEvTmh)
* <https://tsublogs.wordpress.com/2023/02/15/javasecurity101-1-java-reflection/>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://duc193.gitbook.io/notes/web-security/java-deserialize/serialization-va-deserialization-trong-java.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
