SessionSetupAndXResponseExtended.cs 2.7 KB

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