A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.
Java application-securityappsecbug-bountyburp-alternativeburp-suitedasthttp-proxyinfosecintercepting-proxyjavaowaspowasp-zappenetration-testingpentestingsecuritysecurity-toolsvulnerability-scannerweb-application-securityweb-security
package opensuite.gate;
import net.sf.json.JSONObject;
import org.parosproxy.paros.db.RecordAlert;
import org.parosproxy.paros.db.TableAlert;
import org.parosproxy.paros.model.Model;
import org.zaproxy.zap.extension.api.ApiException;
import org.zaproxy.zap.extension.api.ApiImplementor;
import org.zaproxy.zap.extension.api.ApiResponse;
import org.zaproxy.zap.extension.api.ApiResponseSet;
import org.zaproxy.zap.extension.api.ApiView;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
* Three views. One lists the alerts raised since a given one; one tells
* which rules an audit has started on which addresses ({@link RuleSteps});
* one gives the requests recorded since a given one.
*
* <p>The engine's own listing of its requests reads every record from the
* first on every call, and gives up on the whole listing at the first record
* it cannot read back. An audit rule now and then sends a request the engine
* itself cannot read again, and from then on nothing more could be listed:
* the scan went on, but its requests stopped being counted or logged. The
* view here starts from the record the caller last had and passes over one
* that cannot be read, saying how many it passed.
*
* <p>The engine's own listing reads every alert from the start on every call
* and compares each with all those before it, so its cost grows with the
* square of the number of alerts: on an application of some tens of thousands
* of pages one call takes minutes, and it is asked for every few seconds.
* This view names only the alerts newer than the one the caller last had,
* which costs the same however many came before. The caller then reads each
* new alert once, by its number.
*/
public class GateApi extends ApiImplementor {
public static final String PREFIX = "opensuitegate";
private static final String VIEW_ALERT_IDS = "alertIds";
private static final String VIEW_RULE_STEPS = "ruleSteps";
private static final String VIEW_MESSAGES = "messages";
private static final int MOST = 2000;
/** The most steps given in one answer; the caller asks again for the rest. */
private static final int STEPS = 50_000;
public GateApi() {
addApiView(new ApiView(VIEW_ALERT_IDS, new String[]{"after"}, new String[]{"baseurl", "count"}));
addApiView(new ApiView(VIEW_RULE_STEPS, new String[]{"scanId"}, new String[]{"after", "names", "addresses"}));
addApiView(new ApiView(VIEW_MESSAGES, new String[]{"after"}, new String[]{"count"}));
}
@Override public String getPrefix() { return PREFIX; }
@Override
public ApiResponse handleApiView(String name, JSONObject params) throws ApiException {
if (VIEW_RULE_STEPS.equals(name)) return ruleSteps(params);
if (VIEW_MESSAGES.equals(name)) return messages(params);
if (!VIEW_ALERT_IDS.equals(name)) throw new ApiException(ApiException.Type.BAD_VIEW);
int after, most;
try {
after = Integer.parseInt(params.getString("after").trim());
most = params.containsKey("count") && !params.getString("count").isBlank()
? Integer.parseInt(params.getString("count").trim()) : MOST;
} catch (RuntimeException e) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, "after");
}
String baseurl = params.containsKey("baseurl") ? params.getString("baseurl") : "";
try {
TableAlert table = Model.getSingleton().getDb().getTableAlert();
List<Integer> all = table.getAlertList();
GateRules.Newer newer = GateRules.newer(all, after, most, baseurl.isEmpty() ? null : id -> {
try {
RecordAlert r = table.read(id);
return r != null && r.getUri() != null && r.getUri().startsWith(baseurl);
} catch (Exception e) {
return false;
}
});
Map<String, String> out = new LinkedHashMap<>();
out.put("ids", newer.idList());
out.put("last", String.valueOf(newer.last()));
out.put("top", String.valueOf(newer.top()));
return new ApiResponseSet<>(VIEW_ALERT_IDS, out);
} catch (Exception e) {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, e.getMessage());
}
}
private static int number(JSONObject params, String key) throws ApiException {
try {
return params.containsKey(key) && !params.getString(key).isBlank() ? Integer.parseInt(params.getString(key).trim()) : 0;
} catch (RuntimeException e) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, key);
}
}
/**
* The rules of a scan as they stand, and the steps, rule names and
* addresses the caller has not had: it says how many of each it holds.
*/
private ApiResponse ruleSteps(JSONObject params) throws ApiException {
int id;
try {
id = Integer.parseInt(params.getString("scanId").trim());
} catch (RuntimeException e) {
throw new ApiException(ApiException.Type.ILLEGAL_PARAMETER, "scanId");
}
org.zaproxy.zap.extension.ascan.ExtensionActiveScan ext = org.parosproxy.paros.control.Control.getSingleton()
.getExtensionLoader().getExtension(org.zaproxy.zap.extension.ascan.ExtensionActiveScan.class);
org.zaproxy.zap.extension.ascan.ActiveScan scan = ext == null ? null : ext.getScan(id);
if (scan == null) throw new ApiException(ApiException.Type.DOES_NOT_EXIST, "scanId");
try {
RuleWatch watch = RuleWatch.of(scan);
RuleSteps.Batch b = watch.steps.after(number(params, "after"), number(params, "names"), number(params, "addresses"), STEPS);
Map<String, String> out = new LinkedHashMap<>();
out.put("rules", watch.rules(scan));
out.put("steps", b.steps());
out.put("names", b.names());
out.put("addresses", b.addresses());
out.put("next", String.valueOf(b.next()));
out.put("more", b.more() ? "1" : "0");
return new ApiResponseSet<>(VIEW_RULE_STEPS, out);
} catch (RuntimeException e) {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, String.valueOf(e));
}
}
/**
* The requests recorded after the one numbered {@code after}, oldest
* first, at most {@code count} of them, each as the engine's own listing
* gives it. The last item is not a request: it says where the caller is
* now ("next", the number of the last record looked at), how many records
* could not be read and were passed over ("skipped"), and whether there
* are more to ask for ("more").
*/
private ApiResponse messages(JSONObject params) throws ApiException {
int after = number(params, "after");
int most = number(params, "count");
if (most <= 0) most = 500;
try {
org.parosproxy.paros.db.TableHistory table = Model.getSingleton().getDb().getTableHistory();
long session = Model.getSingleton().getSession().getSessionId();
List<Integer> ids = new java.util.ArrayList<>(table.getHistoryIdsStartingAt(session, after + 1));
java.util.Collections.sort(ids);
org.zaproxy.zap.extension.api.ApiResponseList out = new org.zaproxy.zap.extension.api.ApiResponseList(VIEW_MESSAGES);
int next = after, skipped = 0, looked = 0;
boolean more = false;
for (int id : ids) {
if (id <= after) continue;
if (looked >= most) { more = true; break; }
looked++;
next = id;
try {
org.parosproxy.paros.db.RecordHistory r = table.read(id);
if (r == null) { skipped++; continue; }
out.addItem(org.zaproxy.zap.extension.api.ApiResponseConversionUtils.httpMessageToSet(id, r.getHistoryType(), r.getHttpMessage()));
} catch (Exception | LinkageError e) {
skipped++; // a record the engine cannot read back: it is passed over, and the rest are still given
}
}
Map<String, String> mark = new LinkedHashMap<>();
mark.put("next", String.valueOf(next));
mark.put("skipped", String.valueOf(skipped));
mark.put("more", more ? "1" : "0");
out.addItem(new ApiResponseSet<>("mark", mark));
return out;
} catch (Exception e) {
throw new ApiException(ApiException.Type.INTERNAL_ERROR, String.valueOf(e));
}
}
}