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.4 KB
Raw Blame
package opensuite.ui;

import opensuite.core.SuiteContext;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

/** What the Help menu's entries do: diagnostics, the browser health check and removing the suite's files. */
public final class HelpActions {

    private HelpActions() {}

    /** The facts someone needs when reporting a problem, as plain text. Nothing about the project or its targets. */
    public static String diagnostics() {
        Runtime rt = Runtime.getRuntime();
        long mb = 1024L * 1024L;
        StringBuilder sb = new StringBuilder();
        sb.append("Open Suite ").append(Main.VERSION).append('\n');
        sb.append("Java: ").append(System.getProperty("java.version")).append(" (").append(System.getProperty("java.vendor")).append(")\n");
        sb.append("Runtime: ").append(System.getProperty("java.vm.name")).append(' ').append(System.getProperty("java.vm.version")).append('\n');
        sb.append("Operating system: ").append(System.getProperty("os.name")).append(' ').append(System.getProperty("os.version"))
          .append(" (").append(System.getProperty("os.arch")).append(")\n");
        sb.append("Processors: ").append(rt.availableProcessors()).append('\n');
        sb.append("Memory: ").append((rt.totalMemory() - rt.freeMemory()) / mb).append(" MB used, ").append(rt.totalMemory() / mb)
          .append(" MB allocated, ").append(rt.maxMemory() / mb).append(" MB limit\n");
        sb.append("Headless: ").append(GraphicsEnvironment.isHeadless()).append('\n');
        if (!GraphicsEnvironment.isHeadless()) {
            try {
                Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
                sb.append("Screen: ").append(screen.width).append(" x ").append(screen.height).append('\n');
            } catch (RuntimeException ignored) {}
        }
        sb.append("Display scale: ").append(System.getProperty("sun.java2d.uiScale", "1")).append('\n');
        sb.append("Temporary files: ").append(System.getProperty("java.io.tmpdir")).append('\n');
        sb.append("Browser: ").append(opensuite.proxy.EmbeddedBrowser.available() ? "found" : "not found").append('\n');
        return sb.toString();
    }

    public static void showDiagnostics(Component parent) {
        JTextArea text = new JTextArea(diagnostics());
        text.setFont(Ui.MONO);
        text.setEditable(false);
        JScrollPane sp = Ui.scroll(text);
        sp.setPreferredSize(new Dimension(560, 260));
        Object[] options = { "Copy to clipboard", "Close" };
        int r = JOptionPane.showOptionDialog(parent, sp, "Diagnostics", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[1]);
        if (r == 0) MessageActions.clip(text.getText());
    }

    /** One line of the browser health check. */
    public record Check(String name, boolean ok, String detail) {}

    /** The checks behind "Health check for Open Suite's browser": is there a browser, a driver, a listener and a CA to serve. */
    public static List<Check> browserHealth(String binary, String driver, boolean listenerRunning, boolean caReady) {
        List<Check> out = new ArrayList<>();
        out.add(new Check("Browser found", binary != null, binary != null ? binary : "Browser not installed. Open browser downloads the Open Suite browser on first use."));
        boolean runnable = binary != null && Files.isExecutable(Path.of(binary));
        out.add(new Check("Browser can be started", runnable, runnable ? "The file is executable." : "The browser file is missing or not executable."));
        out.add(new Check("Browser driver found", driver != null, driver != null ? driver : "Driver not found. The browser still opens, without automation."));
        out.add(new Check("Proxy listener running", listenerRunning, listenerRunning ? "A proxy listener is available to receive browser traffic."
                : "No listener is running. Start one in Settings > Tools > Proxy."));
        out.add(new Check("CA certificate ready", caReady, caReady ? "HTTPS traffic can be intercepted." : "The CA certificate could not be read."));
        return out;
    }

    public static void showBrowserHealth(Component parent, boolean listenerRunning, boolean caReady) {
        List<Check> checks = browserHealth(opensuite.proxy.EmbeddedBrowser.findBinary(), opensuite.proxy.EmbeddedBrowser.findDriver(), listenerRunning, caReady);
        StringBuilder sb = new StringBuilder("<html><table cellpadding='3'>");
        for (Check c : checks) {
            sb.append("<tr><td><b>").append(c.ok() ? "Pass" : "Fail").append("</b></td><td>").append(c.name()).append("</td><td>")
              .append(c.detail().replace("&", "&amp;").replace("<", "&lt;")).append("</td></tr>");
        }
        sb.append("</table></html>");
        JLabel l = new JLabel(sb.toString());
        l.setFont(Ui.PLAIN);
        JOptionPane.showMessageDialog(parent, l, "Browser health check", JOptionPane.PLAIN_MESSAGE);
    }

    /** Delete a folder and everything in it. Returns how many files and folders went. */
    public static int deleteTree(Path root) throws IOException {
        if (root == null || !Files.exists(root)) return 0;
        int[] n = { 0 };
        Files.walkFileTree(root, new SimpleFileVisitor<>() {
            @Override public FileVisitResult visitFile(Path f, BasicFileAttributes a) throws IOException { Files.delete(f); n[0]++; return FileVisitResult.CONTINUE; }
            @Override public FileVisitResult postVisitDirectory(Path d, IOException e) throws IOException { Files.delete(d); n[0]++; return FileVisitResult.CONTINUE; }
        });
        return n[0];
    }

    /**
     * "Clean Open Suite from computer": after the owner confirms by typing the
     * word, the settings, the CA certificate and the library are removed and
     * the suite closes. Project files are the owner's and are not touched.
     */
    public static void cleanFromComputer(Component parent, SuiteContext ctx) {
        Path home = Path.of(System.getProperty("user.home", "."), ".opensuite");
        String typed = JOptionPane.showInputDialog(parent,
                "This removes Open Suite's data from this computer and closes the application:\n\n"
              + "  - all user settings\n"
              + "  - the CA certificate and its key (browsers that trust it will require the new certificate)\n"
              + "  - the configuration library and saved items in " + home + "\n\n"
              + "Project files saved elsewhere are not affected. This cannot be undone.\n\n"
              + "Type CLEAN to confirm.", "Clean Open Suite from computer", JOptionPane.WARNING_MESSAGE);
        if (typed == null || !typed.trim().equals("CLEAN")) return;
        try {
            deleteTree(home);
            for (String k : ctx.settings().snapshot().keySet()) ctx.settings().remove(k);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(parent, "Could not remove all data: " + e.getMessage(), "Clean Open Suite from computer", JOptionPane.ERROR_MESSAGE);
            return;
        }
        JOptionPane.showMessageDialog(parent, "Open Suite's data has been removed. The application will now close.", "Clean Open Suite from computer", JOptionPane.INFORMATION_MESSAGE);
        System.exit(0);
    }
}