NegotiateHelper.cs 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.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, INTLMAuthenticationProvider users)
  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. ChallengeMessage challengeMessage = users.GetChallengeMessage(CreateNegotiateMessage());
  40. response.Challenge = challengeMessage.ServerChallenge;
  41. response.DomainName = String.Empty;
  42. response.ServerName = String.Empty;
  43. return response;
  44. }
  45. internal static NegotiateResponseNTLMExtended GetNegotiateResponseExtended(NegotiateRequest request, Guid serverGuid)
  46. {
  47. NegotiateResponseNTLMExtended response = new NegotiateResponseNTLMExtended();
  48. response.DialectIndex = (ushort)request.Dialects.IndexOf(SMBServer.NTLanManagerDialect);
  49. response.SecurityMode = SecurityMode.UserSecurityMode | SecurityMode.EncryptPasswords;
  50. response.MaxMpxCount = 50;
  51. response.MaxNumberVcs = 1;
  52. response.MaxBufferSize = 16644;
  53. response.MaxRawSize = 65536;
  54. response.Capabilities = ServerCapabilities.Unicode |
  55. ServerCapabilities.LargeFiles |
  56. ServerCapabilities.NTSMB |
  57. ServerCapabilities.NTStatusCode |
  58. ServerCapabilities.NTFind |
  59. ServerCapabilities.LargeRead |
  60. ServerCapabilities.LargeWrite |
  61. ServerCapabilities.ExtendedSecurity;
  62. response.SystemTime = DateTime.UtcNow;
  63. response.ServerTimeZone = (short)-TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).TotalMinutes;
  64. response.ServerGuid = serverGuid;
  65. return response;
  66. }
  67. private static NegotiateMessage CreateNegotiateMessage()
  68. {
  69. NegotiateMessage negotiateMessage = new NegotiateMessage();
  70. negotiateMessage.NegotiateFlags = NegotiateFlags.UnicodeEncoding |
  71. NegotiateFlags.OEMEncoding |
  72. NegotiateFlags.Sign |
  73. NegotiateFlags.LanManagerKey |
  74. NegotiateFlags.NTLMKey |
  75. NegotiateFlags.AlwaysSign |
  76. NegotiateFlags.Version |
  77. NegotiateFlags.Use128BitEncryption |
  78. NegotiateFlags.Use56BitEncryption;
  79. negotiateMessage.Version = NTLMVersion.Server2003;
  80. return negotiateMessage;
  81. }
  82. }
  83. }