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.8 KB
Raw Blame
package opensuite.proxy;

import opensuite.core.Bytes;
import opensuite.core.HttpMessage;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/** A match-and-replace rule over a request or response, header or body. */
public class MatchReplaceRule implements Serializable {
    private static final long serialVersionUID = 1L;

    public static final String REQ_HEADER  = "request header";
    public static final String REQ_BODY    = "request body";
    public static final String RESP_HEADER = "response header";
    public static final String RESP_BODY   = "response body";
    public static final String REQ_FIRST_LINE = "request first line";
    public static final String[] TYPES = { REQ_HEADER, REQ_BODY, RESP_HEADER, RESP_BODY, REQ_FIRST_LINE };

    // WebSocket rules: which direction a text message must be travelling in.
    public static final String WS_TO_SERVER = "to server";
    public static final String WS_TO_CLIENT = "to client";
    public static final String WS_BOTH      = "both directions";
    public static final String[] WS_TYPES = { WS_BOTH, WS_TO_SERVER, WS_TO_CLIENT };

    public boolean enabled = true;
    public String type = REQ_HEADER;
    public String match = "";
    public String replace = "";
    public String comment = "";
    /** An optional name for the rule, shown in its own column. */
    public String name = "";
    /** Match the text as written rather than as a regular expression. */
    public boolean literal;

    public MatchReplaceRule() {}

    /** The pattern this rule searches with: its text as a regex, or quoted when literal. */
    private String pattern() { return literal ? Pattern.quote(match) : match; }

    /** How the type reads in a table: a leading capital, as "Request header". */
    public static String shown(String type) {
        if (type == null || type.isEmpty()) return "";
        if (WS_TO_SERVER.equals(type)) return "Client to server";
        if (WS_TO_CLIENT.equals(type)) return "Server to client";
        return Character.toUpperCase(type.charAt(0)) + type.substring(1);
    }

    public MatchReplaceRule(boolean enabled, String type, String match, String replace, String comment) {
        this.enabled = enabled; this.type = type; this.match = match;
        this.replace = replace; this.comment = comment;
    }

    public boolean appliesToRequest()  { return type.startsWith("request"); }
    public boolean isHeaderRule()      { return type.endsWith("header"); }

    public static void applyAll(List<MatchReplaceRule> rules, HttpMessage m, boolean isRequest) {
        if (m == null) return;
        for (MatchReplaceRule r : rules) {
            if (!r.enabled) continue;
            if (r.appliesToRequest() != isRequest) continue;
            r.apply(m);
        }
    }

    /** Run the WebSocket rules over one text message; returns the (possibly unchanged) text. */
    public static String applyAllWs(List<MatchReplaceRule> rules, String text, boolean toServer) {
        String out = text;
        for (MatchReplaceRule r : rules) {
            if (!r.enabled || r.match == null || r.match.isBlank()) continue;
            if (WS_TO_SERVER.equals(r.type) && !toServer) continue;
            if (WS_TO_CLIENT.equals(r.type) && toServer) continue;
            out = regexReplace(out, r.pattern(), r.replace == null ? "" : r.replace, false);
        }
        return out;
    }

    private void apply(HttpMessage m) {
        if (REQ_FIRST_LINE.equals(type)) {
            if (match == null || match.isBlank()) return;
            byte[] raw = m.raw();
            int nl = Bytes.indexOf(raw, Bytes.bytes("\r\n"));
            if (nl < 0) return;
            String first = new String(raw, 0, nl, java.nio.charset.StandardCharsets.ISO_8859_1);
            String out = regexReplace(first, pattern(), replace == null ? "" : replace, false);
            if (!out.equals(first)) m.setRaw(Bytes.concat(Bytes.bytes(out), Bytes.slice(raw, nl, raw.length)));
            return;
        }
        if (isHeaderRule()) {
            String headers = m.headerText();
            // Preserve the trailing blank line that terminates the header block.
            String trimmed = headers.endsWith("\r\n\r\n")
                    ? headers.substring(0, headers.length() - 2) : headers;

            if (match == null || match.isBlank()) {
                if (replace != null && !replace.isBlank()) {
                    String ins = replace.endsWith("\r\n") ? replace : replace + "\r\n";
                    trimmed = trimmed.substring(0, trimmed.length() - 2) + ins + "\r\n";
                    m.setRaw(Bytes.concat(Bytes.bytes(trimmed), m.body()));
                }
                return;
            }
            String out = regexReplace(trimmed, pattern(), replace == null ? "" : replace, true);
            // A rule with an empty replacement removes the matched header line.
            out = out.replaceAll("(\r\n)\r\n(?=.)", "$1");
            if (!out.endsWith("\r\n\r\n")) {
                if (out.endsWith("\r\n")) out = out + "\r\n";
                else out = out + "\r\n\r\n";
            }
            m.setRaw(Bytes.concat(Bytes.bytes(out), m.body()));
        } else {
            if (match == null || match.isBlank()) return;
            String body = m.bodyText();
            String out = regexReplace(body, pattern(), replace == null ? "" : replace, false);
            m.setBody(Bytes.bytes(out));
        }
    }

    private static String regexReplace(String input, String pattern, String replacement, boolean multiline) {
        try {
            int flags = Pattern.CASE_INSENSITIVE | (multiline ? Pattern.MULTILINE : 0);
            return Pattern.compile(pattern, flags).matcher(input)
                    .replaceAll(Matcher.quoteReplacement(replacement));
        } catch (Exception e) {
            return input.replace(pattern, replacement);
        }
    }

    /** The stock rules, all switched off until wanted. */
    public static java.util.List<MatchReplaceRule> defaults() {
        java.util.List<MatchReplaceRule> l = new java.util.ArrayList<>();
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^User-Agent.*$",
                "User-Agent: Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)", "Emulate IE"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^User-Agent.*$",
                "User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 14_0_1 like Mac OS X) AppleWebKit/605.1.15 "
                + "(KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1", "Emulate iOS"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^User-Agent.*$",
                "User-Agent: Mozilla/5.0 (Linux; Android 10; Mobile) AppleWebKit/537.36 (KHTML, like Gecko) "
                + "Chrome/120.0 Mobile Safari/537.36", "Emulate Android"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^If-Modified-Since.*$", "", "Require non-cached response"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^If-None-Match.*$", "", "Require non-cached response"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^Referer.*$", "", "Hide Referer header"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^Accept-Encoding.*$", "", "Require non-compressed responses"));
        l.add(new MatchReplaceRule(false, RESP_HEADER, "^Set-Cookie.*$", "", "Ignore cookies"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "^Host: foo.example.org$", "Host: bar.example.org", "Rewrite Host header"));
        l.add(new MatchReplaceRule(false, REQ_HEADER, "", "Origin: foo.example.org", "Add spoofed CORS origin"));
        l.add(new MatchReplaceRule(false, RESP_HEADER, "^Strict\\-Transport\\-Security.*$", "", "Remove HSTS headers"));
        l.add(new MatchReplaceRule(false, RESP_HEADER, "", "X-XSS-Protection: 0", "Disable browser XSS protection"));
        return l;
    }
}