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.

Intruder: the payload processing rules are the reference's fourteen, each with a box of its own

scottsturrock3b343757adee
diff --git a/src/opensuite/intruder/AttackEngine.java b/src/opensuite/intruder/AttackEngine.java
index f0e303d..45fa7f4 100644
--- a/src/opensuite/intruder/AttackEngine.java
+++ b/src/opensuite/intruder/AttackEngine.java
@@ -250,8 +250,9 @@ public class AttackEngine {
switch (type) {
case SNIPER -> {
- List<String> set = payloadsPerSet.get(0);
+ // One list for every position, or a list for each where the payloads depend on the position.
for (int p = 0; p < positions && out.size() < MAX_REQUESTS; p++) {
+ List<String> set = payloadsPerSet.get(payloadsPerSet.size() == positions ? p : 0);
for (String payload : set) {
if (out.size() >= MAX_REQUESTS) break;
String[] values = new String[positions];
diff --git a/src/opensuite/intruder/IntruderTool.java b/src/opensuite/intruder/IntruderTool.java
index e84b9d1..6cf8755 100644
--- a/src/opensuite/intruder/IntruderTool.java
+++ b/src/opensuite/intruder/IntruderTool.java
@@ -533,10 +533,14 @@ public class IntruderTool implements ToolModule {
List<String> base = originalValues(positions.markedText());
for (PayloadSet s : sets) s.setBase(base);
final List<List<String>> materialised = new ArrayList<>();
- for (int k = 0; k < sets.size(); k++) {
- List<String> p = sets.get(k).payloads();
+ PayloadProcessor.collaborator = ctx::collaborator;
+ // A sniper attack puts one set into every position in turn. Where a rule
+ // works from the position's own value, each position gets payloads of its own.
+ boolean perPosition = positions.attackType() == AttackType.SNIPER && sets.size() == 1 && sets.get(0).usesBase();
+ for (int k = 0; k < (perPosition ? positionCount : sets.size()); k++) {
+ List<String> p = sets.get(perPosition ? 0 : k).payloads(k < base.size() ? base.get(k) : "");
materialised.add(p);
- if (p.isEmpty()) errors.add("Payload position " + (k + 1) + ": The payload list is empty.");
+ if (p.isEmpty() && (!perPosition || k == 0)) errors.add("Payload position " + (k + 1) + ": The payload list is empty.");
}
final HttpService svc = target.service();
@@ -549,7 +553,8 @@ public class IntruderTool implements ToolModule {
final ResponseGrep grep = options.responseGrep();
// What each payload was before it was URL-encoded, for "Match against pre-URL-encoded payloads".
java.util.Map<String, String> originals = new java.util.HashMap<>();
- for (PayloadSet s : sets) originals.putAll(s.originals());
+ for (int k = 0; k < (perPosition ? positionCount : sets.size()); k++)
+ originals.putAll(sets.get(perPosition ? 0 : k).originals(k < base.size() ? base.get(k) : ""));
grep.setOriginals(originals);
String text = positions.markedText();
final String marked = target.updateHostHeader() ? withHost(text, svc) : text;
diff --git a/src/opensuite/intruder/PayloadProcessor.java b/src/opensuite/intruder/PayloadProcessor.java
index 894f9d0..f359dca 100644
--- a/src/opensuite/intruder/PayloadProcessor.java
+++ b/src/opensuite/intruder/PayloadProcessor.java
@@ -29,18 +29,22 @@ public class PayloadProcessor extends JPanel {
"do not modify", "to lower case", "to upper case", "to Propername", "to ProperName"
};
- /** What a rule does. The label is what the table shows. */
+ /** What a rule does, in the order the reference lists them. The label is what the list and the table show. */
enum Kind {
- CASE("Change case"),
- MATCH_REPLACE("Match/replace"),
PREFIX("Add prefix"),
SUFFIX("Add suffix"),
+ MATCH_REPLACE("Match/replace"),
+ SUBSTRING("Substring"),
+ REVERSE_SUBSTRING("Reverse substring"),
+ CASE("Modify case"),
+ ENCODE("Encode"),
+ DECODE("Decode"),
+ HASH("Hash"),
+ RAW("Add raw payload"),
SKIP("Skip if matches regex"),
- URL_ENCODE_ALL("URL-encode all characters"),
- URL_DECODE("URL-decode"),
- BASE64_ENCODE("Base64-encode"),
- BASE64_DECODE("Base64-decode"),
- HASH("Hash");
+ EXTENSION("Invoke extension"),
+ BASE("Replace placeholder with base value"),
+ COLLABORATOR("Replace placeholder with collaborator payload");
final String label;
Kind(String label) { this.label = label; }
@@ -51,22 +55,29 @@ public class PayloadProcessor extends JPanel {
static final class Rule {
boolean enabled = true;
Kind kind;
- String a = ""; // regex, prefix, suffix, hash algorithm, or case option
+ String a = ""; // prefix, suffix, regex, placeholder, or the option chosen from the rule's list
String b = ""; // replacement
- boolean all = true; // replace every match rather than the first
+ int from, length; // substring
+ boolean location = true; // a collaborator payload with its server's address, not its identifier alone
Rule(Kind kind) { this.kind = kind; }
/** How the rule reads in the table. */
String describe() {
return switch (kind) {
- case CASE -> "Change case: " + a;
- case MATCH_REPLACE -> "Match/replace: \"" + a + "\" -> \"" + b + "\"";
- case PREFIX -> "Add prefix: " + a;
- case SUFFIX -> "Add suffix: " + a;
- case SKIP -> "Skip if matches: " + a;
- case HASH -> "Hash: " + a;
- default -> kind.label;
+ case PREFIX -> "Add prefix: " + a;
+ case SUFFIX -> "Add suffix: " + a;
+ case MATCH_REPLACE -> "Match [" + a + "] replace with [" + b + "]";
+ case SUBSTRING -> "Substring: from " + from + ", length " + length;
+ case REVERSE_SUBSTRING -> "Reverse substring: from " + from + ", length " + length;
+ case CASE -> "Modify case: " + a;
+ case ENCODE, DECODE -> a;
+ case HASH -> "Hash: " + a;
+ case RAW -> a;
+ case SKIP -> "Skip if matches: " + a;
+ case EXTENSION -> "Invoke extension: " + (a.isEmpty() ? "no processor" : a);
+ case BASE -> "Replace placeholder [" + a + "] with base value";
+ case COLLABORATOR -> "Replace placeholder [" + a + "] with collaborator payload";
};
}
}
@@ -196,52 +207,69 @@ public class PayloadProcessor extends JPanel {
// ---- running the chain --------------------------------------------------
+ /** Where a collaborator payload comes from; the suite sets this, and with none the rule leaves the payload alone. */
+ static java.util.function.Supplier<opensuite.oast.Collaborator> collaborator = () -> null;
+
/** The processed payload, or null when a rule drops it. */
- public String process(String payload) {
- String s = payload;
- for (Rule r : rules) {
- if (!r.enabled) continue;
- s = apply(r, s);
- if (s == null) return null;
- }
- return encodeOn.isSelected() ? urlEncode(s, urlChars.getText()) : s;
+ public String process(String payload) { return process(payload, ""); }
+
+ /** As {@link #process(String)}, for a position whose own value in the request is {@code base}. */
+ public String process(String payload, String base) {
+ String s = processPlain(payload, base);
+ return s == null ? null : encodeOn.isSelected() ? urlEncode(s, urlChars.getText()) : s;
}
/** The payload after its rules but before the URL-encoding of the request; null when a rule drops it. */
- public String processPlain(String payload) {
+ public String processPlain(String payload) { return processPlain(payload, ""); }
+
+ public String processPlain(String payload, String base) {
String s = payload;
for (Rule r : rules) {
if (!r.enabled) continue;
- s = apply(r, s);
+ s = apply(r, s, payload, base);
if (s == null) return null;
}
return s;
}
- private static String apply(Rule r, String s) {
- switch (r.kind) {
- case CASE: {
- int mode = java.util.Arrays.asList(CASE_OPTIONS).indexOf(r.a);
- return applyCase(s, Math.max(0, mode));
- }
- case MATCH_REPLACE: {
- if (r.a.isEmpty()) return s;
- try {
- Matcher m = Pattern.compile(r.a).matcher(s);
- String with = Matcher.quoteReplacement(r.b);
- return r.all ? m.replaceAll(with) : m.replaceFirst(with);
- } catch (Exception e) { return s; }
- }
- case PREFIX: return r.a + s;
- case SUFFIX: return s + r.a;
- case SKIP: return skips(s, r.a) ? null : s;
- case URL_ENCODE_ALL: return urlEncodeAll(s);
- case URL_DECODE: return urlDecode(s);
- case BASE64_ENCODE: return Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.ISO_8859_1));
- case BASE64_DECODE: return b64Decode(s);
- case HASH: return hash(s, r.a);
- default: return s;
- }
+ /** Whether a rule that is switched on works from the position's own value, so each position needs its own payloads. */
+ public boolean usesBase() {
+ for (Rule r : rules) if (r.enabled && r.kind == Kind.BASE) return true;
+ return false;
+ }
+
+ /**
+ * One rule applied to {@code s}. {@code raw} is the payload as it was
+ * before any rule, {@code base} the value the position holds in the request.
+ */
+ static String apply(Rule r, String s, String raw, String base) {
+ return switch (r.kind) {
+ case PREFIX -> r.a + s;
+ case SUFFIX -> s + r.a;
+ case MATCH_REPLACE -> PayloadRules.matchReplace(s, r.a, r.b);
+ case SUBSTRING -> PayloadRules.substring(s, r.from, r.length);
+ case REVERSE_SUBSTRING -> PayloadRules.reverseSubstring(s, r.from, r.length);
+ case CASE -> PayloadRules.modifyCase(s, r.a);
+ case ENCODE -> PayloadRules.encode(s, r.a);
+ case DECODE -> PayloadRules.decode(s, r.a);
+ case HASH -> PayloadRules.hash(s, r.a);
+ case RAW -> r.a.startsWith("Prepend") ? raw + s : s + raw;
+ case SKIP -> skips(s, r.a) ? null : s;
+ case EXTENSION -> s; // no extension defines a processor to hand it to
+ case BASE -> r.a.isEmpty() ? s : s.replace(r.a, base == null ? "" : base);
+ case COLLABORATOR -> PayloadRules.replaceEach(s, r.a, () -> collaboratorPayload(r.location));
+ };
+ }
+
+ /** A fresh collaborator payload, starting the collaborator if it is not yet running; null when there is none to be had. */
+ private static String collaboratorPayload(boolean withLocation) {
+ opensuite.oast.Collaborator c = collaborator.get();
+ if (c == null) return null;
+ String host = c.payloadHost();
+ if (host == null) { c.start(); host = c.payloadHost(); }
+ if (host == null) return null;
+ int dot = host.indexOf('.');
+ return withLocation || dot < 0 ? host : host.substring(0, dot);
}
static String urlEncode(String s, String chars) {
@@ -254,24 +282,6 @@ public class PayloadProcessor extends JPanel {
return out.toString();
}
- private static String urlEncodeAll(String s) {
- StringBuilder out = new StringBuilder(s.length() * 3);
- for (byte b : s.getBytes(StandardCharsets.ISO_8859_1)) out.append('%').append(String.format("%02X", b & 0xFF));
- return out.toString();
- }
-
- private static String hash(String s, String algorithm) {
- try {
- MessageDigest md = MessageDigest.getInstance(algorithm == null || algorithm.isEmpty() ? "MD5" : algorithm);
- byte[] d = md.digest(s.getBytes(StandardCharsets.ISO_8859_1));
- StringBuilder sb = new StringBuilder(d.length * 2);
- for (byte b : d) sb.append(String.format("%02x", b));
- return sb.toString();
- } catch (Exception e) {
- return s;
- }
- }
-
static boolean skips(String payload, String regex) {
if (regex == null || regex.isEmpty()) return false;
try { return Pattern.compile(regex).matcher(payload).find(); }
@@ -320,69 +330,157 @@ public class PayloadProcessor extends JPanel {
return out.toString();
}
- /** Add / edit one rule: pick the kind, then fill in what it needs. */
+ /**
+ * Add or edit one rule, laid out as the reference's window: a line of
+ * guidance beside a help mark, the rule type across the width, and under
+ * it only what that type needs.
+ */
private static final class RuleDialog {
+ private static final String PICK = "Select rule type";
+
static Rule show(Component parent, Rule existing) {
- JComboBox<Kind> kind = Ui.combo(Kind.values());
- JTextField a = Ui.field("");
- JTextField b = Ui.field("");
- JCheckBox all = Ui.check("Replace all", true);
- JComboBox<String> caseCombo = Ui.combo(CASE_OPTIONS);
- JComboBox<String> hashAlgo = Ui.combo(new String[]{"MD5", "SHA-1", "SHA-256", "SHA-384", "SHA-512"});
+ JComboBox<Object> kind = new JComboBox<>();
+ kind.setFont(Ui.PLAIN);
+ if (existing == null) kind.addItem(PICK);
+ for (Kind k : Kind.values()) kind.addItem(k);
+ kind.setMaximumRowCount(Kind.values().length + 1); // the whole list at once, with no scrolling
+
+ // Every type has fields of its own, so nothing typed for one shows up in another.
+ JTextField prefix = Ui.field(""), suffix = Ui.field(""), match = Ui.field(""), replace = Ui.field(""),
+ from = Ui.field("0"), length = Ui.field("0"), rfrom = Ui.field("0"), rlength = Ui.field("0"),
+ skip = Ui.field(""), basePlaceholder = Ui.field("{base}"), collabPlaceholder = Ui.field("\\{domain\\}");
+ JComboBox<String> cases = Ui.combo(PayloadRules.CASES), encodings = Ui.combo(PayloadRules.ENCODINGS),
+ decodings = Ui.combo(PayloadRules.DECODINGS), hashes = Ui.combo(PayloadRules.hashes()),
+ raws = Ui.combo(PayloadRules.RAW), processors = Ui.combo(new String[]{"No processors defined"});
+ encodings.setMaximumRowCount(PayloadRules.ENCODINGS.length);
+ hashes.setMaximumRowCount(15);
+ JCheckBox location = Ui.check("", true);
+
if (existing != null) {
kind.setSelectedItem(existing.kind);
- a.setText(existing.a);
- b.setText(existing.b);
- all.setSelected(existing.all);
- caseCombo.setSelectedItem(existing.a);
- hashAlgo.setSelectedItem(existing.a);
+ switch (existing.kind) {
+ case PREFIX -> prefix.setText(existing.a);
+ case SUFFIX -> suffix.setText(existing.a);
+ case MATCH_REPLACE -> { match.setText(existing.a); replace.setText(existing.b); }
+ case SUBSTRING -> { from.setText("" + existing.from); length.setText("" + existing.length); }
+ case REVERSE_SUBSTRING -> { rfrom.setText("" + existing.from); rlength.setText("" + existing.length); }
+ case CASE -> cases.setSelectedItem(existing.a);
+ case ENCODE -> encodings.setSelectedItem(existing.a);
+ case DECODE -> decodings.setSelectedItem(existing.a);
+ case HASH -> hashes.setSelectedItem(existing.a);
+ case RAW -> raws.setSelectedItem(existing.a);
+ case SKIP -> skip.setText(existing.a);
+ case EXTENSION -> { }
+ case BASE -> basePlaceholder.setText(existing.a);
+ case COLLABORATOR -> { collabPlaceholder.setText(existing.a); location.setSelected(existing.location); }
+ }
}
JPanel fields = new JPanel(new CardLayout());
fields.setOpaque(false);
- fields.add(new JPanel(), "none");
- fields.add(Ui.form("Case:", Ui.natural(caseCombo)), "case");
- fields.add(Ui.form("Match regex:", a, "Replace with:", b, "", all), "replace");
- fields.add(Ui.form("Text:", a), "text");
- fields.add(Ui.form("Regex:", a), "regex");
- fields.add(Ui.form("Algorithm:", Ui.natural(hashAlgo)), "hash");
-
+ fields.add(card(), PICK);
+ fields.add(card(row("Prefix:", prefix)), Kind.PREFIX.name());
+ fields.add(card(row("Suffix:", suffix)), Kind.SUFFIX.name());
+ fields.add(card(Ui.form("Match regex:", match, "Replace with:", replace)), Kind.MATCH_REPLACE.name());
+ fields.add(card(Ui.form("From:", from, "Length:", length)), Kind.SUBSTRING.name());
+ fields.add(card(Ui.form("From:", rfrom, "Length:", rlength)), Kind.REVERSE_SUBSTRING.name());
+ fields.add(card(cases), Kind.CASE.name());
+ fields.add(card(encodings), Kind.ENCODE.name());
+ fields.add(card(decodings), Kind.DECODE.name());
+ fields.add(card(hashes), Kind.HASH.name());
+ fields.add(card(raws), Kind.RAW.name());
+ fields.add(card(row("Match regex:", skip)), Kind.SKIP.name());
+ fields.add(card(row("Select processor:", processors)), Kind.EXTENSION.name());
+ fields.add(card(row("Placeholder to replace:", basePlaceholder)), Kind.BASE.name());
+ fields.add(card(Ui.form("Placeholder regex to replace:", collabPlaceholder,
+ "Include Collaborator server location:", location)), Kind.COLLABORATOR.name());
+
+ JButton ok = Ui.button("OK", null), cancel = Ui.button("Cancel", null);
+ Dimension buttonSize = new Dimension(86, 26 + 2 * Ui.fontGrowth());
+ ok.setPreferredSize(buttonSize); cancel.setPreferredSize(buttonSize);
Runnable showCard = () -> {
- Kind k = (Kind) kind.getSelectedItem();
- String card = switch (k == null ? Kind.CASE : k) {
- case CASE -> "case";
- case MATCH_REPLACE -> "replace";
- case PREFIX, SUFFIX -> "text";
- case SKIP -> "regex";
- case HASH -> "hash";
- default -> "none";
- };
- ((CardLayout) fields.getLayout()).show(fields, card);
+ Object k = kind.getSelectedItem();
+ ((CardLayout) fields.getLayout()).show(fields, k instanceof Kind kk ? kk.name() : PICK);
+ ok.setEnabled(k instanceof Kind);
};
kind.addActionListener(e -> showCard.run());
showCard.run();
- JPanel body = new JPanel(new BorderLayout(0, 8));
- body.setOpaque(false);
- body.add(Ui.form("Rule type:", Ui.natural(kind)), BorderLayout.NORTH);
- body.add(fields, BorderLayout.CENTER);
-
- int ok = JOptionPane.showConfirmDialog(parent, body,
- existing == null ? "Add payload processing rule" : "Edit payload processing rule",
- JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
- if (ok != JOptionPane.OK_OPTION) return null;
-
- Kind k = (Kind) kind.getSelectedItem();
- Rule r = new Rule(k == null ? Kind.CASE : k);
+ JLabel help = new JLabel(opensuite.ui.Glyphs.help());
+ help.setVerticalAlignment(SwingConstants.TOP);
+ help.setBorder(Ui.pad(0, 0, 0, 10));
+ JPanel middle = new JPanel(new BorderLayout(0, 6));
+ middle.setOpaque(false);
+ middle.add(kind, BorderLayout.NORTH);
+ middle.add(fields, BorderLayout.CENTER);
+ JPanel top = new JPanel(new BorderLayout(0, 18));
+ top.setOpaque(false);
+ top.add(Ui.label("Enter the details of the payload processing rule."), BorderLayout.NORTH);
+ top.add(middle, BorderLayout.CENTER);
+ JPanel buttons = new JPanel(new FlowLayout(FlowLayout.RIGHT, 6, 0));
+ buttons.setOpaque(false);
+ buttons.add(ok); buttons.add(cancel);
+ JPanel body = new JPanel(new BorderLayout(0, 10));
+ body.setBackground(opensuite.ui.Theme.BG);
+ body.setBorder(Ui.pad(18, 16, 14, 10));
+ body.add(help, BorderLayout.WEST);
+ body.add(top, BorderLayout.CENTER);
+ body.add(buttons, BorderLayout.SOUTH);
+
+ JDialog d = new JDialog(SwingUtilities.getWindowAncestor(parent),
+ existing == null ? "Add payload processing rule" : "Edit payload processing rule", Dialog.ModalityType.APPLICATION_MODAL);
+ d.setContentPane(body);
+ d.setSize(600 + 40 * Ui.fontGrowth(), 300 + 20 * Ui.fontGrowth());
+ d.setLocationRelativeTo(SwingUtilities.getWindowAncestor(parent));
+ boolean[] accepted = {false};
+ ok.addActionListener(e -> { accepted[0] = true; d.dispose(); });
+ cancel.addActionListener(e -> d.dispose());
+ d.getRootPane().setDefaultButton(ok);
+ d.getRootPane().registerKeyboardAction(e -> d.dispose(), KeyStroke.getKeyStroke("ESCAPE"), JComponent.WHEN_IN_FOCUSED_WINDOW);
+ d.setVisible(true);
+ if (!accepted[0] || !(kind.getSelectedItem() instanceof Kind k)) return null;
+
+ Rule r = new Rule(k);
r.enabled = existing == null || existing.enabled;
- r.b = b.getText();
- r.all = all.isSelected();
- r.a = switch (r.kind) {
- case CASE -> String.valueOf(caseCombo.getSelectedItem());
- case HASH -> String.valueOf(hashAlgo.getSelectedItem());
- default -> a.getText();
- };
+ switch (k) {
+ case PREFIX -> r.a = prefix.getText();
+ case SUFFIX -> r.a = suffix.getText();
+ case MATCH_REPLACE -> { r.a = match.getText(); r.b = replace.getText(); }
+ case SUBSTRING -> { r.from = number(from); r.length = number(length); }
+ case REVERSE_SUBSTRING -> { r.from = number(rfrom); r.length = number(rlength); }
+ case CASE -> r.a = String.valueOf(cases.getSelectedItem());
+ case ENCODE -> r.a = String.valueOf(encodings.getSelectedItem());
+ case DECODE -> r.a = String.valueOf(decodings.getSelectedItem());
+ case HASH -> r.a = String.valueOf(hashes.getSelectedItem());
+ case RAW -> r.a = String.valueOf(raws.getSelectedItem());
+ case SKIP -> r.a = skip.getText();
+ case EXTENSION -> { }
+ case BASE -> r.a = basePlaceholder.getText();
+ case COLLABORATOR -> { r.a = collabPlaceholder.getText(); r.location = location.isSelected(); }
+ }
return r;
}
+
+ private static int number(JTextField f) {
+ try { return Math.max(0, Integer.parseInt(f.getText().strip())); }
+ catch (NumberFormatException e) { return 0; }
+ }
+
+ /** A label and its box on one line, the box taking the rest of the width. */
+ private static JPanel row(String label, JComponent box) {
+ JPanel p = new JPanel(new BorderLayout(5, 0));
+ p.setOpaque(false);
+ p.add(Ui.label(label), BorderLayout.WEST);
+ p.add(box, BorderLayout.CENTER);
+ return p;
+ }
+
+ /** What a rule type shows, kept to the top of the space under the type. */
+ private static JPanel card(JComponent... content) {
+ JPanel p = new JPanel(new BorderLayout());
+ p.setOpaque(false);
+ if (content.length > 0) p.add(content[0], BorderLayout.NORTH);
+ return p;
+ }
}
}
diff --git a/src/opensuite/intruder/PayloadRules.java b/src/opensuite/intruder/PayloadRules.java
new file mode 100644
index 0000000..d6c9b2a
--- /dev/null
+++ b/src/opensuite/intruder/PayloadRules.java
@@ -0,0 +1,173 @@
+package opensuite.intruder;
+
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.List;
+import java.util.TreeSet;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * What the payload processing rules do to a payload, as plain functions of
+ * text. A payload is treated as bytes in ISO-8859-1, one character a byte, as
+ * the rest of Intruder does.
+ */
+final class PayloadRules {
+
+ private PayloadRules() {}
+
+ static final String[] CASES = {"To lower case", "To upper case", "To Propername", "To ProperName"};
+
+ static final String[] ENCODINGS = {
+ "URL-encode key characters", "URL-encode all characters", "URL-encode all characters (Unicode)",
+ "HTML-encode key characters", "HTML-encode all characters", "HTML-encode all characters (numeric entities)",
+ "HTML-encode all characters (hex entities)", "Base64-encode", "Encode as ASCII hex",
+ "Convert to JavaScript constructed string", "Convert to Microsoft SQL Server constructed string",
+ "Convert to Oracle constructed string", "Convert to MySQL constructed string"};
+
+ static final String[] DECODINGS = {"URL-decode", "HTML-decode", "Base64-decode", "Decode as ASCII hex"};
+
+ static final String[] RAW = {"Append preprocessed payload", "Prepend preprocessed payload"};
+
+ /** Every digest this Java offers, in name order. */
+ static String[] hashes() {
+ TreeSet<String> names = new TreeSet<>(java.security.Security.getAlgorithms("MessageDigest"));
+ return names.toArray(new String[0]);
+ }
+
+ /** {@code length} characters starting at {@code from}, counted from the start; what lies outside the payload is left out. */
+ static String substring(String s, int from, int length) {
+ int start = Math.max(0, Math.min(from, s.length()));
+ int end = Math.max(start, Math.min((long) start + Math.max(0, length), s.length()) > Integer.MAX_VALUE ? s.length()
+ : (int) Math.min((long) start + Math.max(0, length), s.length()));
+ return s.substring(start, end);
+ }
+
+ /** As {@link #substring}, but the end is {@code from} characters back from the payload's end, and the length runs back from there. */
+ static String reverseSubstring(String s, int from, int length) {
+ int end = Math.max(0, s.length() - Math.max(0, from));
+ int start = (int) Math.max(0, (long) end - Math.max(0, length));
+ return s.substring(start, end);
+ }
+
+ static String modifyCase(String s, String option) {
+ int i = java.util.Arrays.asList(CASES).indexOf(option);
+ return PayloadProcessor.applyCase(s, i + 1);
+ }
+
+ static String matchReplace(String s, String regex, String with) {
+ if (regex.isEmpty()) return s;
+ try { return Pattern.compile(regex).matcher(s).replaceAll(Matcher.quoteReplacement(with)); }
+ catch (Exception e) { return s; }
+ }
+
+ private static final String URL_KEY = " \"#$%&+,/:;<=>?@[\\]^`{|}";
+
+ static String encode(String s, String how) {
+ StringBuilder out = new StringBuilder(s.length() * 4);
+ switch (how) {
+ case "URL-encode key characters" -> {
+ for (char c : s.toCharArray()) {
+ if (c < 0x20 || c > 0x7E || URL_KEY.indexOf(c) >= 0) out.append(String.format("%%%02x", c & 0xFF));
+ else out.append(c);
+ }
+ }
+ case "URL-encode all characters" -> { for (char c : s.toCharArray()) out.append(String.format("%%%02x", c & 0xFF)); }
+ case "URL-encode all characters (Unicode)" -> { for (char c : s.toCharArray()) out.append(String.format("%%u%04x", (int) c)); }
+ case "HTML-encode key characters" -> { for (char c : s.toCharArray()) { String n = named(c); out.append(n != null ? n : String.valueOf(c)); } }
+ case "HTML-encode all characters" -> { for (char c : s.toCharArray()) { String n = named(c); out.append(n != null ? n : "&#x" + Integer.toHexString(c) + ";"); } }
+ case "HTML-encode all characters (numeric entities)" -> { for (char c : s.toCharArray()) out.append("&#").append((int) c).append(';'); }
+ case "HTML-encode all characters (hex entities)" -> { for (char c : s.toCharArray()) out.append("&#x").append(Integer.toHexString(c)).append(';'); }
+ case "Base64-encode" -> out.append(Base64.getEncoder().encodeToString(s.getBytes(StandardCharsets.ISO_8859_1)));
+ case "Encode as ASCII hex" -> { for (char c : s.toCharArray()) out.append(String.format("%02x", c & 0xFF)); }
+ case "Convert to JavaScript constructed string" -> out.append("String.fromCharCode(").append(codes(s, ",")).append(')');
+ case "Convert to Microsoft SQL Server constructed string" -> out.append(each(s, "CHAR(", ")", "+"));
+ case "Convert to Oracle constructed string" -> out.append(each(s, "CHR(", ")", "||"));
+ case "Convert to MySQL constructed string" -> out.append("CHAR(").append(codes(s, ",")).append(')');
+ default -> out.append(s);
+ }
+ return out.toString();
+ }
+
+ private static String named(char c) {
+ return switch (c) {
+ case '<' -> "&lt;";
+ case '>' -> "&gt;";
+ case '&' -> "&amp;";
+ case '"' -> "&quot;";
+ case '\'' -> "&apos;";
+ default -> null;
+ };
+ }
+
+ private static String codes(String s, String between) {
+ List<String> out = new ArrayList<>();
+ for (char c : s.toCharArray()) out.add(String.valueOf((int) c));
+ return String.join(between, out);
+ }
+
+ private static String each(String s, String before, String after, String between) {
+ List<String> out = new ArrayList<>();
+ for (char c : s.toCharArray()) out.add(before + (int) c + after);
+ return String.join(between, out);
+ }
+
+ /** The decoded payload; one that is not in the encoding named comes back as it was. */
+ static String decode(String s, String how) {
+ try {
+ switch (how) {
+ case "URL-decode": return java.net.URLDecoder.decode(s, StandardCharsets.ISO_8859_1);
+ case "Base64-decode": return new String(Base64.getDecoder().decode(s.strip()), StandardCharsets.ISO_8859_1);
+ case "Decode as ASCII hex": {
+ String h = s.replaceAll("\\s", "");
+ if (h.length() % 2 != 0 || !h.matches("[0-9a-fA-F]*")) return s;
+ StringBuilder out = new StringBuilder(h.length() / 2);
+ for (int i = 0; i < h.length(); i += 2) out.append((char) Integer.parseInt(h.substring(i, i + 2), 16));
+ return out.toString();
+ }
+ case "HTML-decode": {
+ Matcher m = Pattern.compile("&(#[xX][0-9a-fA-F]+|#[0-9]+|lt|gt|amp|quot|apos|nbsp);").matcher(s);
+ StringBuilder out = new StringBuilder();
+ while (m.find()) {
+ String e = m.group(1), to = switch (e) {
+ case "lt" -> "<"; case "gt" -> ">"; case "amp" -> "&"; case "quot" -> "\""; case "apos" -> "'"; case "nbsp" -> " ";
+ default -> String.valueOf((char) (e.charAt(1) == 'x' || e.charAt(1) == 'X'
+ ? Integer.parseInt(e.substring(2), 16) : Integer.parseInt(e.substring(1))));
+ };
+ m.appendReplacement(out, Matcher.quoteReplacement(to));
+ }
+ m.appendTail(out);
+ return out.toString();
+ }
+ default: return s;
+ }
+ } catch (Exception e) { return s; }
+ }
+
+ static String hash(String s, String algorithm) {
+ try {
+ byte[] d = MessageDigest.getInstance(algorithm).digest(s.getBytes(StandardCharsets.ISO_8859_1));
+ StringBuilder sb = new StringBuilder(d.length * 2);
+ for (byte b : d) sb.append(String.format("%02x", b));
+ return sb.toString();
+ } catch (Exception e) { return s; }
+ }
+
+ /** Each match of the placeholder gives way to a value of its own from {@code payloads}; nothing changes when there is none to give. */
+ static String replaceEach(String s, String regex, java.util.function.Supplier<String> payloads) {
+ if (regex.isEmpty()) return s;
+ try {
+ Matcher m = Pattern.compile(regex).matcher(s);
+ StringBuilder out = new StringBuilder();
+ while (m.find()) {
+ String v = payloads.get();
+ if (v == null) return s;
+ m.appendReplacement(out, Matcher.quoteReplacement(v));
+ }
+ m.appendTail(out);
+ return out.toString();
+ } catch (Exception e) { return s; }
+ }
+}
diff --git a/src/opensuite/intruder/PayloadSet.java b/src/opensuite/intruder/PayloadSet.java
index 1e59819..c072a3f 100644
--- a/src/opensuite/intruder/PayloadSet.java
+++ b/src/opensuite/intruder/PayloadSet.java
@@ -100,21 +100,29 @@ public class PayloadSet extends JPanel {
public int count() { return current().count(); }
/** Each payload as it will be sent, against what it was before the request's URL-encoding, where the two differ. */
- public java.util.Map<String, String> originals() {
+ public java.util.Map<String, String> originals() { return originals(""); }
+
+ public java.util.Map<String, String> originals(String base) {
java.util.Map<String, String> out = new java.util.HashMap<>();
for (String p : current().generate()) {
- String sent = processor.process(p), plain = processor.processPlain(p);
+ String sent = processor.process(p, base), plain = processor.processPlain(p, base);
if (sent != null && plain != null && !sent.equals(plain)) out.putIfAbsent(sent, plain);
}
return out;
}
+ /** Whether the payloads depend on the value of the position they go into. */
+ public boolean usesBase() { return processor.usesBase(); }
+
/** The processed payloads this set will contribute to an attack. */
- public List<String> payloads() {
+ public List<String> payloads() { return payloads(""); }
+
+ /** The processed payloads for a position whose own value in the request is {@code base}. */
+ public List<String> payloads(String base) {
List<String> raw = current().generate();
java.util.List<String> out = new java.util.ArrayList<>(raw.size());
for (String p : raw) {
- String processed = processor.process(p);
+ String processed = processor.process(p, base);
if (processed != null) out.add(processed); // null = a skip rule dropped it
}
return out;
diff --git a/tests/opensuite/intruder/PayloadProcessorTest.java b/tests/opensuite/intruder/PayloadProcessorTest.java
index bfebd9e..1e6529a 100644
--- a/tests/opensuite/intruder/PayloadProcessorTest.java
+++ b/tests/opensuite/intruder/PayloadProcessorTest.java
@@ -26,10 +26,78 @@ public final class PayloadProcessorTest {
Expect.equal(PayloadProcessor.b64Decode("not base64!!"), "not base64!!", "bad base64 passes through");
});
+ Harness.test("every rule type does what it is named for", () -> {
+ Expect.equal(rule(PayloadProcessor.Kind.PREFIX, "pre-", "", 0, 0, "abc"), "pre-abc", "add prefix");
+ Expect.equal(rule(PayloadProcessor.Kind.SUFFIX, "-end", "", 0, 0, "abc"), "abc-end", "add suffix");
+ Expect.equal(rule(PayloadProcessor.Kind.MATCH_REPLACE, "[0-9]+", "N", 0, 0, "a1b22"), "aNbN", "match and replace");
+ Expect.equal(rule(PayloadProcessor.Kind.SUBSTRING, "", "", 1, 3, "abcdef"), "bcd", "substring");
+ Expect.equal(rule(PayloadProcessor.Kind.SUBSTRING, "", "", 4, 9, "abcdef"), "ef", "substring past the end");
+ Expect.equal(rule(PayloadProcessor.Kind.SUBSTRING, "", "", 9, 2, "abcdef"), "", "substring from beyond the end");
+ Expect.equal(rule(PayloadProcessor.Kind.REVERSE_SUBSTRING, "", "", 1, 3, "abcdef"), "cde", "reverse substring");
+ Expect.equal(rule(PayloadProcessor.Kind.REVERSE_SUBSTRING, "", "", 0, 99, "abcdef"), "abcdef", "reverse substring longer than the payload");
+ Expect.equal(rule(PayloadProcessor.Kind.CASE, "To upper case", "", 0, 0, "aBc"), "ABC", "modify case");
+ Expect.equal(rule(PayloadProcessor.Kind.CASE, "To Propername", "", 0, 0, "aBc dE"), "Abc de", "Propername");
+ Expect.equal(rule(PayloadProcessor.Kind.HASH, "MD5", "", 0, 0, "abc"), "900150983cd24fb0d6963f7d28e17f72", "hash");
+ Expect.equal(rule(PayloadProcessor.Kind.SKIP, "^a", "", 0, 0, "abc"), null, "skip if matches");
+ Expect.equal(rule(PayloadProcessor.Kind.SKIP, "^z", "", 0, 0, "abc"), "abc", "kept when it does not match");
+ Expect.equal(rule(PayloadProcessor.Kind.EXTENSION, "", "", 0, 0, "abc"), "abc", "no processor leaves it alone");
+ });
+
+ Harness.test("each encoding, and each decoding back", () -> {
+ Expect.equal(PayloadRules.encode("a b&<", "URL-encode key characters"), "a%20b%26%3c", "URL key");
+ Expect.equal(PayloadRules.encode("ab", "URL-encode all characters"), "%61%62", "URL all");
+ Expect.equal(PayloadRules.encode("ab", "URL-encode all characters (Unicode)"), "%u0061%u0062", "URL Unicode");
+ Expect.equal(PayloadRules.encode("a<b>&\"", "HTML-encode key characters"), "a&lt;b&gt;&amp;&quot;", "HTML key");
+ Expect.equal(PayloadRules.encode("a<", "HTML-encode all characters"), "&#x61;&lt;", "HTML all");
+ Expect.equal(PayloadRules.encode("a<", "HTML-encode all characters (numeric entities)"), "&#97;&#60;", "HTML numeric");
+ Expect.equal(PayloadRules.encode("a<", "HTML-encode all characters (hex entities)"), "&#x61;&#x3c;", "HTML hex");
+ Expect.equal(PayloadRules.encode("hello", "Base64-encode"), "aGVsbG8=", "Base64");
+ Expect.equal(PayloadRules.encode("ab", "Encode as ASCII hex"), "6162", "ASCII hex");
+ Expect.equal(PayloadRules.encode("ab", "Convert to JavaScript constructed string"), "String.fromCharCode(97,98)", "JavaScript");
+ Expect.equal(PayloadRules.encode("ab", "Convert to Microsoft SQL Server constructed string"), "CHAR(97)+CHAR(98)", "SQL Server");
+ Expect.equal(PayloadRules.encode("ab", "Convert to Oracle constructed string"), "CHR(97)||CHR(98)", "Oracle");
+ Expect.equal(PayloadRules.encode("ab", "Convert to MySQL constructed string"), "CHAR(97,98)", "MySQL");
+ Expect.equal(PayloadRules.decode("a%20b%26", "URL-decode"), "a b&", "URL-decode");
+ Expect.equal(PayloadRules.decode("&lt;a&#x62;&#99;&amp;", "HTML-decode"), "<abc&", "HTML-decode");
+ Expect.equal(PayloadRules.decode("aGVsbG8=", "Base64-decode"), "hello", "Base64-decode");
+ Expect.equal(PayloadRules.decode("6162", "Decode as ASCII hex"), "ab", "hex decode");
+ Expect.equal(PayloadRules.decode("xyz", "Decode as ASCII hex"), "xyz", "not hex is left alone");
+ for (String e : PayloadRules.ENCODINGS) Expect.isTrue(!PayloadRules.encode("a<", e).equals("a<") || e.contains("key characters") && !e.startsWith("HTML"), e + " changes the payload");
+ });
+
+ Harness.test("the raw payload, the base value and a collaborator payload are put in", () -> {
+ PayloadProcessor.Rule up = new PayloadProcessor.Rule(PayloadProcessor.Kind.CASE); up.a = "To upper case";
+ PayloadProcessor.Rule append = new PayloadProcessor.Rule(PayloadProcessor.Kind.RAW); append.a = "Append preprocessed payload";
+ PayloadProcessor.Rule prepend = new PayloadProcessor.Rule(PayloadProcessor.Kind.RAW); prepend.a = "Prepend preprocessed payload";
+ String s = PayloadProcessor.apply(up, "abc", "abc", "");
+ Expect.equal(PayloadProcessor.apply(append, s, "abc", ""), "ABCabc", "appended as it was before the rules");
+ Expect.equal(PayloadProcessor.apply(prepend, s, "abc", ""), "abcABC", "prepended");
+
+ PayloadProcessor.Rule base = new PayloadProcessor.Rule(PayloadProcessor.Kind.BASE); base.a = "{base}";
+ Expect.equal(PayloadProcessor.apply(base, "x{base}y{base}", "", "42"), "x42y42", "base value");
+
+ String[] next = {"one.oast.test", "two.oast.test"};
+ int[] i = {0};
+ Expect.equal(PayloadRules.replaceEach("a.{domain} b.{domain}", "\\{domain\\}", () -> next[i[0]++]), "a.one.oast.test b.two.oast.test", "a payload of its own for each placeholder");
+ Expect.equal(PayloadRules.replaceEach("a.{domain}", "\\{domain\\}", () -> null), "a.{domain}", "left alone with no collaborator");
+ });
+
+ Harness.test("the rule window has a box for every type that needs one", () -> {
+ for (PayloadProcessor.Kind k : PayloadProcessor.Kind.values()) Expect.isTrue(!k.label.isBlank(), "labelled");
+ Expect.equal(PayloadProcessor.Kind.values().length, 14, "the reference's fourteen types");
+ Expect.equal(PayloadProcessor.Kind.values()[0].label, "Add prefix", "first in its order");
+ });
+
Harness.test("case transforms behave as named", () -> {
Expect.equal(PayloadProcessor.applyCase("AbC", 1), "abc", "to lower");
Expect.equal(PayloadProcessor.applyCase("AbC", 2), "ABC", "to upper");
Expect.equal(PayloadProcessor.applyCase("hello world", 4), "Hello World", "proper case");
});
}
+
+ private static String rule(PayloadProcessor.Kind kind, String a, String b, int from, int length, String payload) {
+ PayloadProcessor.Rule r = new PayloadProcessor.Rule(kind);
+ r.a = a; r.b = b; r.from = from; r.length = length;
+ return PayloadProcessor.apply(r, payload, payload, "");
+ }
}