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.

3.1 KB
Raw Blame
package opensuite.script.api;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.function.Consumer;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import opensuite.core.Bytes;

/**
 * The helpers a script is given: output, the clipboard, encodings and hashes.
 * Hashes take and return bytes, so a signature covers exactly the bytes sent.
 */
public final class Utilities {

    /** The most a run keeps of its output. */
    public static final int MAX_LINES = 1000, MAX_CHARS = 64 * 1024;

    private final Consumer<String> clipboard;
    private final List<String> log = new ArrayList<>();
    private int chars;
    private boolean cut;

    /** @param clipboard where {@link #clipboard(String)} puts its text */
    public Utilities(Consumer<String> clipboard) { this.clipboard = clipboard; }

    /** Add a line to the action's output; a line past the limit is kept up to it. */
    public synchronized void log(Object value) {
        if (cut || log.size() >= MAX_LINES) { cut = true; return; }
        String s = String.valueOf(value);
        if (chars + s.length() > MAX_CHARS) { s = s.substring(0, MAX_CHARS - chars); cut = true; }
        log.add(s);
        chars += s.length();
    }

    /** A copy of the output so far. */
    public synchronized List<String> logLines() { return List.copyOf(log); }

    /** True when output past the limit was dropped. */
    public synchronized boolean logCut() { return cut; }

    public void clipboard(String text) { clipboard.accept(text == null ? "" : text); }

    /** UTF-8 bytes of {@code s}. */
    public byte[] bytes(String s) { return (s == null ? "" : s).getBytes(StandardCharsets.UTF_8); }

    public byte[] concat(byte[]... parts) { return Bytes.concat(parts); }

    /** Lower-case hexadecimal. */
    public String hex(byte[] b) { return Bytes.toHex(b); }

    public String base64Encode(byte[] b) { return Base64.getEncoder().encodeToString(b); }

    public byte[] base64Decode(String s) { return Base64.getDecoder().decode(s.strip()); }

    public String urlEncode(String s) { return URLEncoder.encode(s, StandardCharsets.UTF_8); }

    public String urlDecode(String s) { return URLDecoder.decode(s, StandardCharsets.UTF_8); }

    public byte[] sha256(byte[] data) { return digest("SHA-256", data); }

    public byte[] md5(byte[] data) { return digest("MD5", data); }

    public byte[] hmacSha256(byte[] key, byte[] data) {
        try {
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(new SecretKeySpec(key, "HmacSHA256"));
            return mac.doFinal(data);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }
    }

    private static byte[] digest(String algorithm, byte[] data) {
        try {
            return MessageDigest.getInstance(algorithm).digest(data);
        } catch (GeneralSecurityException e) {
            throw new IllegalStateException(e);
        }
    }
}