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.

4.4 KB
Raw Blame
package opensuite.intruder;

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

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Flips one bit of a value at a time. Aimed at testing how an application
 * handles tampered encrypted tokens: with some cipher modes a flipped
 * ciphertext bit changes the decrypted value in a predictable way, so this
 * reveals whether integrity is actually checked.
 */
class BitFlipperGenerator extends AbstractGenerator {

    private final JRadioButton fromPosition = Ui.radio("base value of payload position", true);
    private final JRadioButton fromString = Ui.radio("specific string:", false);
    private final JTextField literal = Ui.field("", 24);
    private final JRadioButton asLiteral = Ui.radio("literal value", true);
    private final JRadioButton asHex = Ui.radio("ASCII hex", false);
    private final JCheckBox[] bits = new JCheckBox[8];
    private final JPanel panel;

    BitFlipperGenerator() {
        ButtonGroup g = new ButtonGroup();
        g.add(fromPosition); g.add(fromString);
        ButtonGroup f = new ButtonGroup();
        f.add(asLiteral); f.add(asHex);
        JPanel bitRow = new JPanel(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 4, 0));
        bitRow.setOpaque(false);
        for (int i = 0; i < 8; i++) {
            bits[i] = Ui.check(String.valueOf(i + 1), true);
            bits[i].addActionListener(e -> fire());
            bitRow.add(bits[i]);
        }
        for (AbstractButton b : new AbstractButton[]{fromPosition, fromString, asLiteral, asHex}) {
            b.addActionListener(e -> fire());
        }
        literal.getDocument().addDocumentListener(new javax.swing.event.DocumentListener() {
            public void insertUpdate(javax.swing.event.DocumentEvent e)  { fire(); }
            public void removeUpdate(javax.swing.event.DocumentEvent e)  { fire(); }
            public void changedUpdate(javax.swing.event.DocumentEvent e) { fire(); }
        });
        panel = Ui.vbox(
                Ui.label("operate on:"), fromPosition, Ui.row(fromString, literal),
                Ui.label("format of original data:"), Ui.row(asLiteral, asHex),
                Ui.label("select bits to flip (1 is the least significant):"), bitRow);
    }

    private List<String> sources() {
        return fromString.isSelected() ? List.of(literal.getText()) : base;
    }

    private int mask() {
        int m = 0;
        for (int i = 0; i < 8; i++) if (bits[i].isSelected()) m |= 1 << i;
        return m;
    }

    @Override public String label() { return " bit flipper"; }
    @Override public JComponent config() { return panel; }

    @Override public int count() {
        long n = 0;
        int perByte = Integer.bitCount(mask());
        for (String s : sources()) n += (long) (asHex.isSelected() ? s.length() / 2 : s.length()) * perByte;
        return (int) Math.min(n, CAP);
    }

    @Override public List<String> generate() {
        List<String> out = new ArrayList<>();
        for (String s : sources()) {
            for (String v : flip(s, asHex.isSelected(), mask())) {
                if (out.size() >= CAP) return out;
                out.add(v);
            }
        }
        return out;
    }

    /**
     * One variant per selected bit of every byte, lowest bit first. With
     * {@code hex} the value is read and written as ASCII hex; a value that is
     * not valid hex gives nothing.
     */
    static List<String> flip(String value, boolean hex, int mask) {
        List<String> out = new ArrayList<>();
        byte[] data;
        if (hex) {
            if (value.length() % 2 != 0 || !value.matches("[0-9a-fA-F]*")) return out;
            data = new byte[value.length() / 2];
            for (int i = 0; i < data.length; i++) data[i] = (byte) Integer.parseInt(value.substring(2 * i, 2 * i + 2), 16);
        } else {
            data = Bytes.bytes(value);
        }
        for (int i = 0; i < data.length; i++) {
            for (int bit = 0; bit < 8; bit++) {
                if ((mask & (1 << bit)) == 0) continue;
                byte[] copy = data.clone();
                copy[i] ^= (byte) (1 << bit);
                if (hex) {
                    StringBuilder sb = new StringBuilder();
                    for (byte b : copy) sb.append(String.format("%02x", b & 0xff));
                    out.add(sb.toString());
                } else {
                    out.add(Bytes.str(copy));
                }
            }
        }
        return out;
    }
}