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.

15.4 KB
Raw Blame
package opensuite.decoder;

import opensuite.core.Bytes;
import opensuite.core.SuiteContext;
import opensuite.core.ToolModule;
import opensuite.ui.Ui;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * A vertical chain of stages. Each transform applied to a stage appends its
 * result below as a new stage, so a nested encoding is peeled one layer at a
 * time and every intermediate form stays on screen.
 */
public class DecoderTool implements ToolModule {

    private static final int CONTROL_WIDTH = 172 + Math.round(9.5f * Ui.fontGrowth());   // 191 at 13px, as measured
    private static final int RADIO_ROW = Ui.roomy(21);
    private static final int STAGE_HEIGHT = RADIO_ROW + 5 + 4 * Ui.tall(22) + 15;   // 129, or 140 at 13px

    private final JPanel root = new JPanel(new BorderLayout(0, 6));
    private final JPanel column = new JPanel();
    private final List<Stage> stages = new ArrayList<>();

    /** Suppresses the editor and combo listeners while the chain is rebuilt. */
    private boolean updating;

    public DecoderTool() {
        column.setLayout(new BoxLayout(column, BoxLayout.Y_AXIS));
        column.setOpaque(false);

        JPanel holder = new JPanel(new BorderLayout());
        holder.setOpaque(false);
        holder.add(column, BorderLayout.NORTH);
        JScrollPane scroll = new JScrollPane(holder);
        scroll.setBorder(null);
        scroll.setOpaque(false);
        scroll.getViewport().setBackground(opensuite.ui.Theme.BG);
        scroll.getVerticalScrollBar().setUnitIncrement(25);

        // 20px under the tab strip, 10px at the sides, on the page colour.
        root.setBackground(opensuite.ui.Theme.BG);
        root.setBorder(Ui.pad(20, 10, 10, 10));
        root.add(scroll, BorderLayout.CENTER);

        setData(new byte[0]);
    }

    @Override public String name() { return "decoder"; }

    @Override public Tab[] tabs() { return new Tab[]{ new Tab("decoder", root) }; }

    @Override public java.io.Serializable saveState() {
        if (stages.isEmpty()) return null;
        String input = stages.get(0).area.getText();
        return (input == null || input.isEmpty()) ? null : input;
    }

    @Override public void restoreState(java.io.Serializable state) {
        // Setting the first stage's text fires its listener, which re-runs the
        // whole decode pipeline.
        if (state instanceof String s && !stages.isEmpty()) stages.get(0).area.setText(s);
    }

    @Override public void init(SuiteContext ctx) {
        ctx.registerSink("decoder", (svc, req, resp, caption) -> {
            byte[] data = req != null ? req.raw() : resp != null ? resp.raw() : new byte[0];
            SwingUtilities.invokeLater(() -> setData(data));
        });
    }

    // ---- the chain ----

    /** Replace everything with a single stage holding these bytes. */
    private void setData(byte[] data) {
        stages.clear();
        Stage first = new Stage(true);
        first.data = data == null ? new byte[0] : data;
        chooseView(first);
        stages.add(first);
        render(first);
        relayout();
    }

    /**
     * Recompute every stage from the first editor downwards, adding the stage
     * a transform produces and dropping anything left over below the chain.
     */
    private void rerun() {
        Stage first = stages.get(0);
        first.data = read(first);
        int i = 0;
        while (i < stages.size()) {
            Stage s = stages.get(i);
            s.clearError();
            if (s.opKind == null) break;
            byte[] out;
            try {
                out = Transforms.apply(s.opKind, s.opName, s.data);
            } catch (Exception ex) {
                out = s.data;                    // malformed input leaves the data alone
                s.showError(describe(ex));
            }
            Stage next;
            if (i + 1 < stages.size()) {
                next = stages.get(i + 1);
                next.data = out;
            } else {
                next = new Stage(false);
                next.data = out;
                chooseView(next);
                stages.add(next);
            }
            render(next);
            i++;
        }
        dropBelow(i);
        relayout();
    }

    private void apply(Stage s, String kind, String name) {
        s.opKind = kind;
        s.opName = name;
        dropBelow(stages.indexOf(s));
        rerun();
    }

    private void dropBelow(int index) {
        while (stages.size() > index + 1) stages.remove(stages.size() - 1);
    }

    private void relayout() {
        column.removeAll();
        for (Stage s : stages) column.add(s.panel);
        column.revalidate();
        column.repaint();
    }

    // ---- stage data and display ----

    private void render(Stage s) {
        boolean was = updating;
        updating = true;
        s.area.setText(s.hexView ? Transforms.spacedHex(s.data) : Bytes.str(s.data));
        s.area.setCaretPosition(0);
        updating = was;
    }

    private byte[] read(Stage s) {
        return s.hexView ? Transforms.looseHex(s.area.getText()) : Bytes.bytes(s.area.getText());
    }

