NTLMv2ClientChallenge.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /// NTLMv2_CLIENT_CHALLENGE
  15. /// </summary>
  16. public class NTLMv2ClientChallenge
  17. {
  18. public const byte StructureVersion = 0x01;
  19. public static readonly DateTime EpochTime = DateTime.FromFileTimeUtc(0);
  20. public byte CurrentVersion;
  21. public byte MaximumSupportedVersion;
  22. public ushort Reserved1;
  23. public uint Reserved2;
  24. public DateTime TimeStamp;
  25. public uint Reserved3;
  26. public byte[] ClientChallenge; // 8-byte challenge generated by the client
  27. public KeyValuePairList<AVPairKey, byte[]> AVPairs;
  28. public NTLMv2ClientChallenge()
  29. {
  30. }
  31. public NTLMv2ClientChallenge(DateTime timeStamp, byte[] clientChallenge, string domainName, string computerName)
  32. {
  33. CurrentVersion = StructureVersion;
  34. MaximumSupportedVersion = StructureVersion;
  35. TimeStamp = timeStamp;
  36. ClientChallenge = clientChallenge;
  37. AVPairs = new KeyValuePairList<AVPairKey, byte[]>();
  38. AVPairs.Add(AVPairKey.NbDomainName, UnicodeEncoding.Unicode.GetBytes(domainName));
  39. AVPairs.Add(AVPairKey.NbComputerName, UnicodeEncoding.Unicode.GetBytes(computerName));
  40. }
  41. public NTLMv2ClientChallenge(byte[] buffer) : this(buffer, 0)
  42. {
  43. }
  44. public NTLMv2ClientChallenge(byte[] buffer, int offset)
  45. {
  46. CurrentVersion = ByteReader.ReadByte(buffer, offset + 0);
  47. MaximumSupportedVersion = ByteReader.ReadByte(buffer, offset + 1);
  48. Reserved1 = LittleEndianConverter.ToUInt16(buffer, offset + 2);
  49. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + 4);
  50. TimeStamp = FileTimeHelper.ReadFileTime(buffer, offset + 8);
  51. ClientChallenge = ByteReader.ReadBytes(buffer, offset + 16, 8);
  52. Reserved3 = LittleEndianConverter.ToUInt32(buffer, offset + 24);
  53. AVPairs = AVPairUtils.ReadAVPairSequence(buffer, offset + 28);
  54. }
  55. public byte[] GetBytes()
  56. {
  57. byte[] sequenceBytes = AVPairUtils.GetAVPairSequenceBytes(AVPairs);
  58. byte[] buffer = new byte[28 + sequenceBytes.Length];
  59. ByteWriter.WriteByte(buffer, 0, CurrentVersion);
  60. ByteWriter.WriteByte(buffer, 1, MaximumSupportedVersion);
  61. LittleEndianWriter.WriteUInt16(buffer, 2, Reserved1);
  62. LittleEndianWriter.WriteUInt32(buffer, 4, Reserved2);
  63. FileTimeHelper.WriteFileTime(buffer, 8, TimeStamp);
  64. ByteWriter.WriteBytes(buffer, 16, ClientChallenge, 8);
  65. LittleEndianWriter.WriteUInt32(buffer, 24, Reserved3);
  66. ByteWriter.WriteBytes(buffer, 28, sequenceBytes);
  67. return buffer;
  68. }
  69. /// <summary>
  70. /// [MS-NLMP] Page 60, Response key calculation algorithm:
  71. /// To create 'temp', 4 zero bytes will be appended to NTLMv2_CLIENT_CHALLENGE
  72. /// </summary>
  73. public byte[] GetBytesPadded()
  74. {
  75. return ByteUtils.Concatenate(GetBytes(), new byte[4]);
  76. }
  77. }
  78. }