|
| 1 | +using System.Security.Cryptography; |
| 2 | +using System.Text; |
| 3 | + |
| 4 | +namespace CosmoSQLClient.MsSql.Auth; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Implements the NTLMv2 authentication protocol used by Windows domain auth. |
| 8 | +/// Ported from CosmoSQLClient-Swift. |
| 9 | +/// </summary> |
| 10 | +internal static class NtlmAuth |
| 11 | +{ |
| 12 | + private static readonly byte[] NtlmSignature = { 0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50, 0x00 }; |
| 13 | + private const uint NegotiateFlags = 0x62088235; |
| 14 | + |
| 15 | + public static byte[] BuildNegotiate() |
| 16 | + { |
| 17 | + using var ms = new MemoryStream(); |
| 18 | + using var bw = new BinaryWriter(ms); |
| 19 | + |
| 20 | + bw.Write(NtlmSignature); |
| 21 | + bw.Write(1u); // MessageType = 1 |
| 22 | + bw.Write(NegotiateFlags); |
| 23 | + // DomainNameFields (len=0, maxLen=0, offset=0) |
| 24 | + bw.Write(new byte[8]); |
| 25 | + // WorkstationFields (len=0, maxLen=0, offset=0) |
| 26 | + bw.Write(new byte[8]); |
| 27 | + // Version (8 bytes: Windows 10 marker) |
| 28 | + bw.Write(new byte[] { 0x0A, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x0F }); |
| 29 | + |
| 30 | + return ms.ToArray(); |
| 31 | + } |
| 32 | + |
| 33 | + public record NtlmChallenge(byte[] ServerChallenge, byte[] TargetInfo, uint Flags); |
| 34 | + |
| 35 | + public static NtlmChallenge ParseChallenge(byte[] data) |
| 36 | + { |
| 37 | + if (data.Length < 56) throw new Exception("Invalid NTLM challenge length."); |
| 38 | + |
| 39 | + for (int i = 0; i < 8; i++) |
| 40 | + if (data[i] != NtlmSignature[i]) throw new Exception("Invalid NTLM signature."); |
| 41 | + |
| 42 | + uint type = BitConverter.ToUInt32(data, 8); |
| 43 | + if (type != 2) throw new Exception("Invalid NTLM message type."); |
| 44 | + |
| 45 | + uint flags = BitConverter.ToUInt32(data, 20); |
| 46 | + byte[] challenge = new byte[8]; |
| 47 | + Array.Copy(data, 24, challenge, 0, 8); |
| 48 | + |
| 49 | + short tiLen = BitConverter.ToInt16(data, 40); |
| 50 | + int tiOffset = BitConverter.ToInt32(data, 44); |
| 51 | + byte[] targetInfo = new byte[0]; |
| 52 | + if (tiLen > 0 && tiOffset + tiLen <= data.Length) |
| 53 | + { |
| 54 | + targetInfo = new byte[tiLen]; |
| 55 | + Array.Copy(data, tiOffset, targetInfo, 0, tiLen); |
| 56 | + } |
| 57 | + |
| 58 | + return new NtlmChallenge(challenge, targetInfo, flags); |
| 59 | + } |
| 60 | + |
| 61 | + public static byte[] BuildAuthenticate( |
| 62 | + byte[] challengeData, |
| 63 | + string username, |
| 64 | + string password, |
| 65 | + string domain, |
| 66 | + string workstation) |
| 67 | + { |
| 68 | + var challenge = ParseChallenge(challengeData); |
| 69 | + |
| 70 | + // Derive NTLMv2 key |
| 71 | + byte[] ntHash = ComputeMd4(Encoding.Unicode.GetBytes(password)); |
| 72 | + byte[] identity = Encoding.Unicode.GetBytes(username.ToUpperInvariant() + domain); |
| 73 | + byte[] ntlmv2Key = ComputeHmacMd5(ntHash, identity); |
| 74 | + |
| 75 | + // NTLMv2 blob |
| 76 | + byte[] clientChallenge = new byte[8]; |
| 77 | + using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(clientChallenge); } |
| 78 | + |
| 79 | + byte[] timestamp = GetWindowsTimestamp(); |
| 80 | + byte[] blob = BuildBlob(timestamp, clientChallenge, challenge.TargetInfo); |
| 81 | + |
| 82 | + // NT response = HMAC-MD5(ntlmv2Key, serverChallenge + blob) + blob |
| 83 | + byte[] challengeAndBlob = new byte[8 + blob.Length]; |
| 84 | + Array.Copy(challenge.ServerChallenge, 0, challengeAndBlob, 0, 8); |
| 85 | + Array.Copy(blob, 0, challengeAndBlob, 8, blob.Length); |
| 86 | + |
| 87 | + byte[] ntProofStr = ComputeHmacMd5(ntlmv2Key, challengeAndBlob); |
| 88 | + byte[] ntResponse = new byte[ntProofStr.Length + blob.Length]; |
| 89 | + Array.Copy(ntProofStr, 0, ntResponse, 0, ntProofStr.Length); |
| 90 | + Array.Copy(blob, 0, ntResponse, ntProofStr.Length, blob.Length); |
| 91 | + |
| 92 | + // LM response = HMAC-MD5(ntlmv2Key, serverChallenge + clientChallenge) + clientChallenge |
| 93 | + byte[] challengeAndClient = new byte[8 + 8]; |
| 94 | + Array.Copy(challenge.ServerChallenge, 0, challengeAndClient, 0, 8); |
| 95 | + Array.Copy(clientChallenge, 0, challengeAndClient, 8, 8); |
| 96 | + byte[] lmProofStr = ComputeHmacMd5(ntlmv2Key, challengeAndClient); |
| 97 | + byte[] lmResponse = new byte[lmProofStr.Length + 8]; |
| 98 | + Array.Copy(lmProofStr, 0, lmResponse, 0, lmProofStr.Length); |
| 99 | + Array.Copy(clientChallenge, 0, lmResponse, lmProofStr.Length, 8); |
| 100 | + |
| 101 | + byte[] domainBytes = Encoding.Unicode.GetBytes(domain); |
| 102 | + byte[] userBytes = Encoding.Unicode.GetBytes(username); |
| 103 | + byte[] wsBytes = Encoding.Unicode.GetBytes(workstation); |
| 104 | + |
| 105 | + int headerSize = 72; |
| 106 | + using var ms = new MemoryStream(); |
| 107 | + using var bw = new BinaryWriter(ms); |
| 108 | + |
| 109 | + var payload = new List<byte[]>(); |
| 110 | + |
| 111 | + byte[] BuildSecBuf(byte[] data) |
| 112 | + { |
| 113 | + short len = (short)data.Length; |
| 114 | + int offset = headerSize + payload.Sum(p => p.Length); |
| 115 | + payload.Add(data); |
| 116 | + |
| 117 | + var buf = new byte[8]; |
| 118 | + Array.Copy(BitConverter.GetBytes(len), 0, buf, 0, 2); |
| 119 | + Array.Copy(BitConverter.GetBytes(len), 0, buf, 2, 2); |
| 120 | + Array.Copy(BitConverter.GetBytes(offset), 0, buf, 4, 4); |
| 121 | + return buf; |
| 122 | + } |
| 123 | + |
| 124 | + byte[] lmBuf = BuildSecBuf(lmResponse); |
| 125 | + byte[] ntBuf = BuildSecBuf(ntResponse); |
| 126 | + byte[] domBuf = BuildSecBuf(domainBytes); |
| 127 | + byte[] usrBuf = BuildSecBuf(userBytes); |
| 128 | + byte[] wsBuf = BuildSecBuf(wsBytes); |
| 129 | + |
| 130 | + bw.Write(NtlmSignature); |
| 131 | + bw.Write(3u); // MessageType = 3 |
| 132 | + bw.Write(lmBuf); |
| 133 | + bw.Write(ntBuf); |
| 134 | + bw.Write(domBuf); |
| 135 | + bw.Write(usrBuf); |
| 136 | + bw.Write(wsBuf); |
| 137 | + bw.Write(new byte[8]); // EncryptedRandomSessionKey |
| 138 | + bw.Write(NegotiateFlags); |
| 139 | + |
| 140 | + foreach (var p in payload) bw.Write(p); |
| 141 | + |
| 142 | + return ms.ToArray(); |
| 143 | + } |
| 144 | + |
| 145 | + private static byte[] BuildBlob(byte[] timestamp, byte[] clientChallenge, byte[] targetInfo) |
| 146 | + { |
| 147 | + using var ms = new MemoryStream(); |
| 148 | + using var bw = new BinaryWriter(ms); |
| 149 | + |
| 150 | + bw.Write(new byte[] { 0x01, 0x01, 0x00, 0x00 }); // Blob signature |
| 151 | + bw.Write(0u); // Reserved |
| 152 | + bw.Write(timestamp); |
| 153 | + bw.Write(clientChallenge); |
| 154 | + bw.Write(0u); // Reserved |
| 155 | + bw.Write(targetInfo); |
| 156 | + bw.Write(0u); // MsvAvEOL |
| 157 | + |
| 158 | + return ms.ToArray(); |
| 159 | + } |
| 160 | + |
| 161 | + private static byte[] GetWindowsTimestamp() |
| 162 | + { |
| 163 | + return BitConverter.GetBytes(DateTime.UtcNow.ToFileTimeUtc()); |
| 164 | + } |
| 165 | + |
| 166 | + private static byte[] ComputeMd4(byte[] input) |
| 167 | + { |
| 168 | + return MD4.Hash(input); |
| 169 | + } |
| 170 | + |
| 171 | + private static byte[] ComputeHmacMd5(byte[] key, byte[] data) |
| 172 | + { |
| 173 | + using var hmac = new HMACMD5(key); |
| 174 | + return hmac.ComputeHash(data); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +internal static class MD4 |
| 179 | +{ |
| 180 | + public static byte[] Hash(byte[] input) |
| 181 | + { |
| 182 | + uint a = 0x67452301; |
| 183 | + uint b = 0xefcdab89; |
| 184 | + uint c = 0x98badcfe; |
| 185 | + uint d = 0x10325476; |
| 186 | + |
| 187 | + byte[] data = new byte[((input.Length + 8) / 64 + 1) * 64]; |
| 188 | + Array.Copy(input, data, input.Length); |
| 189 | + data[input.Length] = 0x80; |
| 190 | + |
| 191 | + byte[] lengthBits = BitConverter.GetBytes((ulong)input.Length * 8); |
| 192 | + Array.Copy(lengthBits, 0, data, data.Length - 8, 8); |
| 193 | + |
| 194 | + for (int i = 0; i < data.Length; i += 64) |
| 195 | + { |
| 196 | + uint[] x = new uint[16]; |
| 197 | + for (int j = 0; j < 16; j++) |
| 198 | + x[j] = BitConverter.ToUInt32(data, i + j * 4); |
| 199 | + |
| 200 | + uint aa = a, bb = b, cc = c, dd = d; |
| 201 | + |
| 202 | + a = Round1(a, b, c, d, x[0], 3); d = Round1(d, a, b, c, x[1], 7); |
| 203 | + c = Round1(c, d, a, b, x[2], 11); b = Round1(b, c, d, a, x[3], 19); |
| 204 | + a = Round1(a, b, c, d, x[4], 3); d = Round1(d, a, b, c, x[5], 7); |
| 205 | + c = Round1(c, d, a, b, x[6], 11); b = Round1(b, c, d, a, x[7], 19); |
| 206 | + a = Round1(a, b, c, d, x[8], 3); d = Round1(d, a, b, c, x[9], 7); |
| 207 | + c = Round1(c, d, a, b, x[10], 11); b = Round1(b, c, d, a, x[11], 19); |
| 208 | + a = Round1(a, b, c, d, x[12], 3); d = Round1(d, a, b, c, x[13], 7); |
| 209 | + c = Round1(c, d, a, b, x[14], 11); b = Round1(b, c, d, a, x[15], 19); |
| 210 | + |
| 211 | + a = Round2(a, b, c, d, x[0], 3); d = Round2(d, a, b, c, x[4], 5); |
| 212 | + c = Round2(c, d, a, b, x[8], 9); b = Round2(b, c, d, a, x[12], 13); |
| 213 | + a = Round2(a, b, c, d, x[1], 3); d = Round2(d, a, b, c, x[5], 5); |
| 214 | + c = Round2(c, d, a, b, x[9], 9); b = Round2(b, c, d, a, x[13], 13); |
| 215 | + a = Round2(a, b, c, d, x[2], 3); d = Round2(d, a, b, c, x[6], 5); |
| 216 | + c = Round2(c, d, a, b, x[10], 9); b = Round2(b, c, d, a, x[14], 13); |
| 217 | + a = Round2(a, b, c, d, x[3], 3); d = Round2(d, a, b, c, x[7], 5); |
| 218 | + c = Round2(c, d, a, b, x[11], 9); b = Round2(b, c, d, a, x[15], 13); |
| 219 | + |
| 220 | + a = Round3(a, b, c, d, x[0], 3); d = Round3(d, a, b, c, x[8], 9); |
| 221 | + c = Round3(c, d, a, b, x[4], 11); b = Round3(b, c, d, a, x[12], 15); |
| 222 | + a = Round3(a, b, c, d, x[2], 3); d = Round3(d, a, b, c, x[10], 9); |
| 223 | + c = Round3(c, d, a, b, x[6], 11); b = Round3(b, c, d, a, x[14], 15); |
| 224 | + a = Round3(a, b, c, d, x[1], 3); d = Round3(d, a, b, c, x[9], 9); |
| 225 | + c = Round3(c, d, a, b, x[5], 11); b = Round3(b, c, d, a, x[13], 15); |
| 226 | + a = Round3(a, b, c, d, x[3], 3); d = Round3(d, a, b, c, x[11], 9); |
| 227 | + c = Round3(c, d, a, b, x[7], 11); b = Round3(b, c, d, a, x[15], 15); |
| 228 | + |
| 229 | + a += aa; b += bb; c += cc; d += dd; |
| 230 | + } |
| 231 | + |
| 232 | + byte[] res = new byte[16]; |
| 233 | + Array.Copy(BitConverter.GetBytes(a), 0, res, 0, 4); |
| 234 | + Array.Copy(BitConverter.GetBytes(b), 0, res, 4, 4); |
| 235 | + Array.Copy(BitConverter.GetBytes(c), 0, res, 8, 4); |
| 236 | + Array.Copy(BitConverter.GetBytes(d), 0, res, 12, 4); |
| 237 | + return res; |
| 238 | + } |
| 239 | + |
| 240 | + private static uint Round1(uint a, uint b, uint c, uint d, uint x, int s) => Rol(a + ((b & c) | (~b & d)) + x, s); |
| 241 | + private static uint Round2(uint a, uint b, uint c, uint d, uint x, int s) => Rol(a + ((b & c) | (b & d) | (c & d)) + x + 0x5a827999, s); |
| 242 | + private static uint Round3(uint a, uint b, uint c, uint d, uint x, int s) => Rol(a + (b ^ c ^ d) + x + 0x6ed9eba1, s); |
| 243 | + private static uint Rol(uint x, int n) => (x << n) | (x >> (32 - n)); |
| 244 | +} |
0 commit comments