    /** Binary data is unreadable as text, so such a stage opens in hex. */
    private void chooseView(Stage s) {
        boolean binary = false;
        for (byte b : s.data) {
            int v = b & 0xff;
            if (v > 127 || v == 127 || (v < 32 && v != 9 && v != 10 && v != 13)) {
                binary = true;
                break;
            }
        }
        s.hexView = binary;
        boolean was = updating;
        updating = true;
        s.hex.setSelected(binary);
        s.text.setSelected(!binary);
        updating = was;
    }

    private void select(JComboBox<String> combo, String item) {
        boolean was = updating;
        updating = true;
        combo.setSelectedItem(item);
        updating = was;
    }

    private void reset(JComboBox<String> combo) {
        select(combo, combo.getItemAt(0));
    }

    private static String describe(Exception ex) {
        String m = ex.getMessage();
        if (m == null || m.isEmpty()) m = ex.getClass().getSimpleName();
        if (Character.isUpperCase(m.charAt(0))) m = Character.toLowerCase(m.charAt(0)) + m.substring(1);
        return m;
    }

    /** The colour each transform is drawn in, in the dropdowns and on the stage it produced. */
    static Color opColour(String name) {
        if (name == null) return null;
        return switch (name.trim().toLowerCase()) {
            case "url" -> new Color(0xE0, 0x3C, 0x3C);
            case "html" -> new Color(0xE8, 0x82, 0x0C);
            case "base64" -> new Color(0xD4, 0xC2, 0x00);
            case "ascii hex" -> new Color(0x2E, 0xA0, 0x4A);
            case "hex" -> opensuite.ui.Theme.CONTROL;
            case "octal" -> new Color(0x1F, 0x9E, 0xA8);
            case "binary" -> new Color(0xC0, 0x3C, 0xC0);
            case "gzip", "deflate", "brotli" -> new Color(0xE0, 0x7A, 0x7A);
            default -> null;
        };
    }

    /** How strongly a transform's colour shows on the pane it was applied to, out of 255. */
    private static final int TINT_STRENGTH = 60;

    /**
     * The background of a pane the named transform was applied to: the transform's colour laid
     * lightly over the pane's own ground, or the ground itself when the transform has no colour.
     * Always one solid colour. A see-through background on a text area is painted over what is
     * already on screen, so the text smeared and the tint grew stronger each time the pane scrolled.
     */
    static Color paneColour(String opName, Color ground) {
        Color c = opColour(opName);
        if (c == null) return ground;
        return new Color(mix(c.getRed(), ground.getRed()), mix(c.getGreen(), ground.getGreen()),
                mix(c.getBlue(), ground.getBlue()));
    }

    private static int mix(int over, int under) {
        return Math.round((over * TINT_STRENGTH + under * (255 - TINT_STRENGTH)) / 255f);
    }

    /** Dropdown rows tinted by their transform's colour. */
    private static final class OpRenderer extends DefaultListCellRenderer {
        @Override public Component getListCellRendererComponent(JList<?> list, Object v, int i, boolean sel, boolean f) {
            super.getListCellRendererComponent(list, v, i, sel, f);
            Color c = opColour(String.valueOf(v));
            if (c != null && !sel && i >= 0) { setOpaque(true); setBackground(c); setForeground(Color.WHITE); }
            return this;
        }
    }

    /** One link in the chain: the data, how it is shown, and what it feeds. */
    private final class Stage {

        final boolean first;
        byte[] data = new byte[0];
        boolean hexView;
        String opKind;
        String opName;

        final JTextArea area = new JTextArea();
        final JRadioButton text = Ui.radio("Text", true);
        final JRadioButton hex = Ui.radio("Hex", false);
        final JComboBox<String> decode = Ui.combo(Transforms.decodeItems());
        final JComboBox<String> encode = Ui.combo(Transforms.encodeItems());
        final JComboBox<String> hash = Ui.combo(Transforms.hashItems());
        final JButton smart = Ui.button("Smart decode", "Detect the encoding and decode one layer");
        final JLabel error = Ui.label(" ");
        final JPanel panel = new JPanel(new BorderLayout(10, 0));

