Skip to content

Commit

Permalink
NTLMCryptography: Add .NET 5.0 \ 6.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
TalAloni committed May 20, 2023
1 parent 4c59c21 commit 0416e56
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
12 changes: 11 additions & 1 deletion SMBLibrary.Tests/NTLM/NTLMAuthenticationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2014-2019 Tal Aloni <[email protected]>. All rights reserved.
/* Copyright (C) 2014-2023 Tal Aloni <[email protected]>. 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,
Expand Down Expand Up @@ -27,6 +27,15 @@ public void LMv1HashTest()
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]
public void NTv1HashTest()
{
Expand Down Expand Up @@ -155,6 +164,7 @@ public void NTLMv2AuthenticateMessageTest()
public void TestAll()
{
LMv1HashTest();
LMv1HashTestEmptyPassword();
NTv1HashTest();
NTv2HashTest();
LMv1ResponseTest();
Expand Down
31 changes: 25 additions & 6 deletions SMBLibrary/Authentication/NTLM/Helpers/NTLMCryptography.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2014-2017 Tal Aloni <[email protected]>. All rights reserved.
/* Copyright (C) 2014-2023 Tal Aloni <[email protected]>. 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,
Expand Down Expand Up @@ -78,11 +78,26 @@ public static ICryptoTransform CreateWeakDesEncryptor(CipherMode mode, byte[] rg
{
DES des = DES.Create();
des.Mode = mode;
DESCryptoServiceProvider sm = des as DESCryptoServiceProvider;
MethodInfo mi = sm.GetType().GetMethod("_NewEncryptor", BindingFlags.NonPublic | BindingFlags.Instance);
object[] Par = { rgbKey, mode, rgbIV, sm.FeedbackSize, 0 };
ICryptoTransform trans = mi.Invoke(sm, Par) as ICryptoTransform;
return trans;
ICryptoTransform transform;
if (DES.IsWeakKey(rgbKey) || DES.IsSemiWeakKey(rgbKey))
{
#if NETSTANDARD2_0
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>
Expand Down Expand Up @@ -123,7 +138,11 @@ public static byte[] DesLongEncrypt(byte[] key, byte[] plainText)

public static Encoding GetOEMEncoding()
{
#if NETSTANDARD2_0
return ASCIIEncoding.GetEncoding(28591);
#else
return Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage);
#endif
}

/// <summary>
Expand Down

0 comments on commit 0416e56

Please sign in to comment.