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.

14.4 KB
Raw Blame
package opensuite.intruder;

import opensuite.core.*;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Runs one Intruder attack on a bounded thread pool. It expands the payload
 * sets into request combinations according to the attack type, substitutes
 * them into the marked request, sends each, records the exchange into history
 * and feeds the result to the {@link ResultsWindow}.
 */
public class AttackEngine {

    /** Upper bound on the number of requests a single attack will issue. */
    static final int MAX_REQUESTS = 50_000;

    private final SuiteContext ctx;
    private final HttpService svc;
    private final String markedText;
    private final AttackType type;
    private final int positions;
    private final List<List<String>> payloadsPerSet;
    private final ResponseGrep grep;
    private final int threads;
    private final boolean followRedirects;
    private final long throttleMs;
    private final HttpMode mode;
    private final ResultsWindow window;

    private final Options opts;

    private volatile ExecutorService pool;
    private volatile boolean stopped;
    private volatile boolean paused;

    /** How much of a payload is kept in a result when full payloads are not stored. */
    static final int PAYLOAD_KEPT = 100;

    /** The payload as a result keeps it: whole, or its first {@value #PAYLOAD_KEPT} characters and a mark that more followed. */
    static String storedPayload(String payload, boolean full) {
        if (full || payload == null || payload.length() <= PAYLOAD_KEPT) return payload;
        return payload.substring(0, PAYLOAD_KEPT) + "\u2026";
    }

    /** The reference's error-handling, header and result-capture options for an attack. */
    public static class Options {
        public boolean updateContentLength = true;
        public boolean setConnectionHeader = true;
        public int retries = 0;
        public int retryPauseMs = 2000;
        public boolean storeRequests = true;
        public boolean storeResponses = true;
        /** Keep each result's payload whole; otherwise a long payload is kept by its start only. */
        public boolean storeFullPayloads = false;
        /** Auto-pause looks for these, as plain text or as regular expressions. */
        public java.util.List<String> pauseExpressions = java.util.List.of();
        public boolean pauseRegex, pauseCaseSensitive;
        /** Which redirections are followed: {@code never}, {@code onsite}, {@code inscope} or {@code always}. */
        public String redirects = "always";
        public boolean cookiesInRedirects;
        /** Whether HTTP/1 connections are kept for the next request; null leaves it to the project's setting. */
        public Boolean keepAlive;
        /** Write the attack beside the project file when it finishes. */
        public boolean saveToProject = false;
        public boolean baselineRequest = false;
        public boolean dosMode = false;
        public boolean autoPause = false;
        public boolean pauseOnMissing = false;   // else pause when a flag expression appears
    }

    public AttackEngine(SuiteContext ctx, HttpService svc, String markedText, AttackType type,
                        int positions, List<List<String>> payloadsPerSet, ResponseGrep grep,
                        int threads, boolean followRedirects, long throttleMs, HttpMode mode,
                        ResultsWindow window) {
        this(ctx, svc, markedText, type, positions, payloadsPerSet, grep, threads,
                followRedirects, throttleMs, mode, window, new Options());
    }

    public AttackEngine(SuiteContext ctx, HttpService svc, String markedText, AttackType type,
                        int positions, List<List<String>> payloadsPerSet, ResponseGrep grep,
                        int threads, boolean followRedirects, long throttleMs, HttpMode mode,
                        ResultsWindow window, Options opts) {
        this.ctx = ctx;
        this.svc = svc;
        this.markedText = markedText;
        this.type = type;
        this.positions = positions;
        this.payloadsPerSet = payloadsPerSet;
        this.grep = grep == null ? ResponseGrep.none() : grep;
        this.threads = Math.max(1, threads);
        this.followRedirects = followRedirects;
        this.throttleMs = Math.max(0, throttleMs);
        this.mode = mode == null ? HttpMode.HTTP_1 : mode;
        this.window = window;
        this.opts = opts == null ? new Options() : opts;
    }

    /** Pause or resume the attack; running threads wait between requests while paused. */
    public void setPaused(boolean p) { this.paused = p; }
    public boolean isPaused() { return paused; }

    /** One request: the per-position payloads (null = keep base) and a label. */
    private record Combo(String[] values, String label) {}

