SessionSetupAndXResponse.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_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 = String.Empty; // 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 = String.Empty; // 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 = String.Empty; // 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. }
  31. public SessionSetupAndXResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  32. {
  33. Action = (SessionSetupAction)LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  34. int dataOffset = 0;
  35. if (isUnicode)
  36. {
  37. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  38. // Note: SMBData starts at an odd offset.
  39. dataOffset++;
  40. }
  41. NativeOS = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  42. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  43. PrimaryDomain = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  44. }
  45. public override byte[] GetBytes(bool isUnicode)
  46. {
  47. this.SMBParameters = new byte[ParametersLength];
  48. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, (ushort)Action);
  49. int offset = 0;
  50. if (isUnicode)
  51. {
  52. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  53. // Note: SMBData starts at an odd offset.
  54. int padding = 1;
  55. this.SMBData = new byte[padding + NativeOS.Length * 2 + NativeLanMan.Length * 2 + PrimaryDomain.Length * 2 + 6];
  56. offset = padding;
  57. }
  58. else
  59. {
  60. this.SMBData = new byte[NativeOS.Length + NativeLanMan.Length + PrimaryDomain.Length + 3];
  61. }
  62. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  63. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  64. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, PrimaryDomain);
  65. return base.GetBytes(isUnicode);
  66. }
  67. public override CommandName CommandName
  68. {
  69. get
  70. {
  71. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  72. }
  73. }
  74. }
  75. }