XXE with PHP stream wrappers
PHP XXE php://filter attacks extend classic file reads on PHP targets: the php://filter wrapper base64-encodes a file before the entity returns it, so source code and files containing XML metacharacters survive, while the expect:// wrapper (when installed) can turn XXE into command execution.
- Target formats
- Plain XMLSOAP
- Affected parsers
- PHP libxml (DOMDocument/SimpleXML with unsafe flags)
- Impact
- Source code disclosureConfig/secret theftRCE (expect://)
How it works
A plain file:// read breaks whenever the file contains <, & or other XML-significant bytes — the parser chokes before returning the data. On PHP, the php://filter stream wrapper solves this: php://filter/convert.base64-encode/resource=... returns the file as base64, which is XML-safe, and you decode it afterwards.
If the (non-default) expect extension is loaded, the expect:// wrapper runs a shell command, escalating XXE to remote code execution.
Which parser configuration makes it work
Requires a PHP application parsing untrusted XML with external entities enabled (e.g. libxml < 2.9, or LIBXML_NOENT/LIBXML_DTDLOAD passed explicitly). expect:// additionally requires the PECL expect extension to be installed.
Representative payloads
Replace callback hosts and target paths with your own authorized test values.
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=/var/www/html/config.php">
]>
<foo>&xxe;</foo>Decode the returned base64 to recover the raw source, metacharacters and all.
<!DOCTYPE foo [
<!ENTITY xxe SYSTEM "expect://id">
]>
<foo>&xxe;</foo>Detection
As a tester, use php://filter whenever a raw read returns nothing or malformed output. As a defender, inspect XML bodies for php://, expect:// and data:// schemes in SYSTEM identifiers, and confirm whether the application ever passes LIBXML_NOENT or LIBXML_DTDLOAD to its loader — those flags re-enable the entity resolution that modern libxml disables by default.
- SYSTEM entities using php://, expect:// or data:// schemes
- Code paths calling loadXML/simplexml with LIBXML_NOENT or LIBXML_DTDLOAD
- Base64 blobs in responses that decode to local source files
Remediation
Never pass LIBXML_NOENT or LIBXML_DTDLOAD when loading untrusted XML — modern libxml (2.9+) disables external entity substitution by default, and these flags are what switch it back on. On PHP versions before 8.0 you can additionally call libxml_disable_entity_loader(true) as defence in depth (it is deprecated and unnecessary on 8.0+). Do not install the expect extension on servers that parse untrusted XML, and validate input against a schema rather than trusting entity output.
Fix checklist
- Do not pass LIBXML_NOENT / LIBXML_DTDLOAD to loadXML / simplexml_load_string
- On PHP < 8, call libxml_disable_entity_loader(true) as defence in depth
- Keep the expect PECL extension off XML-processing servers
- Validate parsed data against an allow-list schema
Parser-specific configuration: PHP.