You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XPath_Injection.java 2.4 kB

3 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package XPath_Injection;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.util.logging.Logger;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.xml.xpath.XPath;
  10. import javax.xml.xpath.XPathConstants;
  11. import javax.xml.xpath.XPathExpressionException;
  12. import javax.xml.xpath.XPathFactory;
  13. import org.w3c.dom.NodeList;
  14. import org.xml.sax.InputSource;
  15. public class XPath_Injection {
  16. static final Logger log = Logger.getLogger("logger");
  17. public void bad(HttpServletRequest request) throws XPathExpressionException {
  18. String username = request.getParameter("name");
  19. String dir = "C:" + File.separator + "EmployeesData.xml";
  20. File d = new File(dir);
  21. XPathFactory factory = XPathFactory.newInstance();
  22. XPath xPath = factory.newXPath();
  23. NodeList nodes = null;
  24. InputStream in = null;
  25. InputSource inputSource = null;
  26. try {
  27. in = new FileInputStream(d);
  28. inputSource = new InputSource(in);
  29. String expression = "/employees/employee[loginID/text()='"
  30. + username + "']";
  31. nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET); // bad XPath注入
  32. log.info(nodes.item(1).getLocalName());
  33. } catch (FileNotFoundException e) {
  34. log.info("FileNotFoundException");
  35. } finally {
  36. try {
  37. if(in!=null){
  38. in.close();
  39. }
  40. } catch (IOException e) {
  41. log.info("IOException");
  42. }
  43. }
  44. }
  45. public void good(HttpServletRequest request)
  46. throws XPathExpressionException {
  47. String username = "foo";
  48. String dir = "C:" + File.separator + "EmployeesData.xml";
  49. File d = new File(dir);
  50. XPathFactory factory = XPathFactory.newInstance();
  51. XPath xPath = factory.newXPath();
  52. NodeList nodes = null;
  53. InputStream in = null;
  54. InputSource inputSource = null;
  55. try {
  56. in = new FileInputStream(d);
  57. inputSource = new InputSource(in);
  58. String expression = "/employees/employee[loginID/text()='"
  59. + username + "']";
  60. nodes = (NodeList) xPath.evaluate(expression, inputSource, XPathConstants.NODESET); // good XPath注入
  61. log.info(nodes.item(1).getLocalName());
  62. } catch (FileNotFoundException e) {
  63. log.info("FileNotFoundException");
  64. } finally {
  65. try {
  66. if(in!=null){
  67. in.close();
  68. }
  69. } catch (IOException e) {
  70. log.info("IOException");
  71. }
  72. }
  73. }
  74. }

No Description

Contributors (1)