NegotiateMessage.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  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. int fixedLength = 32;
  46. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  47. {
  48. fixedLength += 8;
  49. }
  50. int payloadLength = DomainName.Length * 2 + Workstation.Length * 2;
  51. byte[] buffer = new byte[fixedLength + payloadLength];
  52. ByteWriter.WriteAnsiString(buffer, 0, AuthenticateMessage.ValidSignature, 8);
  53. LittleEndianWriter.WriteUInt32(buffer, 8, (uint)MessageType);
  54. LittleEndianWriter.WriteUInt32(buffer, 12, (uint)NegotiateFlags);
  55. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  56. {
  57. Version.WriteBytes(buffer, 32);
  58. }
  59. int offset = fixedLength;
  60. AuthenticationMessageUtils.WriteBufferPointer(buffer, 16, (ushort)(DomainName.Length * 2), (uint)offset);
  61. ByteWriter.WriteUTF16String(buffer, ref offset, DomainName);
  62. AuthenticationMessageUtils.WriteBufferPointer(buffer, 16, (ushort)(Workstation.Length * 2), (uint)offset);
  63. ByteWriter.WriteUTF16String(buffer, ref offset, Workstation);
  64. return buffer;
  65. }
  66. }
  67. }