GSSAPIHelper.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Copyright (C) 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 Utilities;
  10. namespace SMBLibrary.Authentication
  11. {
  12. public class GSSAPIHelper
  13. {
  14. public static readonly byte[] NTLMSSPIdentifier = new byte[] { 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0x37, 0x02, 0x02, 0x0a };
  15. /// <summary>
  16. /// https://msdn.microsoft.com/en-us/library/ms995330.aspx
  17. /// </summary>
  18. public static byte[] GetNTLMSSPMessage(byte[] tokenBytes)
  19. {
  20. SimpleProtectedNegotiationToken token = SimpleProtectedNegotiationToken.ReadToken(tokenBytes, 0);
  21. if (token != null)
  22. {
  23. if (token is SimpleProtectedNegotiationTokenInit)
  24. {
  25. List<TokenInitEntry> tokens = ((SimpleProtectedNegotiationTokenInit)token).Tokens;
  26. foreach (TokenInitEntry entry in tokens)
  27. {
  28. foreach (byte[] identifier in entry.MechanismTypeList)
  29. {
  30. if (ByteUtils.AreByteArraysEqual(identifier, NTLMSSPIdentifier))
  31. {
  32. return entry.MechanismToken;
  33. }
  34. }
  35. }
  36. }
  37. else
  38. {
  39. List<TokenResponseEntry> tokens = ((SimpleProtectedNegotiationTokenResponse)token).Tokens;
  40. if (tokens.Count > 0)
  41. {
  42. return tokens[0].ResponseToken;
  43. }
  44. }
  45. }
  46. return null;
  47. }
  48. public static byte[] GetGSSTokenInitNTLMSSPBytes()
  49. {
  50. SimpleProtectedNegotiationTokenInit token = new SimpleProtectedNegotiationTokenInit();
  51. TokenInitEntry entry = new TokenInitEntry();
  52. entry.MechanismTypeList = new List<byte[]>();
  53. entry.MechanismTypeList.Add(NTLMSSPIdentifier);
  54. token.Tokens.Add(entry);
  55. return SimpleProtectedNegotiationToken.GetTokenBytes(token);
  56. }
  57. public static byte[] GetGSSTokenResponseBytesFromNTLMSSPMessage(byte[] messageBytes)
  58. {
  59. SimpleProtectedNegotiationTokenResponse token = new SimpleProtectedNegotiationTokenResponse();
  60. TokenResponseEntry entry = new TokenResponseEntry();
  61. entry.NegState = NegState.AcceptIncomplete;
  62. entry.SupportedMechanism = NTLMSSPIdentifier;
  63. entry.ResponseToken = messageBytes;
  64. token.Tokens.Add(entry);
  65. return token.GetBytes();
  66. }
  67. public static byte[] GetGSSTokenAcceptCompletedResponse()
  68. {
  69. SimpleProtectedNegotiationTokenResponse token = new SimpleProtectedNegotiationTokenResponse();
  70. TokenResponseEntry entry = new TokenResponseEntry();
  71. entry.NegState = NegState.AcceptCompleted;
  72. token.Tokens.Add(entry);
  73. return token.GetBytes();
  74. }
  75. }
  76. }