File tree Expand file tree Collapse file tree
lib/semmle/code/powershell/security/cryptography
src/queries/security/cwe-327
test/query-tests/security/cwe-327/WeakAsymmetricKeySize Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -36,3 +36,9 @@ abstract class BlockMode extends CryptographicAlgorithm {
3636 else result = unknownAlgorithm ( )
3737 }
3838}
39+
40+ abstract class AsymmetricKeyCreation extends CryptographicArtifact {
41+ abstract string getAlgorithmName ( ) ;
42+
43+ abstract int getKeySize ( ) ;
44+ }
Original file line number Diff line number Diff line change @@ -194,3 +194,45 @@ class CipherBlockModeEnum extends BlockMode {
194194
195195 override string getName ( ) { result = modeName }
196196}
197+
198+ class RsaCreateKeyCreation extends AsymmetricKeyCreation , DataFlow:: CallNode {
199+ int keySize ;
200+
201+ RsaCreateKeyCreation ( ) {
202+ exists ( string method |
203+ method = [ "create" , "new" ] and
204+ this =
205+ API:: getTopLevelMember ( "system" )
206+ .getMember ( "security" )
207+ .getMember ( "cryptography" )
208+ .getMember ( [ "rsa" , "rsacryptoserviceprovider" ] )
209+ .getMember ( method )
210+ .asCall ( )
211+ ) and
212+ keySize = this .getAnArgument ( ) .asExpr ( ) .getExpr ( ) .( ConstExpr ) .getValueString ( ) .toInt ( )
213+ }
214+
215+ override string getAlgorithmName ( ) { result = "rsa" }
216+
217+ override int getKeySize ( ) { result = keySize }
218+ }
219+
220+ class RsaCspObjectKeyCreation extends AsymmetricKeyCreation , CryptoAlgorithmObjectCreation {
221+ int keySize ;
222+
223+ RsaCspObjectKeyCreation ( ) {
224+ objectName =
225+ [
226+ "system.security.cryptography.rsacryptoserviceprovider" ,
227+ "rsacryptoserviceprovider"
228+ ] and
229+ exists ( DataFlow:: Node arg |
230+ arg = this .getAnArgument ( ) and
231+ keySize = arg .asExpr ( ) .getExpr ( ) .( ConstExpr ) .getValueString ( ) .toInt ( )
232+ )
233+ }
234+
235+ override string getAlgorithmName ( ) { result = "rsa" }
236+
237+ override int getKeySize ( ) { result = keySize }
238+ }
Original file line number Diff line number Diff line change 1+ <!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+ <qhelp >
3+ <overview >
4+ <p >
5+ Modern encryption relies on it being computationally infeasible to break the cipher and
6+ decode a message without the key. As computational power increases, the ability to break
7+ ciphers grows and keys need to become larger.
8+ </p >
9+ <p >
10+ RSA keys smaller than 2048 bits are considered weak and can potentially be broken using
11+ modern hardware. Using such keys compromises the confidentiality and integrity of
12+ encrypted data.
13+ </p >
14+ </overview >
15+
16+ <recommendation >
17+ <p >
18+ Use an RSA key size of at least 2048 bits. For long-term security, consider using
19+ 4096-bit keys.
20+ </p >
21+ <p >
22+ When calling <code >[System.Security.Cryptography.RSA]::Create()</code > or creating an
23+ <code >RSACryptoServiceProvider</code >, always specify a key size of 2048 or greater.
24+ </p >
25+ </recommendation >
26+
27+ <references >
28+ <li >NIST, SP 800-131A: <a href =" https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final" >Transitioning the Use of Cryptographic Algorithms and Key Lengths</a >.</li >
29+ <li >Wikipedia: <a href =" https://en.wikipedia.org/wiki/RSA_(cryptosystem)" >RSA cryptosystem</a >.</li >
30+ <li >CWE-327: <a href =" https://cwe.mitre.org/data/definitions/327.html" >Use of a Broken or Risky Cryptographic Algorithm</a >.</li >
31+ </references >
32+ </qhelp >
Original file line number Diff line number Diff line change 1+ /**
2+ * @name Weak asymmetric key size
3+ * @description Using RSA keys smaller than 2048 bits does not provide adequate security.
4+ * @kind problem
5+ * @problem.severity error
6+ * @security-severity 7.5
7+ * @precision high
8+ * @id powershell/weak-asymmetric-key-size
9+ * @tags security
10+ * external/cwe/cwe-327
11+ */
12+
13+ import powershell
14+ import semmle.code.powershell.dataflow.DataFlow
15+ import semmle.code.powershell.security.cryptography.Concepts
16+
17+ from AsymmetricKeyCreation keyCreation , int keySize
18+ where
19+ keySize = keyCreation .getKeySize ( ) and
20+ keySize < 2048
21+ select keyCreation ,
22+ "RSA key size " + keySize .toString ( ) + " bits is below the minimum of 2048 bits."
Original file line number Diff line number Diff line change 1+ | test.ps1:6:8:6:55 | Call to create | RSA key size 1024 bits is below the minimum of 2048 bits. |
2+ | test.ps1:9:8:9:54 | Call to create | RSA key size 512 bits is below the minimum of 2048 bits. |
3+ | test.ps1:12:8:12:73 | Call to new | RSA key size 1024 bits is below the minimum of 2048 bits. |
Original file line number Diff line number Diff line change 1+ queries/security/cwe-327/WeakAsymmetricKeySize.ql
Original file line number Diff line number Diff line change 1+ # ===================================================================
2+ # ========== TRUE POSITIVES (should trigger alert) ==================
3+ # ===================================================================
4+
5+ # --- Case 1: RSA.Create with 1024-bit key ---
6+ $rsa = [System.Security.Cryptography.RSA ]::Create(1024 ) # BAD
7+
8+ # --- Case 2: RSA.Create with 512-bit key ---
9+ $rsa = [System.Security.Cryptography.RSA ]::Create(512 ) # BAD
10+
11+ # --- Case 3: RSACryptoServiceProvider with 1024-bit key via ::new() ---
12+ $rsa = [System.Security.Cryptography.RSACryptoServiceProvider ]::new(1024 ) # BAD
13+
14+ # ===================================================================
15+ # ========== TRUE NEGATIVES (should NOT trigger alert) ==============
16+ # ===================================================================
17+
18+ # --- Safe: RSA.Create with 2048-bit key ---
19+ $rsa = [System.Security.Cryptography.RSA ]::Create(2048 ) # GOOD
20+
21+ # --- Safe: RSA.Create with 4096-bit key ---
22+ $rsa = [System.Security.Cryptography.RSA ]::Create(4096 ) # GOOD
23+
24+ # --- Safe: RSA.Create with no argument (default key size) ---
25+ $rsa = [System.Security.Cryptography.RSA ]::Create() # GOOD
You can’t perform that action at this time.
0 commit comments