        Stage(boolean first) {
            this.first = first;
            area.setFont(Ui.PLAIN);
            area.setLineWrap(true);
            area.setEditable(first);
            area.setBackground(opensuite.ui.Theme.CONTENT);

            ButtonGroup group = new ButtonGroup();
            group.add(text);
            group.add(hex);

            error.setForeground(Ui.ERROR_RED);

            if (first) {
                area.getDocument().addDocumentListener(new DocumentListener() {
                    @Override public void insertUpdate(DocumentEvent e) { edited(); }
                    @Override public void removeUpdate(DocumentEvent e) { edited(); }
                    @Override public void changedUpdate(DocumentEvent e) { edited(); }
                });
            }

            Runnable view = () -> {
                if (updating) return;
                data = read(this);               // read under the old view before switching
                hexView = hex.isSelected();
                render(this);
                if (first) rerun();
            };
            text.addActionListener(e -> view.run());
            hex.addActionListener(e -> view.run());

            decode.setRenderer(new OpRenderer());
            encode.setRenderer(new OpRenderer());
            decode.addActionListener(e -> chosen(decode, Transforms.DECODE, encode, hash));
            encode.addActionListener(e -> chosen(encode, Transforms.ENCODE, decode, hash));
            hash.addActionListener(e -> chosen(hash, Transforms.HASH, decode, encode));
            smart.addActionListener(e -> smartDecode());

            JScrollPane sp = Ui.scroll(area);
            sp.setPreferredSize(new Dimension(200, STAGE_HEIGHT));
            panel.setOpaque(false);
            panel.setBorder(Ui.pad(0, 0, 10, 0));
            // The pane keeps its own height; the controls beside it may run a line longer.
            JPanel paneTop = new JPanel(new BorderLayout());
            paneTop.setOpaque(false);
            paneTop.add(sp, BorderLayout.NORTH);
            panel.add(paneTop, BorderLayout.CENTER);
            panel.add(controls(), BorderLayout.EAST);
        }

        private JComponent controls() {
            JPanel p = new JPanel();
            p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
            p.setOpaque(false);
            // Text at the left edge, Hex 50px in, the help mark 123px in; a 21px row.
            JPanel radios = new JPanel(null);
            radios.setOpaque(false);
            for (JRadioButton r : new JRadioButton[] { text, hex }) { r.setBorder(null); r.setOpaque(false); radios.add(r); }
            int g = Ui.fontGrowth();
            text.setBounds(0, 0, 50 + 3 * g, RADIO_ROW);
            hex.setBounds(50 + Math.round(3.5f * g), 0, 60 + 3 * g, RADIO_ROW);
            if (first) {
                JButton help = opensuite.ui.Glyphs.button(opensuite.ui.Glyphs.help(21, opensuite.ui.Theme.TEXT), "About Decoder", () ->
                        JOptionPane.showMessageDialog(panel, "Enter or paste data in the top pane, then select a decode, encode or hash operation.\n"
                                + "Each result opens in a new pane below, and the source text is highlighted in the pane above.", "Decoder", JOptionPane.INFORMATION_MESSAGE));
                help.setBorder(null);
                help.setBounds(123 + 8 * g, 0, RADIO_ROW, RADIO_ROW);
                radios.add(help);
            }
            radios.setPreferredSize(new Dimension(CONTROL_WIDTH, RADIO_ROW));
            for (JComponent c : new JComponent[] { decode, encode, hash, smart }) {
                c.setPreferredSize(new Dimension(CONTROL_WIDTH, Ui.tall(22)));
            }
            p.add(fixed(radios));
            p.add(Box.createVerticalStrut(5));
            p.add(fixed(decode));
            p.add(Box.createVerticalStrut(5));
            p.add(fixed(encode));
            p.add(Box.createVerticalStrut(5));
            p.add(fixed(hash));
            p.add(Box.createVerticalStrut(5));
            p.add(fixed(smart));
            p.add(fixed(error));
            return p;
        }

        /** Pin a control to the column width so long names cannot widen it. */
        private JComponent fixed(JComponent c) {
            int h = c.getPreferredSize().height;
            c.setPreferredSize(new Dimension(CONTROL_WIDTH, h));
            c.setMaximumSize(new Dimension(CONTROL_WIDTH, h));
            c.setAlignmentX(Component.LEFT_ALIGNMENT);
            return c;
        }

        private void edited() {
            if (updating) return;
            // the rerun rewrites the documents below, which Swing forbids
            // while a document event is still being delivered
            SwingUtilities.invokeLater(DecoderTool.this::rerun);
        }

        private void chosen(JComboBox<String> combo, String kind, JComboBox<String> a, JComboBox<String> b) {
            if (updating || combo.getSelectedIndex() <= 0) return;
            reset(a);
            reset(b);
            String name = (String) combo.getSelectedItem();
            apply(this, kind, name);
            // The combo returns to its prompt; the colour of the pane below says what was applied.
            reset(combo);
            showOpColour();
        }

        /** Tint the pane for the transform now applied to it, or clear the tint when it has no colour. */
        private void showOpColour() {
            area.setBackground(paneColour(opName, opensuite.ui.Theme.CONTENT));
        }

        private void smartDecode() {
            if (updating) return;
            String guess = Transforms.guess(data);
            if (guess == null) {
                showError("no encoding recognised");
                return;
            }
            select(decode, guess);
            reset(encode);
            reset(hash);
            apply(this, Transforms.DECODE, guess);
            showOpColour();
        }

        void showError(String message) {
            error.setText(message);
            error.setToolTipText(message);
        }

        void clearError() {
            error.setText(" ");
            error.setToolTipText(null);
        }
    }
}