NTTransactRequest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. /// <summary>
  14. /// SMB_COM_NT_TRANSACT Request
  15. /// </summary>
  16. public class NTTransactRequest : SMB1Command
  17. {
  18. public const int FixedSMBParametersLength = 38;
  19. // Parameters:
  20. public byte MaxSetupCount;
  21. public ushort Reserved1;
  22. public uint TotalParameterCount;
  23. public uint TotalDataCount;
  24. public uint MaxParameterCount;
  25. public uint MaxDataCount;
  26. //uint ParameterCount;
  27. //uint ParameterOffset;
  28. //uint DataCount;
  29. //uint DataOffset;
  30. //byte SetupCount; // In 2-byte words
  31. public NTTransactSubcommandName Function;
  32. public byte[] Setup;
  33. // Data:
  34. // Padding (alignment to 4 byte boundary)
  35. public byte[] TransParameters; // Trans_Parameters
  36. // Padding (alignment to 4 byte boundary)
  37. public byte[] TransData; // Trans_Data
  38. public NTTransactRequest() : base()
  39. {
  40. }
  41. public NTTransactRequest(byte[] buffer, int offset) : base(buffer, offset, false)
  42. {
  43. int readOffset = 0;
  44. MaxSetupCount = ByteReader.ReadByte(this.SMBParameters, ref readOffset);
  45. Reserved1 = LittleEndianReader.ReadUInt16(this.SMBParameters, ref readOffset);
  46. TotalParameterCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  47. TotalDataCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  48. MaxParameterCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  49. MaxDataCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  50. uint parameterCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  51. uint parameterOffset = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  52. uint dataCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  53. uint dataOffset = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  54. byte setupCount = ByteReader.ReadByte(this.SMBParameters, ref readOffset);
  55. Function = (NTTransactSubcommandName)LittleEndianReader.ReadUInt16(this.SMBParameters, ref readOffset);
  56. Setup = ByteReader.ReadBytes(this.SMBParameters, ref readOffset, setupCount * 2);
  57. TransParameters = ByteReader.ReadBytes(buffer, (int)parameterOffset, (int)parameterCount);
  58. TransData = ByteReader.ReadBytes(buffer, (int)dataOffset, (int)dataCount);
  59. }
  60. public override byte[] GetBytes(bool isUnicode)
  61. {
  62. byte setupCount = (byte)(Setup.Length / 2);
  63. uint parameterCount = (ushort)TransParameters.Length;
  64. uint dataCount = (ushort)TransData.Length;
  65. // WordCount + ByteCount are additional 3 bytes
  66. uint parameterOffset = (ushort)(SMB1Header.Length + 3 + (FixedSMBParametersLength + Setup.Length));
  67. int padding1 = (int)(4 - (parameterOffset % 4)) % 4;
  68. parameterOffset += (ushort)padding1;
  69. uint dataOffset = (ushort)(parameterOffset + parameterCount);
  70. int padding2 = (int)(4 - (dataOffset % 4)) % 4;
  71. dataOffset += (ushort)padding2;
  72. this.SMBParameters = new byte[FixedSMBParametersLength + Setup.Length];
  73. int writeOffset = 0;
  74. ByteWriter.WriteByte(this.SMBParameters, ref writeOffset, MaxSetupCount);
  75. LittleEndianWriter.WriteUInt16(this.SMBParameters, ref writeOffset, Reserved1);
  76. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, TotalParameterCount);
  77. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, TotalDataCount);
  78. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, MaxParameterCount);
  79. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, MaxDataCount);
  80. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, parameterCount);
  81. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, parameterOffset);
  82. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, dataCount);
  83. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, dataOffset);
  84. ByteWriter.WriteByte(this.SMBParameters, ref writeOffset, setupCount);
  85. LittleEndianWriter.WriteUInt16(this.SMBParameters, ref writeOffset, (ushort)Function);
  86. ByteWriter.WriteBytes(this.SMBParameters, ref writeOffset, Setup);
  87. this.SMBData = new byte[padding1 + parameterCount + padding2 + dataCount];
  88. ByteWriter.WriteBytes(this.SMBData, padding1, TransParameters);
  89. ByteWriter.WriteBytes(this.SMBData, (int)(padding1 + parameterCount + padding2), TransData);
  90. return base.GetBytes(isUnicode);
  91. }
  92. public override CommandName CommandName
  93. {
  94. get
  95. {
  96. return CommandName.SMB_COM_NT_TRANSACT;
  97. }
  98. }
  99. }
  100. }