IOCtlRequest.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 IOCtlRequest : SMB2Command
  16. {
  17. public const int FixedLength = 56;
  18. public const int DeclaredSize = 57;
  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. public uint MaxInputResponse;
  26. private uint OutputOffset;
  27. private uint OutputCount;
  28. public uint MaxOutputResponse;
  29. public IOCtlRequestFlags Flags;
  30. public uint Reserved2;
  31. public byte[] Input = new byte[0];
  32. public byte[] Output = new byte[0];
  33. public IOCtlRequest() : base(SMB2CommandName.IOCtl)
  34. {
  35. StructureSize = DeclaredSize;
  36. }
  37. public IOCtlRequest(byte[] buffer, int offset) : base(buffer, offset)
  38. {
  39. StructureSize = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 0);
  40. Reserved = LittleEndianConverter.ToUInt16(buffer, offset + SMB2Header.Length + 2);
  41. CtlCode = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 4);
  42. FileId = new FileID(buffer, offset + SMB2Header.Length + 8);
  43. InputOffset = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 24);
  44. InputCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 28);
  45. MaxInputResponse = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 32);
  46. OutputOffset = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 36);
  47. OutputCount = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 40);
  48. MaxOutputResponse = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 44);
  49. Flags = (IOCtlRequestFlags)LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 48);
  50. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + SMB2Header.Length + 52);
  51. Input = ByteReader.ReadBytes(buffer, offset + (int)InputOffset, (int)InputCount);
  52. Output = ByteReader.ReadBytes(buffer, offset + (int)OutputOffset, (int)OutputCount);
  53. }
  54. public override void WriteCommandBytes(byte[] buffer, int offset)
  55. {
  56. InputOffset = 0;
  57. InputCount = (uint)Input.Length;
  58. OutputOffset = 0;
  59. OutputCount = (uint)Output.Length;
  60. if (Input.Length > 0)
  61. {
  62. InputOffset = SMB2Header.Length + FixedLength;
  63. }
  64. if (Output.Length > 0)
  65. {
  66. OutputOffset = SMB2Header.Length + FixedLength + (uint)Input.Length;
  67. }
  68. LittleEndianWriter.WriteUInt16(buffer, offset + 0, StructureSize);
  69. LittleEndianWriter.WriteUInt16(buffer, offset + 2, Reserved);
  70. LittleEndianWriter.WriteUInt32(buffer, offset + 4, CtlCode);
  71. FileId.WriteBytes(buffer, offset + 8);
  72. LittleEndianWriter.WriteUInt32(buffer, offset + 24, InputOffset);
  73. LittleEndianWriter.WriteUInt32(buffer, offset + 28, InputCount);
  74. LittleEndianWriter.WriteUInt32(buffer, offset + 32, MaxInputResponse);
  75. LittleEndianWriter.WriteUInt32(buffer, offset + 36, OutputOffset);
  76. LittleEndianWriter.WriteUInt32(buffer, offset + 40, OutputCount);
  77. LittleEndianWriter.WriteUInt32(buffer, offset + 44, MaxOutputResponse);
  78. LittleEndianWriter.WriteUInt32(buffer, offset + 48, (uint)Flags);
  79. LittleEndianWriter.WriteUInt32(buffer, offset + 52, Reserved2);
  80. if (Input.Length > 0)
  81. {
  82. ByteWriter.WriteBytes(buffer, offset + FixedLength, Input);
  83. }
  84. if (Output.Length > 0)
  85. {
  86. ByteWriter.WriteBytes(buffer, offset + FixedLength + Input.Length, Output);
  87. }
  88. }
  89. public bool IsFSCtl
  90. {
  91. get
  92. {
  93. return (Flags & IOCtlRequestFlags.IsFSCtl) > 0;
  94. }
  95. set
  96. {
  97. if (value)
  98. {
  99. Flags |= IOCtlRequestFlags.IsFSCtl;
  100. }
  101. else
  102. {
  103. Flags &= ~IOCtlRequestFlags.IsFSCtl;
  104. }
  105. }
  106. }
  107. public override int CommandLength
  108. {
  109. get
  110. {
  111. return FixedLength + Input.Length + Output.Length;
  112. }
  113. }
  114. }
  115. }