首页 > 解决方案 > SQL CLR标量UDF中的DES解密不起作用

问题描述

我们有一个使用 SQL Server 作为其后端的旧应用程序。作为一些安全问题的一部分,它使用(单个)DES 使用硬编码到应用程序代码中的密钥和 IV 对从用户收集的一些字段进行加密,然后 Base64 对加密字节进行编码,最后将该字符串存储在 varchar 中数据库中的列。在这一点上(可能是第一次编码时)非常不安全,以及有问题的设计/实现,但它就是这样。我的任务是在 SQL Server 中实现一个 CLR 用户定义的标量函数,它可以解密这种类型的数据。

作为概念证明,我创建了以下简短的控制台应用程序,以确保我了解 C# 中的 DES 解密过程:

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class My_Decrypt
{
    static void Main(string[] args)
    {
        DES des = new DESCryptoServiceProvider();
        byte[] IV = BitConverter.GetBytes(0xFECAEFBEEDFECEFA);
        byte[] Key = Encoding.ASCII.GetBytes("password");

        foreach (string cipherText in args)
        {
            byte[] cipherBytes = Convert.FromBase64String(cipherText);
            MemoryStream es = new MemoryStream(cipherBytes);
            CryptoStream cs = new CryptoStream(
                es,
                des.CreateDecryptor(Key, IV),
                CryptoStreamMode.Read
            );
            byte[] plainBytes = new byte[cipherBytes.Length];
            cs.Read(plainBytes, 0, plainBytes.Length);
            string plainText = Encoding.ASCII.GetString(plainBytes);
            Console.WriteLine(
                "'{0}' == '{1}'\nusing key = '{2}', IV = '{3}'\ndecrypts to '{4}' == '{5}'.\n",
                cipherText,
                BitConverter.ToString(cipherBytes),
                BitConverter.ToString(Key),
                BitConverter.ToString(IV),
                BitConverter.ToString(plainBytes),
                plainText
            );
        }
    }
}

编译后,我可以运行以下命令:

C:\>My_Decrypt.exe KDdSnfYYnMQawhwuaWo2WA==
'KDdSnfYYnMQawhwuaWo2WA==' == '28-37-52-9D-F6-18-9C-C4-1A-C2-1C-2E-69-6A-36-58'
using key = '70-61-73-73-77-6F-72-64', IV = 'FA-CE-FE-ED-BE-EF-CA-FE'
decrypts to '73-65-63-72-65-74-20-64-61-74-61-00-00-00-00-00' == 'secret data     '.

看起来正确,我使用openssl进行了验证。

因此,在确定了这一点后,我接下来尝试在 CLR 标量 UDF 中使用相同的代码,如下所示:

    [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true)]
    public static SqlString DES_Decrypt( SqlString CipherText, SqlBinary DES_Key, SqlBinary DES_IV )
    {
        if (CipherText.IsNull || DES_Key.IsNull || DES_IV.IsNull)
            return SqlString.Null;
        string cipherText = CipherText.ToString();
        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        MemoryStream es = new MemoryStream(cipherBytes);
        DES des = new DESCryptoServiceProvider();
        byte[] IV = (byte[]) DES_IV;
        byte[] Key = (byte[]) DES_Key;
        CryptoStream cs = new CryptoStream(
            es, des.CreateEncryptor(Key, IV), CryptoStreamMode.Read );
        byte[] plainBytes = new byte[cipherBytes.Length];
        cs.Read(plainBytes, 0, plainBytes.Length);
        cs.Close();
        string plainText = new ASCIIEncoding().GetString(plainBytes);
        return new SqlString(plainText);
    }

但是,在编译后,将程序集加载到 MSSQL 中,创建函数并尝试执行它——我得到了输出的垃圾。因此,经过多次尝试完成这项工作(包括创建上面的 POC 应用程序),我return将最后一行中的替换为以下内容:

        throw new ArgumentException(String.Format(
            "\n'{0}' == '{1}'\nusing key = '{2}', IV = '{3}'\ndecrypts to '{4}' == '{5}'.",
            cipherText,
            BitConverter.ToString(cipherBytes),
            BitConverter.ToString(Key),
            BitConverter.ToString(IV),
            BitConverter.ToString(plainBytes),
            plainText
        ));

