نسخهٔ فعلی |
متن شما |
سطر ۱: |
سطر ۱: |
− | این آسیب پذیری ابتدا با تزریق در xml آغاز می شود و سپس به علت کنترل نادرست ورودی های می تواند منجر به آسیب پذیری های مختلف شود.
| |
− |
| |
− | [[پرونده:Xxe.png]]
| |
− |
| |
− | برای مثال در تصویر بالا مهاجم پس از تزریق دستور زیر
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | <!DOCTYPE foo [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
| |
− | <stockCheck><productId>&xxe;</productId>
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | باعث نمایش فایل /etc/passwd می شود.
| |
− |
| |
− | در xml می توانیم به موجودیت بیرون (External) از xml جاری با استفاده از DOCTYPE اشاره کنیم.
| |
− |
| |
− | به این صورت که ابتدا نامی برای element تعریف می کنیم و سپس محتوای آن را توسط ENTITY مشخص می کنیم.
| |
− |
| |
− | برای دسترسی به ENTITY باید نامی برای آن تعریف کنیم(xxe) و با استفاده از دستور SYSTEM، دستور(file:///etc/passwd) را اجرا می کنیم.
| |
− |
| |
− | == روش های جلوگیری ==
| |
− |
| |
− | === روش های جلوگیری در PHP ===
| |
− |
| |
− | مثال 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | <?php
| |
− | $goodXML = '<test><testing>my value</testing></test>';
| |
− | $badXml = '<!DOCTYPE root [ <!ENTITY foo SYSTEM "http://test.localhost:8080/contents.txt"> ]> <test><testing>&foo;</testing></test>';
| |
− | $doc = simplexml_load_string($badXml );
| |
− | echo $doc->testing;
| |
− | ?>
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | مثال 2)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | // In XML processors based on libxml2, dangerous LIBXML constants include: LIBXML_DTDLOAD, LIBXML_DTDVALID, LIBXML_NOENT, LIBXML_XINCLUDE
| |
− |
| |
− | $xml = $_GET["xml"];
| |
− |
| |
− | $doc = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOENT); // Noncompliant
| |
− |
| |
− | $doc = new DOMDocument();
| |
− | $doc->loadXML($xml, LIBXML_NOENT); // Noncompliant
| |
− |
| |
− | $reader = new XMLReader();
| |
− | $reader->XML($xml, NULL, LIBXML_NOENT); // Noncompliant
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | راه حل 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | <?php
| |
− | $goodXML = '<test><testing>my value</testing></test>';
| |
− | $badXml = '<!DOCTYPE root [ <!ENTITY foo SYSTEM "http://test.localhost:8080/contents.txt"> ]> <test><testing>&foo;</testing></test>';
| |
− | libxml_disable_entity_loader(true);
| |
− | $doc = simplexml_load_string($badXml);
| |
− | echo $doc->testing;
| |
− | ?>
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | راه حل 2)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | // Starting with libxml2 version 2.9, dangerous functionality is disabled by default
| |
− |
| |
− | $xml = $_GET["xml"];
| |
− |
| |
− | $doc = simplexml_load_string($xml);
| |
− |
| |
− | $doc = new DOMDocument();
| |
− | $doc->loadXML($xml);
| |
− |
| |
− | $reader = new XMLReader();
| |
− | $reader->XML($xml);
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | === روش های جلوگیری در ASP.NET ===
| |
− |
| |
− | مثال 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | StreamReader stream = new StreamReader(data);
| |
− | XmlReaderSettings settings = new XmlReaderSettings();
| |
− | settings.DtdProcessing = DtdProcessing.Parse;
| |
− | XmlReader xmlReader = XmlReader.Create(stream, settings);
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | راه حل 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | StreamReader stream = new StreamReader(data);
| |
− | XmlReaderSettings settings = new XmlReaderSettings();
| |
− | settings.DtdProcessing = DtdProcessing.Ignore;
| |
− | XmlReader xmlReader = XmlReader.Create(stream, settings);
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | === روش های جلوگیری در JAVA ===
| |
− |
| |
− | مثال 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | import java.io.FileInputStream;
| |
− | import java.io.IOException;
| |
− | import java.io.InputStream;
| |
− |
| |
− | import javax.xml.parsers.ParserConfigurationException;
| |
− | import javax.xml.parsers.SAXParser;
| |
− | import javax.xml.parsers.SAXParserFactory;
| |
− |
| |
− | import org.xml.sax.SAXException;
| |
− | import org.xml.sax.helpers.DefaultHandler;
| |
− |
| |
− | class XXE {
| |
− | private static void receiveXMLStream(InputStream inStream,
| |
− | DefaultHandler defaultHandler)
| |
− | throws ParserConfigurationException, SAXException, IOException {
| |
− | SAXParserFactory factory = SAXParserFactory.newInstance();
| |
− | SAXParser saxParser = factory.newSAXParser();
| |
− | saxParser.parse(inStream, defaultHandler);
| |
− | }
| |
− |
| |
− | public static void main(String[] args) throws ParserConfigurationException,
| |
− | SAXException, IOException {
| |
− | try {
| |
− | receiveXMLStream(new FileInputStream("evil.xml"), new DefaultHandler());
| |
− | } catch (java.net.MalformedURLException mue) {
| |
− | System.err.println("Malformed URL Exception: " + mue);
| |
− | }
| |
− | }
| |
− | }
| |
− | </pre>
| |
− | </div>
| |
− |
| |
− | راه حل 1)
| |
− |
| |
− | <div lang="en" dir="ltr" class="mw-content-ltr">
| |
− | <pre>
| |
− | import java.io.FileInputStream;
| |
− | import java.io.IOException;
| |
− | import java.io.InputStream;
| |
− |
| |
− | import javax.xml.parsers.ParserConfigurationException;
| |
− | import javax.xml.parsers.SAXParser;
| |
− | import javax.xml.parsers.SAXParserFactory;
| |
− |
| |
− | import org.xml.sax.InputSource;
| |
− | import org.xml.sax.SAXException;
| |
− | import org.xml.sax.XMLReader;
| |
− | import org.xml.sax.helpers.DefaultHandler;
| |
− |
| |
− | class XXE {
| |
− | private static void receiveXMLStream(InputStream inStream,
| |
− | DefaultHandler defaultHandler) throws ParserConfigurationException,
| |
− | SAXException, IOException {
| |
− | SAXParserFactory factory = SAXParserFactory.newInstance();
| |
− | SAXParser saxParser = factory.newSAXParser();
| |
− |
| |
− | // Create an XML reader to set the entity resolver.
| |
− | XMLReader reader = saxParser.getXMLReader();
| |
− | reader.setEntityResolver(new CustomResolver());
| |
− | reader.setContentHandler(defaultHandler);
| |
− |
| |
− | InputSource is = new InputSource(inStream);
| |
− | reader.parse(is);
| |
− | }
| |
− |
| |
− | public static void main(String[] args) throws ParserConfigurationException,
| |
− | SAXException, IOException {
| |
− | try {
| |
− | receiveXMLStream(new FileInputStream("evil.xml"), new DefaultHandler());
| |
− | } catch (java.net.MalformedURLException mue) {
| |
− | System.err.println("Malformed URL Exception: " + mue);
| |
− | }
| |
− | }
| |
− | }
| |
− | </pre>
| |
− | </div>
| |
− |
| |
| [[category:وب(WEB)]] | | [[category:وب(WEB)]] |