Skip to content

Commit 81cac67

Browse files
Improve C++ legacy crypto algorithm query
Adds the name of the detected algorithm to help identify the substring that triggered the detection, and reduces false positives when detecting DES by using a regex to exclude common words like "description" and "nodes".
1 parent d994c7c commit 81cac67

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

cpp/src/crypto/UseOfLegacyAlgorithm.ql

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,35 @@
1111

1212
import cpp
1313

14-
from FunctionCall call
14+
from FunctionCall call, string functionName, string cipherName
1515
where
16-
call.getTarget()
17-
.getQualifiedName()
18-
.toLowerCase()
19-
.matches([
20-
// Hash functions
21-
"%md2%", "%md4%", "%md5%", "%ripemd%", "%sha1%", "%whirlpool%", "%streebog%",
22-
// KDFs
23-
"%pbkdf1%",
24-
// Symmetric ciphers
25-
"%arcfour%", "%blowfish%", "%cast%", "%des%", "%idea%", "%kasumi%",
26-
"%magma%", "%rc2%", "%rc4%", "%tdea%"
27-
])
16+
functionName = call.getTarget()
17+
.getQualifiedName()
18+
.toLowerCase()
19+
and
20+
(
21+
exists(string cn |
22+
cn in [
23+
"MD2", "MD4", "MD5", "RIPEMD", "SHA1", "Whirlpool", "Streebog",
24+
"PBKDF1",
25+
"ArcFour", "Blowfish", "CAST", "DES", "IDEA", "Kasumi",
26+
"Magma", "RC2", "RC4", "TDEA"
27+
]
28+
and cipherName = cn
29+
and functionName.matches("%" + cn.toLowerCase() + "%")
30+
)
31+
/* match DES, but avoid false positives by not matching common terms containing it:
32+
nodes
33+
modes
34+
codes
35+
describe
36+
description
37+
descriptor
38+
design
39+
descend
40+
destroy
41+
*/
42+
or cipherName = "DES" and functionName.regexpMatch(".*(?<!no|mo|co)des(?!cri(be|ption|ptor)|ign|cend|troy).*")
43+
)
2844
select call.getLocation(),
29-
"Potential use of legacy cryptographic algorithm " + call.getTarget().getQualifiedName() +
30-
" detected"
45+
"Potential use of legacy cryptographic algorithm " + cipherName + " detected in function name " + call.getTarget().getQualifiedName()

0 commit comments

Comments
 (0)