SessionSetupHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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, GSSProvider 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. header.Status = securityProvider.NTLMAuthenticate(state.AuthenticationContext, message);
  29. if (header.Status != NTStatus.STATUS_SUCCESS)
  30. {
  31. state.LogToServer(Severity.Information, "User '{0}' failed authentication, NTStatus: {1}", message.UserName, header.Status);
  32. return new ErrorResponse(request.CommandName);
  33. }
  34. object accessToken = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.AccessToken);
  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, accessToken);
  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, accessToken);
  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, GSSProvider 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[] outputToken;
  72. NTStatus status = securityProvider.AcceptSecurityContext(ref state.AuthenticationContext, request.SecurityBlob, out outputToken);
  73. if (status != NTStatus.STATUS_SUCCESS && status != NTStatus.SEC_I_CONTINUE_NEEDED)
  74. {
  75. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  76. state.LogToServer(Severity.Information, "User '{0}' failed authentication, NTStatus: {1}", userName, status);
  77. header.Status = status;
  78. return new ErrorResponse(request.CommandName);
  79. }
  80. if (outputToken != null)
  81. {
  82. response.SecurityBlob = outputToken;
  83. }
  84. // According to [MS-SMB] 3.3.5.3, a UID MUST be allocated if the server returns STATUS_MORE_PROCESSING_REQUIRED
  85. if (header.UID == 0)
  86. {
  87. ushort? userID = state.AllocateUserID();
  88. if (!userID.HasValue)
  89. {
  90. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  91. return new ErrorResponse(request.CommandName);
  92. }
  93. header.UID = userID.Value;
  94. }
  95. if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  96. {
  97. header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  98. }
  99. else // header.Status == NTStatus.STATUS_SUCCESS
  100. {
  101. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  102. string machineName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.MachineName) as string;
  103. object accessToken = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.AccessToken);
  104. bool? isGuest = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.IsGuest) as bool?;
  105. if (!isGuest.HasValue || !isGuest.Value)
  106. {
  107. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully.", userName);
  108. state.CreateSession(header.UID, userName, machineName, accessToken);
  109. }
  110. else
  111. {
  112. state.LogToServer(Severity.Information, "User '{0}' failed authentication, logged in as guest.", userName);
  113. state.CreateSession(header.UID, "Guest", machineName, accessToken);
  114. response.Action = SessionSetupAction.SetupGuest;
  115. }
  116. }
  117. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  118. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  119. return response;
  120. }
  121. private static AuthenticateMessage CreateAuthenticateMessage(string accountNameToAuth, byte[] lmChallengeResponse, byte[] ntChallengeResponse)
  122. {
  123. AuthenticateMessage authenticateMessage = new AuthenticateMessage();
  124. authenticateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  125. NegotiateFlags.OEMEncoding |
  126. NegotiateFlags.Sign |
  127. NegotiateFlags.LanManagerKey |
  128. NegotiateFlags.NTLMKey |
  129. NegotiateFlags.AlwaysSign |
  130. NegotiateFlags.Version |
  131. NegotiateFlags.Use128BitEncryption |
  132. NegotiateFlags.Use56BitEncryption;
  133. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSecurity(lmChallengeResponse) ||
  134. AuthenticationMessageUtils.IsNTLMv2NTResponse(ntChallengeResponse))
  135. {
  136. authenticateMessage.NegotiateFlags |= NegotiateFlags.ExtendedSecurity;
  137. }
  138. authenticateMessage.UserName = accountNameToAuth;
  139. authenticateMessage.LmChallengeResponse = lmChallengeResponse;
  140. authenticateMessage.NtChallengeResponse = ntChallengeResponse;
  141. authenticateMessage.Version = NTLMVersion.Server2003;
  142. return authenticateMessage;
  143. }
  144. }
  145. }