TreeConnectAndXRequest.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_TREE_CONNECT_ANDX Request
  15. /// </summary>
  16. public class TreeConnectAndXRequest : SMBAndXCommand
  17. {
  18. public const int ParametersLength = 8;
  19. // Parameters:
  20. public TreeConnectFlags Flags;
  21. // ushort PasswordLength;
  22. // Data:
  23. public byte[] Password;
  24. // Padding
  25. public string Path; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  26. public ServiceName Service; // OEM string
  27. public TreeConnectAndXRequest()
  28. {
  29. Password = new byte[0];
  30. }
  31. public TreeConnectAndXRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  32. {
  33. int parametersOffset = 4;
  34. Flags = (TreeConnectFlags)LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
  35. ushort passwordLength = LittleEndianReader.ReadUInt16(this.SMBParameters, ref parametersOffset);
  36. int dataOffset = 0;
  37. Password = ByteReader.ReadBytes(this.SMBData, ref dataOffset, passwordLength);
  38. if (isUnicode)
  39. {
  40. // wordCount is 1 byte
  41. int padding = (1 + passwordLength) % 2;
  42. dataOffset += padding;
  43. }
  44. Path = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  45. // Should be read as OEM string but it doesn't really matter
  46. string serviceString = ByteReader.ReadNullTerminatedAnsiString(this.SMBData, ref dataOffset);
  47. Service = ServiceNameHelper.GetServiceName(serviceString);
  48. }
  49. public override byte[] GetBytes(bool isUnicode)
  50. {
  51. ushort passwordLength = (ushort)Password.Length;
  52. this.SMBParameters = new byte[ParametersLength];
  53. int parametersOffset = 4;
  54. LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, (ushort)Flags);
  55. LittleEndianWriter.WriteUInt16(this.SMBParameters, ref parametersOffset, passwordLength);
  56. string serviceString = ServiceNameHelper.GetServiceString(Service);
  57. int dataLength = Password.Length + serviceString.Length + 1;
  58. if (isUnicode)
  59. {
  60. int padding = (1 + passwordLength) % 2;
  61. dataLength += Path.Length * 2 + 2 + padding;
  62. }
  63. else
  64. {
  65. dataLength += Path.Length + 1;
  66. }
  67. this.SMBData = new byte[dataLength];
  68. int dataOffset = 0;
  69. ByteWriter.WriteBytes(this.SMBData, ref dataOffset, Password);
  70. if (isUnicode)
  71. {
  72. // wordCount is 1 byte
  73. int padding = (1 + passwordLength) % 2;
  74. dataOffset += padding;
  75. }
  76. SMB1Helper.WriteSMBString(this.SMBData, ref dataOffset, isUnicode, Path);
  77. ByteWriter.WriteNullTerminatedAnsiString(this.SMBData, ref dataOffset, serviceString);
  78. return base.GetBytes(isUnicode);
  79. }
  80. public override CommandName CommandName
  81. {
  82. get
  83. {
  84. return CommandName.SMB_COM_TREE_CONNECT_ANDX;
  85. }
  86. }
  87. }
  88. }