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.5 KB
Raw Blame
package opensuite.clickjack;

/**
 * Builds a clickjacking proof of concept: an HTML page that loads a target URL
 * in an iframe under a semi-transparent decoy, so a tester can see at a glance
 * whether the target refuses to be framed (a correct {@code X-Frame-Options} or
 * {@code Content-Security-Policy: frame-ancestors}) or sits inside the frame,
 * which is the finding. This is the engagement-tool equivalent of the
 * reference's clickjacking recorder, without its browser capture step.
 *
 * <p>Pure text, no Swing or network, so it is unit tested.
 */
public final class ClickjackPoc {
    private ClickjackPoc() {}

    /** The overlay opacity is a percentage; these keep it visible but see-through. */
    public static final int MIN_OPACITY = 0;
    public static final int MAX_OPACITY = 100;

    /**
     * An HTML page that frames {@code targetUrl}. {@code opacity} (0-100) sets
     * how solid the decoy overlay is, {@code offsetX}/{@code offsetY} nudge it
     * over the control the tester wants to line up, and {@code decoyText} is the
     * bait shown on the overlay.
     */
    public static String generate(String targetUrl, int opacity, int offsetX, int offsetY, String decoyText) {
        int op = Math.max(MIN_OPACITY, Math.min(MAX_OPACITY, opacity));
        double frac = op / 100.0;
        String url = esc(targetUrl == null ? "" : targetUrl.trim());
        String bait = esc(decoyText == null || decoyText.isBlank() ? "Click here" : decoyText);
        return "<!doctype html>\n<html>\n<head>\n<meta charset=\"utf-8\">\n"
                + "<title>Clickjacking proof of concept</title>\n<style>\n"
                + "  body { margin: 0; font-family: sans-serif; }\n"
                + "  #decoy {\n"
                + "    position: absolute;\n"
                + "    top: " + offsetY + "px;\n"
                + "    left: " + offsetX + "px;\n"
                + "    z-index: 2;\n"
                + "    padding: 12px 20px;\n"
                + "    background: #db612f;\n"
                + "    color: #fff;\n"
                + "    font-size: 18px;\n"
                + "    border-radius: 4px;\n"
                + "    pointer-events: none;\n"
                + "  }\n"
                + "  #target {\n"
                + "    position: absolute;\n"
                + "    top: 0;\n"
                + "    left: 0;\n"
                + "    width: 100%;\n"
                + "    height: 100%;\n"
                + "    border: 0;\n"
                + "    z-index: 1;\n"
                + "    opacity: " + trim(frac) + ";\n"
                + "  }\n"
                + "</style>\n</head>\n<body>\n"
                + "  <div id=\"decoy\">" + bait + "</div>\n"
                + "  <iframe id=\"target\" src=\"" + url + "\"></iframe>\n"
                + "  <p style=\"position:absolute;bottom:8px;left:8px;z-index:3;color:#555;font-size:12px\">\n"
                + "    If the target site appears above, it can be framed and is a clickjacking candidate.\n"
                + "    If this area stays blank, the site refuses framing.\n"
                + "  </p>\n"
                + "</body>\n</html>\n";
    }

    private static String trim(double d) {
        String s = String.format(java.util.Locale.ROOT, "%.2f", d);
        while (s.endsWith("0")) s = s.substring(0, s.length() - 1);
        if (s.endsWith(".")) s = s + "0";
        return s;
    }

    static String esc(String s) {
        if (s == null) return "";
        return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;");
    }
}