NegotiateHelper.cs 4.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. public class NegotiateHelper
  20. {
  21. internal static NegotiateResponseNTLM GetNegotiateResponse(SMB1Header header, NegotiateRequest request, GSSProvider securityProvider, ConnectionState state)
  22. {
  23. NegotiateResponseNTLM response = new NegotiateResponseNTLM();
  24. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  25. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  26. response.MaxMpxCount = 50;
  27. response.MaxNumberVcs = 1;
  28. response.MaxBufferSize = 16644;
  29. response.MaxRawSize = 65536;
  30. response.Capabilities = ServerCapabilities.Unicode |
  31. ServerCapabilities.LargeFiles |
  32. ServerCapabilities.NTSMB |
  33. ServerCapabilities.NTStatusCode |
  34. ServerCapabilities.NTFind |
  35. ServerCapabilities.LargeRead |
  36. ServerCapabilities.LargeWrite;
  37. response.SystemTime = DateTime.UtcNow;
  38. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  39. NegotiateMessage negotiateMessage = CreateNegotiateMessage();
  40. ChallengeMessage challengeMessage;
  41. NTStatus status = securityProvider.GetNTLMChallengeMessage(out state.AuthenticationContext, negotiateMessage, out challengeMessage);
  42. if (status == NTStatus.SEC_I_CONTINUE_NEEDED)
  43. {
  44. response.Challenge = challengeMessage.ServerChallenge;
  45. }
  46. response.DomainName = String.Empty;
  47. response.ServerName = String.Empty;
  48. return response;
  49. }
  50. internal static NegotiateResponseNTLMExtended GetNegotiateResponseExtended(NegotiateRequest request, Guid serverGuid)
  51. {
  52. NegotiateResponseNTLMExtended response = new NegotiateResponseNTLMExtended();
  53. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  54. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  55. response.MaxMpxCount = 50;
  56. response.MaxNumberVcs = 1;
  57. response.MaxBufferSize = 16644;
  58. response.MaxRawSize = 65536;
  59. response.Capabilities = ServerCapabilities.Unicode |
  60. ServerCapabilities.LargeFiles |
  61. ServerCapabilities.NTSMB |
  62. ServerCapabilities.NTStatusCode |
  63. ServerCapabilities.NTFind |
  64. ServerCapabilities.LargeRead |
  65. ServerCapabilities.LargeWrite |
  66. ServerCapabilities.ExtendedSecurity;
  67. response.SystemTime = DateTime.UtcNow;
  68. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  69. response.ServerGuid = serverGuid;
  70. return response;
  71. }
  72. private static NegotiateMessage CreateNegotiateMessage()
  73. {
  74. NegotiateMessage negotiateMessage = new NegotiateMessage();
  75. negotiateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  76. NegotiateFlags.OEMEncoding |
  77. NegotiateFlags.Sign |
  78. NegotiateFlags.LanManagerKey |
  79. NegotiateFlags.NTLMKey |
  80. NegotiateFlags.AlwaysSign |
  81. NegotiateFlags.Version |
  82. NegotiateFlags.Use128BitEncryption |
  83. NegotiateFlags.Use56BitEncryption;
  84. negotiateMessage.Version = NTLMVersion.Server2003;
  85. return negotiateMessage;
  86. }
  87. }
  88. }