【问题标题】:Is this code vulnerable to a padding oracle attack?此代码是否容易受到填充预言机攻击?
【发布时间】:2019-09-22 17:40:21
【问题描述】:

以下代码是否容易受到填充 oracle 攻击,因为无论填充是否有效(CBC、PKCS#7)都会返回?

代码直接取自微软网页,可以在 dotnetfiddle.net 等在线编译器上轻松运行。

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

namespace RijndaelManaged_Example
{
    class RijndaelExample
    {
        public static void Main()
        {
            try
            {

                string original = "Here is some data to encrypt!";

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization 
                // vector (IV).
                using (RijndaelManaged myRijndael = new RijndaelManaged())
                {

                    myRijndael.GenerateKey();
                    myRijndael.GenerateIV();
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    // Decrypt the bytes to a string.
                    string roundtrip = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Original:   {0}", original);
                    Console.WriteLine("Round Trip: {0}", roundtrip);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
        }
        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create an encryptor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {

                            //Write all data to the stream.
                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;

                // Create a decryptor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;

        }
    }
}

我目前正在研究的程序使用上面的代码来加密一些 XML 文件。这是该程序的示例文件(只有一个元素的 XML 文件)。

【问题讨论】:

  • 欢迎来到密码学。你可以看看How does a padding oracle attack work?
  • 我已经删除了最新的附加问题,例如攻击代码,因为这在 StackOverflow 上是明确的主题。

标签: c# encryption aes padding padding-oracle-attack


【解决方案1】:

是的,代码很容易受到攻击,因为 .NET 默认为 CBC,默认情况下带有 PKCS#7 兼容的填充。您可以通过更改密文(它的最后 16 个字节)并检查是否引发了异常来轻松测试这一点。请注意,错误条件对于填充预言机的工作不是必需的,时间差异可能已经泄漏了足够的信息。

当然,这并不一定意味着使用该代码的系统易受攻击。如果代码用于对静态数据执行加密(例如文件加密),则很可能无法构造填充预言,并且无法满足攻击的必要条件。

请注意,填充预言机攻击是一种特定类型的明文预言机攻击。即使使用不同的分组密码模式,其他攻击也是可能的。通常,您需要经过身份验证的加密来确保无法使用纯文本预言机:只有在验证了消息完整性和真实性之后才采取行动。

所示代码对于传输模式的安全性并不安全。当然,为了获得安全的传输安全性,CBC 填充预言机的可能性只是众多漏洞之一;可以说代码片段根本没有证明传输安全性。

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • 预言机是否必须有权访问此私钥才能工作?
猜你喜欢
  • 2015-12-31
  • 1970-01-01
  • 2016-04-29
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
相关资源
最近更新 更多