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.

9.3 KB
Raw Blame
package opensuite.intruder;

import opensuite.ui.Ui;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/** A hand-built list of payload strings. */
class SimpleListGenerator extends AbstractGenerator {

    private final DefaultTableModel model =
            new DefaultTableModel(new Object[]{"item"}, 0) {
                @Override public boolean isCellEditable(int r, int c) { return false; }
            };
    private final JTable table = new JTable(model);
    private final JTextField entry = Ui.field("");
    private final JLabel countLabel = Ui.label("0 items");
    private final JPanel panel;

    SimpleListGenerator() {
        Ui.styleTable(table);
        Ui.setWidths(table, 300);
        JScrollPane sp = Ui.scroll(table);
        sp.setPreferredSize(new Dimension(200, 150));

        JButton add    = Ui.button("Add");
        JButton load   = Ui.button("Load...", "Load payloads from a file");
        JButton paste  = Ui.button("Paste");
        JButton delete = Ui.button("Remove");
        JButton clear  = Ui.button("Clear");
        JButton dedupe = Ui.button("Deduplicate", "Remove duplicate items, keeping the first occurrence of each");

        entry.putClientProperty("JTextField.placeholderText", "Enter a new item");
        entry.putClientProperty(Ui.ITALIC_PLACEHOLDER, Boolean.TRUE);
        add.addActionListener(e -> addEntry());
        entry.addActionListener(e -> addEntry());
        load.addActionListener(e -> load());
        paste.addActionListener(e -> paste());
        delete.addActionListener(e -> deleteSelected());
        clear.addActionListener(e -> { model.setRowCount(0); changed(); });
        dedupe.addActionListener(e -> deduplicate());

        // As in the reference: the button column to the left of a plain list
        // (no header), then Add with its entry field across the row, and the
        // built-in list chooser across the full width under that.
        table.setTableHeader(null);
        JPanel buttons = Ui.buttonColumn(paste, load, delete, clear, dedupe);
        JComboBox<String> fromList = Ui.combo(BUILT_IN_NAMES);
        // The lists on offer are the built-in ones, or the text files of the
        // directory chosen under Settings > Tools > Intruder; read each time the list opens.
        final java.util.List<java.io.File> files = new java.util.ArrayList<>();
        fromList.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            @Override public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent e) {
                java.io.File dir = customLists.get();
                files.clear();
                DefaultComboBoxModel<String> m = new DefaultComboBoxModel<>();
                if (dir != null && dir.isDirectory()) {
                    m.addElement(BUILT_IN_NAMES[0]);
                    java.io.File[] found = dir.listFiles(f -> f.isFile() && !f.isHidden());
                    if (found != null) {
                        java.util.Arrays.sort(found);
                        for (java.io.File f : found) { files.add(f); m.addElement(f.getName().replaceFirst("\\.(txt|lst|list)$", "")); }
                    }
                } else {
                    for (String n : BUILT_IN_NAMES) m.addElement(n);
                }
                fromList.setModel(m);
            }
            @Override public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent e) { }
            @Override public void popupMenuCanceled(javax.swing.event.PopupMenuEvent e) { }
        });
        fromList.addActionListener(e -> {
            int i = fromList.getSelectedIndex();
            if (i <= 0) return;
            if (!files.isEmpty()) {
                if (i - 1 < files.size()) {
                    try { addAll(java.nio.file.Files.readAllLines(files.get(i - 1).toPath()).toArray(new String[0])); }
                    catch (java.io.IOException | java.io.UncheckedIOException ex) {
                        JOptionPane.showMessageDialog(fromList, "Could not read " + files.get(i - 1).getName() + ": " + ex.getMessage(),
                                "Add from list", JOptionPane.ERROR_MESSAGE);
                    }
                }
            } else if (i - 1 < BUILT_IN.length) {
                addAll(BUILT_IN[i - 1]);
            }
            fromList.setSelectedIndex(0);
        });

        // Add sits under the button column at the same width, so the entry
        // field starts exactly where the list does.
        int colWidth = buttons.getPreferredSize().width;
        add.setPreferredSize(new Dimension(colWidth, add.getPreferredSize().height));
        JPanel addRow = new JPanel(new BorderLayout(5, 0));
        addRow.setOpaque(false);
        addRow.add(add, BorderLayout.WEST);
        addRow.add(entry, BorderLayout.CENTER);

        JPanel upper = new JPanel(new BorderLayout(5, 0));
        upper.setOpaque(false);
        upper.add(buttons, BorderLayout.WEST);
        upper.add(sp, BorderLayout.CENTER);

        JPanel lower = new JPanel(new BorderLayout(0, 0));
        lower.setOpaque(false);
        lower.setBorder(Ui.pad(5, 0, 15, 0));
        lower.add(addRow, BorderLayout.NORTH);
        JPanel listWrap = new JPanel(new BorderLayout());
        listWrap.setOpaque(false);
        listWrap.setBorder(Ui.pad(5, 0, 0, 0));
        listWrap.add(fromList, BorderLayout.CENTER);
        lower.add(listWrap, BorderLayout.SOUTH);

        panel = new JPanel(new BorderLayout());
        panel.setOpaque(false);
        panel.add(upper, BorderLayout.CENTER);
        panel.add(lower, BorderLayout.SOUTH);
    }

    /** The directory of custom payload lists, or null for the built-in ones; set by the Intruder tool. */
    static volatile java.util.function.Supplier<java.io.File> customLists = () -> null;

    private static final String[] BUILT_IN_NAMES = {
            "Add from list...", "Short words", "Common directories", "Common filenames",
            "HTTP methods", "Fuzzing - quick", "Numbers 0-99", "User agents" };

    /** The built-in lists the "Add from list ..." chooser offers. */
    private static final String[][] BUILT_IN = {
        {"a", "b", "c", "d", "e", "f", "g", "test", "foo", "bar"},
        {"admin", "api", "assets", "backup", "cgi-bin", "config", "css", "images", "js", "logs",
         "private", "public", "static", "tmp", "uploads"},
        {"index.html", "index.php", "login.php", "admin.php", "config.php", "web.config",
         ".env", ".git/HEAD", "robots.txt", "sitemap.xml", "backup.zip"},
        {"GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "TRACE", "CONNECT"},
        {"'", "\"", "<script>", "../", "..\\", "%00", "${7*7}", "{{7*7}}", "|id", ";id", "`id`"},
        null,   // filled below: 0-99
        {"Mozilla/5.0", "curl/8.5.0", "python-requests/2.31.0", "Googlebot/2.1", "Wget/1.21"},
    };
    static {
        String[] nums = new String[100];
        for (int i = 0; i < 100; i++) nums[i] = String.valueOf(i);
        BUILT_IN[5] = nums;
    }

    private void addAll(String[] items) {
        for (String i : items) model.addRow(new Object[]{i});
        changed();
    }

    /** Drop repeated items, keeping the first of each. */
    private void deduplicate() {
        java.util.LinkedHashSet<String> seen = new java.util.LinkedHashSet<>();
        for (int r = 0; r < model.getRowCount(); r++) seen.add(String.valueOf(model.getValueAt(r, 0)));
        model.setRowCount(0);
        for (String v : seen) model.addRow(new Object[]{v});
        changed();
    }

    private void addEntry() {
        String t = entry.getText();
        if (t.isEmpty()) return;
        model.addRow(new Object[]{t});
        entry.setText("");
        changed();
    }

    private void deleteSelected() {
        int[] rows = table.getSelectedRows();
        for (int i = rows.length - 1; i >= 0; i--) model.removeRow(rows[i]);
        changed();
    }

    private void load() {
        JFileChooser fc = new opensuite.ui.FileChooser();
        if (fc.showOpenDialog(panel) != JFileChooser.APPROVE_OPTION) return;
        try (BufferedReader r = new BufferedReader(new FileReader(fc.getSelectedFile()))) {
            String line;
            while ((line = r.readLine()) != null) model.addRow(new Object[]{line});
            changed();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(panel, "Could not read file: " + ex.getMessage());
        }
    }

    private void paste() {
        try {
            String data = (String) Toolkit.getDefaultToolkit().getSystemClipboard()
                    .getData(java.awt.datatransfer.DataFlavor.stringFlavor);
            if (data == null) return;
            for (String line : data.split("\r\n|\r|\n")) model.addRow(new Object[]{line});
            changed();
        } catch (Exception ignored) {}
    }

    private void changed() {
        countLabel.setText(model.getRowCount() + " items");
        fire();
    }

    @Override public String label() { return " simple list"; }
    @Override public JComponent config() { return panel; }

    @Override public List<String> generate() {
        List<String> out = new ArrayList<>();
        for (int i = 0; i < model.getRowCount() && out.size() < CAP; i++) {
            out.add(String.valueOf(model.getValueAt(i, 0)));
        }
        return out;
    }

    @Override public int count() { return model.getRowCount(); }
}