IndependentNTLMAuthenticationProvider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using SMBLibrary.Authentication.GSSAPI;
  10. using Utilities;
  11. namespace SMBLibrary.Authentication.NTLM
  12. {
  13. public delegate string GetUserPassword(string userName);
  14. public class IndependentNTLMAuthenticationProvider : NTLMAuthenticationProviderBase
  15. {
  16. public class AuthContext
  17. {
  18. public string WorkStation;
  19. public byte[] ServerChallenge;
  20. public string UserName;
  21. public byte[] SessionKey;
  22. public bool IsGuest;
  23. public AuthContext(string workStation, byte[] serverChallenge)
  24. {
  25. WorkStation = workStation;
  26. ServerChallenge = serverChallenge;
  27. }
  28. }
  29. private GetUserPassword m_GetUserPassword;
  30. /// <param name="getUserPassword">
  31. /// The NTLM challenge response will be compared against the provided password.
  32. /// </param>
  33. public IndependentNTLMAuthenticationProvider(GetUserPassword getUserPassword)
  34. {
  35. m_GetUserPassword = getUserPassword;
  36. }
  37. public override NTStatus GetChallengeMessage(out object context, NegotiateMessage negotiateMessage, out ChallengeMessage challengeMessage)
  38. {
  39. byte[] serverChallenge = GenerateServerChallenge();
  40. context = new AuthContext(negotiateMessage.Workstation, serverChallenge);
  41. challengeMessage = new ChallengeMessage();
  42. // https://msdn.microsoft.com/en-us/library/cc236691.aspx
  43. challengeMessage.NegotiateFlags = NegotiateFlags.TargetTypeServer |
  44. NegotiateFlags.TargetInfo |
  45. NegotiateFlags.TargetNameSupplied |
  46. NegotiateFlags.Version;
  47. // [MS-NLMP] NTLMSSP_NEGOTIATE_NTLM MUST be set in the [..] CHALLENGE_MESSAGE to the client.
  48. challengeMessage.NegotiateFlags |= NegotiateFlags.NTLMSessionSecurity;
  49. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.UnicodeEncoding) > 0)
  50. {
  51. challengeMessage.NegotiateFlags |= NegotiateFlags.UnicodeEncoding;
  52. }
  53. else if ((negotiateMessage.NegotiateFlags & NegotiateFlags.OEMEncoding) > 0)
  54. {
  55. challengeMessage.NegotiateFlags |= NegotiateFlags.OEMEncoding;
  56. }
  57. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.ExtendedSessionSecurity) > 0)
  58. {
  59. challengeMessage.NegotiateFlags |= NegotiateFlags.ExtendedSessionSecurity;
  60. }
  61. else if ((negotiateMessage.NegotiateFlags & NegotiateFlags.LanManagerKey) > 0)
  62. {
  63. challengeMessage.NegotiateFlags |= NegotiateFlags.LanManagerKey;
  64. }
  65. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Sign) > 0)
  66. {
  67. // [MS-NLMP] If the client sends NTLMSSP_NEGOTIATE_SIGN to the server in the NEGOTIATE_MESSAGE,
  68. // the server MUST return NTLMSSP_NEGOTIATE_SIGN to the client in the CHALLENGE_MESSAGE.
  69. challengeMessage.NegotiateFlags |= NegotiateFlags.Sign;
  70. }
  71. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Seal) > 0)
  72. {
  73. // [MS-NLMP] If the client sends NTLMSSP_NEGOTIATE_SEAL to the server in the NEGOTIATE_MESSAGE,
  74. // the server MUST return NTLMSSP_NEGOTIATE_SEAL to the client in the CHALLENGE_MESSAGE.
  75. challengeMessage.NegotiateFlags |= NegotiateFlags.Seal;
  76. }
  77. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Sign) > 0 ||
  78. (negotiateMessage.NegotiateFlags & NegotiateFlags.Seal) > 0)
  79. {
  80. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Use56BitEncryption) > 0)
  81. {
  82. // [MS-NLMP] If the client sends NTLMSSP_NEGOTIATE_SEAL or NTLMSSP_NEGOTIATE_SIGN with
  83. // NTLMSSP_NEGOTIATE_56 to the server in the NEGOTIATE_MESSAGE, the server MUST return
  84. // NTLMSSP_NEGOTIATE_56 to the client in the CHALLENGE_MESSAGE.
  85. challengeMessage.NegotiateFlags |= NegotiateFlags.Use56BitEncryption;
  86. }
  87. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Use128BitEncryption) > 0)
  88. {
  89. // [MS-NLMP] If the client sends NTLMSSP_NEGOTIATE_128 to the server in the NEGOTIATE_MESSAGE,
  90. // the server MUST return NTLMSSP_NEGOTIATE_128 to the client in the CHALLENGE_MESSAGE only if
  91. // the client sets NTLMSSP_NEGOTIATE_SEAL or NTLMSSP_NEGOTIATE_SIGN.
  92. challengeMessage.NegotiateFlags |= NegotiateFlags.Use128BitEncryption;
  93. }
  94. }
  95. challengeMessage.TargetName = Environment.MachineName;
  96. challengeMessage.ServerChallenge = serverChallenge;
  97. challengeMessage.TargetInfo = AVPairUtils.GetAVPairSequence(Environment.MachineName, Environment.MachineName);
  98. challengeMessage.Version = NTLMVersion.Server2003;
  99. return NTStatus.SEC_I_CONTINUE_NEEDED;
  100. }
  101. public override NTStatus Authenticate(object context, AuthenticateMessage message)
  102. {
  103. AuthContext authContext = context as AuthContext;
  104. if (authContext == null)
  105. {
  106. // There are two possible reasons for authContext to be null:
  107. // 1. We have a bug in our implementation, let's assume that's not the case,
  108. // according to [MS-SMB2] 3.3.5.5.1 we aren't allowed to return SEC_E_INVALID_HANDLE anyway.
  109. // 2. The client sent AuthenticateMessage without sending NegotiateMessage first,
  110. // in this case the correct response is SEC_E_INVALID_TOKEN.
  111. return NTStatus.SEC_E_INVALID_TOKEN;
  112. }
  113. authContext.UserName = message.UserName;
  114. authContext.SessionKey = message.EncryptedRandomSessionKey;
  115. if ((message.NegotiateFlags & NegotiateFlags.Anonymous) > 0)
  116. {
  117. if (this.EnableGuestLogin)
  118. {
  119. authContext.IsGuest = true;
  120. return NTStatus.STATUS_SUCCESS;
  121. }
  122. else
  123. {
  124. return NTStatus.STATUS_LOGON_FAILURE;
  125. }
  126. }
  127. string password = m_GetUserPassword(message.UserName);
  128. if (password == null)
  129. {
  130. if (this.EnableGuestLogin)
  131. {
  132. authContext.IsGuest = true;
  133. return NTStatus.STATUS_SUCCESS;
  134. }
  135. else
  136. {
  137. return NTStatus.STATUS_LOGON_FAILURE;
  138. }
  139. }
  140. bool success;
  141. byte[] serverChallenge = authContext.ServerChallenge;
  142. if ((message.NegotiateFlags & NegotiateFlags.ExtendedSessionSecurity) > 0)
  143. {
  144. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSecurity(message.LmChallengeResponse))
  145. {
  146. // NTLM v1 Extended Security:
  147. success = AuthenticateV1Extended(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  148. }
  149. else
  150. {
  151. // NTLM v2:
  152. success = AuthenticateV2(message.DomainName, message.UserName, password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  153. }
  154. }
  155. else
  156. {
  157. success = AuthenticateV1(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  158. }
  159. if (success)
  160. {
  161. return NTStatus.STATUS_SUCCESS;
  162. }
  163. else
  164. {
  165. return NTStatus.STATUS_LOGON_FAILURE;
  166. }
  167. }
  168. public override void DeleteSecurityContext(ref object context)
  169. {
  170. }
  171. public override object GetContextAttribute(object context, GSSAttributeName attributeName)
  172. {
  173. AuthContext authContext = context as AuthContext;
  174. if (authContext != null)
  175. {
  176. switch (attributeName)
  177. {
  178. case GSSAttributeName.IsGuest:
  179. return authContext.IsGuest;
  180. case GSSAttributeName.MachineName:
  181. return authContext.WorkStation;
  182. case GSSAttributeName.SessionKey:
  183. return authContext.SessionKey;
  184. case GSSAttributeName.UserName:
  185. return authContext.UserName;
  186. }
  187. }
  188. return null;
  189. }
  190. private bool EnableGuestLogin
  191. {
  192. get
  193. {
  194. return (m_GetUserPassword("Guest") == String.Empty);
  195. }
  196. }
  197. /// <summary>
  198. /// LM v1 / NTLM v1
  199. /// </summary>
  200. private static bool AuthenticateV1(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  201. {
  202. byte[] expectedLMResponse = NTLMCryptography.ComputeLMv1Response(serverChallenge, password);
  203. if (ByteUtils.AreByteArraysEqual(expectedLMResponse, lmResponse))
  204. {
  205. return true;
  206. }
  207. byte[] expectedNTResponse = NTLMCryptography.ComputeNTLMv1Response(serverChallenge, password);
  208. return ByteUtils.AreByteArraysEqual(expectedNTResponse, ntResponse);
  209. }
  210. /// <summary>
  211. /// LM v1 / NTLM v1 Extended Security
  212. /// </summary>
  213. private static bool AuthenticateV1Extended(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  214. {
  215. byte[] clientChallenge = ByteReader.ReadBytes(lmResponse, 0, 8);
  216. byte[] expectedNTLMv1Response = NTLMCryptography.ComputeNTLMv1ExtendedSecurityResponse(serverChallenge, clientChallenge, password);
  217. return ByteUtils.AreByteArraysEqual(expectedNTLMv1Response, ntResponse);
  218. }
  219. /// <summary>
  220. /// LM v2 / NTLM v2
  221. /// </summary>
  222. private bool AuthenticateV2(string domainName, string accountName, string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  223. {
  224. byte[] _LMv2ClientChallenge = ByteReader.ReadBytes(lmResponse, 16, 8);
  225. byte[] expectedLMv2Response = NTLMCryptography.ComputeLMv2Response(serverChallenge, _LMv2ClientChallenge, password, accountName, domainName);
  226. if (ByteUtils.AreByteArraysEqual(expectedLMv2Response, lmResponse))
  227. {
  228. return true;
  229. }
  230. if (AuthenticationMessageUtils.IsNTLMv2NTResponse(ntResponse))
  231. {
  232. byte[] clientNTProof = ByteReader.ReadBytes(ntResponse, 0, 16);
  233. byte[] clientChallengeStructurePadded = ByteReader.ReadBytes(ntResponse, 16, ntResponse.Length - 16);
  234. byte[] expectedNTProof = NTLMCryptography.ComputeNTLMv2Proof(serverChallenge, clientChallengeStructurePadded, password, accountName, domainName);
  235. return ByteUtils.AreByteArraysEqual(clientNTProof, expectedNTProof);
  236. }
  237. return false;
  238. }
  239. /// <summary>
  240. /// Generate 8-byte server challenge
  241. /// </summary>
  242. private static byte[] GenerateServerChallenge()
  243. {
  244. byte[] serverChallenge = new byte[8];
  245. new Random().NextBytes(serverChallenge);
  246. return serverChallenge;
  247. }
  248. }
  249. }