ChallengeMessage.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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] CHALLENGE_MESSAGE (Type 2 Message)
  15. /// </summary>
  16. public class ChallengeMessage
  17. {
  18. public string Signature; // 8 bytes
  19. public MessageTypeName MessageType;
  20. public string TargetName;
  21. public NegotiateFlags NegotiateFlags;
  22. public byte[] ServerChallenge; // 8 bytes
  23. // Reserved - 8 bytes
  24. public byte[] TargetInfo; // sequence of AV_PAIR structures
  25. public NTLMVersion Version;
  26. public ChallengeMessage()
  27. {
  28. Signature = AuthenticateMessage.ValidSignature;
  29. MessageType = MessageTypeName.Challenge;
  30. }
  31. public ChallengeMessage(byte[] buffer)
  32. {
  33. Signature = ByteReader.ReadAnsiString(buffer, 0, 8);
  34. MessageType = (MessageTypeName)LittleEndianConverter.ToUInt32(buffer, 8);
  35. TargetName = AuthenticationMessageUtils.ReadUnicodeStringBufferPointer(buffer, 12);
  36. NegotiateFlags = (NegotiateFlags)LittleEndianConverter.ToUInt32(buffer, 20);
  37. ServerChallenge = ByteReader.ReadBytes(buffer, 24, 8);
  38. // Reserved
  39. TargetInfo = AuthenticationMessageUtils.ReadBufferPointer(buffer, 40);
  40. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  41. {
  42. Version = new NTLMVersion(buffer, 48);
  43. }
  44. }
  45. public byte[] GetBytes()
  46. {
  47. int fixedLength = 48;
  48. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  49. {
  50. fixedLength += 8;
  51. }
  52. int payloadLength = TargetName.Length * 2 + TargetInfo.Length;
  53. byte[] buffer = new byte[fixedLength + payloadLength];
  54. ByteWriter.WriteAnsiString(buffer, 0, AuthenticateMessage.ValidSignature, 8);
  55. LittleEndianWriter.WriteUInt32(buffer, 8, (uint)MessageType);
  56. LittleEndianWriter.WriteUInt32(buffer, 20, (uint)NegotiateFlags);
  57. ByteWriter.WriteBytes(buffer, 24, ServerChallenge);
  58. if ((NegotiateFlags & NegotiateFlags.Version) > 0)
  59. {
  60. Version.WriteBytes(buffer, 48);
  61. }
  62. int offset = fixedLength;
  63. AuthenticationMessageUtils.WriteBufferPointer(buffer, 12, (ushort)(TargetName.Length * 2), (uint)offset);
  64. ByteWriter.WriteUTF16String(buffer, ref offset, TargetName);
  65. AuthenticationMessageUtils.WriteBufferPointer(buffer, 40, (ushort)TargetInfo.Length, (uint)offset);
  66. ByteWriter.WriteBytes(buffer, ref offset, TargetInfo);
  67. return buffer;
  68. }
  69. }
  70. }