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.intruder;
import opensuite.core.Bytes;
import opensuite.core.HttpMessage;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* The response analysis an attack applies to every result: grep-match flags
* responses containing an expression, grep-extract pulls a value out of each
* response, and grep-payloads counts how often the submitted payloads are
* reflected. Each rule becomes one extra column in the results table.
*
* <p>Pure logic with no Swing in it, so the whole of it is unit tested.
*/
public final class ResponseGrep {
/** The grep-match list and how its expressions are compared. */
public record Match(List<String> expressions, boolean regex, boolean caseSensitive,
boolean excludeHeaders) {
public static Match simple(List<String> expressions) {
return new Match(expressions, false, false, false);
}
}
/**
* One grep-extract rule. With a regex the first capture group is taken (the
* whole match when the pattern has no group); otherwise the text after
* {@code startAfter} up to {@code endAt}, or to the end of the line when no
* end delimiter is given.
*/
public record Extract(String name, String startAfter, String endAt, String regex,
boolean caseSensitive, boolean excludeHeaders, int maxLength) {
/** The column caption: the given name, else the expression that defines the rule. */
public String caption() {
if (name != null && !name.isBlank()) return name.trim();
if (regex != null && !regex.isEmpty()) return regex;
return startAfter == null ? "" : startAfter;
}
boolean usesRegex() { return regex != null && !regex.isEmpty(); }
}
/**
* The grep-payloads settings. With {@code preEncoded} the response is
* searched for each payload as it was before it was URL-encoded for the
* request, which is how an application usually echoes it; without, for the
* payload exactly as it was sent.
*/
public record Payloads(boolean enabled, boolean caseSensitive, boolean excludeHeaders,
boolean preEncoded) {
public static final Payloads OFF = new Payloads(false, false, false, false);
}
/** The caption of the grep-payloads column. */
public static final String PAYLOAD_COLUMN = "Payload grep";
private final Match match;
private final List<Extract> extracts;
private final Payloads payloads;
private final Pattern[] matchPatterns; // null entry = expression that did not compile
private final Pattern[] extractPatterns; // null entry = delimiter rule, or a bad regex
public ResponseGrep(Match match, List<Extract> extracts, Payloads payloads) {
this.match = match == null ? Match.simple(List.of()) : match;
this.extracts = extracts == null ? List.of() : List.copyOf(extracts);
this.payloads = payloads == null ? Payloads.OFF : payloads;
List<String> exprs = this.match.expressions();
matchPatterns = new Pattern[exprs.size()];
for (int i = 0; i < exprs.size(); i++) {
String e = exprs.get(i);
matchPatterns[i] = compile(this.match.regex() ? e : Pattern.quote(e), this.match.caseSensitive());
}
extractPatterns = new Pattern[this.extracts.size()];
for (int i = 0; i < this.extracts.size(); i++) {
Extract x = this.extracts.get(i);
if (x.usesRegex()) extractPatterns[i] = compile(x.regex(), x.caseSensitive());
}
}
/** No analysis at all: no extra columns. */
public static ResponseGrep none() {
return new ResponseGrep(null, null, null);
}
private static Pattern compile(String regex, boolean caseSensitive) {
try {
return Pattern.compile(regex, caseSensitive ? Pattern.DOTALL : Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
} catch (Exception e) {
return null; // a bad expression never matches rather than stopping the attack
}
}
/** The number of extra result columns. */
public int columnCount() {
return match.expressions().size() + extracts.size() + (payloads.enabled() ? 1 : 0);
}
/** The captions of the extra columns: match expressions, extract rules, then payload grep. */
public List<String> columnNames() {
List<String> out = new ArrayList<>(match.expressions());
for (Extract x : extracts) out.add(x.caption());
if (payloads.enabled()) out.add(PAYLOAD_COLUMN);
return out;
}
/** The value type of each extra column, in {@link #columnNames()} order. */
public Class<?>[] columnClasses() {
Class<?>[] out = new Class<?>[columnCount()];
int i = 0;
for (int m = 0; m < match.expressions().size(); m++) out[i++] = Boolean.class;
for (int x = 0; x < extracts.size(); x++) out[i++] = String.class;
if (payloads.enabled()) out[i] = Integer.class;
return out;
}
/**
* Analyse one response. {@code sent} holds the payloads placed in the
* request (null entries are positions left at their base value).
*/
public Object[] evaluate(HttpMessage resp, String[] sent) {
Object[] out = new Object[columnCount()];
int i = 0;
String whole = resp == null ? null : Bytes.str(resp.raw());
String body = resp == null ? null : Bytes.str(resp.body());
for (Pattern p : matchPatterns) {
String text = match.excludeHeaders() ? body : whole;
out[i++] = text != null && p != null && p.matcher(text).find();
}
for (int x = 0; x < extracts.size(); x++) {
Extract rule = extracts.get(x);
String text = rule.excludeHeaders() ? body : whole;
out[i++] = text == null ? "" : extract(rule, extractPatterns[x], text);
}
if (payloads.enabled()) {
String text = payloads.excludeHeaders() ? body : whole;
out[i] = text == null ? 0 : countPayloads(text, sent);
}
return out;
}
private static String extract(Extract rule, Pattern regex, String text) {
String found;
if (rule.usesRegex()) {
if (regex == null) return "";
Matcher m = regex.matcher(text);
if (!m.find()) return "";
found = m.groupCount() >= 1 && m.group(1) != null ? m.group(1) : m.group();
} else {
String start = rule.startAfter() == null ? "" : rule.startAfter();
int from = 0;
if (!start.isEmpty()) {
int at = indexOf(text, start, 0, rule.caseSensitive());
if (at < 0) return "";
from = at + start.length();
}
String end = rule.endAt() == null ? "" : rule.endAt();
int to;
if (end.isEmpty()) {
to = lineEnd(text, from);
} else {
to = indexOf(text, end, from, rule.caseSensitive());
if (to < 0) to = text.length();
}
found = text.substring(from, to);
}
int max = rule.maxLength();
return max > 0 && found.length() > max ? found.substring(0, max) : found;
}
private static int lineEnd(String text, int from) {
for (int i = from; i < text.length(); i++) {
char c = text.charAt(i);
if (c == '\r' || c == '\n') return i;
}
return text.length();
}
private static int indexOf(String text, String needle, int from, boolean caseSensitive) {
if (caseSensitive) return text.indexOf(needle, from);
int n = needle.length();
for (int i = from; i + n <= text.length(); i++) {
if (text.regionMatches(true, i, needle, 0, n)) return i;
}
return -1;
}
private int countPayloads(String text, String[] sent) {
if (sent == null) return 0;
int total = 0;
List<String> seen = new ArrayList<>();
for (String p : sent) {
if (p == null || p.isEmpty() || seen.contains(p)) continue;
seen.add(p);
String needle = payloads.preEncoded() ? originals.getOrDefault(p, p) : p;
if (!needle.isEmpty()) total += count(text, needle, payloads.caseSensitive());
}
return total;
}
/** Each payload as sent, against what it was before it was URL-encoded for the request. */
private volatile java.util.Map<String, String> originals = java.util.Map.of();
/** Tell the grep what the payloads were before encoding; one that is not named is taken to be unchanged. */
public void setOriginals(java.util.Map<String, String> sentToOriginal) {
originals = sentToOriginal == null ? java.util.Map.of() : java.util.Map.copyOf(sentToOriginal);
}
private static int count(String text, String needle, boolean caseSensitive) {
int n = 0, at = 0;
while ((at = indexOf(text, needle, at, caseSensitive)) >= 0) {
n++;
at += needle.length();
}
return n;
}
}