SessionSetupRequest.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 SESSION_SETUP Request
  14. /// </summary>
  15. public class SessionSetupRequest : SMB2Command
  16. {
  17. public const int FixedSize = 24;
  18. public const int DeclaredSize = 25;
  19. private ushort StructureSize;
  20. public SessionSetupFlags Flags;
  21. public SecurityMode SecurityMode;
  22. public Capabilities Capabilities; // Values other than SMB2_GLOBAL_CAP_DFS should be treated as reserved.
  23. public uint Channel;
  24. private ushort SecurityBufferOffset;
  25. private ushort SecurityBufferLength;
  26. public ulong PreviousSessionId;
  27. public byte[] SecurityBuffer = new byte[0];
  28. public SessionSetupRequest() : base(SMB2CommandName.SessionSetup)
  29. {
  30. StructureSize = DeclaredSize;
  31. }
  32. public SessionSetupRequest(byte[] buffer, int offset) : base(buffer, offset)
  33. {
  34. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  35. Flags = (SessionSetupFlags)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 2);
  36. SecurityMode = (SecurityMode)ByteReader.ReadByte(buffer, offset + SMB2Header.Length + 3);
  37. Capabilities = (Capabilities)LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  38. Channel = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 8);
  39. SecurityBufferOffset = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 12);
  40. SecurityBufferLength = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 14);
  41. PreviousSessionId = LittleEndianConverter.ToUInt64(buffer, offset + SMB2Header.Length + 16);
  42. if (SecurityBufferLength > 0)
  43. {
  44. SecurityBuffer = ByteReader.ReadBytes(buffer, offset + SecurityBufferOffset, SecurityBufferLength);
  45. }
  46. }
  47. public override void WriteCommandBytes(byte[] buffer, int offset)
  48. {
  49. SecurityBufferOffset = 0;
  50. SecurityBufferLength = (ushort)SecurityBuffer.Length;
  51. if (SecurityBuffer.Length > 0)
  52. {
  53. SecurityBufferOffset = SMB2Header.Length + FixedSize;
  54. }
  55. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  56. ByteWriter.WriteByte(buffer, offset + 2, (byte)Flags);
  57. ByteWriter.WriteByte(buffer, offset + 3, (byte)SecurityMode);
  58. LittleEndianWriter.WriteUInt32(buffer, offset + 4, (uint)Capabilities);
  59. LittleEndianWriter.WriteUInt32(buffer, offset + 8, Channel);
  60. LittleEndianWriter.WriteUInt16(buffer, offset + 12, SecurityBufferOffset);
  61. LittleEndianWriter.WriteUInt16(buffer, offset + 14, SecurityBufferLength);
  62. LittleEndianWriter.WriteUInt64(buffer, offset + 16, PreviousSessionId);
  63. ByteWriter.WriteBytes(buffer, offset + FixedSize, SecurityBuffer);
  64. }
  65. public override int CommandLength
  66. {
  67. get
  68. {
  69. return FixedSize + SecurityBuffer.Length;
  70. }
  71. }
  72. }
  73. }