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.

13.1 KB
Raw Blame
package opensuite.dashboard;

import opensuite.core.SuiteContext;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import javax.swing.JSpinner;

/**
 * Builds the scan configuration panel and drives it the way the wizard does.
 *
 * <p>New scan did nothing at all in a shipped build. The panel pulled the text
 * field out of each spinner's editor and put it on a row, which takes it out of
 * the editor, since a component has one parent. The editor was left childless
 * and the next setValue threw, so the wizard died on the way up and no window
 * ever appeared.
 *
 * <p>Nothing caught it because the tests and the manual sweeps opened tabs and
 * pages; neither pressed the button. These cases press it.
 */
public final class ScanWizardTest {

    private interface Body { void run() throws Exception; }

    private static void check(String name, Body body) {
        Harness.test(name, () -> {
            try { body.run(); }
            catch (AssertionError e) { throw e; }
            catch (Exception e) { throw new AssertionError(e.toString(), e); }
        });
    }

    public void run() {
        Harness.suite("scan wizard");

        Harness.test("a scope rule that names only a host offers a start only when there is one to offer", () -> {
            java.util.List<String> seen = java.util.List.of("http://127.0.0.1:3000", "https://shop.test", "http://127.0.0.1:4200", "http://one.test:8080");
            java.util.List<String> none = java.util.List.of();
            Expect.equal(NewScanWizard.startingUrls("any", "127.0.0.1", "", seen), none, "two sites at the host: neither is offered");
            Expect.equal(NewScanWizard.startingUrls("any", "127.0.0.1", "3000", seen), java.util.List.of("http://127.0.0.1:3000/"), "a named port: the one site there");
            Expect.equal(NewScanWizard.startingUrls("any", "one.test", "", seen), java.util.List.of("http://one.test:8080/"), "one site at the host");
            Expect.equal(NewScanWizard.startingUrls("http", "shop.test", "", seen), none, "a protocol the host was not seen on");
            Expect.equal(NewScanWizard.startingUrls("any", "new.test", "", seen), none, "never seen: nothing is guessed");
            Expect.equal(NewScanWizard.prefixUrls("127.0.0.1", seen), none, "a prefix that is only a host, seen on two ports");
            Expect.equal(NewScanWizard.prefixUrls("127.0.0.1:3000/rest", seen), java.util.List.of("http://127.0.0.1:3000/rest"), "a prefix with a port and a path");
            Expect.equal(NewScanWizard.prefixUrls("new.test", seen), none, "a prefix never seen");
        });

        Harness.test("a scan puts its own site in scope and not the host's other ports", () -> {
            opensuite.core.Scope s = new opensuite.core.Scope();
            s.includeSite(opensuite.core.HttpService.parse("http://127.0.0.1:3000/"));
            Expect.equal(s.include().size(), 1, "one rule");
            Expect.equal(s.include().get(0).prefix, "http://127.0.0.1:3000", "the site as scanned");
            Expect.isTrue(s.isInScope(opensuite.core.HttpService.parse("http://127.0.0.1:3000/"), "/rest/x"), "the site");
            Expect.isFalse(s.isInScope(opensuite.core.HttpService.parse("http://127.0.0.1:4200/"), "/"), "another port of the host");
            Expect.isFalse(s.isInScope(opensuite.core.HttpService.parse("http://127.0.0.1/"), "/"), "the host's usual port");
            s.includeSite(opensuite.core.HttpService.parse("http://127.0.0.1:3000/"));
            Expect.equal(s.include().size(), 1, "not added twice");

            opensuite.core.Scope usual = new opensuite.core.Scope();
            usual.includeSite(opensuite.core.HttpService.parse("http://shop.test/"));
            Expect.isTrue(usual.isInScope(opensuite.core.HttpService.parse("https://shop.test/"), "/"), "http redirecting to https stays in scope");
            Expect.isTrue(usual.isInScope(opensuite.core.HttpService.parse("http://shop.test/"), "/"), "as does http");
            Expect.isFalse(usual.isInScope(opensuite.core.HttpService.parse("https://shop.test:8443/"), "/"), "but no other port");
            Expect.isFalse(usual.isInScope(opensuite.core.HttpService.parse("https://api.shop.test/"), "/"), "and no other host");
        });

        Harness.test("the page picker finds a page by a piece of its address", () -> {
            java.util.List<String> pages = java.util.List.of("http://shop.test/rest/user/Logout", "http://shop.test/api/Users/",
                    "http://shop.test/rest/user/whoami", "http://shop.test/assets/logo.png");
            Expect.equal(ApplicationLoginPanel.matching(pages, "logout"), java.util.List.of("http://shop.test/rest/user/Logout"), "capitals make no difference");
            Expect.equal(ApplicationLoginPanel.matching(pages, "  user  "), java.util.List.of("http://shop.test/rest/user/Logout",
                    "http://shop.test/api/Users/", "http://shop.test/rest/user/whoami"), "every page with it, in the order given");
            Expect.equal(ApplicationLoginPanel.matching(pages, "rest who"), java.util.List.of("http://shop.test/rest/user/whoami"), "several words: all of them");
            Expect.equal(ApplicationLoginPanel.matching(pages, ""), pages, "nothing typed: every page");
            Expect.equal(ApplicationLoginPanel.matching(pages, null), pages, "no text at all: every page");
            Expect.equal(ApplicationLoginPanel.matching(pages, "signout"), java.util.List.of(), "no page has it: none");
        });

        Harness.test("the login step shares its height and counts what each tab holds", () -> {
            Expect.equal(ApplicationLoginPanel.tabTitle("Out of scope", 0), "Out of scope", "an empty list: the name alone");
            Expect.equal(ApplicationLoginPanel.tabTitle("Out of scope", 3), "Out of scope (3)", "a list with entries: how many");
            Expect.equal(ApplicationLoginPanel.sessionShare(500), 225, "the session takes a little under half of what is left");
            Expect.equal(ApplicationLoginPanel.sessionShare(0), 0, "no room: none");
        });


        check("the configuration panel builds in every mode", () -> {
            for (ScanConfigPanel.Mode m : ScanConfigPanel.Mode.values()) {
                ScanConfigPanel panel = new ScanConfigPanel(m);
                Expect.notNull(panel.config(), m + " produced no configuration");
            }
        });

        check("a number box left empty means no limit, and a configuration goes in and comes back", () -> {
            javax.swing.JTextField f = new javax.swing.JTextField("");
            Expect.equal(ScanConfigPanel.intOf(f, 0), 0, "empty");
            f.setText(" 150 ");
            Expect.equal(ScanConfigPanel.intOf(f, 0), 150, "a number");
            f.setText("abc");
            Expect.equal(ScanConfigPanel.intOf(f, 7), 7, "not a number");
            ScanConfigPanel panel = new ScanConfigPanel(ScanConfigPanel.Mode.CRAWL_AND_AUDIT);
            for (String name : new String[]{"Lightweight", "Fast", "Balanced", "Deep"}) {
                opensuite.engine.ScanConfig c = opensuite.engine.ScanConfig.preset(name);
                panel.apply(c);
                opensuite.engine.ScanConfig back = panel.config();
                Expect.equal(back.crawlDepth(), c.crawlDepth(), name + ": crawl depth");
                Expect.equal(back.crawlDurationMin(), c.crawlDurationMin(), name + ": crawl time");
                Expect.equal(back.scanDurationMin(), c.scanDurationMin(), name + ": scan time");
                Expect.equal(back.injectableMask(), c.injectableMask(), name + ": insertion points");
                Expect.equal(back.rpcMask(), c.rpcMask(), name + ": body formats");
                Expect.equal(back.ajaxSpider(), c.ajaxSpider(), name + ": browser");
                Expect.equal(back.attackStrength().equals("INSANE") ? "HIGH" : back.attackStrength(), c.attackStrength().equals("INSANE") ? "HIGH" : c.attackStrength(), name + ": audit speed");
            }
        });

        check("the options of a custom configuration reach the engine's settings and survive saving", () -> {
            ScanConfigPanel panel = new ScanConfigPanel(ScanConfigPanel.Mode.CRAWL_AND_AUDIT);
            panel.apply(ScanConfigPanel.customDefaults());
            opensuite.engine.ScanConfig.Options o = panel.config().more();
            Expect.isTrue(o.robots() && o.sitemap() && o.hiddenLinks() && o.clickAll(), "the discovery boxes are ticked");
            Expect.equal(o.maxLocations(), 1500, "locations limit");
            Expect.equal(o.maxRequests(), 0, "an empty box is no limit");
            Expect.equal(o.crawlTimeoutSecs(), -1, "an empty timeout leaves the engine's own");
            Expect.equal(o.userAgent(), "", "no User-Agent of its own");
            Expect.isTrue(o.skips() != null && !o.skips().isEmpty(), "the parameter lists are sent");
            javax.swing.JTextField a = new javax.swing.JTextField(""), b = new javax.swing.JTextField("0");
            Expect.equal(ScanConfigPanel.timeoutOf(a, b), 0, "0 turns the timeout off");
            b.setText("45"); a.setText("10");
            Expect.equal(ScanConfigPanel.timeoutOf(a, b), 45, "the longer of the two");

            opensuite.engine.ScanConfig.Skip is = new opensuite.engine.ScanConfig.Skip("Cookie", "Is", "asp.net_sessionid", false);
            Expect.equal(is.typeId(), 4, "a cookie");
            Expect.isTrue(java.util.regex.Pattern.matches(is.regex(), "ASP.NET_SessionId"), "a plain name matches whatever its case");
            Expect.isTrue(!java.util.regex.Pattern.matches(is.regex(), "aspXnet_sessionid"), "and its dot is a dot");
            opensuite.engine.ScanConfig.Skip re = new opensuite.engine.ScanConfig.Skip("XML attribute", "Matches regex", "xmlns.*", true);
            Expect.equal(re.typeId(), -1, "an XML attribute is matched anywhere");
            Expect.isTrue(java.util.regex.Pattern.matches(re.regex(), "xmlns:soap"), "an expression matches");

            opensuite.engine.ScanConfig.Options lim = new opensuite.engine.ScanConfig.Options(true, true, true, false, "", -1, -1, 100, 500, null, false, 0);
            Expect.isTrue(opensuite.engine.ZapScanner.crawlCap(lim, 99, 499) == null, "inside both limits");
            Expect.isTrue(opensuite.engine.ZapScanner.crawlCap(lim, 100, 0).contains("100 unique locations"), "at the locations limit");
            Expect.isTrue(opensuite.engine.ZapScanner.crawlCap(lim, 5, 500).contains("500 requests"), "at the requests limit");
            Expect.isTrue(opensuite.engine.ZapScanner.crawlCap(opensuite.engine.ScanConfig.Options.DEFAULT, 99999, 99999) == null, "no limits set");

            Expect.equal(opensuite.engine.ZapScanner.globalTimeoutSecs(120_000, 120_000), 120, "the settings' own");
            Expect.equal(opensuite.engine.ZapScanner.globalTimeoutSecs(15_000, 30_000), 30, "the longer of the two");
            Expect.equal(opensuite.engine.ZapScanner.globalTimeoutSecs(Integer.MAX_VALUE, 30_000), 0, "no limit in the settings is no limit");
            Expect.equal(o.sitemapMax(), 1000, "site map limit of a custom configuration");
            Expect.isTrue(!o.graphqlCommon(), "common GraphQL endpoints are not tried unless ticked");
            java.util.List<String> locs = opensuite.engine.ZapScanner.sitemapLocations(
                    "<urlset><url><loc> http://a.test/one?a=1&amp;b=2 </loc></url><url><loc><![CDATA[http://a.test/two]]></loc></url><url><LOC>http://a.test/three</LOC></url></urlset>");
            Expect.equal(locs, java.util.List.of("http://a.test/one?a=1&b=2", "http://a.test/two", "http://a.test/three"), "the addresses of a site map, in order");
            Expect.isTrue(opensuite.engine.ZapScanner.sitemapLocations("not a map").isEmpty(), "nothing from what is not one");
            Expect.isTrue(opensuite.engine.ZapScanner.asksForWsdl("http://a.test/soap/calc?wsdl"), "the usual form");
            Expect.isTrue(opensuite.engine.ZapScanner.asksForWsdl("http://a.test/Service.svc?singleWsdl"), "the single-file form");
            Expect.isTrue(opensuite.engine.ZapScanner.asksForWsdl("http://a.test/s?x=1&WSDL"), "whatever its case, among other parameters");
            Expect.isTrue(!opensuite.engine.ZapScanner.asksForWsdl("http://a.test/soap/calc?wsdl=c:/Windows/system.ini"), "not an attack on that parameter");
            Expect.isTrue(!opensuite.engine.ZapScanner.asksForWsdl("http://a.test/soap/calc.wsdl"), "a file the engine reads itself");
            Expect.isTrue(!opensuite.engine.ZapScanner.asksForWsdl("http://a.test/page?q=wsdl"), "a value is not a name");
        });

        check("the wizard itself can be constructed", () -> {
            SuiteContext ctx = new SuiteContext();
            NewScanWizard w = new NewScanWizard(ctx, null);
            Expect.isFalse(w.started(), "a wizard that was never run reports started");
            w.dispose();
        });
    }

    private static java.util.List<JSpinner> spinnersIn(java.awt.Component c) {
        java.util.List<JSpinner> out = new java.util.ArrayList<>();
        collect(c, out);
        return out;
    }

    private static void collect(java.awt.Component c, java.util.List<JSpinner> out) {
        if (c instanceof JSpinner s) out.add(s);
        if (c instanceof java.awt.Container k) {
            for (java.awt.Component ch : k.getComponents()) collect(ch, out);
        }
    }
}