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.3 KB
Raw Blame
package opensuite.engagement;

import opensuite.core.Bytes;
import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.core.LogEntry;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

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

/** Tests the find comments, find scripts, find references and analyse target tools. */
public final class EngagementToolsTest {

    private static final HttpService SVC = new HttpService("shop.test", 443, true);

    private static HttpMessage resp(String type, String body) {
        return new HttpMessage(Bytes.bytes("HTTP/1.1 200 OK\r\nContent-Type: " + type + "\r\n\r\n" + body));
    }

    private static LogEntry entry(HttpService svc, String requestLine, String reqBody, HttpMessage resp) {
        String head = requestLine + " HTTP/1.1\r\nHost: " + svc.host() + "\r\nCookie: sid=1\r\n"
                + (reqBody.isEmpty() ? "" : "Content-Type: application/x-www-form-urlencoded\r\n")
                + "\r\n" + reqBody;
        LogEntry e = new LogEntry("proxy", svc, new HttpMessage(Bytes.bytes(head)));
        if (resp != null) e.setResponse(resp);
        return e;
    }

    public void run() {
        Harness.suite("engagement tools");

        Harness.test("find comments takes HTML comments and the comments inside scripts", () -> {
            HttpMessage r = resp("text/html", "<html><!-- todo: remove debug --><script>\n"
                    + "var u = \"http://x.test/a\"; // call home\n/* block\n note */ var s = '<!-- not one -->';\n"
                    + "</script><p>text // not a comment</p></html>");
            List<String> c = ContentFinder.comments(r);
            Expect.equal(c, List.of("todo: remove debug", "call home", "block\n note"), "comments found");
        });

        Harness.test("a script response is scanned as script, and binary content is skipped", () -> {
            Expect.equal(ContentFinder.comments(resp("application/javascript", "let a = 1; // one\n")),
                    List.of("one"), "line comment in a script file");
            Expect.isTrue(ContentFinder.comments(resp("image/png", "<!-- x -->")).isEmpty(), "images are not searched");
            Expect.isTrue(ContentFinder.comments(null).isEmpty(), "no response, no comments");
        });

        Harness.test("find scripts lists external references and inline source", () -> {
            HttpMessage r = resp("text/html", "<script src=\"/js/app.js\"></script><SCRIPT type='module' src=/m.js></SCRIPT>"
                    + "<script> alert(1) </script><script>  </script>");
            List<ContentFinder.Script> s = ContentFinder.scripts(r);
            Expect.equal(s.size(), 3, "empty script elements are dropped");
            Expect.equal(s.get(0).src(), "/js/app.js", "quoted src");
            Expect.equal(s.get(1).src(), "/m.js", "unquoted src");
            Expect.isFalse(s.get(2).external(), "inline script");
            Expect.equal(s.get(2).summary(), "alert(1)", "inline summary");
            Expect.equal(ContentFinder.scripts(resp("text/javascript", "x()")).size(), 1, "a script file is one script");
        });

        Harness.test("find references matches the path on a boundary, or the full URL from anywhere", () -> {
            HttpService other = new HttpService("other.test", 443, true);
            List<LogEntry> all = new ArrayList<>();
            all.add(entry(SVC, "GET /cart", "", resp("text/html", "<a href=\"/cart\">self</a>")));
            all.add(entry(SVC, "GET /home", "", resp("text/html", "<a href=\"/cart\">cart</a>")));
            all.add(entry(SVC, "GET /about", "", resp("text/html", "<a href=\"/cartoon\">no</a>")));
            all.add(entry(other, "GET /ext", "", resp("text/html", "<a href='https://shop.test/cart'>x</a>")));
            all.add(entry(other, "GET /rel", "", resp("text/html", "<a href='/cart'>their own cart</a>")));
            List<FindWindow.Hit> hits = FindWindow.search(all, FindWindow.Kind.REFERENCES, SVC, "/cart");
            Expect.equal(hits.size(), 2, "the linking page and the absolute link from another host");
            Expect.equal(hits.get(0).entry().request().path(), "/home", "same-host relative link");
            Expect.equal(hits.get(1).entry().request().path(), "/ext", "cross-host absolute link");
        });

        Harness.test("comments are searched at and below the selected URL, once per URL", () -> {
            List<LogEntry> all = new ArrayList<>();
            all.add(entry(SVC, "GET /admin/users", "", resp("text/html", "<!-- a --><!-- b -->")));
            all.add(entry(SVC, "GET /admin/users", "", resp("text/html", "<!-- a --><!-- b -->")));
            all.add(entry(SVC, "GET /administrator", "", resp("text/html", "<!-- c -->")));
            all.add(entry(SVC, "GET /admin/plain", "", resp("text/html", "nothing")));
            List<FindWindow.Hit> hits = FindWindow.search(all, FindWindow.Kind.COMMENTS, SVC, "/admin");
            Expect.equal(hits.size(), 1, "one hit: duplicates and the sibling folder are left out");
            Expect.equal(FindWindow.summary(FindWindow.Kind.COMMENTS, hits, "x"), "2 comments in 1 item under x", "summary");
            Expect.equal(FindWindow.search(all, FindWindow.Kind.COMMENTS, SVC, "/").size(), 2, "the root covers the host");
        });

        Harness.test("analyse target splits dynamic from static URLs and ignores cookies", () -> {
            List<LogEntry> all = new ArrayList<>();
            all.add(entry(SVC, "GET /item?id=1&ref=a", "", null));
            all.add(entry(SVC, "GET /item?id=2", "", null));
            all.add(entry(SVC, "POST /login", "user=a&id=9", null));
            all.add(entry(SVC, "GET /static/app.css", "", null));
            TargetAnalysis a = TargetAnalysis.of(all);
            Expect.equal(a.urlCount(), 3, "distinct URLs");
            Expect.equal(a.dynamicUrls().size(), 2, "dynamic URLs");
            Expect.equal(a.staticUrls().size(), 1, "the cookie alone does not make a URL dynamic");
            Expect.equal(a.parameterInstances(), 4, "id+ref on /item, user+id on /login");
            Expect.equal(a.parameters().size(), 3, "unique names: id, ref, user");
            TargetAnalysis.Parameter id = a.parameters().get(0);
            Expect.equal(id.name, "id", "first parameter seen");
            Expect.equal(id.urls.size(), 2, "id appears on two URLs");
        });
    }
}