NegotiateResponse.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_NEGOTIATE Response, NT LAN Manager dialect
  15. /// </summary>
  16. public class NegotiateResponse : SMB1Command
  17. {
  18. public const int ParametersLength = 34;
  19. // Parameters:
  20. public ushort DialectIndex;
  21. public SecurityMode SecurityMode;
  22. public ushort MaxMpxCount;
  23. public ushort MaxNumberVcs;
  24. public uint MaxBufferSize;
  25. public uint MaxRawSize;
  26. public uint SessionKey;
  27. public Capabilities Capabilities;
  28. public DateTime SystemTime;
  29. public short ServerTimeZone;
  30. private byte ChallengeLength;
  31. // Data:
  32. public byte[] Challenge;
  33. public string DomainName; // SMB_STRING (If Unicode, this field MUST be aligned to start on a 2-byte boundary from the start of the SMB header)
  34. public string ServerName; // SMB_STRING (this field WILL be aligned to start on a 2-byte boundary from the start of the SMB header)
  35. public NegotiateResponse() : base()
  36. {
  37. Challenge = new byte[0];
  38. DomainName = String.Empty;
  39. ServerName = String.Empty;
  40. }
  41. public NegotiateResponse(byte[] buffer, int offset, bool isUnicode) : base(buffer, offset, isUnicode)
  42. {
  43. DialectIndex = LittleEndianConverter.ToUInt16(this.SMBParameters, 0);
  44. SecurityMode = (SecurityMode)ByteReader.ReadByte(this.SMBParameters, 2);
  45. MaxMpxCount = LittleEndianConverter.ToUInt16(this.SMBParameters, 3);
  46. MaxNumberVcs = LittleEndianConverter.ToUInt16(this.SMBParameters, 5);
  47. MaxBufferSize = LittleEndianConverter.ToUInt32(this.SMBParameters, 7);
  48. MaxRawSize = LittleEndianConverter.ToUInt32(this.SMBParameters, 11);
  49. SessionKey = LittleEndianConverter.ToUInt32(this.SMBParameters, 15);
  50. Capabilities = (Capabilities)LittleEndianConverter.ToUInt32(this.SMBParameters, 19);
  51. SystemTime = FileTimeHelper.ReadFileTime(this.SMBParameters, 23);
  52. ServerTimeZone = LittleEndianConverter.ToInt16(this.SMBParameters, 31);
  53. ChallengeLength = ByteReader.ReadByte(this.SMBParameters, 33);
  54. int dataOffset = 0;
  55. Challenge = ByteReader.ReadBytes(this.SMBData, ref dataOffset, ChallengeLength);
  56. // [MS-CIFS] <90> Padding is not added before DomainName
  57. // DomainName and ServerName are always in Unicode
  58. DomainName = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, true);
  59. ServerName = SMB1Helper.ReadSMBString(this.SMBData, ref dataOffset, true);
  60. }
  61. public override byte[] GetBytes(bool isUnicode)
  62. {
  63. ChallengeLength = (byte)this.Challenge.Length;
  64. this.SMBParameters = new byte[ParametersLength];
  65. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, DialectIndex);
  66. ByteWriter.WriteByte(this.SMBParameters, 2, (byte)SecurityMode);
  67. LittleEndianWriter.WriteUInt16(this.SMBParameters, 3, MaxMpxCount);
  68. LittleEndianWriter.WriteUInt16(this.SMBParameters, 5, MaxNumberVcs);
  69. LittleEndianWriter.WriteUInt32(this.SMBParameters, 7, MaxBufferSize);
  70. LittleEndianWriter.WriteUInt32(this.SMBParameters, 11, MaxRawSize);
  71. LittleEndianWriter.WriteUInt32(this.SMBParameters, 15, SessionKey);
  72. LittleEndianWriter.WriteUInt32(this.SMBParameters, 19, (uint)Capabilities);
  73. FileTimeHelper.WriteFileTime(this.SMBParameters, 23, SystemTime);
  74. LittleEndianWriter.WriteInt16(this.SMBParameters, 31, ServerTimeZone);
  75. ByteWriter.WriteByte(this.SMBParameters, 33, ChallengeLength);
  76. // [MS-CIFS] <90> Padding is not added before DomainName
  77. // DomainName and ServerName are always in Unicode
  78. this.SMBData = new byte[Challenge.Length + (DomainName.Length + 1) * 2 + (ServerName.Length + 1) * 2];
  79. int offset = 0;
  80. ByteWriter.WriteBytes(this.SMBData, ref offset, Challenge);
  81. SMB1Helper.WriteSMBString(this.SMBData, ref offset, true, DomainName);
  82. SMB1Helper.WriteSMBString(this.SMBData, ref offset, true, ServerName);
  83. return base.GetBytes(isUnicode);
  84. }
  85. public override CommandName CommandName
  86. {
  87. get
  88. {
  89. return CommandName.SMB_COM_NEGOTIATE;
  90. }
  91. }
  92. }
  93. }