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.0 KB
Raw Blame
package opensuite.intruder;

import opensuite.core.HttpMessage;
import opensuite.core.HttpService;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * A saved Intruder attack: its title, its extra (grep) columns and every
 * result with the request and response that produced it, so an attack can be
 * reopened and studied later without sending anything again.
 *
 * <p>The format is plain length-prefixed binary, not Java serialisation: a file
 * from an untrusted source can only ever produce strings, numbers and bytes.
 */
public final class AttackFile {
    private AttackFile() {}

    public static final String EXTENSION = "otattack";
    private static final String MAGIC = "OTATTACK1";

    /** Refuse absurd counts and sizes, so a damaged or hostile file cannot exhaust memory. */
    private static final int MAX_COLUMNS = 512;
    private static final int MAX_ROWS = 5_000_000;
    private static final int MAX_BLOB = 256 * 1024 * 1024;
    private static final int MAX_TEXT = 16 * 1024 * 1024;

    /** A saved attack as read back. */
    public record Saved(String title, List<String> extraColumns, Class<?>[] extraClasses,
                        List<ResultsWindow.Result> results) {}

    public static void write(OutputStream stream, String title, List<String> extraColumns,
                             Class<?>[] extraClasses, List<ResultsWindow.Result> results) throws IOException {
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(stream));
        text(out, MAGIC);
        text(out, title);
        out.writeInt(extraColumns.size());
        for (int i = 0; i < extraColumns.size(); i++) {
            text(out, extraColumns.get(i));
            out.writeByte(tag(i < extraClasses.length ? extraClasses[i] : String.class));
        }
        out.writeInt(results.size());
        for (ResultsWindow.Result r : results) {
            out.writeInt(r.number());
            text(out, r.payload());
            HttpService s = r.svc();
            out.writeBoolean(s != null);
            if (s != null) { text(out, s.host()); out.writeInt(s.port()); out.writeBoolean(s.secure()); }
            blob(out, r.request() == null ? null : r.request().raw());
            blob(out, r.response() == null ? null : r.response().raw());
            out.writeInt(r.status());
            out.writeInt(r.length());
            out.writeLong(r.millis());
            text(out, r.error());
            Object[] extra = r.extra() == null ? new Object[0] : r.extra();
            out.writeInt(extra.length);
            for (Object v : extra) value(out, v);
        }
        out.flush();
    }

    public static Saved read(InputStream stream) throws IOException {
        DataInputStream in = new DataInputStream(new BufferedInputStream(stream));
        if (!MAGIC.equals(text(in))) throw new IOException("not a saved attack file");
        String title = text(in);
        int columns = count(in, MAX_COLUMNS, "columns");
        List<String> names = new ArrayList<>();
        Class<?>[] classes = new Class<?>[columns];
        for (int i = 0; i < columns; i++) {
            names.add(text(in));
            classes[i] = type(in.readByte());
        }
        int rows = count(in, MAX_ROWS, "results");
        List<ResultsWindow.Result> results = new ArrayList<>(Math.min(rows, 10_000));
        for (int i = 0; i < rows; i++) {
            int number = in.readInt();
            String payload = text(in);
            HttpService svc = null;
            if (in.readBoolean()) {
                String host = text(in);
                int port = in.readInt();
                boolean secure = in.readBoolean();
                svc = new HttpService(host, port, secure);
            }
            byte[] req = blob(in);
            byte[] resp = blob(in);
            int status = in.readInt();
            int length = in.readInt();
            long millis = in.readLong();
            String error = text(in);
            Object[] extra = new Object[count(in, MAX_COLUMNS, "values")];
            for (int e = 0; e < extra.length; e++) extra[e] = value(in);
            results.add(new ResultsWindow.Result(number, payload, svc,
                    req == null ? null : new HttpMessage(req), resp == null ? null : new HttpMessage(resp),
                    status, length, millis, error, extra));
        }
        return new Saved(title, names, classes, results);
    }

    private static int count(DataInputStream in, int max, String what) throws IOException {
        int n = in.readInt();
        if (n < 0 || n > max) throw new IOException("corrupt attack file: invalid number of " + what);
        return n;
    }

    private static int tag(Class<?> c) {
        if (c == Integer.class) return 'I';
        if (c == Long.class) return 'L';
        if (c == Boolean.class) return 'B';
        return 'S';
    }

    private static Class<?> type(int tag) {
        return switch (tag) {
            case 'I' -> Integer.class;
            case 'L' -> Long.class;
            case 'B' -> Boolean.class;
            default -> String.class;
        };
    }

    private static void value(DataOutputStream out, Object v) throws IOException {
        if (v == null) out.writeByte('N');
        else if (v instanceof Integer i) { out.writeByte('I'); out.writeInt(i); }
        else if (v instanceof Long l) { out.writeByte('L'); out.writeLong(l); }
        else if (v instanceof Boolean b) { out.writeByte('B'); out.writeBoolean(b); }
        else { out.writeByte('S'); text(out, v.toString()); }
    }

    private static Object value(DataInputStream in) throws IOException {
        int tag = in.readByte();
        return switch (tag) {
            case 'N' -> null;
            case 'I' -> in.readInt();
            case 'L' -> in.readLong();
            case 'B' -> in.readBoolean();
            case 'S' -> text(in);
            default -> throw new IOException("corrupt attack file: unknown value type");
        };
    }

    private static void text(DataOutputStream out, String s) throws IOException {
        blob(out, s == null ? null : s.getBytes(StandardCharsets.UTF_8));
    }

    private static String text(DataInputStream in) throws IOException {
        byte[] b = blob(in, MAX_TEXT);
        return b == null ? null : new String(b, StandardCharsets.UTF_8);
    }

    private static void blob(DataOutputStream out, byte[] b) throws IOException {
        if (b == null) { out.writeInt(-1); return; }
        out.writeInt(b.length);
        out.write(b);
    }

    private static byte[] blob(DataInputStream in) throws IOException { return blob(in, MAX_BLOB); }

    private static byte[] blob(DataInputStream in, int max) throws IOException {
        int n = in.readInt();
        if (n == -1) return null;
        if (n < 0 || n > max) throw new IOException("corrupt attack file: invalid length");
        byte[] b = new byte[n];
        in.readFully(b);
        return b;
    }
}