    public void start() {
        List<Combo> combos = buildCombos();
        // An unmodified baseline request goes first, so results can be compared to it.
        if (opts.baselineRequest) combos.add(0, new Combo(new String[positions], ""));
        window.setTotal(combos.size());

        ThreadFactory tf = r -> {
            Thread t = new Thread(r, "intruder-attack");
            t.setDaemon(true);
            return t;
        };
        pool = Executors.newFixedThreadPool(threads, tf);
        // The baseline is request 0; the payloads count from 1.
        AtomicInteger counter = new AtomicInteger(opts.baselineRequest ? -1 : 0);
        AtomicInteger done = new AtomicInteger(0);
        int total = combos.size();

        for (Combo combo : combos) {
            pool.submit(() -> {
                try {
                    if (stopped || Thread.currentThread().isInterrupted()) return;
                    int number = counter.incrementAndGet();
                    runOne(number, combo);
                } catch (Exception e) {
                    // The Future is discarded, so without this the failure is
                    // invisible and the done counter below never completes.
                    Debug.log("intruder request: " + e);
                } finally {
                    // Counted on every path: an exception or an early stop must
                    // not leave the window stuck short of "finished".
                    if (done.incrementAndGet() == total) finish();
                }
            });
        }
        if (total == 0) finish();
        pool.shutdown();
    }

    /** Stop the attack and release the pool. */
    public void stop() {
        stopped = true;
        if (pool != null) pool.shutdownNow();
    }

    private void finish() { window.finished(); }

    private void runOne(int number, Combo combo) {
        // Honour pause (and throttle) before issuing the request.
        while (paused && !stopped && !Thread.currentThread().isInterrupted()) {
            try { Thread.sleep(100); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; }
        }
        if (stopped) return;
        if (throttleMs > 0) { try { Thread.sleep(throttleMs); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; } }

        String substituted = substitute(markedText, combo.values());
        HttpMessage req = new HttpMessage(Bytes.bytes(substituted));
        boolean keep = opts.keepAlive != null ? opts.keepAlive : HttpClient.defaultKeepAlive;
        if (opts.setConnectionHeader) req.setHeader("Connection", keep ? "keep-alive" : "close");
        if (opts.updateContentLength) req.updateContentLength();

        HttpClient.Options opt = new HttpClient.Options();
        opt.updateContentLength = false;   // already applied above
        opt.unpackGzip = true;
        opt.followRedirects = followRedirects && !"never".equals(opts.redirects);
        if ("onsite".equals(opts.redirects)) {
            opt.redirectFilter = (from, to) -> from.host().equalsIgnoreCase(to.host()) && from.port() == to.port();
        } else if ("inscope".equals(opts.redirects)) {
            opt.redirectFilter = (from, to) -> ctx.scope().isInScope(to, "/");
        }
        opt.cookiesInRedirects = opts.cookiesInRedirects;
        opt.keepAlive = keep;
        opt.session = ctx.sessions();
        opt.sessionTool = "intruder";
        opt.mode = mode;

        // Retry a network failure the configured number of times, pausing between tries.
        long t0 = System.currentTimeMillis();
        HttpClient.Result r = new HttpClient().send(svc, req, opt);
        for (int attempt = 0; attempt < opts.retries && !r.ok() && !stopped; attempt++) {
            if (opts.retryPauseMs > 0) {
                try { Thread.sleep(opts.retryPauseMs); }
                catch (InterruptedException e) { Thread.currentThread().interrupt(); return; }
            }
            r = new HttpClient().send(svc, req, opt);
        }
        long ms = System.currentTimeMillis() - t0;

        HttpMessage resp = r.ok() ? r.response : null;
        int status = resp != null ? resp.statusCode() : 0;
        int length = resp != null ? resp.length() : 0;
        String error = r.ok() ? null : r.error;
        Object[] extra = grep.evaluate(resp, combo.values());

        // Denial-of-service mode sends the request but keeps no result.
        if (!opts.dosMode) {
            HttpMessage storedResp = opts.storeResponses ? resp : null;
            window.addResult(new ResultsWindow.Result(number, storedPayload(combo.label(), opts.storeFullPayloads), svc,
                    opts.storeRequests ? req : null, storedResp, status, length, ms, error, extra,
                    r.sentAt > 0 ? r.sentAt : t0, r.firstByteMillis));
        }
        if (opts.autoPause && !paused && grepFlagged(resp) == !opts.pauseOnMissing) {
            setPaused(true);
            window.autoPaused(number);
        }
        record(req, resp, r.remoteIp, ms, error);
    }

