NegotiateHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.SMB2;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB2
  13. {
  14. /// <summary>
  15. /// Negotiate helper
  16. /// </summary>
  17. public class NegotiateHelper
  18. {
  19. public const string SMB2002Dialect = "SMB 2.002";
  20. public const string SMB2xxxDialect = "SMB 2.???";
  21. // Special case - SMB2 client initially connecting using SMB1
  22. internal static SMB2Command GetNegotiateResponse(List<string> smb2Dialects, ConnectionState state, Guid serverGuid)
  23. {
  24. NegotiateResponse response = new NegotiateResponse();
  25. response.Header.Credits = 1;
  26. if (smb2Dialects.Contains(SMB2xxxDialect))
  27. {
  28. response.DialectRevision = SMB2Dialect.SMB2xx;
  29. }
  30. else if (smb2Dialects.Contains(SMB2002Dialect))
  31. {
  32. state.ServerDialect = SMBDialect.SMB202;
  33. response.DialectRevision = SMB2Dialect.SMB202;
  34. }
  35. else
  36. {
  37. throw new ArgumentException("SMB2 dialect is not present");
  38. }
  39. response.ServerGuid = serverGuid;
  40. response.MaxTransactSize = 65536;
  41. response.MaxReadSize = 65536;
  42. response.MaxWriteSize = 65536;
  43. response.SystemTime = DateTime.Now;
  44. response.ServerStartTime = DateTime.Today;
  45. response.SecurityBuffer = GSSAPIHelper.GetGSSTokenInitNTLMSSPBytes();
  46. return response;
  47. }
  48. internal static SMB2Command GetNegotiateResponse(NegotiateRequest request, ConnectionState state, Guid serverGuid)
  49. {
  50. NegotiateResponse response = new NegotiateResponse();
  51. if (request.Dialects.Contains(SMB2Dialect.SMB210))
  52. {
  53. state.ServerDialect = SMBDialect.SMB210;
  54. response.DialectRevision = SMB2Dialect.SMB210;
  55. }
  56. else if (request.Dialects.Contains(SMB2Dialect.SMB202))
  57. {
  58. state.ServerDialect = SMBDialect.SMB202;
  59. response.DialectRevision = SMB2Dialect.SMB202;
  60. }
  61. else
  62. {
  63. return new ErrorResponse(request.CommandName, NTStatus.STATUS_NOT_SUPPORTED);
  64. }
  65. response.ServerGuid = serverGuid;
  66. response.MaxTransactSize = 65536;
  67. response.MaxReadSize = 65536;
  68. response.MaxWriteSize = 65536;
  69. response.SystemTime = DateTime.Now;
  70. response.ServerStartTime = DateTime.Today;
  71. response.SecurityBuffer = GSSAPIHelper.GetGSSTokenInitNTLMSSPBytes();
  72. return response;
  73. }
  74. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.SMB1Message message)
  75. {
  76. if (message.Commands.Count > 0 && message.Commands[0] is SMBLibrary.SMB1.NegotiateRequest)
  77. {
  78. SMBLibrary.SMB1.NegotiateRequest request = (SMBLibrary.SMB1.NegotiateRequest)message.Commands[0];
  79. return FindSMB2Dialects(request);
  80. }
  81. return new List<string>();
  82. }
  83. internal static List<string> FindSMB2Dialects(SMBLibrary.SMB1.NegotiateRequest request)
  84. {
  85. List<string> result = new List<string>();
  86. if (request.Dialects.Contains(SMB2002Dialect))
  87. {
  88. result.Add(SMB2002Dialect);
  89. }
  90. if (request.Dialects.Contains(SMB2xxxDialect))
  91. {
  92. result.Add(SMB2xxxDialect);
  93. }
  94. return result;
  95. }
  96. }
  97. }