Skip to content

Commit 84720db

Browse files
committed
ascanrulesAlpha: Add example alerts to example rules
- CHANGELOG > Added change note. - Scan Rules > Added example alert handling, updated to conform to the common active scan rule tests. - Scan Rule Unit Tests > Added to assert the example alert and references, as well as common tests. Signed-off-by: kingthorin <[email protected]>
1 parent de14e29 commit 84720db

File tree

7 files changed

+162
-20
lines changed

7 files changed

+162
-20
lines changed

addOns/ascanrulesAlpha/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
77
### Changed
88
- Update minimum ZAP version to 2.15.0.
99
- Maintenance changes.
10+
- The Example scan rules now include example alerts in order to be more representative of what's expected (Issue 6119).
1011

1112
### Fixed
1213
- Alert text for various rules has been updated to more consistently use periods and spaces in a uniform manner.

addOns/ascanrulesAlpha/src/main/java/org/zaproxy/zap/extension/ascanrulesAlpha/ExampleFileActiveScanRule.java

+17-9
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
*
4444
* @author psiinon
4545
*/
46-
public class ExampleFileActiveScanRule extends AbstractAppParamPlugin {
46+
public class ExampleFileActiveScanRule extends AbstractAppParamPlugin
47+
implements CommonActiveScanRuleInfo {
4748

4849
/** Prefix for internationalized messages used by this rule */
4950
private static final String MESSAGE_PREFIX = "ascanalpha.examplefile.";
@@ -155,14 +156,7 @@ public void scan(HttpMessage msg, String param, String value) {
155156
String evidence;
156157
if ((evidence = doesResponseContainString(msg.getResponseBody(), attack)) != null) {
157158
// Raise an alert
158-
newAlert()
159-
.setConfidence(Alert.CONFIDENCE_MEDIUM)
160-
.setParam(param)
161-
.setAttack(attack)
162-
.setOtherInfo(getOtherInfo())
163-
.setEvidence(evidence)
164-
.setMessage(testMsg)
165-
.raise();
159+
createAlert(param, attack, evidence).setMessage(testMsg).raise();
166160
return;
167161
}
168162
}
@@ -194,6 +188,15 @@ private String doesResponseContainString(HttpBody body, String str) {
194188
return null;
195189
}
196190

191+
private AlertBuilder createAlert(String param, String attack, String evidence) {
192+
return newAlert()
193+
.setConfidence(Alert.CONFIDENCE_MEDIUM)
194+
.setParam(param)
195+
.setAttack(attack)
196+
.setOtherInfo(getOtherInfo())
197+
.setEvidence(evidence);
198+
}
199+
197200
private static List<String> loadFile(String file) {
198201
/*
199202
* ZAP will have already extracted the file from the add-on and put it underneath the 'ZAP home' directory
@@ -244,4 +247,9 @@ public int getWascId() {
244247
// The WASC ID
245248
return 0;
246249
}
250+
251+
@Override
252+
public List<Alert> getExampleAlerts() {
253+
return List.of(createAlert("foo", "<SCRIPT>a=/XSS/", "<SCRIPT>a=/XSS/").build());
254+
}
247255
}

addOns/ascanrulesAlpha/src/main/java/org/zaproxy/zap/extension/ascanrulesAlpha/ExampleSimpleActiveScanRule.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.zaproxy.zap.extension.ascanrulesAlpha;
2121

2222
import java.io.IOException;
23+
import java.util.List;
2324
import java.util.Random;
2425
import org.apache.logging.log4j.LogManager;
2526
import org.apache.logging.log4j.Logger;
@@ -39,7 +40,8 @@
3940
*
4041
* @author psiinon
4142
*/
42-
public class ExampleSimpleActiveScanRule extends AbstractAppParamPlugin {
43+
public class ExampleSimpleActiveScanRule extends AbstractAppParamPlugin
44+
implements CommonActiveScanRuleInfo {
4345

4446
// wasc_10 is Denial of Service - well, its just an example ;)
4547
private static final Vulnerability VULN = Vulnerabilities.getDefault().get("wasc_10");
@@ -59,8 +61,7 @@ public int getId() {
5961

6062
@Override
6163
public String getName() {
62-
// Strip off the "Example Active Scan Rule: " part if implementing a real one ;)
63-
return "Example Active Scan Rule: " + VULN.getName();
64+
return Constant.messages.getString("ascanalpha.examplesimple.name");
6465
}
6566

6667
@Override
@@ -118,12 +119,7 @@ public void scan(HttpMessage msg, String param, String value) {
118119
// For this example we're just going to raise the alert at random!
119120

120121
if (rnd.nextInt(10) == 0) {
121-
newAlert()
122-
.setConfidence(Alert.CONFIDENCE_MEDIUM)
123-
.setParam(param)
124-
.setAttack(value)
125-
.setMessage(testMsg)
126-
.raise();
122+
createAlert(param, attack).setMessage(testMsg).raise();
127123
return;
128124
}
129125

@@ -132,6 +128,10 @@ public void scan(HttpMessage msg, String param, String value) {
132128
}
133129
}
134130

131+
private AlertBuilder createAlert(String param, String attack) {
132+
return newAlert().setConfidence(Alert.CONFIDENCE_MEDIUM).setParam(param).setAttack(attack);
133+
}
134+
135135
@Override
136136
public int getRisk() {
137137
return Alert.RISK_HIGH;
@@ -148,4 +148,9 @@ public int getWascId() {
148148
// The WASC ID
149149
return 0;
150150
}
151+
152+
@Override
153+
public List<Alert> getExampleAlerts() {
154+
return List.of(createAlert("foo", "attack").build());
155+
}
151156
}

addOns/ascanrulesAlpha/src/main/javahelp/org/zaproxy/zap/extension/ascanrulesAlpha/resources/help/contents/ascanalpha.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
<H1>Active Scan Rules - Alpha</H1>
1010
The following alpha status active scan rules are included in this add-on:
1111

12-
<H2>An example active scan rule which loads data from a file</H2>
12+
<H2 id="id-60101">An example active scan rule which loads data from a file</H2>
1313
This implements an example active scan rule that loads strings from a file that the user can edit.<br>
1414
For more details see:
1515
<a href="https://www.zaproxy.org/blog/2014-04-30-hacking-zap-4-active-scan-rules/">Hacking ZAP Part 4: Active Scan Rules</a>.
1616
<p>
1717
Latest code: <a href="https://github.com/zaproxy/zap-extensions/blob/main/addOns/ascanrulesAlpha/src/main/java/org/zaproxy/zap/extension/ascanrulesAlpha/ExampleFileActiveScanRule.java">ExampleFileActiveScanRule.java</a>
1818

19-
<H2>Example Active Scan Rule: Denial of Service</H2>
19+
<H2 id="id-60100">Example Active Scan Rule: Denial of Service</H2>
2020
This implements a very simple example active scan rule.<br>
2121
For more details see:
2222
<a href="https://www.zaproxy.org/blog/2014-04-30-hacking-zap-4-active-scan-rules/">Hacking ZAP Part 4: Active Scan Rules</a>.

addOns/ascanrulesAlpha/src/main/resources/org/zaproxy/zap/extension/ascanrulesAlpha/resources/Messages.properties

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ ascanalpha.examplefile.other = This is for information that doesnt fit in any of
66
ascanalpha.examplefile.refs = https://www.zaproxy.org/blog/2014-04-30-hacking-zap-4-active-scan-rules/
77
ascanalpha.examplefile.soln = A general description of how to solve the problem.
88

9+
ascanalpha.examplesimple.name = "Example Active Scan Rule: Denial of Service"
10+
911
#ascanalpha.ldapinjection.alert.attack=[{0}] field [{1}] set to [{2}]
1012
ascanalpha.ldapinjection.alert.attack = parameter [{0}] set to [{1}]
1113
#ascanalpha.ldapinjection.alert.extrainfo=[{0}] field [{1}] on [{2}] [{3}] may be vulnerable to LDAP injection, using an attack with LDAP meta-characters [{4}], yielding known [{5}] error message [{6}], which was not present in the original response.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Zed Attack Proxy (ZAP) and its related class files.
3+
*
4+
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
5+
*
6+
* Copyright 2024 The ZAP Development Team
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.zaproxy.zap.extension.ascanrulesAlpha;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.endsWith;
24+
import static org.hamcrest.Matchers.equalTo;
25+
import static org.hamcrest.Matchers.hasSize;
26+
import static org.hamcrest.Matchers.is;
27+
28+
import java.util.List;
29+
import org.junit.jupiter.api.Test;
30+
import org.parosproxy.paros.core.scanner.Alert;
31+
32+
public class ExampleFileActiveScanRuleUnitTest
33+
extends ActiveScannerTest<ExampleFileActiveScanRule> {
34+
35+
@Override
36+
protected ExampleFileActiveScanRule createScanner() {
37+
return new ExampleFileActiveScanRule();
38+
}
39+
40+
@Test
41+
void shouldHaveExpectedExample() {
42+
// Given / When
43+
List<Alert> alerts = rule.getExampleAlerts();
44+
// Then
45+
assertThat(alerts, hasSize(1));
46+
Alert alert = alerts.get(0);
47+
assertThat(alert.getParam(), is(equalTo("foo")));
48+
}
49+
50+
@Test
51+
void shouldHaveHelpLink() {
52+
// Given / When
53+
String helplink = rule.getHelpLink();
54+
// Then
55+
assertThat(helplink, endsWith("60101"));
56+
}
57+
58+
@Test
59+
@Override
60+
public void shouldHaveValidReferences() {
61+
super.shouldHaveValidReferences();
62+
}
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Zed Attack Proxy (ZAP) and its related class files.
3+
*
4+
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
5+
*
6+
* Copyright 2024 The ZAP Development Team
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
package org.zaproxy.zap.extension.ascanrulesAlpha;
21+
22+
import static org.hamcrest.MatcherAssert.assertThat;
23+
import static org.hamcrest.Matchers.endsWith;
24+
import static org.hamcrest.Matchers.equalTo;
25+
import static org.hamcrest.Matchers.hasSize;
26+
import static org.hamcrest.Matchers.is;
27+
28+
import java.util.List;
29+
import org.junit.jupiter.api.Test;
30+
import org.parosproxy.paros.core.scanner.Alert;
31+
32+
public class ExampleSimpleActiveScanRuleUnitTest
33+
extends ActiveScannerTest<ExampleSimpleActiveScanRule> {
34+
35+
@Override
36+
protected ExampleSimpleActiveScanRule createScanner() {
37+
return new ExampleSimpleActiveScanRule();
38+
}
39+
40+
@Test
41+
void shouldHaveExpectedExample() {
42+
// Given / When
43+
List<Alert> alerts = rule.getExampleAlerts();
44+
// Then
45+
assertThat(alerts, hasSize(1));
46+
Alert alert = alerts.get(0);
47+
assertThat(alert.getParam(), is(equalTo("foo")));
48+
}
49+
50+
@Test
51+
void shouldHaveHelpLink() {
52+
// Given / When
53+
String helplink = rule.getHelpLink();
54+
// Then
55+
assertThat(helplink, endsWith("60100"));
56+
}
57+
58+
@Test
59+
@Override
60+
public void shouldHaveValidReferences() {
61+
super.shouldHaveValidReferences();
62+
}
63+
}

0 commit comments

Comments
 (0)