SessionSetupHelper.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.Text;
  10. using SMBLibrary.Authentication;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB1
  14. {
  15. /// <summary>
  16. /// Session Setup helper
  17. /// </summary>
  18. public class SessionSetupHelper
  19. {
  20. internal static SMB1Command GetSessionSetupResponse(SMB1Header header, SessionSetupAndXRequest request, INTLMAuthenticationProvider users, SMB1ConnectionState state)
  21. {
  22. SessionSetupAndXResponse response = new SessionSetupAndXResponse();
  23. // The PrimaryDomain field in the request is used to determine with domain controller should authenticate the user credentials,
  24. // However, the domain controller itself does not use this field.
  25. // See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa378749%28v=vs.85%29.aspx
  26. AuthenticateMessage message = CreateAuthenticateMessage(request.AccountName, request.OEMPassword, request.UnicodePassword);
  27. bool loginSuccess;
  28. try
  29. {
  30. loginSuccess = users.Authenticate(message);
  31. }
  32. catch (EmptyPasswordNotAllowedException)
  33. {
  34. state.LogToServer(Severity.Information, "User '{0}' authentication using an empty password was rejected", message.UserName);
  35. header.Status = NTStatus.STATUS_ACCOUNT_RESTRICTION;
  36. return new ErrorResponse(request.CommandName);
  37. }
  38. if (loginSuccess)
  39. {
  40. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully", message.UserName);
  41. SMB1Session session = state.CreateSession(message.UserName, message.WorkStation);
  42. if (session == null)
  43. {
  44. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  45. return new ErrorResponse(request.CommandName);
  46. }
  47. header.UID = session.UserID;
  48. response.PrimaryDomain = request.PrimaryDomain;
  49. }
  50. else if (users.FallbackToGuest(message.UserName))
  51. {
  52. state.LogToServer(Severity.Information, "User '{0}' failed authentication. logged in as guest", message.UserName);
  53. SMB1Session session = state.CreateSession("Guest", message.WorkStation);
  54. if (session == null)
  55. {
  56. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  57. return new ErrorResponse(request.CommandName);
  58. }
  59. header.UID = session.UserID;
  60. response.Action = SessionSetupAction.SetupGuest;
  61. response.PrimaryDomain = request.PrimaryDomain;
  62. }
  63. else
  64. {
  65. state.LogToServer(Severity.Information, "User '{0}' failed authentication", message.UserName);
  66. header.Status = NTStatus.STATUS_LOGON_FAILURE;
  67. return new ErrorResponse(request.CommandName);
  68. }
  69. if ((request.Capabilities & ServerCapabilities.LargeRead) > 0)
  70. {
  71. state.LargeRead = true;
  72. }
  73. if ((request.Capabilities & ServerCapabilities.LargeWrite) > 0)
  74. {
  75. state.LargeWrite = true;
  76. }
  77. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  78. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  79. return response;
  80. }
  81. internal static SMB1Command GetSessionSetupResponseExtended(SMB1Header header, SessionSetupAndXRequestExtended request, INTLMAuthenticationProvider users, SMB1ConnectionState state)
  82. {
  83. SessionSetupAndXResponseExtended response = new SessionSetupAndXResponseExtended();
  84. // [MS-SMB] The Windows GSS implementation supports raw Kerberos / NTLM messages in the SecurityBlob
  85. byte[] messageBytes = request.SecurityBlob;
  86. bool isRawMessage = true;
  87. if (!AuthenticationMessageUtils.IsSignatureValid(messageBytes))
  88. {
  89. messageBytes = GSSAPIHelper.GetNTLMSSPMessage(request.SecurityBlob);
  90. isRawMessage = false;
  91. }
  92. if (!AuthenticationMessageUtils.IsSignatureValid(messageBytes))
  93. {
  94. header.Status = NTStatus.STATUS_NOT_IMPLEMENTED;
  95. return new ErrorResponse(request.CommandName);
  96. }
  97. // According to [MS-SMB] 3.3.5.3, a UID MUST be allocated if the server returns STATUS_MORE_PROCESSING_REQUIRED
  98. if (header.UID == 0)
  99. {
  100. ushort? userID = state.AllocateUserID();
  101. if (!userID.HasValue)
  102. {
  103. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  104. return new ErrorResponse(request.CommandName);
  105. }
  106. header.UID = userID.Value;
  107. }
  108. MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(messageBytes);
  109. if (messageType == MessageTypeName.Negotiate)
  110. {
  111. NegotiateMessage negotiateMessage = new NegotiateMessage(messageBytes);
  112. ChallengeMessage challengeMessage = users.GetChallengeMessage(negotiateMessage);
  113. if (isRawMessage)
  114. {
  115. response.SecurityBlob = challengeMessage.GetBytes();
  116. }
  117. else
  118. {
  119. response.SecurityBlob = GSSAPIHelper.GetGSSTokenResponseBytesFromNTLMSSPMessage(challengeMessage.GetBytes());
  120. }
  121. header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  122. }
  123. else // MessageTypeName.Authenticate
  124. {
  125. AuthenticateMessage authenticateMessage = new AuthenticateMessage(messageBytes);
  126. bool loginSuccess;
  127. try
  128. {
  129. loginSuccess = users.Authenticate(authenticateMessage);
  130. }
  131. catch (EmptyPasswordNotAllowedException)
  132. {
  133. state.LogToServer(Severity.Information, "User '{0}' authentication using an empty password was rejected", authenticateMessage.UserName);
  134. header.Status = NTStatus.STATUS_ACCOUNT_RESTRICTION;
  135. return new ErrorResponse(request.CommandName);
  136. }
  137. if (loginSuccess)
  138. {
  139. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully", authenticateMessage.UserName);
  140. state.CreateSession(header.UID, authenticateMessage.UserName, authenticateMessage.WorkStation);
  141. }
  142. else if (users.FallbackToGuest(authenticateMessage.UserName))
  143. {
  144. state.LogToServer(Severity.Information, "User '{0}' failed authentication. logged in as guest", authenticateMessage.UserName);
  145. state.CreateSession(header.UID, "Guest", authenticateMessage.WorkStation);
  146. response.Action = SessionSetupAction.SetupGuest;
  147. }
  148. else
  149. {
  150. state.LogToServer(Severity.Information, "User '{0}' failed authentication", authenticateMessage.UserName);
  151. header.Status = NTStatus.STATUS_LOGON_FAILURE;
  152. return new ErrorResponse(request.CommandName);
  153. }
  154. if (!isRawMessage)
  155. {
  156. response.SecurityBlob = GSSAPIHelper.GetGSSTokenAcceptCompletedResponse();
  157. }
  158. }
  159. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  160. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  161. return response;
  162. }
  163. private static AuthenticateMessage CreateAuthenticateMessage(string accountNameToAuth, byte[] lmChallengeResponse, byte[] ntChallengeResponse)
  164. {
  165. AuthenticateMessage authenticateMessage = new AuthenticateMessage();
  166. authenticateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  167. NegotiateFlags.OEMEncoding |
  168. NegotiateFlags.Sign |
  169. NegotiateFlags.LanManagerKey |
  170. NegotiateFlags.NTLMKey |
  171. NegotiateFlags.AlwaysSign |
  172. NegotiateFlags.Version |
  173. NegotiateFlags.Use128BitEncryption |
  174. NegotiateFlags.Use56BitEncryption;
  175. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSecurity(lmChallengeResponse) ||
  176. AuthenticationMessageUtils.IsNTLMv2NTResponse(ntChallengeResponse))
  177. {
  178. authenticateMessage.NegotiateFlags |= NegotiateFlags.ExtendedSecurity;
  179. }
  180. authenticateMessage.UserName = accountNameToAuth;
  181. authenticateMessage.LmChallengeResponse = lmChallengeResponse;
  182. authenticateMessage.NtChallengeResponse = ntChallengeResponse;
  183. authenticateMessage.Version = Authentication.Version.Server2003;
  184. return authenticateMessage;
  185. }
  186. }
  187. }