SessionSetupHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Copyright (C) 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 SMBLibrary.Authentication;
  10. using SMBLibrary.Authentication.NTLM;
  11. using SMBLibrary.SMB2;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB2
  14. {
  15. /// <summary>
  16. /// Session Setup helper
  17. /// </summary>
  18. public class SessionSetupHelper
  19. {
  20. internal static SMB2Command GetSessionSetupResponse(SessionSetupRequest request, INTLMAuthenticationProvider users, SMB2ConnectionState state)
  21. {
  22. // [MS-SMB2] Windows [..] will also accept raw Kerberos messages and implicit NTLM messages as part of GSS authentication.
  23. SessionSetupResponse response = new SessionSetupResponse();
  24. byte[] messageBytes = request.SecurityBuffer;
  25. bool isRawMessage = true;
  26. if (!AuthenticationMessageUtils.IsSignatureValid(messageBytes))
  27. {
  28. messageBytes = GSSAPIHelper.GetNTLMSSPMessage(request.SecurityBuffer);
  29. isRawMessage = false;
  30. }
  31. if (!AuthenticationMessageUtils.IsSignatureValid(messageBytes))
  32. {
  33. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  34. }
  35. // According to [MS-SMB2] 3.3.5.5.3, response.Header.SessionID must be allocated if the server returns STATUS_MORE_PROCESSING_REQUIRED
  36. if (request.Header.SessionID == 0)
  37. {
  38. ulong? sessionID = state.AllocateSessionID();
  39. if (!sessionID.HasValue)
  40. {
  41. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_SESSIONS);
  42. }
  43. response.Header.SessionID = sessionID.Value;
  44. }
  45. MessageTypeName messageType = AuthenticationMessageUtils.GetMessageType(messageBytes);
  46. if (messageType == MessageTypeName.Negotiate)
  47. {
  48. NegotiateMessage negotiateMessage = new NegotiateMessage(messageBytes);
  49. ChallengeMessage challengeMessage = users.GetChallengeMessage(negotiateMessage);
  50. if (isRawMessage)
  51. {
  52. response.SecurityBuffer = challengeMessage.GetBytes();
  53. }
  54. else
  55. {
  56. response.SecurityBuffer = GSSAPIHelper.GetGSSTokenResponseBytesFromNTLMSSPMessage(challengeMessage.GetBytes());
  57. }
  58. response.Header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  59. }
  60. else // MessageTypeName.Authenticate
  61. {
  62. AuthenticateMessage authenticateMessage = new AuthenticateMessage(messageBytes);
  63. bool loginSuccess;
  64. try
  65. {
  66. loginSuccess = users.Authenticate(authenticateMessage);
  67. }
  68. catch (EmptyPasswordNotAllowedException)
  69. {
  70. state.LogToServer(Severity.Information, "User '{0}' authentication using an empty password was rejected", authenticateMessage.UserName);
  71. return new ErrorResponse(request.CommandName, NTStatus.STATUS_ACCOUNT_RESTRICTION);
  72. }
  73. if (loginSuccess)
  74. {
  75. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully", authenticateMessage.UserName);
  76. state.CreateSession(request.Header.SessionID, authenticateMessage.UserName, authenticateMessage.WorkStation);
  77. }
  78. else if (users.FallbackToGuest(authenticateMessage.UserName))
  79. {
  80. state.LogToServer(Severity.Information, "User '{0}' failed authentication. logged in as guest", authenticateMessage.UserName);
  81. state.CreateSession(request.Header.SessionID, "Guest", authenticateMessage.WorkStation);
  82. response.SessionFlags = SessionFlags.IsGuest;
  83. }
  84. else
  85. {
  86. state.LogToServer(Severity.Information, "User '{0}' failed authentication", authenticateMessage.UserName);
  87. return new ErrorResponse(request.CommandName, NTStatus.STATUS_LOGON_FAILURE);
  88. }
  89. if (!isRawMessage)
  90. {
  91. response.SecurityBuffer = GSSAPIHelper.GetGSSTokenAcceptCompletedResponse();
  92. }
  93. }
  94. return response;
  95. }
  96. }
  97. }