NegotiateHelper.cs 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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;
  11. using SMBLibrary.SMB1;
  12. using Utilities;
  13. namespace SMBLibrary.Server.SMB1
  14. {
  15. /// <summary>
  16. /// Negotiate helper
  17. /// </summary>
  18. public class NegotiateHelper
  19. {
  20. internal static NegotiateResponseNTLM GetNegotiateResponse(SMB1Header header, NegotiateRequest request, INTLMAuthenticationProvider users)
  21. {
  22. NegotiateResponseNTLM response = new NegotiateResponseNTLM();
  23. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  24. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  25. response.MaxMpxCount = 50;
  26. response.MaxNumberVcs = 1;
  27. response.MaxBufferSize = 16644;
  28. response.MaxRawSize = 65536;
  29. response.Capabilities = ServerCapabilities.Unicode |
  30. ServerCapabilities.LargeFiles |
  31. ServerCapabilities.NTSMB |
  32. ServerCapabilities.NTStatusCode |
  33. ServerCapabilities.NTFind |
  34. ServerCapabilities.LargeRead |
  35. ServerCapabilities.LargeWrite;
  36. response.SystemTime = DateTime.UtcNow;
  37. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  38. ChallengeMessage challengeMessage = users.GetChallengeMessage(CreateNegotiateMessage());
  39. response.Challenge = challengeMessage.ServerChallenge;
  40. response.DomainName = String.Empty;
  41. response.ServerName = String.Empty;
  42. return response;
  43. }
  44. internal static NegotiateResponseNTLMExtended GetNegotiateResponseExtended(NegotiateRequest request, Guid serverGuid)
  45. {
  46. NegotiateResponseNTLMExtended response = new NegotiateResponseNTLMExtended();
  47. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  48. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  49. response.MaxMpxCount = 50;
  50. response.MaxNumberVcs = 1;
  51. response.MaxBufferSize = 16644;
  52. response.MaxRawSize = 65536;
  53. response.Capabilities = ServerCapabilities.Unicode |
  54. ServerCapabilities.LargeFiles |
  55. ServerCapabilities.NTSMB |
  56. ServerCapabilities.NTStatusCode |
  57. ServerCapabilities.NTFind |
  58. ServerCapabilities.LargeRead |
  59. ServerCapabilities.LargeWrite |
  60. ServerCapabilities.ExtendedSecurity;
  61. response.SystemTime = DateTime.UtcNow;
  62. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  63. response.ServerGuid = serverGuid;
  64. return response;
  65. }
  66. private static NegotiateMessage CreateNegotiateMessage()
  67. {
  68. NegotiateMessage negotiateMessage = new NegotiateMessage();
  69. negotiateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  70. NegotiateFlags.OEMEncoding |
  71. NegotiateFlags.Sign |
  72. NegotiateFlags.LanManagerKey |
  73. NegotiateFlags.NTLMKey |
  74. NegotiateFlags.AlwaysSign |
  75. NegotiateFlags.Version |
  76. NegotiateFlags.Use128BitEncryption |
  77. NegotiateFlags.Use56BitEncryption;
  78. negotiateMessage.Version = Authentication.Version.Server2003;
  79. return negotiateMessage;
  80. }
  81. }
  82. }