SessionSetupHelper.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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.GSSAPI;
  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, GSSProvider securityProvider, 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[] outputToken;
  25. NTStatus status = securityProvider.AcceptSecurityContext(ref state.AuthenticationContext, request.SecurityBuffer, out outputToken);
  26. if (status != NTStatus.STATUS_SUCCESS && status != NTStatus.SEC_I_CONTINUE_NEEDED)
  27. {
  28. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  29. state.LogToServer(Severity.Information, "User '{0}' failed authentication, NTStatus: {1}", userName, status);
  30. return new ErrorResponse(request.CommandName, status);
  31. }
  32. if (outputToken != null)
  33. {
  34. response.SecurityBuffer = outputToken;
  35. }
  36. // According to [MS-SMB2] 3.3.5.5.3, response.Header.SessionID must be allocated if the server returns STATUS_MORE_PROCESSING_REQUIRED
  37. if (request.Header.SessionID == 0)
  38. {
  39. ulong? sessionID = state.AllocateSessionID();
  40. if (!sessionID.HasValue)
  41. {
  42. return new ErrorResponse(request.CommandName, NTStatus.STATUS_TOO_MANY_SESSIONS);
  43. }
  44. response.Header.SessionID = sessionID.Value;
  45. }
  46. if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  47. {
  48. response.Header.Status = NTStatus.STATUS_MORE_PROCESSING_REQUIRED;
  49. }
  50. else // status == STATUS_SUCCESS
  51. {
  52. string userName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.UserName) as string;
  53. string machineName = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.MachineName) as string;
  54. bool? isGuest = securityProvider.GetContextAttribute(state.AuthenticationContext, GSSAttributeName.IsGuest) as bool?;
  55. if (!isGuest.HasValue || !isGuest.Value)
  56. {
  57. state.LogToServer(Severity.Information, "User '{0}' authenticated successfully.", userName);
  58. state.CreateSession(request.Header.SessionID, userName, machineName);
  59. }
  60. else
  61. {
  62. state.LogToServer(Severity.Information, "User '{0}' failed authentication, logged in as guest.", userName);
  63. state.CreateSession(request.Header.SessionID, "Guest", machineName);
  64. response.SessionFlags = SessionFlags.IsGuest;
  65. }
  66. }
  67. return response;
  68. }
  69. }
  70. }