การส่งผ่านเอนทิตีภายนอก XML (XXE)
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
หมวดหมู่ OWASP: MASVS-CODE: คุณภาพโค้ด
ภาพรวม
การส่งผ่านเอนทิตีภายนอก XML (XXE) เป็นการโจมตีแอปพลิเคชันที่แยกวิเคราะห์อินพุต XML การโจมตี XXE เกิดขึ้นเมื่ออินพุต XML ที่ไม่น่าเชื่อถือซึ่งมีการอ้างอิงไปยังเอนทิตีภายนอกได้รับการประมวลผลโดยโปรแกรมแยกวิเคราะห์ XML ที่กำหนดค่าไม่ดี การโจมตีนี้สามารถใช้เพื่อจัดเตรียมเหตุการณ์หลายรายการได้ ซึ่งรวมถึงการปฏิเสธการให้บริการ การเข้าถึงระบบไฟล์ หรือการลักลอบนำข้อมูลออก
ผลกระทบ
เมื่อแอปพลิเคชันแยกวิเคราะห์เอกสาร XML แอปพลิเคชันนั้นจะสามารถประมวลผล DTD (คำจำกัดความประเภทเอกสาร หรือที่เรียกว่าเอนทิตีภายนอก) ที่มีอยู่ภายในเอกสาร ผู้โจมตีอาจใช้ประโยชน์จากลักษณะการทำงานนี้โดยการแทรกโค้ดที่เป็นอันตรายเป็น DTD จากนั้นโค้ดนี้จะเข้าถึงบางส่วนของระบบไฟล์ของอุปกรณ์ได้ ซึ่งเข้าถึงได้เฉพาะแอปพลิเคชันและอาจมีข้อมูลที่ละเอียดอ่อน
นอกจากนี้ โค้ดที่เป็นอันตรายนี้ยังส่งคำขอจากอุปกรณ์ได้ ซึ่งอาจหลบเลี่ยงมาตรการรักษาความปลอดภัยรอบนอก
สุดท้ายนี้ หากแอปพลิเคชันขยาย DTD ก็อาจทำให้เกิดสถานการณ์ที่มีเอนทิตีที่อ้างอิงซ้ำหลายครั้ง ซึ่งจะทำให้ทรัพยากรของอุปกรณ์หมดลงและนำไปสู่การปฏิเสธการให้บริการ
การลดปัญหา
ปิดใช้ DTD
วิธีป้องกัน XXE ที่ปลอดภัยที่สุดคือการปิดใช้ DTD (เอนทิตีภายนอก) อย่างสมบูรณ์เสมอ วิธีการอาจคล้ายกับตัวอย่างต่อไปนี้สำหรับไลบรารี XML Pull Parser ทั้งนี้ขึ้นอยู่กับโปรแกรมแยกวิเคราะห์ที่ใช้
Java
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
Kotlin
val factory = XmlPullParserFactory.newInstance()
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true)
การปิดใช้ DTD ยังทำให้โปรแกรมแยกวิเคราะห์ปลอดภัยจากการโจมตีแบบปฏิเสธการให้บริการอีกด้วย
หากปิดใช้ DTD ไม่ได้เลย จะต้องปิดใช้การประกาศประเภทเอกสารและเอนทิตีภายนอกด้วยวิธีที่เฉพาะเจาะจงสำหรับโปรแกรมแยกวิเคราะห์แต่ละรายการ
เนื่องจากมีเครื่องมือแยกวิเคราะห์ XML จำนวนมากในตลาด วิธีป้องกันไม่ให้มีการโจมตี XXE จึงแตกต่างกันไปในแต่ละเครื่องมือ คุณอาจต้องดูข้อมูลเพิ่มเติมในเอกสารประกอบของเครื่องมือ
คุณควรกำหนดค่าแอปพลิเคชันใหม่เพื่อไม่ให้ผู้ใช้แทรกโค้ดที่ตนต้องการในบรรทัดนำหน้าของเอกสาร XML การดำเนินการนี้ต้องได้รับการยืนยันจากฝั่งเซิร์ฟเวอร์ เนื่องจากการควบคุมฝั่งไคลเอ็นต์อาจถูกข้ามได้
ใช้ไลบรารีอื่น
หากกำหนดค่าไลบรารีหรือวิธีการที่ใช้ในลักษณะที่ปลอดภัยไม่ได้ คุณควรพิจารณาใช้ไลบรารีหรือวิธีการอื่น ทั้ง XML Pull Parser และ SAX Parser สามารถกําหนดค่าได้อย่างปลอดภัยโดยไม่อนุญาตให้ใช้ DTD และเอนทิตี้
แหล่งข้อมูล
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-26 UTC"],[],[],null,["# XML External Entities Injections (XXE)\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-CODE: Code Quality](https://mas.owasp.org/MASVS/10-MASVS-CODE)\n\nOverview\n--------\n\nAn XML eXternal Entity injection (XXE) is an attack against applications that\nparse XML input. An XXE attack occurs when untrusted XML input with a reference\nto an external entity is processed by a weakly configured XML parser. This\nattack can be used to stage multiple incidents, including denial of service,\nfile system access, or data exfiltration.\n\nImpact\n------\n\nWhen an application parses an XML document, it can process any DTDs (Document\nType Definitions, also known as external entities) contained within the\ndocument. An attacker can exploit this behavior by injecting malicious code as\nDTDs. This code can then access parts of the file system of the device, only\naccessible to the application and potentially containing sensitive data.\nFurthermore, this malicious code can make requests from the device, potentially\nbypassing perimeter security measures.\n\nLastly, if the application expands DTDs,\nthis can create a situation with multiple iterations of referenced entities,\nexhausting the resources of the device and leading to a denial of service.\n\nMitigations\n-----------\n\n### Disable DTDs\n\nThe safest way to prevent XXE is to always disable DTDs (external entities)\ncompletely. Depending on the parser in use, the method could be similar to the\nfollowing example for the XML Pull Parser library: \n\n### Java\n\n XmlPullParserFactory factory = XmlPullParserFactory.newInstance();\n factory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true);\n\n### Kotlin\n\n val factory = XmlPullParserFactory.newInstance()\n factory.setFeature(\"http://apache.org/xml/features/disallow-doctype-decl\", true)\n\nDisabling DTDs also makes the parser secure against denial of service attacks.\nIf it is not possible to disable DTDs completely, then external entities and\nexternal document type declarations must be disabled in a way that's specific to\neach parser.\n\nBecause of the large number of XML parsing engines in the market, the ways to\nprevent XXE attacks differ from engine to engine. You may need to refer to your\nengine documentation for more information.\n\n### Perform input sanitisation\n\nThe application should be reconfigured so that it does not allow users to inject\narbitrary code in the XML document's preamble. This has to be verified\nserver-side, as client-side controls can be bypassed.\n\n### Use a different library\n\nIf the library or method used cannot be configured in a secure manner, a\ndifferent one should be considered. [XML Pull Parser](/reference/org/xmlpull/v1/XmlPullParser) and [SAX Parser](/reference/javax/xml/parsers/SAXParser) can\nboth be configured in a secure manner, disallowing DTDs and entities.\n\nResources\n---------\n\n- [OWASP XXE](https://owasp.org/www-community/vulnerabilities/XML_External_Entity_(XXE)_Processing)\n- [OWASP XXE Prevention Cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/XML_External_Entity_Prevention_Cheat_Sheet.html)\n- [XML Constants: FEATURE_SECURE_PROCESSING](/reference/javax/xml/XMLConstants#FEATURE_SECURE_PROCESSING)\n- [XML Pull Parser](/reference/org/xmlpull/v1/XmlPullParser)\n- [SAX Parser](/reference/javax/xml/parsers/SAXParser)"]]