SessionSetupAndXResponse.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. public class SessionSetupAndXResponse : SMBAndXCommand
  14. {
  15. public const int ParametersLength = 6;
  16. // Parameters:
  17. //CommandName AndXCommand;
  18. //byte AndXReserved;
  19. //ushort AndXOffset;
  20. public SessionSetupAction Action;
  21. // Data:
  22. 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)
  23. public string NativeLanMan; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  24. public string PrimaryDomain; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  25. public SessionSetupAndXResponse() : base()
  26. {
  27. }
  28. public SessionSetupAndXResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  29. {
  30. Action = (SessionSetupAction)LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  31. int dataOffset = 0;
  32. if (isUnicode)
  33. {
  34. dataOffset++;
  35. }
  36. NativeOS = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  37. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  38. PrimaryDomain = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  39. }
  40. public override byte[] GetBytes(bool isUnicode)
  41. {
  42. this.SMBParameters = new byte[ParametersLength];
  43. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, (ushort)Action);
  44. int offset = 0;
  45. if (isUnicode)
  46. {
  47. int padding = 1; // 1 byte padding for 2 byte alignment
  48. this.SMBData = new byte[padding + NativeOS.Length * 2 + NativeLanMan.Length * 2 + PrimaryDomain.Length * 2 + 6];
  49. offset = padding;
  50. }
  51. else
  52. {
  53. this.SMBData = new byte[NativeOS.Length + NativeLanMan.Length + PrimaryDomain.Length + 3];
  54. }
  55. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  56. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  57. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, PrimaryDomain);
  58. return base.GetBytes(isUnicode);
  59. }
  60. public override CommandName CommandName
  61. {
  62. get
  63. {
  64. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  65. }
  66. }
  67. }
  68. }