AVPairUtils.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2014 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. public class AVPairUtils
  14. {
  15. public static KeyValuePairList<AVPairKey, byte[]> GetAVPairSequence(string domainName, string computerName)
  16. {
  17. KeyValuePairList<AVPairKey, byte[]> pairs = new KeyValuePairList<AVPairKey, byte[]>();
  18. pairs.Add(AVPairKey.NbDomainName, UnicodeEncoding.Unicode.GetBytes(domainName));
  19. pairs.Add(AVPairKey.NbComputerName, UnicodeEncoding.Unicode.GetBytes(computerName));
  20. return pairs;
  21. }
  22. public static byte[] GetAVPairSequenceBytes(KeyValuePairList<AVPairKey, byte[]> pairs)
  23. {
  24. int length = GetAVPairSequenceLength(pairs);
  25. byte[] result = new byte[length];
  26. int offset = 0;
  27. WriteAVPairSequence(result, ref offset, pairs);
  28. return result;
  29. }
  30. public static int GetAVPairSequenceLength(KeyValuePairList<AVPairKey, byte[]> pairs)
  31. {
  32. int length = 0;
  33. foreach (KeyValuePair<AVPairKey, byte[]> pair in pairs)
  34. {
  35. length += 4 + pair.Value.Length;
  36. }
  37. return length + 4;
  38. }
  39. public static void WriteAVPairSequence(byte[] buffer, ref int offset, KeyValuePairList<AVPairKey, byte[]> pairs)
  40. {
  41. foreach (KeyValuePair<AVPairKey, byte[]> pair in pairs)
  42. {
  43. WriteAVPair(buffer, ref offset, pair.Key, pair.Value);
  44. }
  45. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)AVPairKey.EOL);
  46. LittleEndianWriter.WriteUInt16(buffer, ref offset, 0);
  47. }
  48. private static void WriteAVPair(byte[] buffer, ref int offset, AVPairKey key, byte[] value)
  49. {
  50. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)key);
  51. LittleEndianWriter.WriteUInt16(buffer, ref offset, (ushort)value.Length);
  52. ByteWriter.WriteBytes(buffer, ref offset, value);
  53. }
  54. public static KeyValuePairList<AVPairKey, byte[]> ReadAVPairSequence(byte[] buffer, int offset)
  55. {
  56. KeyValuePairList<AVPairKey, byte[]> result = new KeyValuePairList<AVPairKey,byte[]>();
  57. AVPairKey key = (AVPairKey)LittleEndianConverter.ToUInt16(buffer, offset);
  58. while (key != AVPairKey.EOL)
  59. {
  60. KeyValuePair<AVPairKey, byte[]> pair = ReadAVPair(buffer, ref offset);
  61. result.Add(pair);
  62. key = (AVPairKey)LittleEndianConverter.ToUInt16(buffer, offset);
  63. }
  64. return result;
  65. }
  66. private static KeyValuePair<AVPairKey, byte[]> ReadAVPair(byte[] buffer, ref int offset)
  67. {
  68. AVPairKey key = (AVPairKey)LittleEndianReader.ReadUInt16(buffer, ref offset);
  69. ushort length = LittleEndianReader.ReadUInt16(buffer, ref offset);
  70. byte[] value = ByteReader.ReadBytes(buffer, ref offset, length);
  71. return new KeyValuePair<AVPairKey, byte[]>(key, value);
  72. }
  73. }
  74. }