-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Python: Port py/weak-crypto-key to use type-tracking #5075
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
4ab61bb
Python: Add a few tests for crypto frameworks
RasmusWL 11cd0db
Python: Add concepts for public-key generation
RasmusWL 1bf9f7d
Python: Add missing annotations to new crypto tests
RasmusWL bd40965
Python: Add modeling for `cryptography` PyPI package
RasmusWL 6e4c627
Python: Add modeling for `pycryptodomex` PyPI package
RasmusWL d5ff477
Python: Add modeling for `pycryptodome` PyPI package
RasmusWL 2429c6c
Python: Rewrite py/weak-crypto-key tests
RasmusWL 46ad611
Python: Port py/weak-crypto-key to use type-tracking
RasmusWL 0e9a54e
Python: Rename WeakCrypto to WeakCryptoKey
RasmusWL 32d0790
Python: Use camelCase for RSA/DSA/ECC
RasmusWL 8d3170b
Python: Fix bad join in crypto models
RasmusWL bfbaa85
Python: Add test of public_key method with cryptodome
RasmusWL 1eabfbd
Python: Port cryptography models to use API graphs (mostly)
RasmusWL 2a8f720
Python: Port cryptodome models to use API graphs
RasmusWL 37f0d5a
Python: Make KeyGeneration range member overrides final
RasmusWL a658334
Python: Add weak crypto key example through function call
RasmusWL dfa223a
Python: Better IntegerLiteral tracking for weak crypto key
RasmusWL bfc8ead
Python: Add example of test-code with weak crypto key
RasmusWL d084261
Python: Ignore weak key-sizes from test-code in weak-crypto-key
RasmusWL 40c592a
Python: Introduce DataFlowOnlyInternalUse to avoid re-evaluation
RasmusWL fd18fd8
Python: Apply suggestions from code review
RasmusWL 2798771
Merge branch 'main' into crypto
RasmusWL c195c64
Python: Use type-tracking for integer literal tracking
RasmusWL 4610b1b
Pyhton: Use type back-tracking for keysize on key-generation
RasmusWL 472ff97
Docs: Add crypto to supported Python frameworks
RasmusWL 7b92012
Python: Apply suggestions from code review
RasmusWL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
lgtm,codescanning | ||
* Updated _Use of weak cryptographic key_ (`py/weak-crypto-key`) query to use the new type-tracking approach instead of points-to analysis. You may see differences in the results found by the query, but overall this change should result in a more robust and accurate analysis. | ||
* Renamed the query file for _Use of weak cryptographic key_ (`py/weak-crypto-key`) from `WeakCrypto.ql` to `WeakCryptoKey.ql` (in the `python/ql/src/Security/CWE-326/` folder). This will affect any custom query suites that include or exclude this query using its path. |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @name Use of weak cryptographic key | ||
* @description Use of a cryptographic key that is too small may allow the encryption to be broken. | ||
* @kind problem | ||
* @problem.severity error | ||
* @precision high | ||
* @id py/weak-crypto-key | ||
* @tags security | ||
* external/cwe/cwe-326 | ||
*/ | ||
|
||
import python | ||
import semmle.python.Concepts | ||
import semmle.python.dataflow.new.DataFlow | ||
import semmle.python.filters.Tests | ||
|
||
from Cryptography::PublicKey::KeyGeneration keyGen, int keySize, DataFlow::Node origin | ||
where | ||
keySize = keyGen.getKeySizeWithOrigin(origin) and | ||
keySize < keyGen.minimumSecureKeySize() and | ||
not origin.getScope().getScope*() instanceof TestScope | ||
select keyGen, | ||
"Creation of an " + keyGen.getName() + " key uses $@ bits, which is below " + | ||
keyGen.minimumSecureKeySize() + " and considered breakable.", origin, keySize.toString() |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Provides classes modeling security-relevant aspects of | ||
* - the `pycryptodome` PyPI package (imported as `Crypto`) | ||
* - the `pycryptodomex` PyPI package (imported as `Cryptodome`) | ||
* See https://pycryptodome.readthedocs.io/en/latest/. | ||
*/ | ||
|
||
private import python | ||
private import semmle.python.dataflow.new.DataFlow | ||
private import semmle.python.Concepts | ||
private import semmle.python.ApiGraphs | ||
|
||
/** | ||
* Provides models for | ||
* - the `pycryptodome` PyPI package (imported as `Crypto`) | ||
* - the `pycryptodomex` PyPI package (imported as `Cryptodome`) | ||
* See https://pycryptodome.readthedocs.io/en/latest/ | ||
*/ | ||
private module CryptodomeModel { | ||
// --------------------------------------------------------------------------- | ||
/** | ||
* A call to `Cryptodome.PublicKey.RSA.generate`/`Crypto.PublicKey.RSA.generate` | ||
* | ||
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/rsa.html#Crypto.PublicKey.RSA.generate | ||
*/ | ||
class CryptodomePublicKeyRsaGenerateCall extends Cryptography::PublicKey::KeyGeneration::RsaRange, | ||
DataFlow::CallCfgNode { | ||
CryptodomePublicKeyRsaGenerateCall() { | ||
this = | ||
API::moduleImport(["Crypto", "Cryptodome"]) | ||
.getMember("PublicKey") | ||
.getMember("RSA") | ||
.getMember("generate") | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getKeySizeArg() { | ||
result in [this.getArg(0), this.getArgByName("bits")] | ||
} | ||
} | ||
|
||
/** | ||
* A call to `Cryptodome.PublicKey.DSA.generate`/`Crypto.PublicKey.DSA.generate` | ||
* | ||
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/dsa.html#Crypto.PublicKey.DSA.generate | ||
*/ | ||
class CryptodomePublicKeyDsaGenerateCall extends Cryptography::PublicKey::KeyGeneration::DsaRange, | ||
DataFlow::CallCfgNode { | ||
CryptodomePublicKeyDsaGenerateCall() { | ||
this = | ||
API::moduleImport(["Crypto", "Cryptodome"]) | ||
.getMember("PublicKey") | ||
.getMember("DSA") | ||
.getMember("generate") | ||
.getACall() | ||
} | ||
|
||
override DataFlow::Node getKeySizeArg() { | ||
result in [this.getArg(0), this.getArgByName("bits")] | ||
} | ||
} | ||
|
||
/** | ||
* A call to `Cryptodome.PublicKey.ECC.generate`/`Crypto.PublicKey.ECC.generate` | ||
* | ||
* See https://pycryptodome.readthedocs.io/en/latest/src/public_key/ecc.html#Crypto.PublicKey.ECC.generate | ||
*/ | ||
class CryptodomePublicKeyEccGenerateCall extends Cryptography::PublicKey::KeyGeneration::EccRange, | ||
DataFlow::CallCfgNode { | ||
CryptodomePublicKeyEccGenerateCall() { | ||
this = | ||
API::moduleImport(["Crypto", "Cryptodome"]) | ||
.getMember("PublicKey") | ||
.getMember("ECC") | ||
.getMember("generate") | ||
.getACall() | ||
} | ||
|
||
/** Gets the argument that specifies the curve to use (a string). */ | ||
DataFlow::Node getCurveArg() { result = this.getArgByName("curve") } | ||
|
||
/** Gets the name of the curve to use, as well as the origin that explains how we obtained this name. */ | ||
string getCurveWithOrigin(DataFlow::Node origin) { | ||
yoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exists(StrConst str | origin = DataFlow::exprNode(str) | | ||
origin = this.getCurveArg().getALocalSource() and | ||
result = str.getText() | ||
) | ||
} | ||
|
||
override int getKeySizeWithOrigin(DataFlow::Node origin) { | ||
exists(string curve | curve = this.getCurveWithOrigin(origin) | | ||
// using list from https://pycryptodome.readthedocs.io/en/latest/src/public_key/ecc.html | ||
curve in ["NIST P-256", "p256", "P-256", "prime256v1", "secp256r1"] and result = 256 | ||
or | ||
curve in ["NIST P-384", "p384", "P-384", "prime384v1", "secp384r1"] and result = 384 | ||
or | ||
curve in ["NIST P-521", "p521", "P-521", "prime521v1", "secp521r1"] and result = 521 | ||
) | ||
} | ||
|
||
// Note: There is not really a key-size argument, since it's always specified by the curve. | ||
override DataFlow::Node getKeySizeArg() { none() } | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.