IndependentNTLMAuthenticationProvider.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. challengeMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  43. NegotiateFlags.TargetNameSupplied |
  44. NegotiateFlags.NTLMKey |
  45. NegotiateFlags.TargetTypeServer |
  46. NegotiateFlags.ExtendedSecurity |
  47. NegotiateFlags.TargetInfo |
  48. NegotiateFlags.Version;
  49. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Sign) > 0)
  50. {
  51. // [MS-NLMP] If the client sends NTLMSSP_NEGOTIATE_SIGN to the server in the NEGOTIATE_MESSAGE,
  52. // the server MUST return NTLMSSP_NEGOTIATE_SIGN to the client in the CHALLENGE_MESSAGE.
  53. challengeMessage.NegotiateFlags |= NegotiateFlags.Sign;
  54. }
  55. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Use56BitEncryption) > 0)
  56. {
  57. challengeMessage.NegotiateFlags |= NegotiateFlags.Use56BitEncryption;
  58. }
  59. if ((negotiateMessage.NegotiateFlags & NegotiateFlags.Use128BitEncryption) > 0)
  60. {
  61. challengeMessage.NegotiateFlags |= NegotiateFlags.Use128BitEncryption;
  62. }
  63. challengeMessage.TargetName = Environment.MachineName;
  64. challengeMessage.ServerChallenge = serverChallenge;
  65. challengeMessage.TargetInfo = AVPairUtils.GetAVPairSequence(Environment.MachineName, Environment.MachineName);
  66. challengeMessage.Version = NTLMVersion.Server2003;
  67. return NTStatus.SEC_I_CONTINUE_NEEDED;
  68. }
  69. public override NTStatus Authenticate(object context, AuthenticateMessage message)
  70. {
  71. AuthContext authContext = context as AuthContext;
  72. if (authContext == null)
  73. {
  74. // There are two possible reasons for authContext to be null:
  75. // 1. We have a bug in our implementation, let's assume that's not the case,
  76. // according to [MS-SMB2] 3.3.5.5.1 we aren't allowed to return SEC_E_INVALID_HANDLE anyway.
  77. // 2. The client sent AuthenticateMessage without sending NegotiateMessage first,
  78. // in this case the correct response is SEC_E_INVALID_TOKEN.
  79. return NTStatus.SEC_E_INVALID_TOKEN;
  80. }
  81. authContext.UserName = message.UserName;
  82. authContext.SessionKey = message.EncryptedRandomSessionKey;
  83. if ((message.NegotiateFlags & NegotiateFlags.Anonymous) > 0)
  84. {
  85. if (this.EnableGuestLogin)
  86. {
  87. authContext.IsGuest = true;
  88. return NTStatus.STATUS_SUCCESS;
  89. }
  90. else
  91. {
  92. return NTStatus.STATUS_LOGON_FAILURE;
  93. }
  94. }
  95. string password = m_GetUserPassword(message.UserName);
  96. if (password == null)
  97. {
  98. if (this.EnableGuestLogin)
  99. {
  100. authContext.IsGuest = true;
  101. return NTStatus.STATUS_SUCCESS;
  102. }
  103. else
  104. {
  105. return NTStatus.STATUS_LOGON_FAILURE;
  106. }
  107. }
  108. bool success;
  109. byte[] serverChallenge = authContext.ServerChallenge;
  110. if ((message.NegotiateFlags & NegotiateFlags.ExtendedSecurity) > 0)
  111. {
  112. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSecurity(message.LmChallengeResponse))
  113. {
  114. // NTLM v1 Extended Security:
  115. success = AuthenticateV1Extended(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  116. }
  117. else
  118. {
  119. // NTLM v2:
  120. success = AuthenticateV2(message.DomainName, message.UserName, password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  121. }
  122. }
  123. else
  124. {
  125. success = AuthenticateV1(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  126. }
  127. if (success)
  128. {
  129. return NTStatus.STATUS_SUCCESS;
  130. }
  131. else
  132. {
  133. return NTStatus.STATUS_LOGON_FAILURE;
  134. }
  135. }
  136. public override void DeleteSecurityContext(ref object context)
  137. {
  138. }
  139. public override object GetContextAttribute(object context, GSSAttributeName attributeName)
  140. {
  141. AuthContext authContext = context as AuthContext;
  142. if (authContext != null)
  143. {
  144. switch (attributeName)
  145. {
  146. case GSSAttributeName.IsGuest:
  147. return authContext.IsGuest;
  148. case GSSAttributeName.MachineName:
  149. return authContext.WorkStation;
  150. case GSSAttributeName.SessionKey:
  151. return authContext.SessionKey;
  152. case GSSAttributeName.UserName:
  153. return authContext.UserName;
  154. }
  155. }
  156. return null;
  157. }
  158. private bool EnableGuestLogin
  159. {
  160. get
  161. {
  162. return (m_GetUserPassword("Guest") == String.Empty);
  163. }
  164. }
  165. /// <summary>
  166. /// LM v1 / NTLM v1
  167. /// </summary>
  168. private static bool AuthenticateV1(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  169. {
  170. byte[] expectedLMResponse = NTLMCryptography.ComputeLMv1Response(serverChallenge, password);
  171. if (ByteUtils.AreByteArraysEqual(expectedLMResponse, lmResponse))
  172. {
  173. return true;
  174. }
  175. byte[] expectedNTResponse = NTLMCryptography.ComputeNTLMv1Response(serverChallenge, password);
  176. return ByteUtils.AreByteArraysEqual(expectedNTResponse, ntResponse);
  177. }
  178. /// <summary>
  179. /// LM v1 / NTLM v1 Extended Security
  180. /// </summary>
  181. private static bool AuthenticateV1Extended(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  182. {
  183. byte[] clientChallenge = ByteReader.ReadBytes(lmResponse, 0, 8);
  184. byte[] expectedNTLMv1Response = NTLMCryptography.ComputeNTLMv1ExtendedSecurityResponse(serverChallenge, clientChallenge, password);
  185. return ByteUtils.AreByteArraysEqual(expectedNTLMv1Response, ntResponse);
  186. }
  187. /// <summary>
  188. /// LM v2 / NTLM v2
  189. /// </summary>
  190. private bool AuthenticateV2(string domainName, string accountName, string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  191. {
  192. byte[] _LMv2ClientChallenge = ByteReader.ReadBytes(lmResponse, 16, 8);
  193. byte[] expectedLMv2Response = NTLMCryptography.ComputeLMv2Response(serverChallenge, _LMv2ClientChallenge, password, accountName, domainName);
  194. if (ByteUtils.AreByteArraysEqual(expectedLMv2Response, lmResponse))
  195. {
  196. return true;
  197. }
  198. if (AuthenticationMessageUtils.IsNTLMv2NTResponse(ntResponse))
  199. {
  200. byte[] clientNTProof = ByteReader.ReadBytes(ntResponse, 0, 16);
  201. byte[] clientChallengeStructurePadded = ByteReader.ReadBytes(ntResponse, 16, ntResponse.Length - 16);
  202. byte[] expectedNTProof = NTLMCryptography.ComputeNTLMv2Proof(serverChallenge, clientChallengeStructurePadded, password, accountName, domainName);
  203. return ByteUtils.AreByteArraysEqual(clientNTProof, expectedNTProof);
  204. }
  205. return false;
  206. }
  207. /// <summary>
  208. /// Generate 8-byte server challenge
  209. /// </summary>
  210. private static byte[] GenerateServerChallenge()
  211. {
  212. byte[] serverChallenge = new byte[8];
  213. new Random().NextBytes(serverChallenge);
  214. return serverChallenge;
  215. }
  216. }
  217. }