/
IntegerOverflowScanRule.java
247 lines (217 loc) · 7.74 KB
/
IntegerOverflowScanRule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Integer Overflow an active scan rule
* Copyright (C) 2015 Institute for Defense Analyses
* @author Mark Rader based upon the example active scanner by psiinon
*/
package org.zaproxy.zap.extension.ascanrulesBeta;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.core.scanner.AbstractAppParamPlugin;
import org.parosproxy.paros.core.scanner.Alert;
import org.parosproxy.paros.core.scanner.Category;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.addon.commonlib.CommonAlertTag;
import org.zaproxy.zap.model.Tech;
import org.zaproxy.zap.model.TechSet;
public class IntegerOverflowScanRule extends AbstractAppParamPlugin
implements CommonActiveScanRuleInfo {
/** Prefix for internationalised messages used by this rule */
private static final String MESSAGE_PREFIX = "ascanbeta.integeroverflow.";
private static final int PLUGIN_ID = 30003;
private static final Map<String, String> ALERT_TAGS =
CommonAlertTag.toMap(
CommonAlertTag.OWASP_2021_A03_INJECTION,
CommonAlertTag.OWASP_2017_A01_INJECTION);
private static final Logger LOGGER = LogManager.getLogger(IntegerOverflowScanRule.class);
@Override
public int getId() {
return PLUGIN_ID;
}
@Override
public String getName() {
return Constant.messages.getString(MESSAGE_PREFIX + "name");
}
@Override
public boolean targets(TechSet technologies) {
return technologies.includes(Tech.C);
}
@Override
public String getDescription() {
return Constant.messages.getString(MESSAGE_PREFIX + "desc");
}
@Override
public int getCategory() {
return Category.INJECTION;
}
@Override
public String getSolution() {
return Constant.messages.getString(MESSAGE_PREFIX + "soln");
}
@Override
public String getReference() {
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
}
private String getError(char c) {
return Constant.messages.getString(MESSAGE_PREFIX + "error" + c);
}
/*
* This method is called by the active scanner for each GET and POST parameter for every page
* @see org.parosproxy.paros.core.scanner.AbstractAppParamPlugin#scan(org.parosproxy.paros.network.HttpMessage, java.lang.String, java.lang.String)
*/
@Override
public void scan(HttpMessage msg, String param, String value) {
if (checkStop() == true) {
return;
}
if (isPage500(getBaseMsg())) // Check to see if the page was initially a 500
{
return; // Stop
}
String returnAttack = randomIntegerString(4); // The number of full length ints to send.
if (attackVector(param, '1', returnAttack) == true) {
return;
}
returnAttack = singleString(4, '0'); // The number of full length ints to send.
if (attackVector(param, '2', returnAttack) == true) {
return;
}
returnAttack = singleString(4, '1'); // The number of full length ints to send.
if (attackVector(param, '3', returnAttack) == true) {
return;
}
returnAttack = singleString(4, '9'); // The number of full length ints to send.
if (attackVector(param, '4', returnAttack) == true) {
return;
}
return;
}
@Override
public int getRisk() {
return Alert.RISK_MEDIUM;
}
@Override
public int getCweId() {
// The CWE id
return 190;
}
@Override
public int getWascId() {
// The WASC ID
return 3;
}
@Override
public Map<String, String> getAlertTags() {
return ALERT_TAGS;
}
private String randomIntegerString(int length) {
int numbercounter = 0;
int character = 0;
long charactercounter = 0;
int maxcharacter = 11;
StringBuilder sb1 = new StringBuilder(maxcharacter * length);
while (numbercounter < length) {
charactercounter = 0;
while (charactercounter < maxcharacter) {
character = 48 + (int) (Math.random() * 10);
while (character > 57 && character < 48) {
character = 48 + (int) (Math.random() * 10);
}
charactercounter++;
sb1.append((char) character);
}
numbercounter++;
}
return sb1.toString();
}
private String singleString(int length, char c) // Single Character String
{
int numbercounter = 0;
long charactercounter = 0;
int maxcharacter = 11;
StringBuilder sb1 = new StringBuilder(maxcharacter * length);
while (numbercounter < length) {
charactercounter = 0;
while (charactercounter < maxcharacter) {
charactercounter++;
sb1.append(c);
}
numbercounter++;
}
return sb1.toString();
}
private boolean checkStop() {
if (this.isStop()) { // Check if the user stopped things
LOGGER.debug("Scan rule {} stopping.", getName());
return true; // Stop!
}
return false;
}
private boolean attackVector(String param, char type, String returnAttack) {
if (checkStop() == true) {
return true;
}
HttpMessage msg;
msg = getNewMsg();
setParameter(msg, param, returnAttack);
try {
sendAndReceive(msg);
if (isPage500(msg)) {
LOGGER.debug("Found Header");
buildAlert(
getBaseMsg().getRequestHeader().getURI().toString(),
param,
returnAttack,
type,
msg.getResponseHeader().getPrimeHeader())
.setMessage(msg)
.raise();
return true;
}
} catch (IOException e) {
LOGGER.debug(e.getMessage(), e);
}
return false;
}
@Override
public List<Alert> getExampleAlerts() {
return List.of(
buildAlert(
"https://example.com/?years=1",
"years",
"95697568703220167658153205694899573480013738",
'1',
"HTTP/1.1 500 Internal Server Error")
.build());
}
private AlertBuilder buildAlert(
String url, String param, String attack, char type, String evidence) {
return newAlert()
.setConfidence(Alert.CONFIDENCE_MEDIUM)
.setUri(url)
.setParam(param)
.setAttack(attack)
.setOtherInfo(this.getError(type))
.setEvidence(evidence);
}
}