mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
88 lines
3.5 KiB
Markdown
88 lines
3.5 KiB
Markdown
{{#include ../../banners/hacktricks-training.md}}
|
|
|
|
Dans ce POST, un exemple utilisant `java.io.Serializable` va être expliqué.
|
|
|
|
# Serializable
|
|
|
|
L'interface Java `Serializable` (`java.io.Serializable`) est une interface de marquage que vos classes doivent implémenter si elles doivent être **sérialisées** et **désérialisées**. La sérialisation d'objet Java (écriture) se fait avec le [ObjectOutputStream](http://tutorials.jenkov.com/java-io/objectoutputstream.html) et la désérialisation (lecture) se fait avec le [ObjectInputStream](http://tutorials.jenkov.com/java-io/objectinputstream.html).
|
|
|
|
Voyons un exemple avec une **classe Personne** qui est **sérialisable**. Cette classe **surcharge la fonction readObject**, donc lorsque **n'importe quel objet** de cette **classe** est **désérialisé**, cette **fonction** va être **exécutée**.\
|
|
Dans l'exemple, la **fonction readObject** de la classe Personne appelle la fonction `eat()` de son animal de compagnie et la fonction `eat()` d'un Chien (pour une raison quelconque) appelle un **calc.exe**. **Nous allons voir comment sérialiser et désérialiser un objet Personne pour exécuter cette calculatrice :**
|
|
|
|
**L'exemple suivant est tiré de [https://medium.com/@knownsec404team/java-deserialization-tool-gadgetinspector-first-glimpse-74e99e493649](https://medium.com/@knownsec404team/java-deserialization-tool-gadgetinspector-first-glimpse-74e99e493649)**
|
|
```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");
|
|
}
|
|
}
|
|
```
|
|
## Conclusion
|
|
|
|
Comme vous pouvez le voir dans cet exemple très basique, la "vulnérabilité" ici apparaît parce que la fonction **readObject** **appelle d'autres fonctions vulnérables**.
|
|
|
|
{{#include ../../banners/hacktricks-training.md}}
|