IntegratedNTLMAuthenticationProvider.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 System.Net;
  10. using System.Text;
  11. using System.Runtime.InteropServices;
  12. using Utilities;
  13. using SMBLibrary.Authentication.GSSAPI;
  14. using SMBLibrary.Authentication.NTLM;
  15. using Microsoft.Win32;
  16. namespace SMBLibrary.Win32.Security
  17. {
  18. public class IntegratedNTLMAuthenticationProvider : NTLMAuthenticationProviderBase
  19. {
  20. public class AuthContext
  21. {
  22. public SecHandle ServerContext;
  23. public string WorkStation;
  24. public string UserName;
  25. public byte[] SessionKey;
  26. public bool IsGuest;
  27. public AuthContext(SecHandle serverContext, string workStation)
  28. {
  29. ServerContext = serverContext;
  30. WorkStation = workStation;
  31. }
  32. }
  33. public override NTStatus GetChallengeMessage(out object context, NegotiateMessage negotiateMessage, out ChallengeMessage challengeMessage)
  34. {
  35. byte[] negotiateMessageBytes = negotiateMessage.GetBytes();
  36. SecHandle serverContext;
  37. byte[] challengeMessageBytes;
  38. try
  39. {
  40. challengeMessageBytes = SSPIHelper.GetType2Message(negotiateMessageBytes, out serverContext);
  41. }
  42. catch (Exception)
  43. {
  44. context = null;
  45. challengeMessage = null;
  46. // We assume that the problem is not with our implementation.
  47. return NTStatus.SEC_E_INVALID_TOKEN;
  48. }
  49. context = new AuthContext(serverContext, negotiateMessage.Workstation);
  50. challengeMessage = new ChallengeMessage(challengeMessageBytes);
  51. return NTStatus.SEC_I_CONTINUE_NEEDED;
  52. }
  53. /// <summary>
  54. /// Authenticate will return false when the password is correct in these cases:
  55. /// 1. The correct password is blank and 'limitblankpassworduse' is set to 1.
  56. /// 2. The user is listed in the "Deny access to this computer from the network" list.
  57. /// </summary>
  58. public override NTStatus Authenticate(object context, AuthenticateMessage message)
  59. {
  60. AuthContext authContext = context as AuthContext;
  61. if (authContext == null)
  62. {
  63. // There are two possible reasons for authContext to be null:
  64. // 1. We have a bug in our implementation, let's assume that's not the case,
  65. // according to [MS-SMB2] 3.3.5.5.1 we aren't allowed to return SEC_E_INVALID_HANDLE anyway.
  66. // 2. The client sent AuthenticateMessage without sending NegotiateMessage first,
  67. // in this case the correct response is SEC_E_INVALID_TOKEN.
  68. return NTStatus.SEC_E_INVALID_TOKEN;
  69. }
  70. authContext.UserName = message.UserName;
  71. authContext.SessionKey = message.EncryptedRandomSessionKey;
  72. if ((message.NegotiateFlags & NegotiateFlags.Anonymous) > 0)
  73. {
  74. if (this.EnableGuestLogin)
  75. {
  76. authContext.IsGuest = true;
  77. return NTStatus.STATUS_SUCCESS;
  78. }
  79. else
  80. {
  81. return NTStatus.STATUS_LOGON_FAILURE;
  82. }
  83. }
  84. byte[] messageBytes = message.GetBytes();
  85. bool success;
  86. try
  87. {
  88. success = SSPIHelper.AuthenticateType3Message(authContext.ServerContext, messageBytes);
  89. }
  90. catch (Exception)
  91. {
  92. // We assume that the problem is not with our implementation.
  93. return NTStatus.SEC_E_INVALID_TOKEN;
  94. }
  95. if (success)
  96. {
  97. return NTStatus.STATUS_SUCCESS;
  98. }
  99. else
  100. {
  101. Win32Error result = (Win32Error)Marshal.GetLastWin32Error();
  102. // Windows will permit fallback when these conditions are met:
  103. // 1. The guest user account is enabled.
  104. // 2. The guest user account does not have a password set.
  105. // 3. The specified account does not exist.
  106. // OR:
  107. // The password is correct but 'limitblankpassworduse' is set to 1 (logon over a network is disabled for accounts without a password).
  108. bool allowFallback = (!IsUserExists(message.UserName) || result == Win32Error.ERROR_ACCOUNT_RESTRICTION);
  109. if (allowFallback && this.EnableGuestLogin)
  110. {
  111. authContext.IsGuest = true;
  112. return NTStatus.STATUS_SUCCESS;
  113. }
  114. else
  115. {
  116. return ToNTStatus(result);
  117. }
  118. }
  119. }
  120. public override void DeleteSecurityContext(ref object context)
  121. {
  122. AuthContext authContext = context as AuthContext;
  123. if (authContext == null)
  124. {
  125. return;
  126. }
  127. SecHandle handle = ((AuthContext)context).ServerContext;
  128. SSPIHelper.DeleteSecurityContext(ref handle);
  129. }
  130. public override object GetContextAttribute(object context, GSSAttributeName attributeName)
  131. {
  132. AuthContext authContext = context as AuthContext;
  133. if (authContext != null)
  134. {
  135. switch (attributeName)
  136. {
  137. case GSSAttributeName.AccessToken:
  138. return SSPIHelper.GetAccessToken(authContext.ServerContext);
  139. case GSSAttributeName.IsGuest:
  140. return authContext.IsGuest;
  141. case GSSAttributeName.MachineName:
  142. return authContext.WorkStation;
  143. case GSSAttributeName.SessionKey:
  144. return authContext.SessionKey;
  145. case GSSAttributeName.UserName:
  146. return authContext.UserName;
  147. }
  148. }
  149. return null;
  150. }
  151. /// <summary>
  152. /// We immitate Windows, Guest logins are disabled in any of these cases:
  153. /// 1. The Guest account is disabled.
  154. /// 2. The Guest account has password set.
  155. /// 3. The Guest account is listed in the "deny access to this computer from the network" list.
  156. /// </summary>
  157. private bool EnableGuestLogin
  158. {
  159. get
  160. {
  161. return LoginAPI.ValidateUserPassword("Guest", String.Empty, LogonType.Network);
  162. }
  163. }
  164. public static bool IsUserExists(string userName)
  165. {
  166. return NetworkAPI.IsUserExists(userName);
  167. }
  168. public static NTStatus ToNTStatus(Win32Error errorCode)
  169. {
  170. switch (errorCode)
  171. {
  172. case Win32Error.ERROR_NO_TOKEN:
  173. return NTStatus.SEC_E_INVALID_TOKEN;
  174. case Win32Error.ERROR_ACCOUNT_RESTRICTION:
  175. return NTStatus.STATUS_ACCOUNT_RESTRICTION;
  176. case Win32Error.ERROR_INVALID_LOGON_HOURS:
  177. return NTStatus.STATUS_INVALID_LOGON_HOURS;
  178. case Win32Error.ERROR_INVALID_WORKSTATION:
  179. return NTStatus.STATUS_INVALID_WORKSTATION;
  180. case Win32Error.ERROR_PASSWORD_EXPIRED:
  181. return NTStatus.STATUS_PASSWORD_EXPIRED;
  182. case Win32Error.ERROR_ACCOUNT_DISABLED:
  183. return NTStatus.STATUS_ACCOUNT_DISABLED;
  184. case Win32Error.ERROR_LOGON_TYPE_NOT_GRANTED:
  185. return NTStatus.STATUS_LOGON_TYPE_NOT_GRANTED;
  186. case Win32Error.ERROR_ACCOUNT_EXPIRED:
  187. return NTStatus.STATUS_ACCOUNT_EXPIRED;
  188. case Win32Error.ERROR_PASSWORD_MUST_CHANGE:
  189. return NTStatus.STATUS_PASSWORD_MUST_CHANGE;
  190. case Win32Error.ERROR_ACCOUNT_LOCKED_OUT:
  191. return NTStatus.STATUS_ACCOUNT_LOCKED_OUT;
  192. default:
  193. return NTStatus.STATUS_LOGON_FAILURE;
  194. }
  195. }
  196. }
  197. }