Skip to content

Commit

Permalink
Merge pull request #3069 from hocnc/develop
Browse files Browse the repository at this point in the history
Update owasp.java.xxe.javax.xml.parsers.DocumentBuilderFactory rule
  • Loading branch information
colleend authored Aug 28, 2023
2 parents d0b83c7 + d4d45cc commit 4217b4b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion contrib/owasp/java/xxe/documentbuilderfactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public String xmlReaderVuln(HttpServletRequest request) {
String body = WebUtils.getRequestBody(request);
logger.info(body);
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.parse(new InputSource(new StringReader(body))); // parse xml
xmlReader.parse(new InputSource(new StringReader(body))); // parse xmldocumentbuilderfactory
return "xmlReader xxe vuln code";
} catch (Exception e) {
logger.error(e.toString());
Expand Down Expand Up @@ -292,6 +292,41 @@ public String DocumentBuilderVuln02(HttpServletRequest request) {
}


@RequestMapping(value = "/DocumentBuilder/vuln03", method = RequestMethod.POST)
public String DocumentBuilderVuln03(HttpServletRequest request) {
try {
String body = WebUtils.getRequestBody(request);
logger.info(body);
// ruleid:owasp.java.xxe.javax.xml.parsers.DocumentBuilderFactory
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
StringReader sr = new StringReader(body);
InputSource is = new InputSource(sr);
Document document = db.parse(is); // parse xml

// 遍历xml节点name和value
StringBuilder result = new StringBuilder();
NodeList rootNodeList = document.getChildNodes();
for (int i = 0; i < rootNodeList.getLength(); i++) {
Node rootNode = rootNodeList.item(i);
NodeList child = rootNode.getChildNodes();
for (int j = 0; j < child.getLength(); j++) {
Node node = child.item(j);
// 正常解析XML,需要判断是否是ELEMENT_NODE类型。否则会出现多余的的节点。
if (child.item(j).getNodeType() == Node.ELEMENT_NODE) {
result.append(String.format("%s: %s\n", node.getNodeName(), node.getFirstChild()));
}
}
}
sr.close();
return result.toString();
} catch (Exception e) {
logger.error(e.toString());
return EXCEPT;
}
}



@RequestMapping(value = "/DocumentBuilder/Sec", method = RequestMethod.POST)
public String DocumentBuilderSec(HttpServletRequest request) {
try {
Expand Down
2 changes: 1 addition & 1 deletion contrib/owasp/java/xxe/documentbuilderfactory.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rules:
DocumentBuilder $DB = $DBF.newDocumentBuilder();
...
$DB.parse(...);
- pattern: DocumentBuilderFactory $DBF = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- pattern: DocumentBuilder $DB = DocumentBuilderFactory.newInstance().newDocumentBuilder();
- pattern-not-inside: |
$RETURNTYPE $METHOD(...) {
...
Expand Down

0 comments on commit 4217b4b

Please sign in to comment.