IntegratedNTLMAuthenticationProvider.cs 8.9 KB

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