SessionSetupAndXResponseExtended.cs 3.9 KB

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