NegotiateMessage.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.Authentication.NTLM
  12. {
  13. /// <summary>
  14. /// [MS-NLMP] NEGOTIATE_MESSAGE (Type 1 Message)
  15. /// </summary>
  16. public class NegotiateMessage
  17. {
  18. public string Signature; // 8 bytes
  19. public MessageTypeName MessageType;
  20. public NegotiateFlags NegotiateFlags;
  21. public string DomainName;
  22. public string Workstation;
  23. public NTLMVersion Version;
  24. public NegotiateMessage()
  25. {
  26. Signature = AuthenticateMessage.ValidSignature;
  27. MessageType = MessageTypeName.Negotiate;
  28. DomainName = String.Empty;
  29. Workstation = String.Empty;
  30. }
  31. public NegotiateMessage(byte[] buffer)
  32. {
  33. Signature = ByteReader.ReadAnsiString(buffer, 0, 8);
  34. MessageType = (MessageTypeName)LittleEndianConverter.ToUInt32(buffer, 8);
  35. NegotiateFlags = (NegotiateFlags)LittleEndianConverter.ToUInt32(buffer, 12);
  36. DomainName = AuthenticationMessageUtils.ReadAnsiStringBufferPointer(buffer, 16);
  37. Workstation = AuthenticationMessageUtils.ReadAnsiStringBufferPointer(buffer, 24);
  38. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  39. {
  40. Version = new NTLMVersion(buffer, 32);
  41. }
  42. }
  43. public byte[] GetBytes()
  44. {
  45. if ((NegotiateFlags & NegotiateFlags.DomainNameSupplied) == 0)
  46. {
  47. DomainName = String.Empty;
  48. }
  49. if ((NegotiateFlags & NegotiateFlags.WorkstationNameSupplied) == 0)
  50. {
  51. Workstation = String.Empty;
  52. }
  53. int fixedLength = 32;
  54. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  55. {
  56. fixedLength += 8;
  57. }
  58. int payloadLength = DomainName.Length * 2 + Workstation.Length * 2;
  59. byte[] buffer = new byte[fixedLength + payloadLength];
  60. ByteWriter.WriteAnsiString(buffer, 0, AuthenticateMessage.ValidSignature, 8);
  61. LittleEndianWriter.WriteUInt32(buffer, 8, (uint)MessageType);
  62. LittleEndianWriter.WriteUInt32(buffer, 12, (uint)NegotiateFlags);
  63. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  64. {
  65. Version.WriteBytes(buffer, 32);
  66. }
  67. int offset = fixedLength;
  68. AuthenticationMessageUtils.WriteBufferPointer(buffer, 16, (ushort)(DomainName.Length * 2), (uint)offset);
  69. ByteWriter.WriteUTF16String(buffer, ref offset, DomainName);
  70. AuthenticationMessageUtils.WriteBufferPointer(buffer, 24, (ushort)(Workstation.Length * 2), (uint)offset);
  71. ByteWriter.WriteUTF16String(buffer, ref offset, Workstation);
  72. return buffer;
  73. }
  74. }
  75. }