|
4 | 4 | import org.springframework.web.bind.annotation.*; |
5 | 5 | import javax.servlet.http.HttpServletRequest; |
6 | 6 | import org.w3c.dom.Document; |
| 7 | +import org.w3c.dom.Node; |
| 8 | +import org.w3c.dom.NodeList; |
7 | 9 | import org.xml.sax.helpers.XMLReaderFactory; |
8 | 10 | import org.xml.sax.XMLReader; |
9 | 11 | import java.io.StringReader; |
@@ -32,7 +34,6 @@ public static String xxe_xmlReader(HttpServletRequest request) { |
32 | 34 | String xml_con = request.getParameter("xml").toString(); |
33 | 35 | System.out.println(xml_con); |
34 | 36 | XMLReader xmlReader = XMLReaderFactory.createXMLReader(); |
35 | | - |
36 | 37 | // fix code start |
37 | 38 |
|
38 | 39 | // xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
@@ -128,5 +129,50 @@ public static String xxe_DocumentBuilder(HttpServletRequest request) { |
128 | 129 | } |
129 | 130 |
|
130 | 131 |
|
| 132 | + @RequestMapping("/DocumentBuilder_xinclude") |
| 133 | + @ResponseBody |
| 134 | + public static String xxe_xinclude_DocumentBuilder(HttpServletRequest request) { |
| 135 | + try { |
| 136 | + String xml_con = request.getParameter("xml").toString(); |
| 137 | + System.out.println(xml_con); |
| 138 | + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
| 139 | + |
| 140 | + dbf.setXIncludeAware(true); // 支持XInclude |
| 141 | + dbf.setNamespaceAware(true); |
| 142 | + |
| 143 | + // fix code start |
| 144 | + |
| 145 | +// dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); |
| 146 | +// dbf.setFeature("http://xml.org/sax/features/external-general-entities", false); |
| 147 | +// dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); |
| 148 | + |
| 149 | + // fix code end |
| 150 | + |
| 151 | + DocumentBuilder db = dbf.newDocumentBuilder(); |
| 152 | + StringReader sr = new StringReader(xml_con); |
| 153 | + InputSource is = new InputSource(sr); |
| 154 | + Document document = db.parse(is); // parse xml |
| 155 | + |
| 156 | + NodeList rootNodeList = document.getChildNodes(); |
| 157 | + |
| 158 | + for (int i = 0; i < rootNodeList.getLength(); i++) { |
| 159 | + Node rootNode = rootNodeList.item(i); |
| 160 | + NodeList xxe = rootNode.getChildNodes(); |
| 161 | + for (int j = 0; j < xxe.getLength(); j++) { |
| 162 | + Node xxeNode = xxe.item(j); |
| 163 | + System.out.println("xxeNode: " + xxeNode.getNodeValue()); // 回显 |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + sr.close(); |
| 169 | + return "test"; |
| 170 | + } catch (Exception e) { |
| 171 | + System.out.println(e); |
| 172 | + return "except"; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + |
131 | 177 |
|
132 | 178 | } |
0 commit comments