SessionSetupAndXResponse.cs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_SESSION_SETUP_ANDX Response
  15. /// </summary>
  16. public class SessionSetupAndXResponse : SMBAndXCommand
  17. {
  18. public const int ParametersLength = 6;
  19. // Parameters:
  20. // CommandName AndXCommand;
  21. // byte AndXReserved;
  22. // ushort AndXOffset;
  23. public SessionSetupAction Action;
  24. // Data:
  25. public string NativeOS; // 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 string NativeLanMan; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  27. public string PrimaryDomain; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  28. public SessionSetupAndXResponse() : base()
  29. {
  30. NativeOS = String.Empty;
  31. NativeLanMan = String.Empty;
  32. PrimaryDomain = String.Empty;
  33. }
  34. public SessionSetupAndXResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  35. {
  36. Action = (SessionSetupAction)LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  37. int dataOffset = 0;
  38. if (isUnicode)
  39. {
  40. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  41. // Note: SMBData starts at an odd offset.
  42. dataOffset++;
  43. }
  44. NativeOS = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  45. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  46. if ((this.SMBData.Length - dataOffset) % 2 == 1)
  47. {
  48. // Workaround for a single terminating null byte
  49. this.SMBData = ByteUtils.Concatenate(this.SMBData, new byte[1]);
  50. }
  51. PrimaryDomain = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  52. }
  53. public override byte[] GetBytes(bool isUnicode)
  54. {
  55. this.SMBParameters = new byte[ParametersLength];
  56. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, (ushort)Action);
  57. int offset = 0;
  58. if (isUnicode)
  59. {
  60. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  61. // Note: SMBData starts at an odd offset.
  62. int padding = 1;
  63. this.SMBData = new byte[padding + NativeOS.Length * 2 + NativeLanMan.Length * 2 + PrimaryDomain.Length * 2 + 6];
  64. offset = padding;
  65. }
  66. else
  67. {
  68. this.SMBData = new byte[NativeOS.Length + NativeLanMan.Length + PrimaryDomain.Length + 3];
  69. }
  70. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  71. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  72. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, PrimaryDomain);
  73. return base.GetBytes(isUnicode);
  74. }
  75. public override CommandName CommandName
  76. {
  77. get
  78. {
  79. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  80. }
  81. }
  82. }
  83. }