SessionSetupAndXResponseExtended.cs 3.9 KB

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