GSSAPIHelper.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. SimpleProtectedNegotiationTokenInit tokenInit = (SimpleProtectedNegotiationTokenInit)token;
  26. foreach (byte[] identifier in tokenInit.MechanismTypeList)
  27. {
  28. if (ByteUtils.AreByteArraysEqual(identifier, NTLMSSPIdentifier))
  29. {
  30. return tokenInit.MechanismToken;
  31. }
  32. }
  33. }
  34. else
  35. {
  36. SimpleProtectedNegotiationTokenResponse tokenResponse = (SimpleProtectedNegotiationTokenResponse)token;
  37. return tokenResponse.ResponseToken;
  38. }
  39. }
  40. return null;
  41. }
  42. public static byte[] GetGSSTokenInitNTLMSSPBytes()
  43. {
  44. SimpleProtectedNegotiationTokenInit token = new SimpleProtectedNegotiationTokenInit();
  45. token.MechanismTypeList = new List<byte[]>();
  46. token.MechanismTypeList.Add(NTLMSSPIdentifier);
  47. return SimpleProtectedNegotiationToken.GetTokenBytes(token);
  48. }
  49. public static byte[] GetGSSTokenResponseBytesFromNTLMSSPMessage(byte[] messageBytes)
  50. {
  51. SimpleProtectedNegotiationTokenResponse token = new SimpleProtectedNegotiationTokenResponse();
  52. token.NegState = NegState.AcceptIncomplete;
  53. token.SupportedMechanism = NTLMSSPIdentifier;
  54. token.ResponseToken = messageBytes;
  55. return token.GetBytes();
  56. }
  57. public static byte[] GetGSSTokenAcceptCompletedResponse()
  58. {
  59. SimpleProtectedNegotiationTokenResponse token = new SimpleProtectedNegotiationTokenResponse();
  60. token.NegState = NegState.AcceptCompleted;
  61. return token.GetBytes();
  62. }
  63. }
  64. }