|
XXE注入
在XML中可以在DTD部分定义实体,而实体分为两类:内部实体(在定义的时候直接进行赋值)、外部实体(使用SYSTEM关键字标记,值从外部获取),由于在引用外部实体时没有对限制导致攻击者可以利用外部实体注入恶意数据
漏洞信息
混子Hacker
01
XML简介
一、XML的基本结构
XML是一种可扩展标记语言类似HTML,常用于数据的存储和传输,由于XML是一种结构化的语言使得其可以在不同系统之间实现数据共享,其结构基本如下
包括了声明、DTD、文档元素
在DTD部分可以定义外部实体和内部实体,实体可被多次引用
二、实体的定义
内部实体
<!ENTITY 实体名 实体值>来对一个实体进行定义,而在这里实体的值可以直接进行赋值如
外部实体
使用SYSTEM标识从外部获取如下从从文件读取内容赋值给note
三、XML解析
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
publicclassMain {
public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
xml_demo();
}
public static void xml_demo() throws ParserConfigurationException, IOException, SAXException {
// 创建DocumentBuilderFactory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建DocumentBuilder
DocumentBuilder builder = factory.newDocumentBuilder();
// 解析XML文件
Document document = builder.parse(new File("demo.xml"));
// 获取根元素
Element root = document.getDocumentElement();
System.out.println("Root element: " + root.getNodeName());
// 遍历子元素
NodeList nodeList = root.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Element: " + element.getNodeName());
System.out.println("Value: " + element.getTextContent());
}
}
}
}
demo.xml
运行之后如下
混子Hacker
02
漏洞成因
一、漏洞验证
由于在引用外部实体时没有对限制导致攻击者可以利用外部实体注入恶意数据
如下xml读取系统文件
运行之后如下
二、无回显利用
无回显的情况下无法得到结果,需要利用带外的形式携带结果访问链接获取
先在攻击机下创建一个恶意的dtd文件
%int;
在xml中利用外部实体访问该dtd
<foo>&send;</foo>
运行之后网站收到数据
不同语言支持的协议不同
三、SYSTEM过滤绕过
1、PUBLIC关键字替换
SYSTEM被过滤的情况下可以尝试使用PUBLIC关键字替换
xml文件
<foo>&send;</foo>
外部恶意的dtd文件
%int;
2、编码绕过
xml内容如下
%xml;
]>
<foo>&send;</foo>
使用了实体嵌套和实体编码,编码内容解码如下
%demotest;
外部dtd如下
%int;
混子Hacker
03
漏洞防护
禁用外部实体
一、JAVA修复
DocumentBuilderFactory修复
DocumentBuilderFactorydbf= DocumentBuilderFactory.newInstance();
StringFEATURE=null;
FEATURE = "http://apache.org/xml/features/disallow-doctype-decl";
dbf.setFeature(FEATURE, true);
FEATURE = "http://xml.org/sax/features/external-general-entities";
dbf.setFeature(FEATURE, false);
FEATURE = "http://xml.org/sax/features/external-parameter-entities";
dbf.setFeature(FEATURE, false);
FEATURE = "http://apache.org/xml/features/nonvalidating/load-external-dtd";
dbf.setFeature(FEATURE, false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilderdocumentBuilder= dbf.newDocumentBuilder();
DomParser 修复
DOMParserdomParser=newDOMParser();domParser.setAttribute(DOMParser.EXPAND_ENTITYREF,false);domParser.parse(newFileInputStream(newFile("poc.xml")));
SaxBuilder 修复
Filefile=newFile("src/main/java/xxe/poc_bind.xml");SAXBuildersb=newSAXBuilder();sb.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);sb.setFeature("http://xml.org/sax/features/external-general-entities",false);sb.setFeature("http://xml.org/sax/features/external-parameter-entities",false);sb.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);
SAXParserFactory 修复
SAXParserFactoryspf=SAXParserFactory.newInstance();spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);spf.setFeature("http://xml.org/sax/features/external-general-entities",false);spf.setFeature("http://xml.org/sax/features/external-parameter-entities",false);spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);Filefile=newFile("poc.xml");SAXParserparser=spf.newSAXParser();parser.parse(file,(HandlerBase)null);
SAXReader 修复
Filefile=newFile("poc.xml");SAXReadersaxReader=newSAXReader();saxReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);saxReader.setFeature("http://xml.org/sax/features/external-general-entities",false);saxReader.setFeature("http://xml.org/sax/features/external-parameter-entities",false);saxReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd",false);saxReader.read(file);
SAXTransformerFactory
SchemaFactoryfactory= SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
Filefile=newFile("src/main/java/xxe/poc.xml");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "");
factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
StreamSourcesource=newStreamSource(file);
Schemaschema= factory.newSchema(source);
SchemaFactory
SchemaFactoryfactory=SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");Filefile=newFile("src/main/java/xxe/poc.xml");factory.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD,"");factory.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA,"");StreamSourcesource=newStreamSource(file);Schemaschema=factory.newSchema(source);
XMLInputFactory
XMLInputFactoryxmlInputFactory= XMLInputFactory.newFactory();
xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
Filefile=newFile("poc.xml");
二、PHP修复
// 禁用外部实体加载
libxml_disable_entity_loader(true);
// 加载 XML 文件
$xml = file_get_contents('example.xml');
// 解析 XML
$dom = new DOMDocument();
$dom->loadXML($xml, LIBXML_NOENT | LIBXML_DTDLOAD);
// 处理 XML 数据
echo $dom->saveXML();
?>
三、PYHOTN修复
from lxml import etree
# 禁用外部实体加载
parser = etree.XMLParser(resolve_entities=False, no_network=True)
<<< END >>>
原创文章|转载请附上原文出处链接
更多漏洞|关注作者查看
作者|混子Hacker
原文始发于微信公众号(混子Hacker):【渗透知识】XXE注入
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论