問題描述
我們對代碼進行了安全審計,其中提到我們的代碼容易受到 XML 外部實體 (XXE) 攻擊.
We had a security audit on our code, and it mentioned that our code is vulnerable to XML EXternal Entity (XXE) attacks.
XML 外部實體攻擊受益于在處理時動態構建文檔的 XML 特性.一個 XMLentity 允許動態地包含來自給定資源的數據.外部實體允許 XML 文檔包含數據來自外部 URI.除非另外配置,否則外部實體會強制 XML 解析器訪問指定的資源通過 URI,例如,本地計算機或遠程系統上的文件.此行為將應用程序暴露給 XML External實體 (XXE) 攻擊,可用于執行本地系統的拒絕服務,獲得對文件的未經授權的訪問本地機器,掃描遠程機器,并對遠程系統執行拒絕服務.
Explanation
XML External Entities attacks benefit from an XML feature to build documents dynamically at the time of processing. An XML entity allows inclusion of data dynamically from a given resource. External entities allow an XML document to include data from an external URI. Unless configured to do otherwise, external entities force the XML parser to access the resource specified by the URI, e.g., a file on the local machine or on a remote system. This behavior exposes the application to XML External Entity (XXE) attacks, which can be used to perform denial of service of the local system, gain unauthorized access to files on the local machine, scan remote machines, and perform denial of service of remote systems.
以下 XML 文檔顯示了 XXE 攻擊的示例.
The following XML document shows an example of an XXE attack.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "file:///dev/random" >]><foo>&xxe;</foo>
如果 XML 解析器嘗試將實體替換為/dev/random 文件.
This example could crash the server (on a UNIX system), if the XML parser attempts to substitute the entity with the contents of the /dev/random file.
應該安全地配置 XML 解組器,以使其不允許外部實體作為傳入 XML 的一部分文件.
The XML unmarshaller should be configured securely so that it does not allow external entities as part of an incoming XML document.
為避免 XXE 注入,請勿使用將 XML 源直接處理為 java.io.File
、java.io.Reader
或java.io.InputStream
.使用安全配置的解析器解析文檔,并使用將安全解析器作為 XML 源的解組方法,如下例所示:
To avoid XXE injection do not use unmarshal methods that process an XML source directly as java.io.File
, java.io.Reader
or
java.io.InputStream
. Parse the document with a securely configured parser and use an unmarshal method that takes the secure parser as the XML source as shown in the following example:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setExpandEntityReferences(false);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(<XML Source>);
Model model = (Model) u.unmarshal(document);
以下代碼是審計發現 XXE 攻擊的地方:
The code below is where the audit found the XXE attack:
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
System.out.println("outputing to : " + outputLocation);
File outputFile = new File(outputLocation);
StreamResult result = new StreamResult(outputFile);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
如何在我的代碼中實施上述建議?我在哪里漏掉了東西?
How can I implement the above recommendation in my code? Where am I missing things?
推薦答案
您可以使用與 DocumentBuilderFactory
相同的方法:
You can use the same approach with DocumentBuilderFactory
:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
...
要讓每個人都自動使用它,您需要創建自己的實現(通過擴展您當前使用的實現;使用您的調試器來找出答案).在構造函數中設置特征.
To make everyone use this automatically, you need to create your own implementation (by extending the one which you're currenly using; use your debugger to find out). Set the feature in the constructor.
然后你可以將System屬性javax.xml.parsers.DocumentBuilderFactory
中的新工廠使用到Java VM,每個人都會使用它.
Then you can pass the new factory to use in the System property javax.xml.parsers.DocumentBuilderFactory
to the Java VM and everyone will use it.
這篇關于如何防止 XXE 攻擊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!