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

import opensuite.core.*;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/** Suite-wide search across recorded traffic. */
public class SearchWindow extends JFrame {

    private final SuiteContext ctx;
    private final JTextField term = Ui.field("", 32);
    private final JCheckBox caseSensitive = Ui.check("Case sensitive", false);
    private final JCheckBox regex = Ui.check("Regex", false);
    private final JCheckBox inScope = Ui.check("In-scope only", false);
    private final JCheckBox negative = Ui.check("Negative search", false);
    private final JCheckBox dynamic = Ui.check("Dynamic update", false);
    private final JCheckBox reqHeaders = Ui.check("Request headers", true);
    private final JCheckBox respHeaders = Ui.check("Response headers", true);
    private final JCheckBox reqBody = Ui.check("Request body", true);
    private final JCheckBox respBody = Ui.check("Response body", true);
    private final JLabel count = Ui.label("0 results");
    private final List<LogEntry> results = new ArrayList<>();
    private final Model model = new Model();
    private final JTable table = new JTable(model);
    private final MessageEditor reqView;
    private final MessageEditor respView;

    public SearchWindow(SuiteContext ctx) {
        super("Search");
        this.ctx = ctx;
        this.reqView = new MessageEditor(ctx, MessageEditor.REQUEST, false);
        this.respView = new MessageEditor(ctx, MessageEditor.RESPONSE, false);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        JPanel root = new JPanel(new BorderLayout(0, 6));
        root.setBorder(Ui.pad(10, 10, 10, 10));

        // Two groups under bold captions, as every other page sets its groups out.
        JButton go = Ui.button("Go");
        JPanel termRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        termRow.setOpaque(false);
        termRow.add(term); termRow.add(Box.createHorizontalStrut(5)); termRow.add(go);
        JPanel optionRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        optionRow.setOpaque(false);
        for (JCheckBox c : new JCheckBox[]{ caseSensitive, regex, inScope, negative, dynamic }) { optionRow.add(c); optionRow.add(Box.createHorizontalStrut(12)); }
        JPanel s = Sections.column(0, Sections.caption("Search"), termRow, optionRow);
        JPanel locRow = new JPanel(new GridLayout(2, 2, 12, 2));
        locRow.setOpaque(false);
        locRow.add(reqHeaders); locRow.add(respHeaders); locRow.add(reqBody); locRow.add(respBody);
        JPanel loc = Sections.column(0, Sections.caption("Locations"), locRow);
        JPanel north = new JPanel(new BorderLayout(30, 0));
        north.setOpaque(false);
        north.add(s, BorderLayout.WEST);
        north.add(loc, BorderLayout.CENTER);
        // "Dynamic update": the search is run again as new items arrive, while the window is open.
        javax.swing.Timer again = new javax.swing.Timer(2000, e -> { if (dynamic.isSelected() && isShowing() && ctx.store().all().size() != searched) run(); });
        again.start();
        addWindowListener(new java.awt.event.WindowAdapter() {
            @Override public void windowClosed(java.awt.event.WindowEvent e) { again.stop(); }
        });
        root.add(north, BorderLayout.NORTH);

        Ui.styleTable(table);
        Ui.distributeWidths(table, 70, 200, 420, 60, 70);
        Ui.centerCells(table);
        JSplitPane split = new JSplitPane(JSplitPane.VERTICAL_SPLIT, Ui.scroll(table), detailPane());
        split.setDividerSize(1);
        Ui.balance(split, 0.45);
        root.add(split, BorderLayout.CENTER);
        root.add(count, BorderLayout.SOUTH);

        go.addActionListener(e -> run());
        term.addActionListener(e -> run());
        table.getSelectionModel().addListSelectionListener(e -> showSelected());

        setContentPane(root);
        setSize(1000, 700);
        setLocationRelativeTo(ctx.frame());
    }

    private JComponent detailPane() {
        JSplitPane sp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, reqView, respView);
        sp.setDividerSize(1);
        Ui.balance(sp, 0.5);
        return sp;
    }

    private void run() {
        results.clear();
        String t = term.getText();
        if (!t.isEmpty()) {
            Pattern p = null;
            if (regex.isSelected()) {
                try { p = Pattern.compile(t, caseSensitive.isSelected() ? 0 : Pattern.CASE_INSENSITIVE); }
                catch (Exception e) {
                    JOptionPane.showMessageDialog(this, "Invalid regex: " + t, "Search", JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            java.util.List<LogEntry> all = ctx.store().all();
            searched = all.size();
            for (LogEntry e : all) {
                if (inScope.isSelected() && !ctx.scope().isInScope(e)) continue;
                if (wanted(matches(e, t, p), negative.isSelected())) results.add(e);
            }
        }
        model.fireTableDataChanged();
        count.setText(results.size() + (results.size() == 1 ? " result" : " results"));
    }

    /** How many items the last search looked through, so a dynamic search knows when there is more. */
    private int searched;

    /** A negative search keeps the items the term is not in. */
    static boolean wanted(boolean found, boolean negative) { return found != negative; }

    private boolean matches(LogEntry e, String t, Pattern p) {
        return hit(e.request() == null ? null : e.request().headerText(), reqHeaders.isSelected(), t, p)
            || hit(e.request() == null ? null : e.request().bodyText(), reqBody.isSelected(), t, p)
            || hit(e.response() == null ? null : e.response().headerText(), respHeaders.isSelected(), t, p)
            || hit(e.response() == null ? null : e.response().bodyText(), respBody.isSelected(), t, p);
    }

    private boolean hit(String hay, boolean enabled, String t, Pattern p) {
        if (!enabled || hay == null) return false;
        if (p != null) return p.matcher(hay).find();
        return caseSensitive.isSelected() ? hay.contains(t)
                : hay.toLowerCase().contains(t.toLowerCase());
    }

    private void showSelected() {
        int i = table.getSelectedRow();
        if (i < 0 || i >= results.size()) return;
        LogEntry e = results.get(i);
        reqView.setMessage(e.service(), e.request() == null ? new HttpMessage(new byte[0]) : e.request().copy());
        respView.setMessage(e.service(), e.response() == null ? new HttpMessage(new byte[0]) : e.response().copy(),
                e.request());
    }

    private class Model extends AbstractTableModel {
        private final String[] cols = {"Source", "Host", "URL", "Status", "Length"};
        @Override public int getRowCount() { return results.size(); }
        @Override public int getColumnCount() { return cols.length; }
        @Override public String getColumnName(int c) { return cols[c]; }
        @Override public Object getValueAt(int r, int c) {
            LogEntry e = results.get(r);
            return switch (c) {
                case 0 -> e.tool();
                case 1 -> e.service().url();
                case 2 -> e.pathAndQuery();
                case 3 -> e.status() == 0 ? "" : String.valueOf(e.status());
                default -> e.responseLen() == 0 ? "" : String.valueOf(e.responseLen());
            };
        }
    }
}