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.

3.4 KB
Raw Blame
package opensuite.engine;

import opensuite.core.RuleProgress;
import opensuite.core.Debug;

import java.util.ArrayList;
import java.util.List;

/**
 * Brings one audit's rule starts from the engine into a task's
 * {@link RuleProgress}. Open Suite's add-on in the engine keeps them; each
 * call here asks for what has been added since the last and for the scan's
 * rules as they stand. Needs version {@link #NEEDS_GATE} of the add-on; with
 * an older one nothing is asked and the task simply has no such figures.
 */
final class RuleFeed {

    static final int NEEDS_GATE = 4;
    /** The most answers taken in one go: enough for a long gap, and an end to a fault that keeps saying there is more. */
    private static final int ROUNDS = 40;

    private final ZapApi api;
    private final String scan;
    private final RuleProgress into;
    private final List<String> names = new ArrayList<>(), addresses = new ArrayList<>();
    private int next;

    RuleFeed(ZapApi api, String scan, RuleProgress into) {
        this.api = api;
        this.scan = scan;
        this.into = into;
    }

    /** Take in whatever is new. A fault here costs the figures, never the scan. */
    void pull() {
        if (scan == null || scan.isBlank() || api.gateVersion() < NEEDS_GATE) return;
        try {
            for (int i = 0; i < ROUNDS; i++) {
                ZapApi.RuleSteps got = api.ruleSteps(scan, next, names.size(), addresses.size());
                next = take(got, names, addresses, into);
                if (!got.more()) break;
            }
        } catch (Exception e) {
            Debug.log("rule progress of scan " + scan + " not read: " + e);
        }
    }

    /**
     * Put one answer into the progress. The answer numbers its rule names and
     * addresses in the order it first gave them, so the lists of those the
     * caller holds grow with each answer; steps are runs of one rule,
     * "rule:address,address;rule:address". Returns where to ask from next.
     */
    static int take(ZapApi.RuleSteps got, List<String> names, List<String> addresses, RuleProgress into) {
        if (got.names() != null && !got.names().isEmpty()) names.addAll(List.of(got.names().split("\n")));
        if (got.addresses() != null && !got.addresses().isEmpty()) addresses.addAll(List.of(got.addresses().split("\n")));
        if (got.steps() != null && !got.steps().isEmpty()) {
            for (String run : got.steps().split(";")) {
                int colon = run.indexOf(':');
                if (colon <= 0) continue;
                try {
                    int rule = Integer.parseInt(run.substring(0, colon));
                    if (rule < 0 || rule >= names.size()) continue;
                    for (String a : run.substring(colon + 1).split(",")) {
                        int address = Integer.parseInt(a);
                        if (address >= 0 && address < addresses.size()) into.started(names.get(rule), addresses.get(address));
                    }
                } catch (NumberFormatException e) {
                    // a run that cannot be read is left out
                }
            }
        }
        List<RuleProgress.Rule> rules = new ArrayList<>();
        if (got.rules() != null) for (String line : got.rules().split("\n")) {
            RuleProgress.Rule r = RuleProgress.rule(line);
            if (r != null) rules.add(r);
        }
        if (!rules.isEmpty()) into.rules(rules);
        return got.next();
    }
}