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.2 KB
Raw Blame
package opensuite.core;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

/** Platform authentication: the published test vectors, and a 401 answered end to end. */
public final class PlatformAuthTest {

    private static byte[] unhex(String s) {
        byte[] out = new byte[s.length() / 2];
        for (int i = 0; i < out.length; i++) out[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
        return out;
    }

    public void run() {
        Harness.suite("platform authentication");

        Harness.test("MD4 matches RFC 1320", () -> {
            Expect.isTrue("31d6cfe0d16ae931b73c59d7e0c089c0".equals(PlatformAuth.hex(PlatformAuth.md4(new byte[0]))), "the empty message");
            Expect.isTrue("a448017aaf21d8525fc10ae87aa6729d".equals(PlatformAuth.hex(PlatformAuth.md4("abc".getBytes()))), "abc");
            Expect.isTrue("e33b4ddc9c38f2199c3e7b164fcc0536".equals(PlatformAuth.hex(PlatformAuth.md4(
                    "12345678901234567890123456789012345678901234567890123456789012345678901234567890".getBytes()))), "a message longer than one block");
        });

        Harness.test("the NTLMv2 hash and response match MS-NLMP 4.2.4", () -> {
            try {
                Expect.isTrue("0c868a403bfd7a93a3001ef22ef02e3f".equals(PlatformAuth.hex(PlatformAuth.ntlmv2Hash("User", "Password", "Domain"))), "NTOWFv2");
                // The specification's challenge message: server challenge 0123456789abcdef, with its target information.
                byte[] targetInfo = unhex("02000c0044006f006d00610069006e0001000c005300650072007600650072000000" + "0000");
                java.nio.ByteBuffer t2 = java.nio.ByteBuffer.allocate(48 + targetInfo.length).order(java.nio.ByteOrder.LITTLE_ENDIAN);
                t2.put("NTLMSSP\0".getBytes(StandardCharsets.US_ASCII)).putInt(2);
                t2.putShort((short) 0).putShort((short) 0).putInt(48);
                t2.putInt(0x00088205).put(unhex("0123456789abcdef")).putLong(0);
                t2.putShort((short) targetInfo.length).putShort((short) targetInfo.length).putInt(48);
                t2.put(targetInfo);
                // Time zero is 1601 in the server's reckoning: the Unix time that maps to it.
                byte[] t3 = PlatformAuth.type3(t2.array(), "User", "Password", "Domain", "COMPUTER", unhex("aaaaaaaaaaaaaaaa"), -11644473600000L);
                java.nio.ByteBuffer b = java.nio.ByteBuffer.wrap(t3).order(java.nio.ByteOrder.LITTLE_ENDIAN);
                int lmOff = b.getInt(16), ntOff = b.getInt(24);
                String lm = PlatformAuth.hex(java.util.Arrays.copyOfRange(t3, lmOff, lmOff + 24));
                String proof = PlatformAuth.hex(java.util.Arrays.copyOfRange(t3, ntOff, ntOff + 16));
                Expect.isTrue("86c35097ac9cec102554764a57cccc19aaaaaaaaaaaaaaaa".equals(lm), "LMv2 response " + lm);
                Expect.isTrue("68cd0ab851e51c96aabc927bebef6a1c".equals(proof), "NTProofStr " + proof);
                Expect.isTrue(b.getInt(8) == 3 && new String(t3, 0, 7, StandardCharsets.US_ASCII).equals("NTLMSSP"), "an authenticate message");
            } catch (Exception e) { throw new IllegalStateException(e); }
        });

        Harness.test("the Digest response matches RFC 2617", () -> {
            try {
                String header = PlatformAuth.digest("realm=\"[email protected]\", qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
                        + "opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"", "GET", "/dir/index.html", "Mufasa", "Circle Of Life", "0a4f113b", 1, null);
                Expect.isTrue(header.contains("response=\"6629fae49393a05397450978507c4ef1\""), header);
                Expect.isTrue(header.contains("opaque=\"5ccc069c403ebaf9f0171e9517f40e41\"") && header.contains("qop=auth") && header.contains("nc=00000001"), header);
            } catch (Exception e) { throw new IllegalStateException(e); }
        });

        Harness.test("credentials are picked by host, and the scheme by what the server offers", () -> {
            String rows = "true\t*.corp.test\tNTLMv2\talice\tpw\tCORP\tPC1\nfalse\t*\tBasic\toff\tx\t\t\ntrue\tapi.test\t\tbob\tpw2\t\t\n";
            Expect.isTrue("alice".equals(PlatformAuth.loginFor(rows, "intranet.corp.test").user()), "by host pattern");
            Expect.isTrue(PlatformAuth.loginFor(rows, "other.test") == null, "a disabled row is not used");
            Expect.isTrue("ntlm".equals(PlatformAuth.choose("NTLMv2", List.of("negotiate", "ntlm", "basic"))), "the named scheme when offered");
            Expect.isTrue("digest".equals(PlatformAuth.choose("", List.of("basic", "digest"))), "else the strongest offered");
            Expect.isTrue("basic".equals(PlatformAuth.choose("NTLMv2", List.of("basic"))), "falling back to what there is");
            Expect.isTrue(PlatformAuth.choose("Basic", List.of("negotiate")) == null, "nothing usable");
        });

        Harness.test("a 401 is answered with Basic credentials, once", () -> {
            SuiteContext ctx = new SuiteContext();
            Settings s = ctx.settings();
            String expected = "Basic " + Base64.getEncoder().encodeToString("bob:s3cret".getBytes());
            int[] hits = new int[1];
            try (java.net.ServerSocket server = new java.net.ServerSocket(0, 5, java.net.InetAddress.getLoopbackAddress())) {
                Thread t = new Thread(() -> {
                    for (int i = 0; i < 2; i++) {
                        try (java.net.Socket c = server.accept()) {
                            byte[] buf = new byte[4096];
                            int n = c.getInputStream().read(buf);
                            String req = new String(buf, 0, Math.max(0, n), StandardCharsets.ISO_8859_1);
                            hits[0]++;
                            boolean ok = req.contains("Authorization: " + expected);
                            String body = ok ? "welcome" : "denied";
                            c.getOutputStream().write(((ok ? "HTTP/1.1 200 OK\r\n" : "HTTP/1.1 401 Unauthorized\r\nWWW-Authenticate: Basic realm=\"x\"\r\n")
                                    + "Connection: close\r\nContent-Length: " + body.length() + "\r\n\r\n" + body).getBytes());
                            c.getOutputStream().flush();
                        } catch (Exception ignored) { }
                    }
                });
                t.setDaemon(true);
                t.start();
                s.set(PlatformAuth.ROWS, "true\t127.0.0.1\tBasic\tbob\ts3cret\t\t\n");
                HttpMessage req = new HttpMessage("GET /private HTTP/1.1\r\nHost: x\r\n\r\n".getBytes());
                HttpClient.Result r = new HttpClient().send(new HttpService("127.0.0.1", server.getLocalPort(), false), req, new HttpClient.Options());
                Expect.isTrue(r.ok() && r.response.statusCode() == 200, "let in on the second send: " + (r.ok() ? r.response.statusCode() : r.error));
                Expect.isTrue(hits[0] == 2, "two sends, no more: " + hits[0]);
            } catch (java.io.IOException e) {
                throw new IllegalStateException(e);
            } finally { s.remove(PlatformAuth.ROWS); HttpClient.setGate(null); PlatformAuth.bind(null); NetworkConfig.bind(null); }
        });
    }
}