/
HttPoxyScanRule.java
200 lines (171 loc) · 6.71 KB
/
HttPoxyScanRule.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
/*
* Zed Attack Proxy (ZAP) and its related class files.
*
* ZAP is an HTTP/HTTPS proxy for assessing web application security.
*
* Copyright 2016 The ZAP Development Team
*
* 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.
*/
package org.zaproxy.zap.extension.ascanrulesBeta;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
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.control.Control;
import org.parosproxy.paros.core.scanner.AbstractAppPlugin;
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.addon.commonlib.http.HttpFieldsNames;
import org.zaproxy.addon.network.ExtensionNetwork;
import org.zaproxy.addon.network.server.HttpMessageHandler;
import org.zaproxy.addon.network.server.HttpMessageHandlerContext;
import org.zaproxy.addon.network.server.Server;
public class HttPoxyScanRule extends AbstractAppPlugin implements CommonActiveScanRuleInfo {
/** Prefix for internationalised messages used by this rule */
private static final String MESSAGE_PREFIX = "ascanbeta.httpoxy.";
private static final int PLUGIN_ID = 10107;
private static final Map<String, String> ALERT_TAGS =
CommonAlertTag.toMap(
CommonAlertTag.OWASP_2021_A06_VULN_COMP,
CommonAlertTag.OWASP_2017_A09_VULN_COMP);
private static final Logger LOGGER = LogManager.getLogger(HttPoxyScanRule.class);
@Override
public int getCategory() {
return Category.SERVER;
}
@Override
public String getDescription() {
return Constant.messages.getString(MESSAGE_PREFIX + "desc");
}
@Override
public int getId() {
return PLUGIN_ID;
}
@Override
public int getRisk() {
return Alert.RISK_HIGH;
}
@Override
public int getCweId() {
return 20; // WASC-20 Improper Input Handling
}
@Override
public int getWascId() {
return 20; // Improper Input Validation
}
@Override
public Map<String, String> getAlertTags() {
return ALERT_TAGS;
}
@Override
public String getName() {
return Constant.messages.getString(MESSAGE_PREFIX + "name");
}
@Override
public String getReference() {
return Constant.messages.getString(MESSAGE_PREFIX + "refs");
}
@Override
public String getSolution() {
return Constant.messages.getString(MESSAGE_PREFIX + "soln");
}
@Override
public void init() {
ExtensionNetwork ext =
Control.getSingleton().getExtensionLoader().getExtension(ExtensionNetwork.class);
if (ext == null) {
getParent()
.pluginSkipped(this, Constant.messages.getString(MESSAGE_PREFIX + "skipped"));
}
}
@Override
public void scan() {
// Set up a listener using all interfaces on another port
HttpoxyListener listener = new HttpoxyListener();
try (Server server =
Control.getSingleton()
.getExtensionLoader()
.getExtension(ExtensionNetwork.class)
.createHttpServer(listener)) {
int proxyPort = server.start("0.0.0.0");
Enumeration<NetworkInterface> allInterfaces = NetworkInterface.getNetworkInterfaces();
// Try all of the available interfaces as we dont know which one(s)
// will be available to the target server
while (allInterfaces.hasMoreElements()) {
NetworkInterface ni = allInterfaces.nextElement();
Enumeration<InetAddress> inetAddrs = ni.getInetAddresses();
while (inetAddrs.hasMoreElements()) {
InetAddress inetAddr = inetAddrs.nextElement();
String hostPort = inetAddr.getHostAddress() + ":" + proxyPort;
HttpMessage newRequest = getNewMsg();
newRequest.getRequestHeader().setHeader(HttpFieldsNames.PROXY, hostPort);
try {
sendAndReceive(newRequest, false);
} catch (Exception e) {
// Ignore
}
if (listener.isMsgReceived()) {
// the server is vulnerable
buildAlert(
hostPort,
getBaseMsg().getRequestHeader().getURI().toString(),
listener.getMsgUrl())
.setMessage(newRequest)
.raise();
// no point in continuing
return;
}
}
}
} catch (Exception e) {
LOGGER.debug(e.getMessage(), e);
}
}
private AlertBuilder buildAlert(String hostPort, String baseUrl, String url) {
return newAlert()
.setConfidence(Alert.CONFIDENCE_HIGH)
.setUri(baseUrl)
.setAttack(HttpFieldsNames.PROXY + ": " + hostPort)
.setOtherInfo(Constant.messages.getString(MESSAGE_PREFIX + "otherinfo", url));
}
@Override
public List<Alert> getExampleAlerts() {
return List.of(
buildAlert("192.168.0.11:1080", "http://example.com/", "http://192.168.0.11:1080/")
.build());
}
private class HttpoxyListener implements HttpMessageHandler {
private boolean msgReceived;
private String msgUrl;
@Override
public void handleMessage(HttpMessageHandlerContext ctx, HttpMessage msg) {
ctx.close();
this.msgReceived = true;
this.msgUrl = msg.getRequestHeader().getURI().toString();
LOGGER.debug("Received request {}", msgUrl);
}
public boolean isMsgReceived() {
return msgReceived;
}
public String getMsgUrl() {
return msgUrl;
}
}
}