Skip to content

Commit 6992ece

Browse files
committed
scan rules: Clean code tweaks
- Add static modifier where applicable - Remove boiler plate or useless comments/JavaDoc attributes. - CHANGELOG > Add maintenance note (if there wasn't already one present). - pscanrules > Made resource message methods private again where example alerts have been implemented. Signed-off-by: kingthorin <[email protected]>
1 parent 9ab7743 commit 6992ece

File tree

151 files changed

+355
-691
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+355
-691
lines changed

addOns/ascanrules/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
99
- The following rules now includes example alert functionality for documentation generation purposes (Issue 6119), as well as now including Alert Tags (OWASP Top 10, WSTG, and updated CWE):
1010
- Server Side Template Injection
1111
- Server Side Template Injection (Blind)
12+
- Maintenance changes.
1213

1314
### Fixed
1415
- False positives in the Path Traversal rule.

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/BufferOverflowScanRule.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public String getOther() {
101101
public void scan(HttpMessage msg, String param, String value) {
102102

103103
if (this.isStop()) { // Check if the user stopped things
104-
LOGGER.debug("Scanner {} Stopping.", this.getName());
104+
LOGGER.debug("Stopping scan rule \"{}\".", this.getName());
105105
return; // Stop!
106106
}
107107
if (isPage500(getBaseMsg())) // Check to see if the page closed initially
@@ -159,17 +159,15 @@ public Map<String, String> getAlertTags() {
159159

160160
@Override
161161
public int getCweId() {
162-
// The CWE id
163162
return 120;
164163
}
165164

166165
@Override
167166
public int getWascId() {
168-
// The WASC ID
169167
return 7;
170168
}
171169

172-
private String randomCharacterString(int length) {
170+
private static String randomCharacterString(int length) {
173171
StringBuilder sb1 = new StringBuilder(length + 1);
174172
int counter = 0;
175173
int character = 0;

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/CodeInjectionScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ public void init() {
155155
@Override
156156
public void scan(HttpMessage msg, String paramName, String value) {
157157

158-
// Begin scan rule execution
159158
LOGGER.debug(
160159
"Checking [{}][{}], parameter [{}] for Dynamic Code Injection Vulnerabilities",
161160
msg.getRequestHeader().getMethod(),

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/CommandInjectionScanRule.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ public class CommandInjectionScanRule extends AbstractAppParamPlugin
282282
NIX_BLIND_OS_PAYLOADS.add("|" + insertedCMD + "#");
283283
}
284284

285-
// Logger instance
286285
private static final Logger LOGGER = LogManager.getLogger(CommandInjectionScanRule.class);
287286

288287
// Get WASC Vulnerability description
@@ -366,7 +365,7 @@ public int getRisk() {
366365
return Alert.RISK_HIGH;
367366
}
368367

369-
private String getOtherInfo(TestType testType, String testValue) {
368+
private static String getOtherInfo(TestType testType, String testValue) {
370369
return Constant.messages.getString(
371370
MESSAGE_PREFIX + "otherinfo." + testType.getNameKey(), testValue);
372371
}
@@ -405,7 +404,6 @@ int getTimeSleep() {
405404
@Override
406405
public void scan(HttpMessage msg, String paramName, String value) {
407406

408-
// Begin scan rule execution
409407
LOGGER.debug(
410408
"Checking [{}][{}], parameter [{}] for OS Command Injection Vulnerabilities",
411409
msg.getRequestHeader().getMethod(),

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/DirectoryBrowsingScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public String getReference() {
9595
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
9696
}
9797

98-
private void checkIfDirectory(HttpMessage msg) throws URIException {
98+
private static void checkIfDirectory(HttpMessage msg) throws URIException {
9999

100100
URI uri = msg.getRequestHeader().getURI();
101101
uri.setQuery(null);

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/ExternalRedirectScanRule.java

+4-26
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,6 @@ public String getReference() {
147147
return VULN.getReferencesAsString();
148148
}
149149

150-
/**
151-
* Scan for External Redirect vulnerabilities
152-
*
153-
* @param msg a request only copy of the original message (the response isn't copied)
154-
* @param param the parameter name that need to be exploited
155-
* @param value the original parameter value
156-
*/
157150
@Override
158151
public void scan(HttpMessage msg, String param, String value) {
159152

@@ -342,7 +335,7 @@ private static boolean isRedirectHost(String value, boolean escaped) throws URIE
342335
* @param msg the current message where reflected redirection should be check into
343336
* @return get back the redirection type if exists
344337
*/
345-
private int isRedirected(String payload, HttpMessage msg) {
338+
private static int isRedirected(String payload, HttpMessage msg) {
346339

347340
// (1) Check if redirection by "Location" header
348341
// http://en.wikipedia.org/wiki/HTTP_location
@@ -471,7 +464,7 @@ private static boolean isRedirectPresent(Pattern pattern, String value) {
471464
* @param type the redirection type
472465
* @return a string representing the reason of this redirection
473466
*/
474-
private String getRedirectionReason(int type) {
467+
private static String getRedirectionReason(int type) {
475468
switch (type) {
476469
case REDIRECT_LOCATION_HEADER:
477470
return Constant.messages.getString(MESSAGE_PREFIX + "reason.location.header");
@@ -493,11 +486,6 @@ private String getRedirectionReason(int type) {
493486
}
494487
}
495488

496-
/**
497-
* Give back the risk associated to this vulnerability (high)
498-
*
499-
* @return the risk according to the Alert enum
500-
*/
501489
@Override
502490
public int getRisk() {
503491
return Alert.RISK_HIGH;
@@ -508,24 +496,14 @@ public Map<String, String> getAlertTags() {
508496
return ALERT_TAGS;
509497
}
510498

511-
/**
512-
* http://cwe.mitre.org/data/definitions/601.html
513-
*
514-
* @return the official CWE id
515-
*/
516499
@Override
517500
public int getCweId() {
518-
return 601;
501+
return 601; // CWE-601: URL Redirection to Untrusted Site ('Open Redirect')
519502
}
520503

521-
/**
522-
* http://projects.webappsec.org/w/page/13246981/URL%20Redirector%20Abuse
523-
*
524-
* @return the official WASC id
525-
*/
526504
@Override
527505
public int getWascId() {
528-
return 38;
506+
return 38; // URL Redirector Abuse
529507
}
530508

531509
@Override

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/FormatStringScanRule.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,15 @@ public String getReference() {
105105
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
106106
}
107107

108-
private String getError(char c) {
108+
private static String getError(char c) {
109109
return Constant.messages.getString(MESSAGE_PREFIX + "error" + c);
110110
}
111111

112-
/*
113-
* This method is called by the active scanner for each GET and POST parameter for every page
114-
* @see org.parosproxy.paros.core.scanner.AbstractAppParamPlugin#scan(org.parosproxy.paros.network.HttpMessage, java.lang.String, java.lang.String)
115-
*/
116112
@Override
117113
public void scan(HttpMessage msg, String param, String value) {
118114

119115
if (this.isStop()) { // Check if the user stopped things
120-
LOGGER.debug("Scanner {} Stopping.", getName());
116+
LOGGER.debug("Stopping scan rule \"{}\".", getName());
121117
return; // Stop!
122118
}
123119

@@ -223,7 +219,7 @@ && isPage200(verificationMsg)) {
223219
// errors. It is only
224220
// used if the GNU and generic C compiler check fails to find a vulnerability.
225221
if (this.isStop()) { // Check if the user stopped things
226-
LOGGER.debug("Scanner {} Stopping.", getName());
222+
LOGGER.debug("Stopping scan rule \"{}\".", getName());
227223
return; // Stop!
228224
}
229225
StringBuilder sb2 = new StringBuilder();
@@ -276,14 +272,12 @@ public Map<String, String> getAlertTags() {
276272

277273
@Override
278274
public int getCweId() {
279-
// The CWE id
280-
return 134;
275+
return 134; // CWE-134: Use of Externally-Controlled Format String
281276
}
282277

283278
@Override
284279
public int getWascId() {
285-
// The WASC ID
286-
return 6;
280+
return 6; // Format String
287281
}
288282

289283
private AlertBuilder createBaseAlert(

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/GetForPostScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void scan() {
8888
// Check if the user stopped things. One request per URL so check before
8989
// sending the request
9090
if (isStop()) {
91-
LOGGER.debug("Scan rule {} Stopping.", getName());
91+
LOGGER.debug("Stopping scan rule \"{}\".", getName());
9292
return;
9393
}
9494

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/HeartBleedActiveScanRule.java

-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class HeartBleedActiveScanRule extends AbstractHostPlugin
5151
/** the timeout, which is controlled by the Attack Strength */
5252
private int timeoutMs = 0;
5353

54-
/** the logger object */
5554
private static final Logger LOGGER = LogManager.getLogger(HeartBleedActiveScanRule.class);
5655

5756
/** Prefix for internationalized messages used by this rule */
@@ -868,7 +867,6 @@ public class HeartBleedActiveScanRule extends AbstractHostPlugin
868867
0x40,
869868
0x00 // payload length to be sent back by the server. 0x40 0x00 = 16384 in decimal
870869
// Note: No actual payload sent!
871-
// Note: No actual padding sent!
872870
};
873871

874872
@Override

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/HiddenFilesScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public void scan() {
113113
for (HiddenFile file : hfList) {
114114

115115
if (isStop()) {
116-
LOGGER.debug("Scan rule {} stopping.", getName());
116+
LOGGER.debug("Stopping scan rule \"{}\".", getName());
117117
return;
118118
}
119119

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/PaddingOracleScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private String getEmptyValueResponse(String paramName) throws IOException {
267267
* @param value the value that need to be checked
268268
* @return true if it seems to be encrypted
269269
*/
270-
private boolean isEncrypted(byte[] value) {
270+
private static boolean isEncrypted(byte[] value) {
271271

272272
// Make sure we have a reasonable sized string
273273
// (encrypted strings tend to be long, and short strings tend to break our numbers)

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/PathTraversalScanRule.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@
4949
public class PathTraversalScanRule extends AbstractAppParamPlugin
5050
implements CommonActiveScanRuleInfo {
5151

52-
/*
53-
* Prefix for internationalised messages used by this rule
54-
*/
52+
// Prefix for internationalised messages used by this rule
5553
private static final String MESSAGE_PREFIX = "ascanrules.pathtraversal.";
5654

5755
private static final Map<String, String> ALERT_TAGS =
@@ -608,7 +606,7 @@ private boolean sendAndCheckPayload(
608606
return false;
609607
}
610608

611-
private String getContentsToMatch(HttpMessage message) {
609+
private static String getContentsToMatch(HttpMessage message) {
612610
return message.getResponseHeader().isHtml()
613611
? StringEscapeUtils.unescapeHtml4(message.getResponseBody().toString())
614612
: message.getResponseHeader().toString() + message.getResponseBody().toString();
@@ -700,7 +698,7 @@ public String match(String contents) {
700698
return matchWinDirectories(contents);
701699
}
702700

703-
private String matchNixDirectories(String contents) {
701+
private static String matchNixDirectories(String contents) {
704702
Pattern procPattern =
705703
Pattern.compile("(?:^|\\W)proc(?:\\W|$)", Pattern.CASE_INSENSITIVE);
706704
Pattern etcPattern = Pattern.compile("(?:^|\\W)etc(?:\\W|$)", Pattern.CASE_INSENSITIVE);
@@ -727,7 +725,7 @@ private String matchNixDirectories(String contents) {
727725
return null;
728726
}
729727

730-
private String matchWinDirectories(String contents) {
728+
private static String matchWinDirectories(String contents) {
731729
if (contents.contains("Windows")
732730
&& Pattern.compile("Program\\sFiles").matcher(contents).find()) {
733731
return "Windows";

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/RemoteCodeExecutionCve20121823ScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class RemoteCodeExecutionCve20121823ScanRule extends AbstractAppPlugin
5454
*/
5555
private static final Vulnerability VULN = Vulnerabilities.getDefault().get("wasc_20");
5656

57-
/** the logger object */
5857
private static final Logger LOGGER =
5958
LogManager.getLogger(RemoteCodeExecutionCve20121823ScanRule.class);
6059

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/RemoteFileIncludeScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.zaproxy.addon.commonlib.vulnerabilities.Vulnerabilities;
3838
import org.zaproxy.addon.commonlib.vulnerabilities.Vulnerability;
3939

40-
/** a scanner that looks for Remote File Include vulnerabilities */
40+
/** a scan rule that looks for Remote File Include vulnerabilities */
4141
public class RemoteFileIncludeScanRule extends AbstractAppParamPlugin
4242
implements CommonActiveScanRuleInfo {
4343

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SourceCodeDisclosureWebInfScanRule.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
import org.zaproxy.addon.commonlib.vulnerabilities.Vulnerability;
4747

4848
/**
49-
* a scanner that looks for Java classes disclosed via the WEB-INF folder and that decompiles them
50-
* to give the Java source code. The scanner also looks for easy pickings in the form of properties
49+
* a scan rule that looks for Java classes disclosed via the WEB-INF folder and that decompiles them
50+
* to give the Java source code. The rule also looks for easy pickings in the form of properties
5151
* files loaded by the Java class.
5252
*
5353
* @author 70pointer
@@ -270,14 +270,8 @@ private HttpMessage createHttpMessage(URI uri) throws HttpMalformedHeaderExcepti
270270
return msg;
271271
}
272272

273-
/**
274-
* gets a candidate URI for a given class path.
275-
*
276-
* @param classname
277-
* @return
278-
* @throws URIException
279-
*/
280-
private URI getClassURI(URI hostURI, String classname) throws URIException {
273+
/** gets a candidate URI for a given class path. */
274+
private static URI getClassURI(URI hostURI, String classname) throws URIException {
281275
return new URI(
282276
hostURI.getScheme()
283277
+ "://"
@@ -288,7 +282,7 @@ private URI getClassURI(URI hostURI, String classname) throws URIException {
288282
false);
289283
}
290284

291-
private URI getPropsFileURI(URI hostURI, String propsfilename) throws URIException {
285+
private static URI getPropsFileURI(URI hostURI, String propsfilename) throws URIException {
292286
return new URI(
293287
hostURI.getScheme()
294288
+ "://"

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/Spring4ShellScanRule.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ public String getDescription() {
7676
return Constant.messages.getString("ascanrules.spring4shell.desc");
7777
}
7878

79-
private boolean is400Response(HttpMessage msg) {
79+
private static boolean is400Response(HttpMessage msg) {
8080
return !msg.getResponseHeader().isEmpty() && msg.getResponseHeader().getStatusCode() == 400;
8181
}
8282

83-
private void setGetPayload(HttpMessage msg, String payload) throws URIException {
83+
private static void setGetPayload(HttpMessage msg, String payload) throws URIException {
8484
msg.getRequestHeader().setMethod("GET");
8585
URI uri = msg.getRequestHeader().getURI();
8686
String query = uri.getEscapedQuery();
@@ -92,7 +92,7 @@ private void setGetPayload(HttpMessage msg, String payload) throws URIException
9292
uri.setEscapedQuery(query);
9393
}
9494

95-
private void setPostPayload(HttpMessage msg, String payload) {
95+
private static void setPostPayload(HttpMessage msg, String payload) {
9696
msg.getRequestHeader().setMethod("POST");
9797
String body = msg.getRequestBody().toString();
9898
if (body.isEmpty()

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SpringActuatorScanRule.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public void scan() {
126126
for (String endpoint : endpointList) {
127127
for (String encodingType : encodingTypes) {
128128
if (isStop()) {
129-
LOGGER.debug("Scan rule {} stopping.", getName());
129+
LOGGER.debug("Stopping scan rule \"{}\".", getName());
130130
return;
131131
}
132132
HttpMessage testMsg = sendActuatorRequest(encodingType, endpoint);

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionHypersonicScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,6 @@ public class SqlInjectionHypersonicScanRule extends AbstractAppParamPlugin
194194
CommonAlertTag.OWASP_2017_A01_INJECTION,
195195
CommonAlertTag.WSTG_V42_INPV_05_SQLI);
196196

197-
/** for logging. */
198197
private static final Logger LOGGER = LogManager.getLogger(SqlInjectionHypersonicScanRule.class);
199198

200199
/** The number of seconds used in time-based attacks (i.e. sleep commands). */

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionMsSqlScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public class SqlInjectionMsSqlScanRule extends AbstractAppParamPlugin
130130
private static final double TIME_CORRELATION_ERROR_RANGE = 0.15;
131131
private static final double TIME_SLOPE_ERROR_RANGE = 0.30;
132132

133-
/** for logging. */
134133
private static final Logger LOGGER = LogManager.getLogger(SqlInjectionMsSqlScanRule.class);
135134

136135
private static final Map<String, String> ALERT_TAGS =

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionMySqlScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ public class SqlInjectionMySqlScanRule extends AbstractAppParamPlugin
213213
CommonAlertTag.OWASP_2017_A01_INJECTION,
214214
CommonAlertTag.WSTG_V42_INPV_05_SQLI);
215215

216-
/** for logging. */
217216
private static final Logger LOGGER = LogManager.getLogger(SqlInjectionMySqlScanRule.class);
218217

219218
private int timeSleepSeconds = DEFAULT_SLEEP_TIME;

addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/SqlInjectionOracleScanRule.java

-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ public class SqlInjectionOracleScanRule extends AbstractAppParamPlugin
149149
CommonAlertTag.OWASP_2017_A01_INJECTION,
150150
CommonAlertTag.WSTG_V42_INPV_05_SQLI);
151151

152-
/** for logging. */
153152
private static final Logger LOGGER = LogManager.getLogger(SqlInjectionOracleScanRule.class);
154153

155154
@Override

0 commit comments

Comments
 (0)