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.2 KB
Raw Blame
package opensuite.engine;

import java.net.HttpURLConnection;
import java.net.URI;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * The lifeline watchdog for systems with no shell to run the one in
 * {@link ZapEngine} (Windows). Run as a small process of its own:
 * {@code java -cp <app> opensuite.engine.EngineGuardian <engine command...>}
 * with its input wired to a pipe held by Open Suite.
 *
 * It starts the engine, then waits on its input. When Open Suite ends for any
 * reason, a kill included, the pipe closes: the guardian asks the engine to
 * stop its browser crawl and shut down, then ends the engine and every process
 * it started. It does the same work, in the same order, as the shell watchdog.
 */
public final class EngineGuardian {
    private EngineGuardian() {}

    public static void main(String[] args) throws Exception {
        if (args.length == 0) { System.exit(2); return; }
        String port = null, key = "", prev = "";
        for (String a : args) {
            if ("-port".equals(prev)) port = a;
            if (a.startsWith("api.key=")) key = a.substring("api.key=".length());
            prev = a;
        }
        Process engine = new ProcessBuilder(args)
                .redirectOutput(ProcessBuilder.Redirect.DISCARD)
                .redirectError(ProcessBuilder.Redirect.DISCARD)
                .start();
        // An engine that ends by itself takes its guardian with it.
        engine.onExit().thenAccept(p -> { if (!ending) Runtime.getRuntime().halt(p.exitValue()); });

        try { while (System.in.read() >= 0) { /* nothing is ever sent; only the end matters */ } }
        catch (java.io.IOException gone) { /* the pipe broke: the same as its end */ }

        // Open Suite is gone.
        ending = true;
        Set<ProcessHandle> all = new LinkedHashSet<>();
        collect(engine, all);
        if (port != null) {
            get("http://127.0.0.1:" + port + "/JSON/ajaxSpider/action/stop/?apikey=" + key);
            get("http://127.0.0.1:" + port + "/JSON/core/action/shutdown/?apikey=" + key);
        }
        // The engine's browsers are recorded while it is still alive: once it has
        // gone they are no longer its descendants and could not be found.
        for (int i = 0; i < 8 && engine.isAlive(); i++) {
            collect(engine, all);
            try { Thread.sleep(1000); } catch (InterruptedException e) { break; }
        }
        engine.destroyForcibly();
        for (ProcessHandle h : all) { try { if (h.isAlive()) h.destroyForcibly(); } catch (RuntimeException ignore) { } }
        Runtime.getRuntime().halt(0);
    }

    private static volatile boolean ending;

    private static void collect(Process engine, Set<ProcessHandle> into) {
        try { engine.toHandle().descendants().forEach(into::add); } catch (RuntimeException ignore) { }
    }

    private static void get(String url) {
        try {
            HttpURLConnection c = (HttpURLConnection) URI.create(url).toURL().openConnection(java.net.Proxy.NO_PROXY);
            c.setConnectTimeout(2000);
            c.setReadTimeout(4000);
            try (java.io.InputStream in = c.getInputStream()) { in.readAllBytes(); }
        } catch (Exception ignore) { /* best effort: the kill below is the guarantee */ }
    }
}