NTTransactSecondaryRequest.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* Copyright (C) 2014 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_SECONDARY Request
  15. /// </summary>
  16. public class NTTransactSecondaryRequest : SMB1Command
  17. {
  18. public const int SMBParametersLength = 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. public byte Reserved2;
  30. // Data:
  31. // Padding (alignment to 4 byte boundary)
  32. public byte[] TransParameters; // Trans_Parameters
  33. // Padding (alignment to 4 byte boundary)
  34. public byte[] TransData; // Trans_Data
  35. public NTTransactSecondaryRequest() : base()
  36. {
  37. Reserved1 = new byte[3];
  38. }
  39. public NTTransactSecondaryRequest(byte[] buffer, int offset) : base(buffer, offset, false)
  40. {
  41. int readOffset = 0;
  42. Reserved1 = ByteReader.ReadBytes(this.SMBParameters, ref readOffset, 3);
  43. TotalParameterCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  44. TotalDataCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  45. uint parameterCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  46. uint parameterOffset = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  47. ParameterDisplacement = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  48. uint dataCount = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  49. uint dataOffset = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  50. DataDisplacement = LittleEndianReader.ReadUInt32(this.SMBParameters, ref readOffset);
  51. Reserved2 = ByteReader.ReadByte(this.SMBParameters, ref readOffset);
  52. TransParameters = ByteReader.ReadBytes(buffer, (int)parameterOffset, (int)parameterCount);
  53. TransData = ByteReader.ReadBytes(buffer, (int)dataOffset, (int)dataCount);
  54. }
  55. public override byte[] GetBytes(bool isUnicode)
  56. {
  57. uint parameterCount = (ushort)TransParameters.Length;
  58. uint dataCount = (ushort)TransData.Length;
  59. // WordCount + ByteCount are additional 3 bytes
  60. uint parameterOffset = (ushort)(SMB1Header.Length + 3 + (SMBParametersLength));
  61. int padding1 = (int)(4 - (parameterOffset % 4)) % 4;
  62. parameterOffset += (ushort)padding1;
  63. uint dataOffset = (ushort)(parameterOffset + parameterCount);
  64. int padding2 = (int)(4 - (dataOffset % 4)) % 4;
  65. dataOffset += (ushort)padding2;
  66. this.SMBParameters = new byte[SMBParametersLength];
  67. int writeOffset = 0;
  68. ByteWriter.WriteBytes(this.SMBParameters, ref writeOffset, Reserved1, 3);
  69. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, TotalParameterCount);
  70. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, TotalDataCount);
  71. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, parameterCount);
  72. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, parameterOffset);
  73. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, ParameterDisplacement);
  74. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, dataCount);
  75. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, dataOffset);
  76. LittleEndianWriter.WriteUInt32(this.SMBParameters, ref writeOffset, DataDisplacement);
  77. ByteWriter.WriteByte(this.SMBParameters, ref writeOffset, Reserved2);
  78. this.SMBData = new byte[parameterCount + dataCount + padding1 + padding2];
  79. ByteWriter.WriteBytes(this.SMBData, padding1, TransParameters);
  80. ByteWriter.WriteBytes(this.SMBData, (int)(padding1 + parameterCount + padding2), TransData);
  81. return base.GetBytes(isUnicode);
  82. }
  83. public override CommandName CommandName
  84. {
  85. get
  86. {
  87. return CommandName.SMB_COM_NT_TRANSACT_SECONDARY;
  88. }
  89. }
  90. }
  91. }