Skip to content

Commit 5c9c46e

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 ebdc081 commit 5c9c46e

File tree

6 files changed

+143
-22
lines changed

6 files changed

+143
-22
lines changed

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

+19-11
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.";
@@ -80,7 +81,7 @@ public String getDescription() {
8081
return Constant.messages.getString(MESSAGE_PREFIX + "desc");
8182
}
8283

83-
private String getOtherInfo() {
84+
private static String getOtherInfo() {
8485
return Constant.messages.getString(MESSAGE_PREFIX + "other");
8586
}
8687

@@ -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,7 +188,16 @@ private String doesResponseContainString(HttpBody body, String str) {
194188
return null;
195189
}
196190

197-
private List<String> loadFile(String file) {
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+
200+
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
200203
*/
@@ -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,53 @@
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.equalTo;
24+
import static org.hamcrest.Matchers.hasSize;
25+
import static org.hamcrest.Matchers.is;
26+
27+
import java.util.List;
28+
import org.junit.jupiter.api.Test;
29+
import org.parosproxy.paros.core.scanner.Alert;
30+
31+
class ExampleFileActiveScanRuleUnitTest extends ActiveScannerTest<ExampleFileActiveScanRule> {
32+
33+
@Override
34+
protected ExampleFileActiveScanRule createScanner() {
35+
return new ExampleFileActiveScanRule();
36+
}
37+
38+
@Test
39+
void shouldHaveExpectedExample() {
40+
// Given / When
41+
List<Alert> alerts = rule.getExampleAlerts();
42+
// Then
43+
assertThat(alerts, hasSize(1));
44+
Alert alert = alerts.get(0);
45+
assertThat(alert.getParam(), is(equalTo("foo")));
46+
}
47+
48+
@Test
49+
@Override
50+
public void shouldHaveValidReferences() {
51+
super.shouldHaveValidReferences();
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.equalTo;
24+
import static org.hamcrest.Matchers.hasSize;
25+
import static org.hamcrest.Matchers.is;
26+
27+
import java.util.List;
28+
import org.junit.jupiter.api.Test;
29+
import org.parosproxy.paros.core.scanner.Alert;
30+
31+
class ExampleSimpleActiveScanRuleUnitTest extends ActiveScannerTest<ExampleSimpleActiveScanRule> {
32+
33+
@Override
34+
protected ExampleSimpleActiveScanRule createScanner() {
35+
return new ExampleSimpleActiveScanRule();
36+
}
37+
38+
@Test
39+
void shouldHaveExpectedExample() {
40+
// Given / When
41+
List<Alert> alerts = rule.getExampleAlerts();
42+
// Then
43+
assertThat(alerts, hasSize(1));
44+
Alert alert = alerts.get(0);
45+
assertThat(alert.getParam(), is(equalTo("foo")));
46+
}
47+
48+
@Test
49+
@Override
50+
public void shouldHaveValidReferences() {
51+
super.shouldHaveValidReferences();
52+
}
53+
}

0 commit comments

Comments
 (0)