SessionSetupHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  37. }
  38. if (loginSuccess)
  39. {
  40. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully", message.UserName);
  41. ushort? userID = state.AddConnectedUser(message.UserName);
  42. if (!userID.HasValue)
  43. {
  44. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  45. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  46. }
  47. header.UID = userID.Value;
  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. ushort? userID = state.AddConnectedUser("Guest");
  54. if (!userID.HasValue)
  55. {
  56. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  57. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  58. }
  59. header.UID = userID.Value;
  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(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  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(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  96. }
  97. MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(messageBytes);
  98. if (messageType == MessageTypeName.Negotiate)
  99. {
  100. NegotiateMessage negotiateMessage = new NegotiateMessage(messageBytes);
  101. ChallengeMessage challengeMessage = users.GetChallengeMessage(negotiateMessage);
  102. if (isRawMessage)
  103. {
  104. response.SecurityBlob = challengeMessage.GetBytes();
  105. }
  106. else
  107. {
  108. response.SecurityBlob = GSSAPIHelper.GetGSSTokenResponseBytesFromNTLMSSPMessage(challengeMessage.GetBytes());
  109. }
  110. header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  111. }
  112. else // MessageTypeName.Authenticate
  113. {
  114. AuthenticateMessage authenticateMessage = new AuthenticateMessage(messageBytes);
  115. bool loginSuccess;
  116. try
  117. {
  118. loginSuccess = users.Authenticate(authenticateMessage);
  119. }
  120. catch (EmptyPasswordNotAllowedException)
  121. {
  122. state.LogToServer(Severity.Information, "User '{0}' authentication using an empty password was rejected", authenticateMessage.UserName);
  123. header.Status = NTStatus.STATUS_ACCOUNT_RESTRICTION;
  124. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  125. }
  126. if (loginSuccess)
  127. {
  128. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully", authenticateMessage.UserName);
  129. ushort? userID = state.AddConnectedUser(authenticateMessage.UserName);
  130. if (!userID.HasValue)
  131. {
  132. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  133. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  134. }
  135. header.UID = userID.Value;
  136. }
  137. else if (users.FallbackToGuest(authenticateMessage.UserName))
  138. {
  139. state.LogToServer(Severity.Information, "User '{0}' failed authentication. logged in as guest", authenticateMessage.UserName);
  140. ushort? userID = state.AddConnectedUser("Guest");
  141. if (!userID.HasValue)
  142. {
  143. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  144. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  145. }
  146. header.UID = userID.Value;
  147. response.Action = SessionSetupAction.SetupGuest;
  148. }
  149. else
  150. {
  151. state.LogToServer(Severity.Information, "User '{0}' failed authentication", authenticateMessage.UserName);
  152. header.Status = NTStatus.STATUS_LOGON_FAILURE;
  153. return new ErrorResponse(CommandName.SMB_COM_SESSION_SETUP_ANDX);
  154. }
  155. }
  156. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  157. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  158. return response;
  159. }
  160. private static AuthenticateMessage CreateAuthenticateMessage(string accountNameToAuth, byte[] lmResponse, byte[] ntlmResponse)
  161. {
  162. AuthenticateMessage authenticateMessage = new AuthenticateMessage();
  163. authenticateMessage.NegotiateFlags = NegotiateFlags.NegotiateUnicode | NegotiateFlags.NegotiateOEM | NegotiateFlags.RequestTarget | NegotiateFlags.NegotiateSign | NegotiateFlags.NegotiateSeal | NegotiateFlags.NegotiateLanManagerKey | NegotiateFlags.NegotiateNTLMKey | NegotiateFlags.NegotiateAlwaysSign | NegotiateFlags.NegotiateVersion | NegotiateFlags.Negotiate128 | NegotiateFlags.Negotiate56;
  164. authenticateMessage.UserName = accountNameToAuth;
  165. authenticateMessage.LmChallengeResponse = lmResponse;
  166. authenticateMessage.NtChallengeResponse = ntlmResponse;
  167. authenticateMessage.Version = Authentication.Version.Server2003;
  168. return authenticateMessage;
  169. }
  170. }
  171. }