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.

7.6 KB
Raw Blame
package opensuite.engagement;

import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.core.LogEntry;
import opensuite.core.SuiteContext;
import opensuite.ui.MessageEditor;
import opensuite.ui.Ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

/**
 * The results window shared by the Find comments, Find scripts and Find
 * references engagement tools: the items that contain something, what was
 * found in the selected one, and its request and response.
 */
public class FindWindow extends JFrame {

    public enum Kind {
        COMMENTS("comments", "Comment"), SCRIPTS("scripts", "Script"), REFERENCES("references", "Referring item");
        final String noun, column;
        Kind(String noun, String column) { this.noun = noun; this.column = column; }
    }

    /** One item with what was found in it. */
    record Hit(LogEntry entry, List<String> found) {}

    private final List<Hit> hits = new ArrayList<>();
    private final DefaultTableModel model = new DefaultTableModel(
            new Object[]{"Host", "Method", "URL", "Status", "Found"}, 0) {
        @Override public boolean isCellEditable(int r, int c) { return false; }
        @Override public Class<?> getColumnClass(int c) { return c == 3 || c == 4 ? Integer.class : String.class; }
    };
    private final JTable table = new JTable(model);
    private final JTextArea detail = new JTextArea();
    private final MessageEditor reqView;
    private final MessageEditor respView;

    public FindWindow(SuiteContext ctx, Kind kind, HttpService svc, HttpMessage req) {
        super("find " + kind.noun);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        reqView = new MessageEditor(ctx, MessageEditor.REQUEST, false);
        respView = new MessageEditor(ctx, MessageEditor.RESPONSE, false);

        String path = req == null || req.path() == null || req.path().isEmpty() ? "/" : req.path();
        hits.addAll(search(ctx.store().all(), kind, svc, path));
        for (Hit h : hits) {
            LogEntry e = h.entry();
            model.addRow(new Object[]{e.service().url(), e.request().method(), e.pathAndQuery(),
                    e.status() == 0 ? null : e.status(), h.found().size()});
        }

        Ui.styleTable(table);
        Ui.distributeWidths(table, 200, 60, 420, 60, 60);
        table.setRowSorter(new opensuite.ui.NumberSorter<>(table.getModel()));
        table.getSelectionModel().addListSelectionListener(e -> { if (!e.getValueIsAdjusting()) showSelected(); });

        detail.setEditable(false);
        detail.setFont(Ui.MONO);
        detail.setLineWrap(true);
        detail.setWrapStyleWord(true);

        JTabbedPane lower = new JTabbedPane();
        lower.setFont(Ui.PLAIN);
        lower.addTab(kind == Kind.REFERENCES ? "Summary" : capital(kind.noun), Ui.scroll(detail));
        lower.addTab("Request", reqView);
        lower.addTab("Response", respView);

        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, Ui.scroll(table), lower);
        split.setDividerSize(1);
        Ui.balance(split, 0.4);

        String scope = svc.url() + path;
        JLabel status = Ui.label(summary(kind, hits, scope));
        status.setBorder(Ui.pad(4, 2, 0, 2));

        JButton export = Ui.button("Export", "Copy every result to the clipboard");
        export.addActionListener(e -> Toolkit.getDefaultToolkit().getSystemClipboard().setContents(
                new java.awt.datatransfer.StringSelection(exportText(hits)), null));
        JPanel south = new JPanel(new BorderLayout());
        south.add(status, BorderLayout.CENTER);
        south.add(export, BorderLayout.EAST);

        JPanel root = new JPanel(new BorderLayout(0, 6));
        root.setBorder(Ui.pad(10, 10, 10, 10));
        root.add(split, BorderLayout.CENTER);
        root.add(south, BorderLayout.SOUTH);
        setContentPane(root);
        setSize(1000, 700);
        setLocationRelativeTo(ctx.frame());
        if (!hits.isEmpty()) table.setRowSelectionInterval(0, 0);
    }

    private static String capital(String s) { return Character.toUpperCase(s.charAt(0)) + s.substring(1); }

    static String summary(Kind kind, List<Hit> hits, String scope) {
        if (kind == Kind.REFERENCES) {
            return hits.size() + (hits.size() == 1 ? " item refers to " : " items refer to ") + scope;
        }
        int total = 0;
        for (Hit h : hits) total += h.found().size();
        return total + " " + kind.noun + " in " + hits.size() + (hits.size() == 1 ? " item under " : " items under ") + scope;
    }

    static String exportText(List<Hit> hits) {
        StringBuilder sb = new StringBuilder();
        for (Hit h : hits) {
            sb.append(h.entry().url()).append('\n');
            for (String f : h.found()) sb.append("    ").append(f.replace("\n", "\n    ")).append('\n');
        }
        return sb.toString();
    }

    /**
     * Search the recorded traffic. Comments and scripts are looked for in the
     * items at or below the selected URL; references are looked for everywhere,
     * since a link to the selected URL can sit on any page. One hit per
     * distinct URL: the same page fetched twice is listed once.
     */
    static List<Hit> search(List<LogEntry> all, Kind kind, HttpService svc, String path) {
        List<Hit> out = new ArrayList<>();
        Set<String> seen = new LinkedHashSet<>();
        String target = svc.url() + path;
        for (LogEntry e : all) {
            if (e.request() == null || e.response() == null) continue;
            boolean sameHost = e.service().host().equalsIgnoreCase(svc.host()) && e.service().port() == svc.port();
            List<String> found = new ArrayList<>();
            if (kind == Kind.REFERENCES) {
                String own = e.request().path();
                if (sameHost && path.equals(own)) continue;       // an item does not refer to itself
                if (!ContentFinder.refersTo(e.response(), sameHost, target, path)) continue;
                found.add(e.url() + " refers to " + target);
            } else {
                if (!sameHost || !under(e.request().path(), path)) continue;
                if (kind == Kind.COMMENTS) found.addAll(ContentFinder.comments(e.response()));
                else for (ContentFinder.Script s : ContentFinder.scripts(e.response())) {
                    found.add(s.external() ? "src: " + s.src() : s.code().strip());
                }
                if (found.isEmpty()) continue;
            }
            if (!seen.add(e.request().method() + " " + e.url())) continue;
            out.add(new Hit(e, found));
        }
        return out;
    }

    /** True when {@code path} is the selected path or sits below it. */
    static boolean under(String path, String selected) {
        if (path == null) return false;
        if (selected.equals("/") || path.equals(selected)) return true;
        String dir = selected.endsWith("/") ? selected : selected + "/";
        return path.startsWith(dir);
    }

    private void showSelected() {
        int view = table.getSelectedRow();
        if (view < 0) return;
        Hit h = hits.get(table.convertRowIndexToModel(view));
        StringBuilder sb = new StringBuilder();
        for (String f : h.found()) {
            if (sb.length() > 0) sb.append("\n\n");
            sb.append(f);
        }
        detail.setText(sb.toString());
        detail.setCaretPosition(0);
        LogEntry e = h.entry();
        reqView.setMessage(e.service(), e.request().copy());
        respView.setMessage(e.service(), e.response().copy(), e.request());
    }
}