NegotiateResponseNTLM.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_NEGOTIATE Response, NT LAN Manager dialect
  15. /// </summary>
  16. public class NegotiateResponseNTLM : 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 ServerCapabilities Capabilities;
  28. public DateTime SystemTime;
  29. public short ServerTimeZone;
  30. //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 NegotiateResponseNTLM() : base()
  36. {
  37. Challenge = new byte[0];
  38. DomainName = String.Empty;
  39. ServerName = String.Empty;
  40. }
  41. public override byte[] GetBytes(bool isUnicode)
  42. {
  43. byte challengeLength = (byte)this.Challenge.Length;
  44. this.SMBParameters = new byte[ParametersLength];
  45. LittleEndianWriter.WriteUInt16(this.SMBParameters, 0, DialectIndex);
  46. ByteWriter.WriteByte(this.SMBParameters, 2, (byte)SecurityMode);
  47. LittleEndianWriter.WriteUInt16(this.SMBParameters, 3, MaxMpxCount);
  48. LittleEndianWriter.WriteUInt16(this.SMBParameters, 5, MaxNumberVcs);
  49. LittleEndianWriter.WriteUInt32(this.SMBParameters, 7, MaxBufferSize);
  50. LittleEndianWriter.WriteUInt32(this.SMBParameters, 11, MaxRawSize);
  51. LittleEndianWriter.WriteUInt32(this.SMBParameters, 15, SessionKey);
  52. LittleEndianWriter.WriteUInt32(this.SMBParameters, 19, (uint)Capabilities);
  53. LittleEndianWriter.WriteInt64(this.SMBParameters, 23, SystemTime.ToFileTimeUtc());
  54. LittleEndianWriter.WriteInt16(this.SMBParameters, 31, ServerTimeZone);
  55. ByteWriter.WriteByte(this.SMBParameters, 33, challengeLength);
  56. int padding = 0;
  57. if (isUnicode)
  58. {
  59. padding = Challenge.Length % 2;
  60. this.SMBData = new byte[Challenge.Length + padding + DomainName.Length * 2 + ServerName.Length * 2 + 4];
  61. }
  62. else
  63. {
  64. this.SMBData = new byte[Challenge.Length + DomainName.Length + ServerName.Length + 2];
  65. }
  66. int offset = 0;
  67. ByteWriter.WriteBytes(this.SMBData, ref offset, Challenge);
  68. offset += padding;
  69. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, DomainName);
  70. SMB1Helper.WriteSMBString(this.SMBData, ref offset, isUnicode, ServerName);
  71. return base.GetBytes(isUnicode);
  72. }
  73. public override CommandName CommandName
  74. {
  75. get
  76. {
  77. return CommandName.SMB_COM_NEGOTIATE;
  78. }
  79. }
  80. }
  81. }