IOCtlResponse.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 IOCTL Request
  14. /// </summary>
  15. public class IOCtlResponse : SMB2Command
  16. {
  17. public const int FixedLength = 48;
  18. public const int DeclaredSize = 49;
  19. private ushort StructureSize;
  20. public ushort Reserved;
  21. public uint CtlCode;
  22. public FileID FileId;
  23. private uint InputOffset;
  24. private uint InputCount;
  25. private uint OutputOffset;
  26. private uint OutputCount;
  27. public uint Flags;
  28. public uint Reserved2;
  29. public byte[] Input = new byte[0];
  30. public byte[] Output = new byte[0];
  31. public IOCtlResponse() : base(SMB2CommandName.IOCtl)
  32. {
  33. Header.IsResponse = true;
  34. StructureSize = DeclaredSize;
  35. }
  36. public IOCtlResponse(byte[] buffer, int offset) : base(buffer, offset)
  37. {
  38. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  39. Reserved = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 2);
  40. CtlCode = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  41. FileId = new FileID(buffer, offset + SMB2Header.Length + 8);
  42. InputOffset = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 24);
  43. InputCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 28);
  44. OutputOffset = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 32);
  45. OutputCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 36);
  46. Flags = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 40);
  47. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 44);
  48. Input = ByteReader.ReadBytes(buffer, offset + (int)InputOffset, (int)InputCount);
  49. Output = ByteReader.ReadBytes(buffer, offset + (int)OutputOffset, (int)OutputCount);
  50. }
  51. public override void WriteCommandBytes(byte[] buffer, int offset)
  52. {
  53. InputOffset = 0;
  54. InputCount = (uint)Input.Length;
  55. OutputOffset = 0;
  56. OutputCount = (uint)Output.Length;
  57. if (Input.Length > 0)
  58. {
  59. InputOffset = SMB2Header.Length + FixedLength;
  60. }
  61. // MS-SMB2: the output offset MUST be set to InputOffset + InputCount rounded up to a multiple of 8
  62. int paddedInputLength = (int)Math.Ceiling((double)Input.Length / 8) * 8;
  63. if (Output.Length > 0)
  64. {
  65. OutputOffset = SMB2Header.Length + FixedLength + (uint)paddedInputLength;
  66. }
  67. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  68. LittleEndianWriter.WriteUInt16(buffer, offset + 2, Reserved);
  69. LittleEndianWriter.WriteUInt32(buffer, offset + 4, CtlCode);
  70. FileId.WriteBytes(buffer, offset + 8);
  71. LittleEndianWriter.WriteUInt32(buffer, offset + 24, InputOffset);
  72. LittleEndianWriter.WriteUInt32(buffer, offset + 28, InputCount);
  73. LittleEndianWriter.WriteUInt32(buffer, offset + 32, OutputOffset);
  74. LittleEndianWriter.WriteUInt32(buffer, offset + 36, OutputCount);
  75. LittleEndianWriter.WriteUInt32(buffer, offset + 40, Flags);
  76. LittleEndianWriter.WriteUInt32(buffer, offset + 44, Reserved2);
  77. if (Input.Length > 0)
  78. {
  79. ByteWriter.WriteBytes(buffer, offset + FixedLength, Input);
  80. }
  81. if (Output.Length > 0)
  82. {
  83. ByteWriter.WriteBytes(buffer, offset + FixedLength + paddedInputLength, Output);
  84. }
  85. }
  86. public override int CommandLength
  87. {
  88. get
  89. {
  90. int paddedInputLength = (int)Math.Ceiling((double)Input.Length / 8) * 8;
  91. return FixedLength + paddedInputLength + Output.Length;
  92. }
  93. }
  94. }
  95. }