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

import opensuite.core.HttpMessage;
import opensuite.core.LogEntry;

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

/**
 * Compares two site maps item by item. Requests are paired by a key built from
 * the parts the user chose to match on; a pair whose responses differ in the
 * parts chosen for comparison is "modified", an item only in the first map is
 * "deleted" and one only in the second is "added".
 */
public final class SiteMapDiff {
    private SiteMapDiff() {}

    public enum State { SAME, MODIFIED, DELETED, ADDED }

    /** How requests are paired and what of their responses is compared. */
    public static final class Options {
        public boolean matchMethod = true;
        public boolean matchQuery = true;
        public boolean matchHost = true;
        public boolean compareStatus = true;
        public boolean compareHeaders = false;
        public boolean compareBody = true;
        public boolean ignoreWhitespace = true;
    }

    /** One line of the result: the item in each map (either may be missing) and what became of it. */
    public record Row(String key, LogEntry one, LogEntry two, State state) {}

    static String key(LogEntry e, Options o) {
        StringBuilder b = new StringBuilder();
        if (o.matchMethod && e.request() != null) b.append(e.request().method()).append(' ');
        if (o.matchHost && e.service() != null) b.append(e.service().url());
        b.append(o.matchQuery ? e.pathAndQuery() : e.requestPath());
        return b.toString();
    }

    public static List<Row> compare(List<LogEntry> one, List<LogEntry> two, Options o) {
        Map<String, LogEntry> a = index(one, o), b = index(two, o);
        List<Row> out = new ArrayList<>();
        for (Map.Entry<String, LogEntry> e : a.entrySet()) {
            LogEntry other = b.get(e.getKey());
            if (other == null) out.add(new Row(e.getKey(), e.getValue(), null, State.DELETED));
            else out.add(new Row(e.getKey(), e.getValue(), other, same(e.getValue(), other, o) ? State.SAME : State.MODIFIED));
        }
        for (Map.Entry<String, LogEntry> e : b.entrySet()) {
            if (!a.containsKey(e.getKey())) out.add(new Row(e.getKey(), null, e.getValue(), State.ADDED));
        }
        return out;
    }

    /** The latest requested exchange for each key; items that were only seen as links are left out. */
    private static Map<String, LogEntry> index(List<LogEntry> items, Options o) {
        Map<String, LogEntry> m = new LinkedHashMap<>();
        for (LogEntry e : items) {
            if (e == null || e.request() == null || "link".equals(e.tool())) continue;
            m.put(key(e, o), e);
        }
        return m;
    }

    static boolean same(LogEntry x, LogEntry y, Options o) {
        HttpMessage a = x.response(), b = y.response();
        if (a == null || b == null) return a == null && b == null;
        if (o.compareStatus && a.statusCode() != b.statusCode()) return false;
        if (o.compareHeaders && !tidy(a.headerText(), o).equals(tidy(b.headerText(), o))) return false;
        if (o.compareBody && !tidy(a.bodyText(), o).equals(tidy(b.bodyText(), o))) return false;
        return true;
    }

    private static String tidy(String s, Options o) {
        if (s == null) return "";
        return o.ignoreWhitespace ? s.replaceAll("\\s+", " ").trim() : s;
    }

    /** How many rows are in each state, in the order of {@link State}. */
    public static int[] counts(List<Row> rows) {
        int[] n = new int[State.values().length];
        for (Row r : rows) n[r.state().ordinal()]++;
        return n;
    }
}