SessionSetupAndXRequest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Request
  15. /// </summary>
  16. public class SessionSetupAndXRequest : SMBAndXCommand
  17. {
  18. public const int ParametersLength = 26;
  19. // Parameters:
  20. public ushort MaxBufferSize;
  21. public ushort MaxMpxCount;
  22. public ushort VcNumber;
  23. public uint SessionKey;
  24. private ushort OEMPasswordLength;
  25. private ushort UnicodePasswordLength;
  26. public uint Reserved;
  27. public Capabilities Capabilities;
  28. // Data:
  29. public byte[] OEMPassword;
  30. public byte[] UnicodePassword;
  31. // Padding
  32. public string AccountName; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  33. public string PrimaryDomain; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  34. public string NativeOS; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  35. public string NativeLanMan; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  36. public SessionSetupAndXRequest(): base()
  37. {
  38. AccountName = String.Empty;
  39. PrimaryDomain = String.Empty;
  40. NativeOS = String.Empty;
  41. NativeLanMan = String.Empty;
  42. }
  43. public SessionSetupAndXRequest(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  44. {
  45. MaxBufferSize = LittleEndianConverter.ToUInt16(this.SMBParameters, 4);
  46. MaxMpxCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 6);
  47. VcNumber = LittleEndianConverter.ToUInt16(this.SMBParameters, 8);
  48. SessionKey = LittleEndianConverter.ToUInt32(this.SMBParameters, 10);
  49. OEMPasswordLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 14);
  50. UnicodePasswordLength = LittleEndianConverter.ToUInt16(this.SMBParameters, 16);
  51. Reserved = LittleEndianConverter.ToUInt32(this.SMBParameters, 18);
  52. Capabilities = (Capabilities)LittleEndianConverter.ToUInt32(this.SMBParameters, 22);
  53. OEMPassword = ByteReader.ReadBytes(this.SMBData, 0, OEMPasswordLength);
  54. UnicodePassword = ByteReader.ReadBytes(this.SMBData, OEMPasswordLength, UnicodePasswordLength);
  55. int dataOffset = OEMPasswordLength + UnicodePasswordLength;
  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. int padding = (1 + OEMPasswordLength + UnicodePasswordLength) % 2;
  61. dataOffset += padding;
  62. }
  63. AccountName = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  64. PrimaryDomain = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  65. NativeOS = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  66. NativeLanMan = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, isUnicode);
  67. }
  68. public override byte[] GetBytes(bool isUnicode)
  69. {
  70. Capabilities &= ~Capabilities.ExtendedSecurity;
  71. OEMPasswordLength = (ushort)OEMPassword.Length;
  72. UnicodePasswordLength = (ushort)UnicodePassword.Length;
  73. this.SMBParameters = new byte[ParametersLength];
  74. LittleEndianWriter.WriteUInt16(this.SMBParameters, 4, MaxBufferSize);
  75. LittleEndianWriter.WriteUInt16(this.SMBParameters, 6, MaxMpxCount);
  76. LittleEndianWriter.WriteUInt16(this.SMBParameters, 8, VcNumber);
  77. LittleEndianWriter.WriteUInt32(this.SMBParameters, 10, SessionKey);
  78. LittleEndianWriter.WriteUInt16(this.SMBParameters, 14, OEMPasswordLength);
  79. LittleEndianWriter.WriteUInt16(this.SMBParameters, 16, UnicodePasswordLength);
  80. LittleEndianWriter.WriteUInt32(this.SMBParameters, 18, Reserved);
  81. LittleEndianWriter.WriteUInt32(this.SMBParameters, 22, (uint)Capabilities);
  82. int padding = 0;
  83. if (isUnicode)
  84. {
  85. // A Unicode string MUST be aligned to a 16-bit boundary with respect to the beginning of the SMB Header.
  86. // Note: SMBData starts at an odd offset.
  87. padding = (1 + OEMPasswordLength + UnicodePasswordLength) % 2;
  88. this.SMBData = new byte[OEMPassword.Length + UnicodePassword.Length + padding + (AccountName.Length + 1) * 2 + (PrimaryDomain.Length + 1) * 2 + (NativeOS.Length + 1) * 2 + (NativeLanMan.Length + 1) * 2];
  89. }
  90. else
  91. {
  92. this.SMBData = new byte[OEMPassword.Length + UnicodePassword.Length + AccountName.Length + 1 + PrimaryDomain.Length + 1 + NativeOS.Length + 1 + NativeLanMan.Length + 1];
  93. }
  94. int offset = 0;
  95. ByteWriter.WriteBytes(this.SMBData, ref offset, OEMPassword);
  96. ByteWriter.WriteBytes(this.SMBData, ref offset, UnicodePassword);
  97. offset += padding;
  98. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, AccountName);
  99. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, PrimaryDomain);
  100. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeOS);
  101. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, NativeLanMan);
  102. return base.GetBytes(isUnicode);
  103. }
  104. public override CommandName CommandName
  105. {
  106. get
  107. {
  108. return CommandName.SMB_COM_SESSION_SETUP_ANDX;
  109. }
  110. }
  111. }
  112. }