|
| 1 | +package de.dominikschadow.xpathi; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import javax.xml.parsers.DocumentBuilder; |
| 6 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 7 | +import javax.xml.xpath.XPathConstants; |
| 8 | +import javax.xml.xpath.XPathExpression; |
| 9 | +import javax.xml.xpath.XPathExpressionException; |
| 10 | +import javax.xml.xpath.XPathFactory; |
| 11 | + |
| 12 | +import org.w3c.dom.Document; |
| 13 | +import org.w3c.dom.Element; |
| 14 | +import org.w3c.dom.Node; |
| 15 | +import org.w3c.dom.NodeList; |
| 16 | + |
| 17 | +public class XPathInjectionSample { |
| 18 | + private Document doc; |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + XPathInjectionSample xis = new XPathInjectionSample(); |
| 22 | + xis.parseXML(); |
| 23 | + // valid input |
| 24 | + xis.evaluateXPath("/customers/customer[@name='Maier' and @password='MaierPassword']/orderLimit"); |
| 25 | + // invalid input |
| 26 | + xis.evaluateXPath("/customers/customer[@name='dummy' and @password='' or '1' = '1']/orderLimit"); |
| 27 | + // Blind XPath Injection |
| 28 | + xis.evaluateXPath("/customers/customer[@name='dummy' and @password=''] | /* | /foo[bar='']/orderLimit"); |
| 29 | + } |
| 30 | + |
| 31 | + private void evaluateXPath(String xpath) { |
| 32 | + System.out.println("XPath " + xpath); |
| 33 | + System.out.println("-----------------------"); |
| 34 | + |
| 35 | + try { |
| 36 | + XPathExpression expression = XPathFactory.newInstance().newXPath().compile(xpath); |
| 37 | + Object result = expression.evaluate(doc, XPathConstants.NODESET); |
| 38 | + |
| 39 | + NodeList nodes = (NodeList) result; |
| 40 | + for (int i = 0; i < nodes.getLength(); i++) { |
| 41 | + System.out.println(nodes.item(i).getTextContent()); |
| 42 | + } |
| 43 | + } catch (XPathExpressionException ex) { |
| 44 | + ex.printStackTrace(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void parseXML() { |
| 49 | + try { |
| 50 | + File xmlFile = new File("src/main/resources/customer.xml"); |
| 51 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 52 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 53 | + doc = dBuilder.parse(xmlFile); |
| 54 | + doc.getDocumentElement().normalize(); |
| 55 | + |
| 56 | + System.out.println("Root element: " + doc.getDocumentElement().getNodeName()); |
| 57 | + NodeList customer = doc.getElementsByTagName("customer"); |
| 58 | + System.out.println("-----------------------"); |
| 59 | + |
| 60 | + for (int id = 0; id < customer.getLength(); id++) { |
| 61 | + Element element = (Element) customer.item(id); |
| 62 | + System.out.println("ID: " + getAttributeValue("id", element)); |
| 63 | + System.out.println("name: " + getAttributeValue("name", element)); |
| 64 | + System.out.println("status: " + getTagValue("status", element)); |
| 65 | + System.out.println("order limit: " + getTagValue("orderLimit", element)); |
| 66 | + } |
| 67 | + } catch (Exception ex) { |
| 68 | + ex.printStackTrace(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private String getTagValue(String tag, Element element) { |
| 73 | + Node elementNode = element.getElementsByTagName(tag).item(0).getChildNodes().item(0); |
| 74 | + |
| 75 | + return elementNode.getNodeValue(); |
| 76 | + } |
| 77 | + |
| 78 | + private String getAttributeValue(String attribute, Element element) { |
| 79 | + Node attributeNode = element.getAttributes().getNamedItem(attribute); |
| 80 | + |
| 81 | + return attributeNode.getNodeValue(); |
| 82 | + } |
| 83 | +} |
0 commit comments