SessionSetupAndXResponseExtended.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, 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. if ((this.SMBData.Length - dataOffset) % 2 == 1)
  50. {
  51. // Workaround for a single terminating null byte
  52. this.SMBData = ByteUtils.Concatenate(this.SMBData, new byte[1]);
  53. }
  54. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  55. }
  56. public override byte[] GetBytes(bool isUnicode)
  57. {
  58. ushort securityBlobLength = (ushort)SecurityBlob.Length;
  59. this.SMBParameters = new byte[ParametersLength];
  60. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, (ushort)Action);
  61. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, securityBlobLength);
  62. int padding = 0;
  63. if (isUnicode)
  64. {
  65. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  66. // Note: SMBData starts at an odd offset.
  67. padding = (1 + securityBlobLength) % 2;
  68. this.SMBData = new byte[SecurityBlob.Length + padding + NativeOS.Length * 2 + NativeLanMan.Length * 2 + 4];
  69. }
  70. else
  71. {
  72. this.SMBData = new byte[SecurityBlob.Length + NativeOS.Length + NativeLanMan.Length + 2];
  73. }
  74. int offset = 0;
  75. ByteWriter.WriteBytes(this.SMBData, ref offset, SecurityBlob);
  76. offset += padding;
  77. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  78. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  79. return base.GetBytes(isUnicode);
  80. }
  81. public override CommandName CommandName
  82. {
  83. get
  84. {
  85. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  86. }
  87. }
  88. }
  89. }