NTLMv2ClientChallenge.cs 4.0 KB

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