NegotiateHelper.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright (C) 2014-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 System.Text;
  10. using SMBLibrary.Authentication.GSSAPI;
  11. using SMBLibrary.Authentication.NTLM;
  12. using SMBLibrary.SMB1;
  13. using Utilities;
  14. namespace SMBLibrary.Server.SMB1
  15. {
  16. /// <summary>
  17. /// Negotiate helper
  18. /// </summary>
  19. internal class NegotiateHelper
  20. {
  21. public const ushort ServerMaxMpxCount = 50;
  22. public const ushort ServerNumberVcs = 1;
  23. public const ushort ServerMaxBufferSize = 65535;
  24. public const uint ServerMaxRawSize = 65536;
  25. internal static NegotiateResponse GetNegotiateResponse(SMB1Header header, NegotiateRequest request, GSSProvider securityProvider, ConnectionState state)
  26. {
  27. NegotiateResponse response = new NegotiateResponse();
  28. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  29. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  30. response.MaxMpxCount = ServerMaxMpxCount;
  31. response.MaxNumberVcs = ServerNumberVcs;
  32. response.MaxBufferSize = ServerMaxBufferSize;
  33. response.MaxRawSize = ServerMaxRawSize;
  34. response.Capabilities = Capabilities.Unicode |
  35. Capabilities.LargeFiles |
  36. Capabilities.NTSMB |
  37. Capabilities.RpcRemoteApi |
  38. Capabilities.NTStatusCode |
  39. Capabilities.NTFind |
  40. Capabilities.InfoLevelPassthrough |
  41. Capabilities.LargeRead |
  42. Capabilities.LargeWrite;
  43. response.SystemTime = DateTime.UtcNow;
  44. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  45. NegotiateMessage negotiateMessage = CreateNegotiateMessage();
  46. ChallengeMessage challengeMessage;
  47. NTStatus status = securityProvider.GetNTLMChallengeMessage(out state.AuthenticationContext, negotiateMessage, out challengeMessage);
  48. if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  49. {
  50. response.Challenge = challengeMessage.ServerChallenge;
  51. }
  52. response.DomainName = String.Empty;
  53. response.ServerName = String.Empty;
  54. return response;
  55. }
  56. internal static NegotiateResponseExtended GetNegotiateResponseExtended(NegotiateRequest request, Guid serverGuid)
  57. {
  58. NegotiateResponseExtended response = new NegotiateResponseExtended();
  59. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  60. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  61. response.MaxMpxCount = ServerMaxMpxCount;
  62. response.MaxNumberVcs = ServerNumberVcs;
  63. response.MaxBufferSize = ServerMaxBufferSize;
  64. response.MaxRawSize = ServerMaxRawSize;
  65. response.Capabilities = Capabilities.Unicode |
  66. Capabilities.LargeFiles |
  67. Capabilities.NTSMB |
  68. Capabilities.RpcRemoteApi |
  69. Capabilities.NTStatusCode |
  70. Capabilities.NTFind |
  71. Capabilities.InfoLevelPassthrough |
  72. Capabilities.LargeRead |
  73. Capabilities.LargeWrite |
  74. Capabilities.ExtendedSecurity;
  75. response.SystemTime = DateTime.UtcNow;
  76. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  77. response.ServerGuid = serverGuid;
  78. return response;
  79. }
  80. private static NegotiateMessage CreateNegotiateMessage()
  81. {
  82. NegotiateMessage negotiateMessage = new NegotiateMessage();
  83. negotiateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  84. NegotiateFlags.OEMEncoding |
  85. NegotiateFlags.Sign |
  86. NegotiateFlags.LanManagerSessionKey |
  87. NegotiateFlags.NTLMSessionSecurity |
  88. NegotiateFlags.AlwaysSign |
  89. NegotiateFlags.Version |
  90. NegotiateFlags.Use128BitEncryption |
  91. NegotiateFlags.Use56BitEncryption;
  92. negotiateMessage.Version = NTLMVersion.Server2003;
  93. return negotiateMessage;
  94. }
  95. }
  96. }