NegotiateHelper.cs 6.7 KB

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