diff --git a/SMBLibrary.Tests/Client/SMB2ClientTests.cs b/SMBLibrary.Tests/Client/SMB2ClientTests.cs index 40e88b3e..a081a307 100644 --- a/SMBLibrary.Tests/Client/SMB2ClientTests.cs +++ b/SMBLibrary.Tests/Client/SMB2ClientTests.cs @@ -10,22 +10,37 @@ using System.Diagnostics; using System.Net; using System.Net.Sockets; +using System.Security.Cryptography; using System.Threading; namespace SMBLibrary.Tests.Client { [TestClass] public class SMB2ClientTests - { - private int m_serverPort; + { + // ensure that multiple tests run at the same instant in time receive different port numbers + // by incrementing the port number for each test + private static readonly int minPort = 1025; + private static readonly int maxPort = 65535; + private static int m_serverPort = minPort + new Random().Next(maxPort - minPort); + private TcpListener m_tcpListener; private bool m_clientConnected; + + private static int GetNextPortNumber() + { + var next = Interlocked.Increment(ref m_serverPort); + + if (next > maxPort) + next = minPort; + return next; + } + [TestInitialize] public void Initialize() { - m_serverPort = 1000 + new Random().Next(50000); - m_tcpListener = new TcpListener(IPAddress.Loopback, m_serverPort); + m_tcpListener = new TcpListener(IPAddress.Loopback, GetNextPortNumber()); m_tcpListener.Start(); } diff --git a/SMBLibrary.Tests/NTFileStore/NTDirectoryFileSystemTests.cs b/SMBLibrary.Tests/NTFileStore/NTDirectoryFileSystemTests.cs index 591572b5..de064818 100644 --- a/SMBLibrary.Tests/NTFileStore/NTDirectoryFileSystemTests.cs +++ b/SMBLibrary.Tests/NTFileStore/NTDirectoryFileSystemTests.cs @@ -14,7 +14,7 @@ namespace SMBLibrary.Tests [TestClass] public class NTDirectoryFileSystemTests : NTFileStoreTests { - private static readonly string TestDirectoryPath = @"C:\Tests"; + private static readonly string TestDirectoryPath = Path.Combine(Path.GetTempPath(), "SMBLibraryTests"); static NTDirectoryFileSystemTests() { diff --git a/SMBLibrary/Authentication/NTLM/Helpers/NTLMCryptography.cs b/SMBLibrary/Authentication/NTLM/Helpers/NTLMCryptography.cs index 4c7e55f2..0e2d8eff 100644 --- a/SMBLibrary/Authentication/NTLM/Helpers/NTLMCryptography.cs +++ b/SMBLibrary/Authentication/NTLM/Helpers/NTLMCryptography.cs @@ -284,7 +284,7 @@ public static byte[] ComputeServerSignKey(byte[] exportedSessionKey) return ComputeSignKey(exportedSessionKey, false); } - private static byte[] ComputeSignKey(byte[] exportedSessionKey, bool isClient) + public static byte[] ComputeSignKey(byte[] exportedSessionKey, bool isClient) { // https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/524cdccb-563e-4793-92b0-7bc321fce096 string str; @@ -312,7 +312,7 @@ public static byte[] ComputeServerSealKey(byte[] exportedSessionKey) return ComputeSealKey(exportedSessionKey, false); } - private static byte[] ComputeSealKey(byte[] exportedSessionKey, bool isClient) + public static byte[] ComputeSealKey(byte[] exportedSessionKey, bool isClient) { // https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-nlmp/524cdccb-563e-4793-92b0-7bc321fce096 string str; diff --git a/SMBLibrary/Authentication/NTLM/IndependentNTLMAuthenticationProvider.cs b/SMBLibrary/Authentication/NTLM/IndependentNTLMAuthenticationProvider.cs index 2a4cea30..73367f89 100644 --- a/SMBLibrary/Authentication/NTLM/IndependentNTLMAuthenticationProvider.cs +++ b/SMBLibrary/Authentication/NTLM/IndependentNTLMAuthenticationProvider.cs @@ -1,4 +1,4 @@ -/* Copyright (C) 2014-2020 Tal Aloni . All rights reserved. +/* Copyright (C) 2014-2026 Tal Aloni . All rights reserved. * * You can redistribute this program and/or modify it under the terms of * the GNU Lesser Public License as published by the Free Software Foundation, @@ -78,6 +78,13 @@ public override NTStatus GetChallengeMessage(out object context, byte[] negotiat byte[] serverChallenge = GenerateServerChallenge(); context = new AuthContext(serverChallenge); + ChallengeMessage challengeMessage = CreateChallengeMessage(negotiateMessage, serverChallenge); + challengeMessageBytes = challengeMessage.GetBytes(); + return NTStatus.SEC_I_CONTINUE_NEEDED; + } + + protected virtual ChallengeMessage CreateChallengeMessage(NegotiateMessage negotiateMessage, byte[] serverChallenge) + { ChallengeMessage challengeMessage = new ChallengeMessage(); // https://msdn.microsoft.com/en-us/library/cc236691.aspx challengeMessage.NegotiateFlags = NegotiateFlags.TargetTypeServer | @@ -147,8 +154,7 @@ public override NTStatus GetChallengeMessage(out object context, byte[] negotiat challengeMessage.ServerChallenge = serverChallenge; challengeMessage.TargetInfo = AVPairUtils.GetAVPairSequence(Environment.MachineName, Environment.MachineName); challengeMessage.Version = NTLMVersion.Server2003; - challengeMessageBytes = challengeMessage.GetBytes(); - return NTStatus.SEC_I_CONTINUE_NEEDED; + return challengeMessage; } public override NTStatus Authenticate(object context, byte[] authenticateMessageBytes) diff --git a/SMBLibrary/SMBLibrary.csproj b/SMBLibrary/SMBLibrary.csproj index 138a16e7..6c6655cb 100644 --- a/SMBLibrary/SMBLibrary.csproj +++ b/SMBLibrary/SMBLibrary.csproj @@ -3,7 +3,7 @@ net20;net40;netstandard2.0 SMBLibrary - 1.5.6.1 + 1.5.6.2 1573;1591 SMBLibrary false