NTTransactResponse.cs 5.8 KB

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