Skip to content

Commit bc073eb

Browse files
committed
python: update py/weak-cryptographic-algorithm to flag use of ECB block mode
1 parent da13544 commit bc073eb

4 files changed

Lines changed: 29 additions & 8 deletions

File tree

python/ql/src/Security/CWE-327/BrokenCryptoAlgorithm.ql

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@
1313
import python
1414
import semmle.python.Concepts
1515

16-
from Cryptography::CryptographicOperation operation, Cryptography::CryptographicAlgorithm algorithm
16+
from
17+
Cryptography::CryptographicOperation operation, Cryptography::CryptographicAlgorithm algorithm,
18+
string msgPrefix
1719
where
1820
algorithm = operation.getAlgorithm() and
19-
algorithm.isWeak() and
2021
// `Cryptography::HashingAlgorithm` and `Cryptography::PasswordHashingAlgorithm` are
2122
// handled by `py/weak-sensitive-data-hashing`
22-
algorithm instanceof Cryptography::EncryptionAlgorithm
23-
select operation,
24-
"The cryptographic algorithm " + algorithm.getName() +
25-
" is broken or weak, and should not be used."
23+
algorithm instanceof Cryptography::EncryptionAlgorithm and
24+
(
25+
algorithm.isWeak() and
26+
msgPrefix = "The cryptographic algorithm " + operation.getAlgorithm().getName()
27+
)
28+
or
29+
operation.getBlockMode().isWeak() and msgPrefix = "The block mode " + operation.getBlockMode()
30+
select operation, msgPrefix + " is broken or weak, and should not be used."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
| test_cryptodome.py:11:13:11:42 | ControlFlowNode for Attribute() | The cryptographic algorithm ARC4 is broken or weak, and should not be used. |
2+
| test_cryptodome.py:16:13:16:42 | ControlFlowNode for Attribute() | The block mode ECB is broken or weak, and should not be used. |
23
| test_cryptography.py:13:13:13:44 | ControlFlowNode for Attribute() | The cryptographic algorithm ARC4 is broken or weak, and should not be used. |
4+
| test_cryptography.py:22:13:22:58 | ControlFlowNode for Attribute() | The block mode ECB is broken or weak, and should not be used. |

python/ql/test/query-tests/Security/CWE-327-BrokenCryptoAlgorithm/test_cryptodome.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# snippet from python/ql/test/experimental/library-tests/frameworks/cryptodome/test_rc4.py
2-
from Cryptodome.Cipher import ARC4
2+
from Cryptodome.Cipher import ARC4, AES
33

44
import os
55

@@ -11,3 +11,8 @@
1111
encrypted = cipher.encrypt(secret_message) # NOT OK
1212

1313
print(secret_message, encrypted)
14+
15+
cipher = AES.new(key, AES.MODE_ECB)
16+
encrypted = cipher.encrypt(secret_message) # NOT OK
17+
18+
print(secret_message, encrypted)

python/ql/test/query-tests/Security/CWE-327-BrokenCryptoAlgorithm/test_cryptography.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# snippet from python/ql/test/experimental/library-tests/frameworks/cryptography/test_rc4.py
2-
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher
2+
from cryptography.hazmat.primitives.ciphers import algorithms, modes, Cipher
33
import os
44

55
key = os.urandom(256//8)
@@ -14,3 +14,12 @@
1414
encrypted += encryptor.finalize()
1515

1616
print(secret_message, encrypted)
17+
18+
algorithm = algorithms.AES(key)
19+
cipher = Cipher(algorithm, mode=modes.ECB())
20+
21+
encryptor = cipher.encryptor()
22+
encrypted = encryptor.update(secret_message + b'\x80\x00') # NOT OK
23+
encrypted += encryptor.finalize()
24+
25+
print(secret_message, encrypted)

0 commit comments

Comments
 (0)