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.

6.7 KB
Raw Blame
package opensuite.proxy;

import opensuite.core.Settings;
import opensuite.core.SuiteContext;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * Tests the TLS pass-through list: which destinations an entry covers, and
 * that a covered CONNECT tunnel is relayed byte for byte rather than
 * intercepted.
 */
public final class TlsPassThroughTest {

    public void run() {
        Harness.suite("TLS pass through");

        Harness.test("an entry covers its host on every port, or one port when given", () -> {
            Expect.isTrue(TlsPassThrough.matches("api.example.com", "API.example.com", 443), "host, any case");
            Expect.isTrue(TlsPassThrough.matches("api.example.com", "api.example.com", 8443), "no port covers every port");
            Expect.isTrue(TlsPassThrough.matches("api.example.com:8443", "api.example.com", 8443), "matching port");
            Expect.isFalse(TlsPassThrough.matches("api.example.com:8443", "api.example.com", 443), "other port");
            Expect.isFalse(TlsPassThrough.matches("api.example.com", "xapi.example.com", 443), "no partial match");
            Expect.isFalse(TlsPassThrough.matches("", "api.example.com", 443), "an empty entry covers nothing");
        });

        Harness.test("wildcards cover subdomains, and *. covers the bare domain too", () -> {
            Expect.isTrue(TlsPassThrough.matches("*.example.com", "a.b.example.com", 443), "nested subdomain");
            Expect.isTrue(TlsPassThrough.matches("*.example.com", "example.com", 443), "bare domain");
            Expect.isFalse(TlsPassThrough.matches("*.example.com", "badexample.com", 443), "different domain");
            Expect.isTrue(TlsPassThrough.matches("cdn*.example.*", "cdn42.example.net", 443), "wildcards anywhere");
            Expect.isTrue(TlsPassThrough.matches("*", "anything.test", 1), "a lone star covers everything");
        });

        Harness.test("the list is stored in settings, tidied and free of duplicates", () -> {
            // Settings write through to the user's preferences, so the key is
            // cleared before and after rather than left behind by the test.
            Settings s = new Settings();
            s.remove(TlsPassThrough.LIST_KEY);
            try {
            Expect.isTrue(TlsPassThrough.entries(s).isEmpty(), "empty by default");
            Expect.isTrue(TlsPassThrough.add(s, " https://Pinned.example.com:8443/path "), "added");
            Expect.isFalse(TlsPassThrough.add(s, "pinned.example.com:8443"), "duplicate ignored");
            Expect.isFalse(TlsPassThrough.add(s, "   "), "blank ignored");
            Expect.equal(TlsPassThrough.entries(s), List.of("Pinned.example.com:8443"), "URL reduced to host and port");
            Expect.isTrue(TlsPassThrough.matches(TlsPassThrough.entries(s), "pinned.example.com", 8443), "list match");
            Expect.isFalse(TlsPassThrough.matches(TlsPassThrough.entries(s), "other.example.com", 8443), "list miss");
            } finally { s.remove(TlsPassThrough.LIST_KEY); }
        });

        Harness.test("a passed-through tunnel is relayed untouched and kept out of the history", () -> {
            try { relay(); }
            catch (Exception e) { throw new RuntimeException(e); }
        });
    }

    private static void relay() throws Exception {
        Path caDir = Files.createTempDirectory("opensuite-pt-ca");
        // Removed when the test run ends: the folder last, after the file it will hold.
        caDir.toFile().deleteOnExit();
        caDir.resolve("ca.p12").toFile().deleteOnExit();
        // Stands in for a TLS server: it echoes whatever bytes arrive, so the
        // client only gets its own bytes back if nothing terminated the tunnel.
        try (ServerSocket echo = new ServerSocket(0, 5, InetAddress.getLoopbackAddress())) {
            Thread t = new Thread(() -> {
                try (Socket c = echo.accept()) {
                    InputStream in = c.getInputStream();
                    OutputStream out = c.getOutputStream();
                    byte[] buf = new byte[1024];
                    int n;
                    while ((n = in.read(buf)) >= 0) { out.write(buf, 0, n); out.flush(); }
                } catch (Exception ignored) {}
            });
            t.setDaemon(true);
            t.start();

            int port;
            try (ServerSocket probe = new ServerSocket(0, 1, InetAddress.getLoopbackAddress())) {
                port = probe.getLocalPort();
            }
            SuiteContext ctx = new SuiteContext();
            TlsPassThrough.add(ctx.settings(), "127.0.0.1:" + echo.getLocalPort());
            ProxyServer server = new ProxyServer(ctx, caDir);
            ProxyListener l = server.listeners().get(0);
            l.port = port;
            server.startAll();
            try (Socket c = new Socket(InetAddress.getLoopbackAddress(), port)) {
                c.setSoTimeout(5000);
                OutputStream out = c.getOutputStream();
                InputStream in = c.getInputStream();
                String target = "127.0.0.1:" + echo.getLocalPort();
                out.write(("CONNECT " + target + " HTTP/1.1\r\nHost: " + target + "\r\n\r\n")
                        .getBytes(StandardCharsets.ISO_8859_1));
                out.flush();
                String reply = readUntilBlankLine(in);
                Expect.isTrue(reply.startsWith("HTTP/1.1 200"), "tunnel established");
                // Not a TLS ClientHello: an intercepting proxy would fail the
                // handshake here; a relay returns it unchanged.
                byte[] sent = "\u0001\u0002not-tls\r\n\r\nraw bytes".getBytes(StandardCharsets.ISO_8859_1);
                out.write(sent);
                out.flush();
                byte[] got = in.readNBytes(sent.length);
                Expect.equal(new String(got, StandardCharsets.ISO_8859_1),
                        new String(sent, StandardCharsets.ISO_8859_1), "bytes relayed both ways unchanged");
            } finally {
                server.stopAll();
                ctx.settings().remove(TlsPassThrough.LIST_KEY);
            }
            Expect.equal(ctx.store().size(), 0, "nothing recorded in the history");
        }
    }

    private static String readUntilBlankLine(InputStream in) throws Exception {
        StringBuilder sb = new StringBuilder();
        int c;
        while ((c = in.read()) >= 0) {
            sb.append((char) c);
            if (sb.length() >= 4 && sb.substring(sb.length() - 4).equals("\r\n\r\n")) break;
        }
        return sb.toString();
    }
}