|
| 1 | +/** |
| 2 | + * OWASP Benchmark Project |
| 3 | + * |
| 4 | + * This file is part of the Open Web Application Security Project (OWASP) |
| 5 | + * Benchmark Project For details, please see |
| 6 | + * <a href="https://www.owasp.org/index.php/Benchmark">https://www.owasp.org/index.php/Benchmark</a>. |
| 7 | + * |
| 8 | + * The OWASP Benchmark is free software: you can redistribute it and/or modify it under the terms |
| 9 | + * of the GNU General Public License as published by the Free Software Foundation, version 2. |
| 10 | + * |
| 11 | + * The OWASP Benchmark is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without |
| 12 | + * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details |
| 14 | + * |
| 15 | + * @author Dave Wichers <a href="https://www.aspectsecurity.com">Aspect Security</a> |
| 16 | + * @created 2015 |
| 17 | + */ |
| 18 | + |
| 19 | +package org.owasp.benchmark.score.parsers; |
| 20 | + |
| 21 | +import java.io.BufferedReader; |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileReader; |
| 24 | +import java.text.SimpleDateFormat; |
| 25 | +import java.util.Date; |
| 26 | + |
| 27 | +import org.json.JSONObject; |
| 28 | + |
| 29 | +public class HCLReader extends Reader { |
| 30 | + |
| 31 | + public static void main(String[] args) throws Exception { |
| 32 | + File f = new File("results/HCL-IAST.hcl"); |
| 33 | + HCLReader cr = new HCLReader(); |
| 34 | + cr.parse(f); |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + public TestResults parse(File f) throws Exception { |
| 39 | + TestResults tr = new TestResults("HCL IAST", true, TestResults.ToolType.IAST); |
| 40 | + |
| 41 | + BufferedReader reader = new BufferedReader(new FileReader(f)); |
| 42 | + String firstLine = null; |
| 43 | + String lastLine = ""; |
| 44 | + String line = ""; |
| 45 | + tr.setToolVersion("1.0"); |
| 46 | + while (line != null) { |
| 47 | + try { |
| 48 | + line = reader.readLine(); |
| 49 | + if (line != null) { |
| 50 | + if (line.contains("writeVulnerabilityToFile")) { |
| 51 | + parseFindings(tr, line); |
| 52 | + } else if (line.contains("Agent Version:")) { |
| 53 | + String version = line.substring(line.indexOf("Version:") + 8); |
| 54 | + tr.setToolVersion(version.trim()); |
| 55 | + } else if (line.contains("[checking URL:") && line.contains("BenchmarkTest00001")) { |
| 56 | + firstLine = line; |
| 57 | + } else if (line.contains("[checking URL:")) { |
| 58 | + lastLine = line; |
| 59 | + } |
| 60 | + } |
| 61 | + } catch (Exception ex) { |
| 62 | + ex.printStackTrace(); |
| 63 | + } |
| 64 | + } |
| 65 | + reader.close(); |
| 66 | + tr.setTime(calculateTime(firstLine, lastLine)); |
| 67 | + return tr; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + private void parseFindings(TestResults tr, String json) throws Exception { |
| 72 | + TestCaseResult tcr = new TestCaseResult(); |
| 73 | + |
| 74 | + try { |
| 75 | + String splitJson = json.split("writeVulnerabilityToFile - ")[1]; |
| 76 | + JSONObject obj = new JSONObject(splitJson); |
| 77 | + JSONObject result = obj.getJSONObject("result"); |
| 78 | + |
| 79 | + String ruleId = result.getString( "ruleId" ); |
| 80 | + tcr.setCWE(cweLookup(ruleId)); |
| 81 | + tcr.setCategory(ruleId); |
| 82 | + |
| 83 | + JSONObject request = result.getJSONObject("request"); |
| 84 | + String uri = request.getString("uri" ); |
| 85 | + |
| 86 | + if ( uri.contains( "BenchmarkTest" ) ) { |
| 87 | + String testNumber = uri.substring( uri.lastIndexOf('/') + "BenchmarkTest".length() + 1 ); |
| 88 | + tcr.setNumber(Integer.parseInt(testNumber)); |
| 89 | + if (tcr.getCWE() != 0) { |
| 90 | + // System.out.println( tcr.getNumber() + "\t" + tcr.getCWE() + "\t" + tcr.getCategory() ); |
| 91 | + tr.put(tcr); |
| 92 | + } |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + // System.err.println("> Parse error: " + json); |
| 96 | + // e.printStackTrace(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + private static int cweLookup(String rule) { |
| 102 | + switch (rule) { |
| 103 | + case "SessionManagement.Cookies": |
| 104 | + return 614; // insecure cookie use |
| 105 | + case "Injection.SQL": |
| 106 | + return 89; // sql injection |
| 107 | + case "Injection.OS": |
| 108 | + return 78; // command injection |
| 109 | + case "Injection.LDAP": |
| 110 | + return 90; // ldap injection |
| 111 | + case "CrossSiteScripting.Reflected": |
| 112 | + return 79; // xss |
| 113 | + case "Injection.XPath": |
| 114 | + return 643; // xpath injection |
| 115 | + case "PathTraversal": |
| 116 | + return 22; // path traversal |
| 117 | + case "Cryptography.Mac": |
| 118 | + return 328; // weak hash |
| 119 | + case "Cryptography.PoorEntropy": |
| 120 | + return 330; // weak random |
| 121 | + case "Cryptography.Ciphers": |
| 122 | + return 327; // weak encryption |
| 123 | + case "Validation.Required": |
| 124 | + return 501; // trust boundary |
| 125 | + } |
| 126 | + return 0; |
| 127 | + } |
| 128 | + |
| 129 | + private String calculateTime(String firstLine, String lastLine) { |
| 130 | + try { |
| 131 | + String start = firstLine.split(" ")[0]; |
| 132 | + String stop = lastLine.split(" ")[0]; |
| 133 | + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); |
| 134 | + Date startTime = sdf.parse(start); |
| 135 | + Date stopTime = sdf.parse(stop); |
| 136 | + long startMillis = startTime.getTime(); |
| 137 | + long stopMillis = stopTime.getTime(); |
| 138 | + long seconds = (stopMillis - startMillis) / 1000; |
| 139 | + return seconds + " seconds"; |
| 140 | + } catch (Exception e) { |
| 141 | + e.printStackTrace(); |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments