hacktricks/pentesting-web/deserialization/basic-java-deserialization-objectinputstream-readobject.md

99 lines
4.5 KiB
Markdown
Raw Normal View History

<details>
<summary><strong>Aprende hacking en AWS de cero a héroe con</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (Experto en Equipo Rojo de AWS de HackTricks)</strong></a><strong>!</strong></summary>
Otras formas de apoyar a HackTricks:
* Si quieres ver tu **empresa anunciada en HackTricks** o **descargar HackTricks en PDF** Consulta los [**PLANES DE SUSCRIPCIÓN**](https://github.com/sponsors/carlospolop)!
* Obtén la [**merchandising oficial de PEASS & HackTricks**](https://peass.creator-spring.com)
* Descubre [**La Familia PEASS**](https://opensea.io/collection/the-peass-family), nuestra colección exclusiva de [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Únete al** 💬 [**grupo de Discord**](https://discord.gg/hRep4RUj7f) o al [**grupo de telegram**](https://t.me/peass) o **síguenos** en **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Comparte tus trucos de hacking enviando PRs a los** [**HackTricks**](https://github.com/carlospolop/hacktricks) y [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositorios de github.
</details>
En este POST se explicará un ejemplo usando `java.io.Serializable`.
2023-06-05 20:33:24 +02:00
# Serializable
La interfaz `Serializable` de Java (`java.io.Serializable` es una interfaz marcadora que tus clases deben implementar si van a ser **serializadas** y **deserializadas**. La serialización de objetos Java (escritura) se realiza con el [ObjectOutputStream](http://tutorials.jenkov.com/java-io/objectoutputstream.html) y la deserialización (lectura) se realiza con el [ObjectInputStream](http://tutorials.jenkov.com/java-io/objectinputstream.html).
Veamos un ejemplo con una **clase Person** que es **serializable**. Esta clase **sobrescribe la función readObject**, por lo que cuando **cualquier objeto** de esta **clase** es **deserializado**, esta **función** se va a **ejecutar**.\
En el ejemplo, la **función readObject** de la clase Person llama a la función `eat()` de su mascota y la función `eat()` de un Perro (por alguna razón) llama a un **calc.exe**. **Vamos a ver cómo serializar y deserializar un objeto Person para ejecutar esta calculadora:**
2023-06-05 20:33:24 +02:00
**El siguiente ejemplo es de [https://medium.com/@knownsec404team/java-deserialization-tool-gadgetinspector-first-glimpse-74e99e493649](https://medium.com/@knownsec404team/java-deserialization-tool-gadgetinspector-first-glimpse-74e99e493649)**
2023-06-05 20:33:24 +02:00
```java
import java.io.Serializable;
import java.io.*;
public class TestDeserialization {
interface Animal {
public void eat();
}
//Class must implements Serializable to be serializable
public static class Cat implements Animal,Serializable {
@Override
public void eat() {
System.out.println("cat eat fish");
}
}
//Class must implements Serializable to be serializable
public static class Dog implements Animal,Serializable {
@Override
public void eat() {
try {
Runtime.getRuntime().exec("calc");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("dog eat bone");
}
}
//Class must implements Serializable to be serializable
public static class Person implements Serializable {
private Animal pet;
public Person(Animal pet){
this.pet = pet;
}
//readObject implementation, will call the readObject from ObjectInputStream and then call pet.eat()
private void readObject(java.io.ObjectInputStream stream)
throws IOException, ClassNotFoundException {
pet = (Animal) stream.readObject();
pet.eat();
}
}
public static void GeneratePayload(Object instance, String file)
throws Exception {
//Serialize the constructed payload and write it to the file
File f = new File(file);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(f));
out.writeObject(instance);
out.flush();
out.close();
}
public static void payloadTest(String file) throws Exception {
//Read the written payload and deserialize it
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
Object obj = in.readObject();
System.out.println(obj);
in.close();
}
public static void main(String[] args) throws Exception {
// Example to call Person with a Dog
Animal animal = new Dog();
Person person = new Person(animal);
GeneratePayload(person,"test.ser");
payloadTest("test.ser");
// Example to call Person with a Cat
//Animal animal = new Cat();
//Person person = new Person(animal);
//GeneratePayload(person,"test.ser");
//payloadTest("test.ser");
}
2023-06-05 20:33:24 +02:00
}
```
## Conclusión
Como puedes ver en este ejemplo muy básico, la "vulnerabilidad" aquí aparece porque la función **readObject** está **llamando a otras funciones vulnerables**.