IndependentNTLMAuthenticationProvider.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 Win32Error 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 Win32Error.ERROR_SUCCESS;
  68. }
  69. public override Win32Error Authenticate(object context, AuthenticateMessage message)
  70. {
  71. AuthContext authContext = context as AuthContext;
  72. if (authContext == null)
  73. {
  74. return Win32Error.ERROR_NO_TOKEN;
  75. }
  76. authContext.UserName = message.UserName;
  77. authContext.SessionKey = message.EncryptedRandomSessionKey;
  78. if ((message.NegotiateFlags & NegotiateFlags.Anonymous) > 0)
  79. {
  80. if (this.EnableGuestLogin)
  81. {
  82. authContext.IsGuest = true;
  83. return Win32Error.ERROR_SUCCESS;
  84. }
  85. else
  86. {
  87. return Win32Error.ERROR_LOGON_FAILURE;
  88. }
  89. }
  90. string password = m_GetUserPassword(message.UserName);
  91. if (password == null)
  92. {
  93. if (this.EnableGuestLogin)
  94. {
  95. authContext.IsGuest = true;
  96. return Win32Error.ERROR_SUCCESS;
  97. }
  98. else
  99. {
  100. return Win32Error.ERROR_LOGON_FAILURE;
  101. }
  102. }
  103. bool success;
  104. byte[] serverChallenge = authContext.ServerChallenge;
  105. if ((message.NegotiateFlags & NegotiateFlags.ExtendedSecurity) > 0)
  106. {
  107. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSecurity(message.LmChallengeResponse))
  108. {
  109. // NTLM v1 Extended Security:
  110. success = AuthenticateV1Extended(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  111. }
  112. else
  113. {
  114. // NTLM v2:
  115. success = AuthenticateV2(message.DomainName, message.UserName, password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  116. }
  117. }
  118. else
  119. {
  120. success = AuthenticateV1(password, serverChallenge, message.LmChallengeResponse, message.NtChallengeResponse);
  121. }
  122. if (success)
  123. {
  124. return Win32Error.ERROR_SUCCESS;
  125. }
  126. else
  127. {
  128. return Win32Error.ERROR_LOGON_FAILURE;
  129. }
  130. }
  131. public override void DeleteSecurityContext(ref object context)
  132. {
  133. }
  134. public override object GetContextAttribute(object context, GSSAttributeName attributeName)
  135. {
  136. AuthContext authContext = context as AuthContext;
  137. if (authContext != null)
  138. {
  139. switch (attributeName)
  140. {
  141. case GSSAttributeName.IsGuest:
  142. return authContext.IsGuest;
  143. case GSSAttributeName.MachineName:
  144. return authContext.WorkStation;
  145. case GSSAttributeName.SessionKey:
  146. return authContext.SessionKey;
  147. case GSSAttributeName.UserName:
  148. return authContext.UserName;
  149. }
  150. }
  151. return null;
  152. }
  153. private bool EnableGuestLogin
  154. {
  155. get
  156. {
  157. return (m_GetUserPassword("Guest") == String.Empty);
  158. }
  159. }
  160. /// <summary>
  161. /// LM v1 / NTLM v1
  162. /// </summary>
  163. private static bool AuthenticateV1(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  164. {
  165. byte[] expectedLMResponse = NTLMCryptography.ComputeLMv1Response(serverChallenge, password);
  166. if (ByteUtils.AreByteArraysEqual(expectedLMResponse, lmResponse))
  167. {
  168. return true;
  169. }
  170. byte[] expectedNTResponse = NTLMCryptography.ComputeNTLMv1Response(serverChallenge, password);
  171. return ByteUtils.AreByteArraysEqual(expectedNTResponse, ntResponse);
  172. }
  173. /// <summary>
  174. /// LM v1 / NTLM v1 Extended Security
  175. /// </summary>
  176. private static bool AuthenticateV1Extended(string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  177. {
  178. byte[] clientChallenge = ByteReader.ReadBytes(lmResponse, 0, 8);
  179. byte[] expectedNTLMv1Response = NTLMCryptography.ComputeNTLMv1ExtendedSecurityResponse(serverChallenge, clientChallenge, password);
  180. return ByteUtils.AreByteArraysEqual(expectedNTLMv1Response, ntResponse);
  181. }
  182. /// <summary>
  183. /// LM v2 / NTLM v2
  184. /// </summary>
  185. private bool AuthenticateV2(string domainName, string accountName, string password, byte[] serverChallenge, byte[] lmResponse, byte[] ntResponse)
  186. {
  187. byte[] _LMv2ClientChallenge = ByteReader.ReadBytes(lmResponse, 16, 8);
  188. byte[] expectedLMv2Response = NTLMCryptography.ComputeLMv2Response(serverChallenge, _LMv2ClientChallenge, password, accountName, domainName);
  189. if (ByteUtils.AreByteArraysEqual(expectedLMv2Response, lmResponse))
  190. {
  191. return true;
  192. }
  193. if (AuthenticationMessageUtils.IsNTLMv2NTResponse(ntResponse))
  194. {
  195. byte[] clientNTProof = ByteReader.ReadBytes(ntResponse, 0, 16);
  196. byte[] clientChallengeStructurePadded = ByteReader.ReadBytes(ntResponse, 16, ntResponse.Length - 16);
  197. byte[] expectedNTProof = NTLMCryptography.ComputeNTLMv2Proof(serverChallenge, clientChallengeStructurePadded, password, accountName, domainName);
  198. return ByteUtils.AreByteArraysEqual(clientNTProof, expectedNTProof);
  199. }
  200. return false;
  201. }
  202. /// <summary>
  203. /// Generate 8-byte server challenge
  204. /// </summary>
  205. private static byte[] GenerateServerChallenge()
  206. {
  207. byte[] serverChallenge = new byte[8];
  208. new Random().NextBytes(serverChallenge);
  209. return serverChallenge;
  210. }
  211. }
  212. }