    /** Whether any of auto-pause's own expressions appears in the response. */
    private boolean grepFlagged(HttpMessage resp) {
        if (resp == null) return false;
        if (pauseGrep == null) {
            pauseGrep = new ResponseGrep(new ResponseGrep.Match(opts.pauseExpressions, opts.pauseRegex,
                    opts.pauseCaseSensitive, false), null, null);
        }
        Object[] cols = pauseGrep.evaluate(resp, null);
        for (Object c : cols) if (c instanceof Boolean b && b) return true;
        return false;
    }

    private volatile ResponseGrep pauseGrep;

    private void record(HttpMessage req, HttpMessage resp, String remoteIp, long ms, String error) {
        SwingUtilities.invokeLater(() -> {
            LogEntry entry = new LogEntry("intruder", svc, req);
            if (resp != null) entry.setResponse(resp);
            if (error != null) entry.setError(error);
            if (remoteIp != null) entry.setRemoteIp(remoteIp);
            entry.setDurationMillis(ms);
            ctx.store().add(entry);
            ctx.siteMapChanged(entry);
        });
    }

    // ---------------- combination expansion ----------------

    private List<Combo> buildCombos() {
        List<Combo> out = new ArrayList<>();
        if (positions <= 0 || payloadsPerSet.isEmpty()) return out;

        switch (type) {
            case SNIPER -> {
                // One list for every position, or a list for each where the payloads depend on the position.
                for (int p = 0; p < positions && out.size() < MAX_REQUESTS; p++) {
                    List<String> set = payloadsPerSet.get(payloadsPerSet.size() == positions ? p : 0);
                    for (String payload : set) {
                        if (out.size() >= MAX_REQUESTS) break;
                        String[] values = new String[positions];
                        values[p] = payload;
                        out.add(new Combo(values, payload));
                    }
                }
            }
            case BATTERING_RAM -> {
                for (String payload : payloadsPerSet.get(0)) {
                    if (out.size() >= MAX_REQUESTS) break;
                    String[] values = new String[positions];
                    for (int p = 0; p < positions; p++) values[p] = payload;
                    out.add(new Combo(values, payload));
                }
            }
            case PITCHFORK -> {
                int n = Integer.MAX_VALUE;
                for (int p = 0; p < positions; p++) n = Math.min(n, set(p).size());
                if (n == Integer.MAX_VALUE) n = 0;
                for (int i = 0; i < n && out.size() < MAX_REQUESTS; i++) {
                    String[] values = new String[positions];
                    for (int p = 0; p < positions; p++) values[p] = set(p).get(i);
                    out.add(new Combo(values, String.join(", ", values)));
                }
            }
            case CLUSTER_BOMB -> cluster(out);
        }
        return out;
    }

    private void cluster(List<Combo> out) {
        int[] idx = new int[positions];
        for (int p = 0; p < positions; p++) if (set(p).isEmpty()) return;
        while (out.size() < MAX_REQUESTS) {
            String[] values = new String[positions];
            for (int p = 0; p < positions; p++) values[p] = set(p).get(idx[p]);
            out.add(new Combo(values, String.join(", ", values)));
            // increment the odometer
            int p = positions - 1;
            while (p >= 0) {
                if (++idx[p] < set(p).size()) break;
                idx[p] = 0;
                p--;
            }
            if (p < 0) break;
        }
    }

    private List<String> set(int p) {
        return p < payloadsPerSet.size() ? payloadsPerSet.get(p) : List.of();
    }

    /** Replace each {@code §…§} region with its payload, dropping all markers. */
    static String substitute(String marked, String[] values) {
        StringBuilder out = new StringBuilder(marked.length());
        int pos = 0, i = 0;
        while (i < marked.length()) {
            char c = marked.charAt(i);
            if (c == PositionsPanel.MARK) {
                int end = marked.indexOf(PositionsPanel.MARK, i + 1);
                if (end < 0) { out.append(marked, i + 1, marked.length()); break; }
                String base = marked.substring(i + 1, end);
                String v = (pos < values.length && values[pos] != null) ? values[pos] : base;
                out.append(v);
                pos++;
                i = end + 1;
            } else {
                out.append(c);
                i++;
            }
        }
        return out.toString();
    }
}