现在,当我在 MSSQL 中运行查询SELECT dbo.DES_Decrypt(N'KDdSnfYYnMQawhwuaWo2WA==', CAST('password' AS binary(8)), 0xFACEFEEDBEEFCAFE);时,我收到异常错误消息:

A .NET Framework error occurred during execution of user-defined routine or aggregate "DES_Decrypt": 
System.ArgumentException: 
'KDdSnfYYnMQawhwuaWo2WA==' == '28-37-52-9D-F6-18-9C-C4-1A-C2-1C-2E-69-6A-36-58'
using key = '70-61-73-73-77-6F-72-64', IV = 'FA-CE-FE-ED-BE-EF-CA-FE'
decrypts to '47-F7-06-E4-88-C4-50-5B-E5-4D-CC-C9-32-C7-8F-BB' == 'G????P[?M??2???'.
System.ArgumentException: 
   at DES_Decryptor.Decrypt(SqlString CipherText, SqlBinary DES_Key, SqlBinary DES_IV)
.

输入处理看起来不错:base64 解码字节匹配,传入的密钥和 IV 的二进制版本也是如此。所以,在我看来,当从 CLR 调用 C# DES 解密例程时,它出现了问题标量UDF,但我很沮丧并且完全没有想法。关于这里可能出现什么问题的任何线索?

标签: c#sql-servercryptographysqlclrdes

解决方案


在两个地方都复制了您的结果并更改了一些内容但未更改输出后,我检查了两组代码,确保它们相同并发现了问题:

在对 的调用中new CryptoStream(),您des.CreateDecryptor(Key, IV)在控制台应用程序中使用(正确),但des.CreateEncryptor(Key, IV)在 SQLCLR 函数中使用(不同且不正确)。将 SQLCLR 函数更改为使用des.CreateDecryptor(Key, IV)会导致预期的输出。

关于代码的一些一般说明:

  1. 您应该使用类型的Value属性Sql*(即输入参数)而不是调用ToString()或强制转换。例如:
    string cipherText = CipherText.Value;
    byte[] IV = DES_IV.Value;
    byte[] Key = DES_Key.Value;
    
  2. 您应该将,的实例包装在块中MemoryStream,以便正确清理外部资源。所有 3 个都实现了 IDisposable 接口。DESCryptoServiceProviderCryptoStreamusing()
  3. 由于NULL3 个输入参数中的任何一个传入的 a 都将返回 a NULL,因此您可以通过在创建 T-SQL 包装函数时设置一个选项来绕过需要在代码中处理它。当然,这不能通过 SSDT / Visual Studio 发布操作自动执行,但您可以手动处理部署,在这种情况下您CREATE FUNCTION自己发布,或者您可以添加发布后部署脚本来执行ALTER FUNCTION. 因此,从代码中删除它:
    if (CipherText.IsNull || DES_Key.IsNull || DES_IV.IsNull)
        return SqlString.Null;
    
    并将以下内容添加到发布后推出 SQL 脚本(SSDT / Visual Studio 至少会处理):
    ALTER FUNCTION [dbo].[DES_Decrypt](
        @CipherText [nvarchar](max),
        @DES_Key    [varbinary](8000),
        @DES_IV [varbinary](8000)
    )
    RETURNS [nvarchar](max)
    WITH EXECUTE AS CALLER, RETURNS NULL ON NULL INPUT
    AS EXTERNAL NAME [YourAssembly].[YourClass].[DES_Decrypt];
    
    子句中的RETURNS NULL ON NULL INPUT选项WITH具有魔力;-)。你得到的 s越NULL多,效率就越高,因为 SQL Server 不需要调用代码,因为它已经知道答案。请记住,NULL如果任何输入为,则此选项返回 a NULL,因此,如果任何输入参数都应传入 a NULL,则此选项将不起作用。

有关一般使用 SQLCLR 的更多信息,请访问我的网站:SQLCLR Info


推荐阅读