NegotiateHelper.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 2017-2020 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.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. /// <summary>
  15. /// Negotiate helper
  16. /// </summary>
  17. internal class NegotiateHelper
  18. {
  19. public const string SMB2002Dialect = "SMB 2.002";
  20. public const string SMB2xxxDialect = "SMB 2.???";
  21. public const uint ServerMaxTransactSize = 65536;
  22. public const uint ServerMaxReadSize = 65536;
  23. public const uint ServerMaxWriteSize = 65536;
  24. public const uint ServerMaxTransactSizeLargeMTU = 8388608;
  25. public const uint ServerMaxReadSizeLargeMTU = 8388608;
  26. public const uint ServerMaxWriteSizeLargeMTU = 8388608;
  27. // Special case - SMB2 client initially connecting using SMB1
  28. internal static SMB2Command GetNegotiateResponse(List<string> smb2Dialects, GSSProvider securityProvider, ConnectionState state, SMBTransportType transportType, Guid serverGuid, DateTime serverStartTime)
  29. {
  30. NegotiateResponse response = new NegotiateResponse();
  31. response.Header.Credits = 1;
  32. if (smb2Dialects.Contains(SMB2xxxDialect))
  33. {
  34. response.DialectRevision = SMB2Dialect.SMB2xx;
  35. }
  36. else if (smb2Dialects.Contains(SMB2002Dialect))
  37. {
  38. state.Dialect = SMBDialect.SMB202;
  39. response.DialectRevision = SMB2Dialect.SMB202;
  40. }
  41. else
  42. {
  43. throw new ArgumentException("SMB2 dialect is not present");
  44. }
  45. response.SecurityMode = SecurityMode.SigningEnabled;
  46. response.ServerGuid = serverGuid;
  47. if (state.Dialect != SMBDialect.SMB202 && transportType == SMBTransportType.DirectTCPTransport)
  48. {
  49. response.Capabilities = Capabilities.LargeMTU;
  50. response.MaxTransactSize = ServerMaxTransactSizeLargeMTU;
  51. response.MaxReadSize = ServerMaxReadSizeLargeMTU;
  52. response.MaxWriteSize = ServerMaxWriteSizeLargeMTU;
  53. }
  54. else
  55. {
  56. response.MaxTransactSize = ServerMaxTransactSize;
  57. response.MaxReadSize = ServerMaxReadSize;
  58. response.MaxWriteSize = ServerMaxWriteSize;
  59. }
  60. response.SystemTime = DateTime.Now;
  61. response.ServerStartTime = serverStartTime;
  62. response.SecurityBuffer = securityProvider.GetSPNEGOTokenInitBytes();
  63. return response;
  64. }
  65. internal static SMB2Command GetNegotiateResponse(NegotiateRequest request, GSSProvider securityProvider, ConnectionState state, SMBTransportType transportType, Guid serverGuid, DateTime serverStartTime)
  66. {
  67. NegotiateResponse response = new NegotiateResponse();
  68. if (request.Dialects.Contains(SMB2Dialect.SMB210))
  69. {
  70. state.Dialect = SMBDialect.SMB210;
  71. response.DialectRevision = SMB2Dialect.SMB210;
  72. }
  73. else if (request.Dialects.Contains(SMB2Dialect.SMB202))
  74. {
  75. state.Dialect = SMBDialect.SMB202;
  76. response.DialectRevision = SMB2Dialect.SMB202;
  77. }
  78. else
  79. {
  80. state.LogToServer(Severity.Verbose, "Negotiate failure: None of the requested SMB2 dialects is supported");
  81. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  82. }
  83. response.SecurityMode = SecurityMode.SigningEnabled;
  84. response.ServerGuid = serverGuid;
  85. if (state.Dialect != SMBDialect.SMB202 && transportType == SMBTransportType.DirectTCPTransport)
  86. {
  87. response.Capabilities = Capabilities.LargeMTU;
  88. response.MaxTransactSize = ServerMaxTransactSizeLargeMTU;
  89. response.MaxReadSize = ServerMaxReadSizeLargeMTU;
  90. response.MaxWriteSize = ServerMaxWriteSizeLargeMTU;
  91. }
  92. else
  93. {
  94. response.MaxTransactSize = ServerMaxTransactSize;
  95. response.MaxReadSize = ServerMaxReadSize;
  96. response.MaxWriteSize = ServerMaxWriteSize;
  97. }
  98. response.SystemTime = DateTime.Now;
  99. response.ServerStartTime = serverStartTime;
  100. response.SecurityBuffer = securityProvider.GetSPNEGOTokenInitBytes();
  101. return response;
  102. }
  103. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.SMB1Message message)
  104. {
  105. if (message.Commands.Count > 0 && message.Commands[0] is SMBLibrary.SMB1.NegotiateRequest)
  106. {
  107. SMBLibrary.SMB1.NegotiateRequest request = (SMBLibrary.SMB1.NegotiateRequest)message.Commands[0];
  108. return FindSMB2Dialects(request);
  109. }
  110. return new List<string>();
  111. }
  112. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.NegotiateRequest request)
  113. {
  114. List<string> result = new List<string>();
  115. if (request.Dialects.Contains(SMB2002Dialect))
  116. {
  117. result.Add(SMB2002Dialect);
  118. }
  119. if (request.Dialects.Contains(SMB2xxxDialect))
  120. {
  121. result.Add(SMB2xxxDialect);
  122. }
  123. return result;
  124. }
  125. }
  126. }