Don't use the ECB encryption mode
Matches methods that call the one-shot ECB APIs of SymmetricAlgorithm (Aes, TripleDES, DES, RC2): EncryptEcb(), DecryptEcb(), TryEncryptEcb(), TryDecryptEcb() and GetCiphertextLengthEcb().
In Electronic Codebook (ECB) mode every block is encrypted independently with the same key, hence two identical plaintext blocks produce two identical ciphertext blocks. An attacker can spot repeated structures, swap or replay blocks, and in many cases recover the content without the key. This is CWE-327 Use of a Broken or Risky Cryptographic Algorithm (OWASP Top 10 A02 Cryptographic Failures).
Assigning CipherMode.ECB to the Mode property cannot be detected because enum values are not visible in the compiled code.
Flagged Code
In Electronic Codebook (ECB) mode every block is encrypted independently with the same key, hence two identical plaintext blocks produce two identical ciphertext blocks. An attacker can spot repeated structures, swap or replay blocks, and in many cases recover the content without the key. This is CWE-327 Use of a Broken or Risky Cryptographic Algorithm (OWASP Top 10 A02 Cryptographic Failures).
Assigning CipherMode.ECB to the Mode property cannot be detected because enum values are not visible in the compiled code.
Flagged Code
static byte[] Encrypt(byte[] plaintext, byte[] key) {
using var aes = Aes.Create();
aes.Key = key;
return aes.EncryptEcb(plaintext, PaddingMode.PKCS7); // flagged: ECB
}
Fixed Codestatic byte[] Encrypt(byte[] plaintext, byte[] key, byte[] nonce, out byte[] tag) {
using var aes = new AesGcm(key, AesGcm.TagByteSizes.MaxSize);
var ciphertext = new byte[plaintext.Length];
tag = new byte[AesGcm.TagByteSizes.MaxSize];
aes.Encrypt(nonce, plaintext, ciphertext, tag); // authenticated encryption
return ciphertext;
}How to fix
Use an authenticated encryption mode: AesGcm or AesCcm with a unique nonce per message. When authenticated encryption is not available, use EncryptCbc() with a random IV per message and protect the ciphertext with an HMAC (encrypt-then-MAC).
Never reuse a nonce or an IV with the same key.
Never reuse a nonce or an IV with the same key.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Don't use the ECB encryption mode</Name>
// <Id>ND3132:DontUseEcbEncryptionMode</Id>
warnif count > 0
// Third-party members are part of the code model only when the application uses them.
// Since .NET 6 the one-shot APIs carry the mode in their name: EncryptEcb(), DecryptEcb(),
// TryEncryptEcb(), TryDecryptEcb(), GetCiphertextLengthEcb().
// Setting SymmetricAlgorithm.Mode = CipherMode.ECB is not detectable: enum values are not visible in IL.
let ecbMembers = ThirdParty.Methods.Where(m =>
m.ParentType.FullName.StartsWith("System.Security.Cryptography.") &&
m.SimpleNameEqualsAny("EncryptEcb", "DecryptEcb", "TryEncryptEcb", "TryDecryptEcb", "GetCiphertextLengthEcb"))
.ToHashSetEx()
where ecbMembers.Any()
from m in Application.Methods.UsingAny(ecbMembers)
let ecbMembersUsed = m.MethodsCalled.Intersect(ecbMembers).ToArray()
select new {
m,
ecbMembersUsed,
Debt = 20.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The method *{0}* calls *{1}*, which encrypts or decrypts with the ECB mode.
// ECB encrypts identical plaintext blocks into identical ciphertext blocks,
// so patterns of the plaintext remain visible in the ciphertext.
//</Expl>
// <Desc>
// Matches methods that call the one-shot ECB APIs of *SymmetricAlgorithm*
// (*Aes*, *TripleDES*, *DES*, *RC2*): *EncryptEcb()*, *DecryptEcb()*,
// *TryEncryptEcb()*, *TryDecryptEcb()* and *GetCiphertextLengthEcb()*.
//
// In Electronic Codebook (ECB) mode every block is encrypted independently
// with the same key, hence two identical plaintext blocks produce two
// identical ciphertext blocks. An attacker can spot repeated structures,
// swap or replay blocks, and in many cases recover the content without
// the key. This is CWE-327 *Use of a Broken or Risky Cryptographic
// Algorithm* (OWASP Top 10 A02 Cryptographic Failures).
//
// Assigning *CipherMode.ECB* to the *Mode* property cannot be detected
// because enum values are not visible in the compiled code.
//
// **Flagged Code**
// ```csharp
// static byte[] Encrypt(byte[] plaintext, byte[] key) {
// using var aes = Aes.Create();
// aes.Key = key;
// return aes.EncryptEcb(plaintext, PaddingMode.PKCS7); // flagged: ECB
// }
// ```
//
// **Fixed Code**
// ```csharp
// static byte[] Encrypt(byte[] plaintext, byte[] key, byte[] nonce, out byte[] tag) {
// using var aes = new AesGcm(key, AesGcm.TagByteSizes.MaxSize);
// var ciphertext = new byte[plaintext.Length];
// tag = new byte[AesGcm.TagByteSizes.MaxSize];
// aes.Encrypt(nonce, plaintext, ciphertext, tag); // authenticated encryption
// return ciphertext;
// }
// ```
// </Desc>
// <HowToFix>
// Use an authenticated encryption mode: *AesGcm* or *AesCcm* with a unique
// nonce per message. When authenticated encryption is not available, use
// *EncryptCbc()* with a random IV per message and protect the ciphertext
// with an HMAC (encrypt-then-MAC).
//
// Never reuse a nonce or an IV with the same key.
// </HowToFix>
Open in the interactive Rules Explorer · All default rules
Security: all 39 items
- Software Composition Analysis (SCA) ND3120
- Transitive Software Composition Analysis (SCA) ND3121
- Don't hard-code secrets in constants ND3114
- Review credentials built from hard-coded values ND3115
- Don't use insecure deserializers ND3110
- Don't use weak hash algorithms (MD5, SHA1) ND3109
- Don't use DES/3DES/RC2 weak cipher algorithms ND3102
- Don't use weak password-based key derivation ND3123
- Don't use the ECB encryption mode ND3132
- Don't disable certificate validation ND3103
- Don't hard-code TLS/SSL protocol versions ND3122
- Review commands vulnerable to SQL injection ND3107
- Review data adapters vulnerable to SQL injection ND3108
- Review raw SQL built from composed strings ND3128
- Review LDAP filters composed from strings ND3137
- Don't enable external XML entity resolution (XXE) ND3111
- Review processes started with a composed arguments string ND3124
- Review web endpoints building file paths from their parameters ND3129
- Review custom archive extraction (Zip Slip) ND3135
- Review assemblies and types loaded from runtime-provided names ND3125
- Review web endpoints without [Authorize] nor [AllowAnonymous] ND3118
- Review state-changing endpoints opened to anonymous callers ND3130
- Review state-changing MVC actions without anti-forgery validation ND3119
- Review HTML-encoding bypasses ND3133
- Review explicitly disabled request validation ND3134
- Review redirects to a URL that is not validated as local ND3126
- Review request-influenced outbound HTTP calls (SSRF) ND3136
- Review insecure ASP.NET Core CORS policies and unguarded developer exception page ND3116
- Review cookies created without HttpOnly and Secure flags ND3117
- Review regular expressions without a match timeout (ReDoS) ND3112
- Review secrets compared with non-constant-time equality ND3113
- Review secrets passed to logging methods ND3127
- Review security operations that log nothing ND3131
- Don't use System.Random for security purposes ND3101
- Don't use CoSetProxyBlanket and CoInitializeSecurity ND3100
- Review publicly visible event handlers ND3104
- Pointers should not be publicly visible ND3105
- Seal methods that satisfy non-public interfaces ND3106
- Restrict sensitive APIs to reviewed components ND3138