Skip to content

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.

4.1 KB
Raw Blame
package opensuite.intruder;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Which results of an attack are wanted: by the words they contain, by the
 * class of their status code and by whether they carry a comment or a
 * highlight. The same choices serve the view filter, which hides rows, and the
 * capture filter, which keeps results out of the table altogether and can also
 * discard those that got no response.
 */
final class AttackFilter {
    String term = "";
    boolean regex, caseSensitive, negative;
    boolean show2xx = true, show3xx = true, show4xx = true, show5xx = true;
    boolean notesOnly, highlightedOnly;
    /** Capture filter only: leave out a request that was never answered. */
    boolean discardUnanswered;

    AttackFilter copy() {
        AttackFilter c = new AttackFilter();
        c.term = term; c.regex = regex; c.caseSensitive = caseSensitive; c.negative = negative;
        c.show2xx = show2xx; c.show3xx = show3xx; c.show4xx = show4xx; c.show5xx = show5xx;
        c.notesOnly = notesOnly; c.highlightedOnly = highlightedOnly; c.discardUnanswered = discardUnanswered;
        return c;
    }

    /** True when nothing is filtered out. */
    boolean all() {
        return term.isEmpty() && show2xx && show3xx && show4xx && show5xx && !notesOnly && !highlightedOnly && !discardUnanswered;
    }

    /** Whether a result is wanted, given the comment and highlight its row carries. */
    boolean accepts(ResultsWindow.Result r, String comment, boolean highlighted) {
        if (discardUnanswered && r.response() == null) return false;
        int hundreds = r.status() / 100;
        if (hundreds == 2 && !show2xx || hundreds == 3 && !show3xx || hundreds == 4 && !show4xx || hundreds == 5 && !show5xx) return false;
        if (notesOnly && (comment == null || comment.isBlank())) return false;
        if (highlightedOnly && !highlighted) return false;
        if (!term.isEmpty() && found(r) == negative) return false;
        return true;
    }

    /** Whether the search term is in the result's payload, request or response. */
    private boolean found(ResultsWindow.Result r) {
        StringBuilder all = new StringBuilder();
        if (r.payload() != null) all.append(r.payload()).append('\n');
        if (r.request() != null) all.append(new String(r.request().raw(), StandardCharsets.ISO_8859_1)).append('\n');
        if (r.response() != null) all.append(new String(r.response().raw(), StandardCharsets.ISO_8859_1));
        if (regex) {
            try { return Pattern.compile(term, caseSensitive ? 0 : Pattern.CASE_INSENSITIVE).matcher(all).find(); }
            catch (Exception bad) { return false; }   // an expression that does not compile matches nothing
        }
        return caseSensitive ? all.indexOf(term) >= 0 : all.toString().toLowerCase().contains(term.toLowerCase());
    }

    /** The filter in words, for the bar it is opened from; {@code nothing} is said when all is wanted. */
    String summary(String nothing) {
        List<String> parts = new ArrayList<>();
        if (discardUnanswered) parts.add("discarding items without responses");
        if (!term.isEmpty()) parts.add((negative ? "hiding items matching \"" : "showing only items matching \"") + term + "\"");
        List<String> codes = new ArrayList<>();
        if (!show2xx) codes.add("2xx");
        if (!show3xx) codes.add("3xx");
        if (!show4xx) codes.add("4xx");
        if (!show5xx) codes.add("5xx");
        if (!codes.isEmpty()) parts.add("hiding " + join(codes) + " responses");
        if (notesOnly) parts.add("showing only items with notes");
        if (highlightedOnly) parts.add("showing only highlighted items");
        if (parts.isEmpty()) return nothing;
        String s = String.join("; ", parts);
        return Character.toUpperCase(s.charAt(0)) + s.substring(1);
    }

    private static String join(List<String> items) {
        if (items.size() == 1) return items.get(0);
        return String.join(", ", items.subList(0, items.size() - 1)) + " and " + items.get(items.size() - 1);
    }
}