org.sonar.l10n.py.rules.python.S5547.html Maven / Gradle / Ivy
This vulnerability makes it possible that the cleartext of the encrypted message might be recoverable without prior knowledge of the key.
Why is this an issue?
Encryption algorithms are essential for protecting sensitive information and ensuring secure communication in various domains. They are used for
several important reasons:
- Confidentiality, privacy, and intellectual property protection
- Security during transmission or on storage devices
- Data integrity, general trust, and authentication
When selecting encryption algorithms, tools, or combinations, you should also consider two things:
- No encryption is unbreakable.
- The strength of an encryption algorithm is usually measured by the effort required to crack it within a reasonable time frame.
For these reasons, as soon as cryptography is included in a project, it is important to choose encryption algorithms that are considered strong and
secure by the cryptography community.
What is the potential impact?
The cleartext of an encrypted message might be recoverable. Additionally, it might be possible to modify the cleartext of an encrypted message.
Below are some real-world scenarios that illustrate some impacts of an attacker exploiting the vulnerability.
Theft of sensitive data
The encrypted message might contain data that is considered sensitive and should not be known to third parties.
By using a weak algorithm the likelihood that an attacker might be able to recover the cleartext drastically increases.
Additional attack surface
By modifying the cleartext of the encrypted message it might be possible for an attacker to trigger other vulnerabilities in the code. Encrypted
values are often considered trusted, since under normal circumstances it would not be possible for a third party to modify them.
How to fix it in Cryptodome
Code examples
The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.
Noncompliant code example
from Crypto.Cipher import DES # pycryptodome
from Cryptodome.Cipher import DES # pycryptodomex
cipher = DES.new(key, DES.MODE_OFB) # Noncompliant
Compliant solution
from Crypto.Cipher import AES # pycryptodome
from Cryptodome.Cipher import AES # pycryptodomex
cipher = AES.new(key, AES.MODE_CCM)
How does this work?
Use a secure algorithm
It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
algorithm is the Advanced Encryption Standard (AES).
For block ciphers, it is not recommended to use algorithms with a block size that is smaller than 128 bits.
How to fix it in pyca
Code examples
The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.
Noncompliant code example
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
from cryptography.hazmat.backends import default_backend
cipher = Cipher(algorithms.TripleDES(key), mode=None, backend=default_backend()) # Noncompliant
Compliant solution
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
How does this work?
Use a secure algorithm
It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
algorithm is the Advanced Encryption Standard (AES).
For block ciphers, it is not recommended to use algorithms with a block size that is smaller than 128 bits.
How to fix it in PyCrypto
Code examples
The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.
Noncompliant code example
from Crypto.Cipher import DES
cipher = DES.new(key) # Noncompliant
Compliant solution
PyCrypto is deprecated, thus it is recommended to use another library like pyca.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
How does this work?
Use a secure algorithm
It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
algorithm is the Advanced Encryption Standard (AES).
For block ciphers, it is not recommended to use algorithms with a block size that is smaller than 128 bits.
How to fix it in pyDes
Code examples
The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.
Noncompliant code example
import pyDes
cipher = pyDes.des(key) # Noncompliant
Compliant solution
Since pyDes only provides DES, it is recommended to use another library like pyca.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
How does this work?
Use a secure algorithm
It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
algorithm is the Advanced Encryption Standard (AES).
For block ciphers, it is not recommended to use algorithms with a block size that is smaller than 128 bits.
How to fix it in ssl
Code examples
The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.
Noncompliant code example
import ssl
ciphers = 'RC4-SHA:RC4-MD5'
ctx = ssl.create_default_context()
ctx.set_ciphers(ciphers) # Noncompliant
Compliant solution
import ssl
ctx = ssl.create_default_context()
How does this work?
It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time.
Resources
Standards
- OWASP - Top 10 2021 Category A2 - Cryptographic Failures
- OWASP - Top 10 2017 Category A3 - Sensitive Data
Exposure
- OWASP - Top 10 2017 Category A6 - Security
Misconfiguration
- CWE - CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
- STIG Viewer - Application Security and
Development: V-222396 - The application must implement DoD-approved encryption to protect the confidentiality of remote access sessions.