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

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * The comparison behind the Compare site maps engagement tool, used for
 * access-control testing: given the same URLs requested in two different
 * sessions (for example as an authenticated and an unauthenticated user), it
 * reports, per URL, what each session got back and whether the responses look
 * alike. Two URLs that return a similar, successful response in both sessions
 * are a sign that the second session reached something it should not.
 *
 * <p>Pure logic with no Swing or network in it, so it is fully unit tested.
 */
public final class SiteMapCompare {
    private SiteMapCompare() {}

    /** One side's result for a URL: the status and body length, or absent. */
    public record Side(boolean present, int status, int length) {
        public static final Side ABSENT = new Side(false, 0, 0);
        public static Side of(int status, int length) { return new Side(true, status, length); }
    }

    public enum Verdict {
        ONLY_FIRST("only in session 1"),
        ONLY_SECOND("only in session 2"),
        DIFFERENT("different"),
        SIMILAR("similar");
        public final String label;
        Verdict(String label) { this.label = label; }
    }

    /** One compared URL. */
    public record Row(String method, String url, Side first, Side second, Verdict verdict) {
        /** True when both sessions got a similar, successful response: a possible access-control failure. */
        public boolean accessControlConcern() {
            return verdict == Verdict.SIMILAR && second.present()
                    && second.status() >= 200 && second.status() < 300;
        }
    }

    /** One item fed into the comparison. */
    public record Item(String method, String url, int status, int length) {}

    /**
     * Compare two sets of results for the same URLs. The bodies are treated as
     * similar when both responses have the same status class and their lengths
     * are within {@code lengthTolerance} (a fraction, e.g. 0.05 for 5%).
     */
    public static List<Row> compare(List<Item> firstSession, List<Item> secondSession, double lengthTolerance) {
        Map<String, Item> a = index(firstSession);
        Map<String, Item> b = index(secondSession);
        List<String> keys = new ArrayList<>(a.keySet());
        for (String k : b.keySet()) if (!a.containsKey(k)) keys.add(k);

        List<Row> rows = new ArrayList<>();
        for (String key : keys) {
            Item ai = a.get(key);
            Item bi = b.get(key);
            Item any = ai != null ? ai : bi;
            Side first = ai != null ? Side.of(ai.status(), ai.length()) : Side.ABSENT;
            Side second = bi != null ? Side.of(bi.status(), bi.length()) : Side.ABSENT;
            Verdict v = verdict(first, second, lengthTolerance);
            rows.add(new Row(any.method(), any.url(), first, second, v));
        }
        return rows;
    }

    private static Map<String, Item> index(List<Item> items) {
        Map<String, Item> m = new LinkedHashMap<>();
        if (items != null) for (Item i : items) m.putIfAbsent(i.method() + " " + i.url(), i);
        return m;
    }

    private static Verdict verdict(Side first, Side second, double tol) {
        if (!second.present()) return Verdict.ONLY_FIRST;
        if (!first.present()) return Verdict.ONLY_SECOND;
        boolean sameClass = first.status() / 100 == second.status() / 100;
        int max = Math.max(first.length(), second.length());
        boolean sameLength = max == 0 || Math.abs(first.length() - second.length()) <= tol * max;
        return sameClass && sameLength ? Verdict.SIMILAR : Verdict.DIFFERENT;
    }
}