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.

6.1 KB
Raw Blame
package opensuite.gate;

import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.core.scanner.AbstractHostPlugin;
import org.parosproxy.paros.core.scanner.Alert;
import org.parosproxy.paros.core.scanner.HostProcess;
import org.parosproxy.paros.core.scanner.Plugin;
import org.parosproxy.paros.core.scanner.PluginStats;
import org.parosproxy.paros.core.scanner.ScannerListener;
import org.parosproxy.paros.network.HttpMessage;
import org.zaproxy.zap.extension.ascan.ActiveScan;
import org.zaproxy.zap.extension.ascan.ExtensionActiveScan;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Listens to an audit and keeps its {@link RuleSteps}: the engine tells its
 * listeners each time it starts a rule on an address, and this is one.
 *
 * <p>A watch has to be listening before the first rule starts, and the engine
 * gives an add-on no word that a scan has begun. So a watch is put on every
 * new scan the moment anything is seen of it: the first request the scan
 * sends, which is one of the probes it makes before any rule runs, or the
 * first time Open Suite asks about it. Should a rule have started by then all
 * the same, its count at that moment is kept as missed, and the caller is told
 * so: it then counts that rule only once the engine reports it complete.
 */
final class RuleWatch implements ScannerListener {

    private static final Map<Integer, RuleWatch> BY_SCAN = new ConcurrentHashMap<>();
    private static volatile int highest = -1;

    final RuleSteps steps = new RuleSteps();
    /** "site\trule id" of each rule that had already started somewhere when the watch began. */
    private final Set<String> missed = ConcurrentHashMap.newKeySet();
    private volatile boolean unsure;

    private static ExtensionActiveScan audits() {
        return Control.getSingleton().getExtensionLoader().getExtension(ExtensionActiveScan.class);
    }

    /** Put a watch on any scan that has none. Cheap when there is no new scan, which is nearly always. */
    static void watchNew() {
        try {
            ExtensionActiveScan ext = audits();
            if (ext == null) return;
            ActiveScan last = ext.getLastScan();
            if (last == null || last.getScanId() <= highest) return;
            synchronized (BY_SCAN) {
                BY_SCAN.keySet().removeIf(id -> ext.getScan(id) == null);   // scans the engine has let go
                for (ActiveScan scan : ext.getAllScans()) of(scan);
            }
        } catch (RuntimeException e) {
            // Never in the way of a request: the next one tries again.
        }
    }

    /** The watch on this scan, put there now if it had none. */
    static RuleWatch of(ActiveScan scan) {
        int id = scan.getScanId();
        RuleWatch w = BY_SCAN.get(id);
        if (w != null) return w;
        synchronized (BY_SCAN) {
            w = BY_SCAN.get(id);
            if (w != null) return w;
            w = new RuleWatch();
            scan.addScannerListener(w);
            // Listening from here on. Whatever started before is counted, not known by address.
            try {
                for (HostProcess hp : new ArrayList<>(scan.getHostProcesses()))
                    for (Plugin p : rulesOf(hp)) {
                        PluginStats s = hp.getPluginStats(p.getId());
                        if (s != null && s.getProgress() > 0) w.missed.add(hp.getHostAndPort() + "\t" + p.getId());
                    }
            } catch (RuntimeException e) {
                w.unsure = true;
            }
            BY_SCAN.put(id, w);
            if (id > highest) highest = id;
            return w;
        }
    }

    private static List<Plugin> rulesOf(HostProcess hp) {
        List<Plugin> all = new ArrayList<>(hp.getCompleted());
        all.addAll(hp.getRunning());
        all.addAll(hp.getPending());
        return all;
    }

    /**
     * The scan's rules as the engine has them now, one to a line: site, rule
     * id, H for a rule run once for the site or A for one run on each address,
     * C, R or P for complete, running or pending, 1 where the engine skipped
     * it, 1 where its starts were missed, and its name.
     */
    String rules(ActiveScan scan) {
        StringBuilder out = new StringBuilder();
        boolean lost = unsure || steps.full();
        for (HostProcess hp : new ArrayList<>(scan.getHostProcesses())) {
            String site = hp.getHostAndPort();
            line(out, hp, site, hp.getCompleted(), 'C', lost);
            line(out, hp, site, hp.getRunning(), 'R', lost);
            line(out, hp, site, hp.getPending(), 'P', lost);
        }
        return out.toString();
    }

    private void line(StringBuilder out, HostProcess hp, String site, List<Plugin> rules, char state, boolean lost) {
        for (Plugin p : new ArrayList<>(rules)) {
            if (out.length() > 0) out.append('\n');
            out.append(site).append('\t').append(p.getId()).append('\t')
               .append(p instanceof AbstractHostPlugin ? 'H' : 'A').append('\t').append(state).append('\t')
               .append(hp.isSkipped(p) ? '1' : '0').append('\t')
               .append(lost || missed.contains(site + "\t" + p.getId()) ? '1' : '0').append('\t')
               .append(String.valueOf(p.getName()).replace('\t', ' ').replace('\n', ' '));
        }
    }

    // ------------------------------------------------- what the engine tells

    @Override
    public void hostProgress(int id, String hostAndPort, String msg, int percentage) {
        try {
            String[] step = RuleSteps.parse(msg);
            if (step != null) steps.started(step[0], step[1]);
        } catch (RuntimeException e) {
            unsure = true;   // a start was lost: say so, and leave the scan alone
        }
    }

    @Override public void scannerComplete(int id) { }
    @Override public void hostNewScan(int id, String hostAndPort, HostProcess hostThread) { }
    @Override public void hostComplete(int id, String hostAndPort) { }
    @Override public void alertFound(Alert alert) { }
    @Override public void notifyNewMessage(HttpMessage msg) { }
}