NTLMv2ClientChallenge.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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(byte[] buffer) : this(buffer, 0)
  43. {
  44. }
  45. public NTLMv2ClientChallenge(byte[] buffer, int offset)
  46. {
  47. CurrentVersion = ByteReader.ReadByte(buffer, offset + 0);
  48. MaximumSupportedVersion = ByteReader.ReadByte(buffer, offset + 1);
  49. Reserved1 = LittleEndianConverter.ToUInt16(buffer, offset + 2);
  50. Reserved2 = LittleEndianConverter.ToUInt32(buffer, offset + 4);
  51. TimeStamp = FileTimeHelper.ReadFileTime(buffer, offset + 8);
  52. ClientChallenge = ByteReader.ReadBytes(buffer, offset + 16, 8);
  53. Reserved3 = LittleEndianConverter.ToUInt32(buffer, offset + 24);
  54. AVPairs = AVPairUtils.ReadAVPairSequence(buffer, offset + 28);
  55. }
  56. public byte[] GetBytes()
  57. {
  58. byte[] sequenceBytes = AVPairUtils.GetAVPairSequenceBytes(AVPairs);
  59. byte[] buffer = new byte[28 + sequenceBytes.Length];
  60. ByteWriter.WriteByte(buffer, 0, CurrentVersion);
  61. ByteWriter.WriteByte(buffer, 1, MaximumSupportedVersion);
  62. LittleEndianWriter.WriteUInt16(buffer, 2, Reserved1);
  63. LittleEndianWriter.WriteUInt32(buffer, 4, Reserved2);
  64. FileTimeHelper.WriteFileTime(buffer, 8, TimeStamp);
  65. ByteWriter.WriteBytes(buffer, 16, ClientChallenge, 8);
  66. LittleEndianWriter.WriteUInt32(buffer, 24, Reserved3);
  67. ByteWriter.WriteBytes(buffer, 28, sequenceBytes);
  68. return buffer;
  69. }
  70. /// <summary>
  71. /// [MS-NLMP] Page 60, Response key calculation algorithm:
  72. /// To create 'temp', 4 zero bytes will be appended to NTLMv2_CLIENT_CHALLENGE
  73. /// </summary>
  74. public byte[] GetBytesPadded()
  75. {
  76. return ByteUtils.Concatenate(GetBytes(), new byte[4]);
  77. }
  78. }
  79. }