SessionSetupHelper.cs 9.4 KB

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