NegotiateRequest.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Utilities;
  10. namespace SMBLibrary.SMB2
  11. {
  12. /// <summary>
  13. /// SMB2 NEGOTIATE Request
  14. /// </summary>
  15. public class NegotiateRequest : SMB2Command
  16. {
  17. public const int DeclaredSize = 36;
  18. private ushort StructureSize;
  19. // ushort DialectCount;
  20. public SecurityMode SecurityMode;
  21. public ushort Reserved;
  22. public Capabilities Capabilities; // If the client does not implements the SMB 3.x dialect family, this field MUST be set to 0.
  23. public Guid ClientGuid;
  24. public DateTime ClientStartTime;
  25. public List<SMB2Dialect> Dialects = new List<SMB2Dialect>();
  26. public NegotiateRequest() : base(SMB2CommandName.Negotiate)
  27. {
  28. StructureSize = DeclaredSize;
  29. }
  30. public NegotiateRequest(byte[] buffer, int offset) : base(buffer, offset)
  31. {
  32. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  33. ushort dialectCount = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 2);
  34. SecurityMode = (SecurityMode)LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 4);
  35. Reserved = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 6);
  36. Capabilities = (Capabilities)LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 8);
  37. ClientGuid = LittleEndianConverter.ToGuid(buffer, offset + SMB2Header.Length + 12);
  38. ClientStartTime = DateTime.FromFileTimeUtc(LittleEndianConverter.ToInt64(buffer, offset + SMB2Header.Length + 28));
  39. for (int index = 0; index < dialectCount; index++)
  40. {
  41. SMB2Dialect dialect = (SMB2Dialect)LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 36 + index * 2);
  42. Dialects.Add(dialect);
  43. }
  44. }
  45. public override void WriteCommandBytes(byte[] buffer, int offset)
  46. {
  47. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  48. LittleEndianWriter.WriteUInt16(buffer, offset + 2, (ushort)Dialects.Count);
  49. LittleEndianWriter.WriteUInt16(buffer, offset + 4, (ushort)SecurityMode);
  50. LittleEndianWriter.WriteUInt16(buffer, offset + 6, Reserved);
  51. LittleEndianWriter.WriteUInt32(buffer, offset + 8, (uint)Capabilities);
  52. LittleEndianWriter.WriteGuidBytes(buffer, offset + 12, ClientGuid);
  53. LittleEndianWriter.WriteInt64(buffer, offset + 28, ClientStartTime.ToFileTimeUtc());
  54. for (int index = 0; index < Dialects.Count; index++)
  55. {
  56. SMB2Dialect dialect = Dialects[index];
  57. LittleEndianWriter.WriteUInt16(buffer, offset + 36 + index * 2, (ushort)dialect);
  58. }
  59. }
  60. public override int CommandLength
  61. {
  62. get
  63. {
  64. return 36 + Dialects.Count * 2;
  65. }
  66. }
  67. }
  68. }