NTLMCryptography: Add .NET 5.0 \ 6.0 support

This commit is contained in:
Tal Aloni 2023-05-20 00:57:05 +03:00
parent 4c59c21670
commit 0416e5670d
2 changed files with 36 additions and 7 deletions

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2014-2019 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved. /* Copyright (C) 2014-2023 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
* *
* You can redistribute this program and/or modify it under the terms of * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -27,6 +27,15 @@ namespace SMBLibrary.Tests
Assert.IsTrue(ByteUtils.AreByteArraysEqual(hash, expected)); Assert.IsTrue(ByteUtils.AreByteArraysEqual(hash, expected));
} }
// Will use weak DES key
[TestMethod]
public void LMv1HashTestEmptyPassword()
{
byte[] hash = NTLMCryptography.LMOWFv1("");
byte[] expected = new byte[] { 0xaa, 0xd3, 0xb4, 0x35, 0xb5, 0x14, 0x04, 0xee, 0xaa, 0xd3, 0xb4, 0x35, 0xb5, 0x14, 0x04, 0xee };
Assert.IsTrue(ByteUtils.AreByteArraysEqual(hash, expected));
}
[TestMethod] [TestMethod]
public void NTv1HashTest() public void NTv1HashTest()
{ {
@ -155,6 +164,7 @@ namespace SMBLibrary.Tests
public void TestAll() public void TestAll()
{ {
LMv1HashTest(); LMv1HashTest();
LMv1HashTestEmptyPassword();
NTv1HashTest(); NTv1HashTest();
NTv2HashTest(); NTv2HashTest();
LMv1ResponseTest(); LMv1ResponseTest();

View file

@ -1,4 +1,4 @@
/* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved. /* Copyright (C) 2014-2023 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
* *
* You can redistribute this program and/or modify it under the terms of * 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, * the GNU Lesser Public License as published by the Free Software Foundation,
@ -78,11 +78,26 @@ namespace SMBLibrary.Authentication.NTLM
{ {
DES des = DES.Create(); DES des = DES.Create();
des.Mode = mode; des.Mode = mode;
DESCryptoServiceProvider sm = des as DESCryptoServiceProvider; ICryptoTransform transform;
MethodInfo mi = sm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance); if (DES.IsWeakKey(rgbKey) || DES.IsSemiWeakKey(rgbKey))
object[] Par = { rgbKey, mode, rgbIV, sm.FeedbackSize, 0 }; {
ICryptoTransform trans = mi.Invoke(sm, Par) as ICryptoTransform; #if NETSTANDARD2_0
return trans; MethodInfo getTransformCoreMethodInfo = des.GetType().GetMethod("CreateTransformCore", BindingFlags.NonPublic | BindingFlags.Static);
object[] getTransformCoreParameters = { mode, des.Padding, rgbKey, rgbIV, des.BlockSize / 8 , des.FeedbackSize / 8, des.BlockSize / 8, true };
transform = getTransformCoreMethodInfo.Invoke(null, getTransformCoreParameters) as ICryptoTransform;
#else
DESCryptoServiceProvider desServiceProvider = des as DESCryptoServiceProvider;
MethodInfo newEncryptorMethodInfo = desServiceProvider.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
object[] encryptorParameters = { rgbKey, mode, rgbIV, desServiceProvider.FeedbackSize, 0 };
transform = newEncryptorMethodInfo.Invoke(desServiceProvider, encryptorParameters) as ICryptoTransform;
#endif
}
else
{
transform = des.CreateEncryptor(rgbKey, rgbIV);
}
return transform;
} }
/// <summary> /// <summary>
@ -123,7 +138,11 @@ namespace SMBLibrary.Authentication.NTLM
public static Encoding GetOEMEncoding() public static Encoding GetOEMEncoding()
{ {
#if NETSTANDARD2_0
return ASCIIEncoding.GetEncoding(28591);
#else
return Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage); return Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
#endif
} }
/// <summary> /// <summary>