SessionSetupHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. internal 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, "Session Setup: User '{0}' failed authentication (Domain: '{1}', OS: '{2}'), NTStatus: {3}", request.AccountName, request.PrimaryDomain, request.NativeOS, header.Status);
  32. return new ErrorResponse(request.CommandName);
  33. }
  34. string osVersion = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.OSVersion) as string;
  35. byte[] sessionKey = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.SessionKey) as byte[];
  36. object accessToken = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.AccessToken);
  37. bool? isGuest = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.IsGuest) as bool?;
  38. SMB1Session session;
  39. if (!isGuest.HasValue || !isGuest.Value)
  40. {
  41. state.LogToServer(Severity.Information, "Session Setup: User '{0}' authenticated successfully (Domain: '{1}', Workstation: '{2}', OS version: '{3}').", message.UserName, message.DomainName, message.WorkStation, osVersion);
  42. session = state.CreateSession(message.UserName, message.WorkStation, sessionKey, accessToken);
  43. }
  44. else
  45. {
  46. state.LogToServer(Severity.Information, "Session Setup: User '{0}' failed authentication (Domain: '{1}', Workstation: '{2}', OS version: '{3}'), logged in as guest.", message.UserName, message.DomainName, message.WorkStation, osVersion);
  47. session = state.CreateSession("Guest", message.WorkStation, sessionKey, accessToken);
  48. response.Action = SessionSetupAction.SetupGuest;
  49. }
  50. if (session == null)
  51. {
  52. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  53. return new ErrorResponse(request.CommandName);
  54. }
  55. header.UID = session.UserID;
  56. response.PrimaryDomain = request.PrimaryDomain;
  57. if ((request.Capabilities & Capabilities.LargeRead) > 0)
  58. {
  59. state.LargeRead = true;
  60. }
  61. if ((request.Capabilities & Capabilities.LargeWrite) > 0)
  62. {
  63. state.LargeWrite = true;
  64. }
  65. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  66. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  67. return response;
  68. }
  69. internal static SMB1Command GetSessionSetupResponseExtended(SMB1Header header, SessionSetupAndXRequestExtended request, GSSProvider securityProvider, SMB1ConnectionState state)
  70. {
  71. SessionSetupAndXResponseExtended response = new SessionSetupAndXResponseExtended();
  72. // [MS-SMB] The Windows GSS implementation supports raw Kerberos / NTLM messages in the SecurityBlob
  73. byte[] outputToken;
  74. NTStatus status = securityProvider.AcceptSecurityContext(ref state.AuthenticationContext, request.SecurityBlob, out outputToken);
  75. if (status != NTStatus.STATUS_SUCCESS && status != NTStatus.SEC_I_CONTINUE_NEEDED)
  76. {
  77. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  78. string domainName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.DomainName) as string;
  79. string machineName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.MachineName) as string;
  80. string osVersion = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.OSVersion) as string;
  81. state.LogToServer(Severity.Information, "Session Setup: User '{0}' failed authentication (Domain: '{1}', Workstation: '{2}', OS version: '{3}'), NTStatus: {4}", userName, domainName, machineName, osVersion, status);
  82. header.Status = status;
  83. return new ErrorResponse(request.CommandName);
  84. }
  85. if (outputToken != null)
  86. {
  87. response.SecurityBlob = outputToken;
  88. }
  89. // According to [MS-SMB] 3.3.5.3, a UID MUST be allocated if the server returns STATUS_MORE_PROCESSING_REQUIRED
  90. if (header.UID == 0)
  91. {
  92. ushort? userID = state.AllocateUserID();
  93. if (!userID.HasValue)
  94. {
  95. header.Status = NTStatus.STATUS_TOO_MANY_SESSIONS;
  96. return new ErrorResponse(request.CommandName);
  97. }
  98. header.UID = userID.Value;
  99. }
  100. if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  101. {
  102. header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  103. }
  104. else // header.Status == NTStatus.STATUS_SUCCESS
  105. {
  106. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  107. string domainName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.DomainName) as string;
  108. string machineName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.MachineName) as string;
  109. string osVersion = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.OSVersion) as string;
  110. byte[] sessionKey = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.SessionKey) as byte[];
  111. object accessToken = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.AccessToken);
  112. bool? isGuest = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.IsGuest) as bool?;
  113. if (!isGuest.HasValue || !isGuest.Value)
  114. {
  115. state.LogToServer(Severity.Information, "Session Setup: User '{0}' authenticated successfully (Domain: '{1}', Workstation: '{2}', OS version: '{3}').", userName, domainName, machineName, osVersion);
  116. state.CreateSession(header.UID, userName, machineName, sessionKey, accessToken);
  117. }
  118. else
  119. {
  120. state.LogToServer(Severity.Information, "Session Setup: User '{0}' failed authentication (Domain: '{1}', Workstation: '{2}', OS version: '{3}'), logged in as guest.", userName, domainName, machineName, osVersion);
  121. state.CreateSession(header.UID, "Guest", machineName, sessionKey, accessToken);
  122. response.Action = SessionSetupAction.SetupGuest;
  123. }
  124. }
  125. response.NativeOS = String.Empty; // "Windows Server 2003 3790 Service Pack 2"
  126. response.NativeLanMan = String.Empty; // "Windows Server 2003 5.2"
  127. return response;
  128. }
  129. private static AuthenticateMessage CreateAuthenticateMessage(string accountNameToAuth, byte[] lmChallengeResponse, byte[] ntChallengeResponse)
  130. {
  131. AuthenticateMessage authenticateMessage = new AuthenticateMessage();
  132. authenticateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  133. NegotiateFlags.OEMEncoding |
  134. NegotiateFlags.Sign |
  135. NegotiateFlags.NTLMSessionSecurity |
  136. NegotiateFlags.AlwaysSign |
  137. NegotiateFlags.Version |
  138. NegotiateFlags.Use128BitEncryption |
  139. NegotiateFlags.Use56BitEncryption;
  140. if (AuthenticationMessageUtils.IsNTLMv1ExtendedSessionSecurity(lmChallengeResponse) ||
  141. AuthenticationMessageUtils.IsNTLMv2NTResponse(ntChallengeResponse))
  142. {
  143. authenticateMessage.NegotiateFlags |= NegotiateFlags.ExtendedSessionSecurity;
  144. }
  145. else
  146. {
  147. authenticateMessage.NegotiateFlags |= NegotiateFlags.LanManagerSessionKey;
  148. }
  149. authenticateMessage.UserName = accountNameToAuth;
  150. authenticateMessage.LmChallengeResponse = lmChallengeResponse;
  151. authenticateMessage.NtChallengeResponse = ntChallengeResponse;
  152. authenticateMessage.Version = NTLMVersion.Server2003;
  153. return authenticateMessage;
  154. }
  155. }
  156. }