Repository navigation
[NEW DATA EXTENSION] C/C++: WinCrypt weak-algorithm, RSA-without-OAEP and weak-PRNG models + queries #326
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
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/cppdata-extension-createweak-crypto-randomness
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[NEW DATA EXTENSION] C/C++: WinCrypt weak-algorithm, RSA-without-OAEP and weak-PRNG models + queries #326
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
88 changes: 88 additions & 0 deletions
88
languages/cpp/custom/src/WeakCryptoRandomness/WeakCryptoRandomness.qll
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,88 @@ | ||
| /** | ||
| * Provides extensible predicates and classes for modeling cryptographic algorithm | ||
| * selection, RSA padding selection, and non-cryptographic pseudo-random number | ||
| * generators in C and C++ APIs such as the Windows CryptoAPI (`wincrypt.h`). | ||
| * | ||
| * Models are supplied as data extensions (see `weak-crypto-randomness.model.yml`). | ||
| */ | ||
|
|
||
| import cpp | ||
| import semmle.code.cpp.dataflow.new.DataFlow | ||
|
|
||
| /** | ||
| * Holds if argument `algorithmArg` (0-based) of the global function `function` | ||
| * selects a cryptographic algorithm, for example the `Algid` argument of | ||
| * `CryptCreateHash`. | ||
| */ | ||
| extensible predicate cryptoAlgorithmArgumentModel(string function, int algorithmArg); | ||
|
|
||
| /** | ||
| * Holds if the macro or enum constant named `constant` selects the cryptographic | ||
| * algorithm `algorithm`, which is of kind `kind`. Supported kinds are | ||
| * `weak-cipher`, `weak-hash` and `rsa`. | ||
| */ | ||
| extensible predicate cryptoAlgorithmConstantModel(string constant, string algorithm, string kind); | ||
|
|
||
| /** | ||
| * Holds if the global function `function` encrypts or decrypts with the key | ||
| * passed as argument `keyArg`, and the padding mode is selected by the flags | ||
| * passed as argument `flagsArg`, where setting the bits in `oaepFlag` selects | ||
| * OAEP padding. | ||
| */ | ||
| extensible predicate rsaPaddingFlagsModel(string function, int keyArg, int flagsArg, int oaepFlag); | ||
|
|
||
| /** | ||
| * Holds if the global (or `std`) function `function` is a statistical, | ||
| * non-cryptographic pseudo-random number generator. | ||
| */ | ||
| extensible predicate weakPrngFunctionModel(string function); | ||
|
|
||
| /** | ||
| * An expression that is the expansion of a macro, or an access of an enum | ||
| * constant, that is modeled as selecting a cryptographic algorithm. | ||
| */ | ||
| class CryptoAlgorithmConstant extends Expr { | ||
| string name; | ||
| string algorithm; | ||
| string kind; | ||
|
|
||
| CryptoAlgorithmConstant() { | ||
| cryptoAlgorithmConstantModel(name, algorithm, kind) and | ||
| ( | ||
| exists(MacroInvocation mi | mi.getMacroName() = name and mi.getExpr() = this) | ||
| or | ||
| this.(EnumConstantAccess).getTarget().getName() = name | ||
| ) | ||
| } | ||
|
|
||
| /** Gets the name of the macro or enum constant. */ | ||
| string getConstantName() { result = name } | ||
|
|
||
| /** Gets the name of the selected algorithm. */ | ||
| string getAlgorithm() { result = algorithm } | ||
|
|
||
| /** Gets the kind of the selected algorithm. */ | ||
| string getKind() { result = kind } | ||
| } | ||
|
|
||
| /** | ||
| * Holds if `call` selects the cryptographic algorithm specified by `constant` | ||
| * through a modeled algorithm argument. | ||
| */ | ||
| predicate selectsCryptoAlgorithm(FunctionCall call, CryptoAlgorithmConstant constant) { | ||
| exists(string function, int algorithmArg | | ||
| cryptoAlgorithmArgumentModel(function, algorithmArg) and | ||
| call.getTarget().hasGlobalName(function) and | ||
| DataFlow::localExprFlow(constant, call.getArgument(algorithmArg)) | ||
| ) | ||
| } | ||
|
|
||
| /** A call to a function modeled as a non-cryptographic pseudo-random number generator. */ | ||
| class WeakPrngCall extends FunctionCall { | ||
| WeakPrngCall() { | ||
| exists(string function | | ||
| weakPrngFunctionModel(function) and | ||
| this.getTarget().hasGlobalOrStdName(function) | ||
| ) | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.yml
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,52 @@ | ||
| extensions: | ||
| # Windows CryptoAPI (wincrypt.h): arguments that select an `ALG_ID`. | ||
| - addsTo: | ||
| pack: languages-cpp-custom-src | ||
| extensible: cryptoAlgorithmArgumentModel | ||
| data: | ||
| - ["CryptCreateHash", 1] | ||
| - ["CryptDeriveKey", 1] | ||
| - ["CryptGenKey", 1] | ||
|
|
||
| # Windows CryptoAPI (wincrypt.h): `CALG_*` algorithm identifiers. | ||
| - addsTo: | ||
| pack: languages-cpp-custom-src | ||
| extensible: cryptoAlgorithmConstantModel | ||
| data: | ||
| - ["CALG_DES", "DES", "weak-cipher"] | ||
| - ["CALG_DESX", "DESX", "weak-cipher"] | ||
| - ["CALG_3DES", "3DES", "weak-cipher"] | ||
| - ["CALG_3DES_112", "3DES", "weak-cipher"] | ||
| - ["CALG_RC2", "RC2", "weak-cipher"] | ||
| - ["CALG_RC4", "RC4", "weak-cipher"] | ||
| - ["CALG_RC5", "RC5", "weak-cipher"] | ||
| - ["CALG_MD2", "MD2", "weak-hash"] | ||
| - ["CALG_MD4", "MD4", "weak-hash"] | ||
| - ["CALG_MD5", "MD5", "weak-hash"] | ||
| - ["CALG_SHA", "SHA-1", "weak-hash"] | ||
| - ["CALG_SHA1", "SHA-1", "weak-hash"] | ||
| - ["CALG_RSA_KEYX", "RSA", "rsa"] | ||
| - ["CALG_RSA_SIGN", "RSA", "rsa"] | ||
|
|
||
| # Windows CryptoAPI (wincrypt.h): `CRYPT_OAEP` (0x40) selects OAEP padding for RSA keys. | ||
| - addsTo: | ||
| pack: languages-cpp-custom-src | ||
| extensible: rsaPaddingFlagsModel | ||
| data: | ||
| - ["CryptEncrypt", 0, 3, 64] | ||
| - ["CryptDecrypt", 0, 3, 64] | ||
|
|
||
| # C standard library and POSIX statistical pseudo-random number generators. | ||
| - addsTo: | ||
| pack: languages-cpp-custom-src | ||
| extensible: weakPrngFunctionModel | ||
| data: | ||
| - ["rand"] | ||
| - ["rand_r"] | ||
| - ["random"] | ||
| - ["drand48"] | ||
| - ["erand48"] | ||
| - ["lrand48"] | ||
| - ["nrand48"] | ||
| - ["mrand48"] | ||
| - ["jrand48"] |
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,22 @@ | ||
| /** | ||
| * @name Use of a non-cryptographic pseudo-random number generator | ||
| * @description Statistical pseudo-random number generators such as `rand()` produce | ||
| * predictable output and must not be used where unpredictability is | ||
| * required for security. | ||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @security-severity 7.8 | ||
| * @precision low | ||
| * @id cpp/custom/weak-prng | ||
| * @tags security | ||
| * external/cwe/cwe-338 | ||
| */ | ||
|
|
||
| import cpp | ||
| import WeakCryptoRandomness.WeakCryptoRandomness | ||
|
|
||
| from WeakPrngCall call | ||
| where not call instanceof ExprInVoidContext | ||
| select call, | ||
| "Call to " + call.getTarget().getName() + | ||
| ", a non-cryptographic pseudo-random number generator whose output is predictable." |
50 changes: 50 additions & 0 deletions
50
languages/cpp/custom/src/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql
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,50 @@ | ||
| /** | ||
| * @name Use of RSA without OAEP padding with the Windows CryptoAPI | ||
| * @description Encrypting or decrypting with an RSA key without the `CRYPT_OAEP` | ||
| * flag uses PKCS #1 v1.5 padding, which is vulnerable to adaptive | ||
| * chosen-ciphertext (Bleichenbacher) attacks. | ||
| * @kind problem | ||
| * @problem.severity error | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id cpp/custom/wincrypt-rsa-without-oaep | ||
| * @tags security | ||
| * external/cwe/cwe-780 | ||
| */ | ||
|
|
||
| import cpp | ||
| import WeakCryptoRandomness.WeakCryptoRandomness | ||
|
|
||
| predicate keyCreationFlowsTo(FunctionCall keyCreation, FunctionCall crypt, int keyArg) { | ||
| exists(DataFlow::Node keyDefinition | | ||
| ( | ||
| keyCreation.getTarget().hasGlobalName("CryptGenKey") and | ||
| keyDefinition.asDefiningArgument() = keyCreation.getArgument(3) | ||
| or | ||
| keyCreation.getTarget().hasGlobalName("CryptDeriveKey") and | ||
| keyDefinition.asDefiningArgument() = keyCreation.getArgument(4) | ||
| ) and | ||
| DataFlow::localFlow(keyDefinition, DataFlow::exprNode(crypt.getArgument(keyArg))) | ||
| ) | ||
| } | ||
|
|
||
| from | ||
| FunctionCall crypt, FunctionCall keyCreation, CryptoAlgorithmConstant rsa, string function, | ||
| int keyArg, int flagsArg, int oaepFlag | ||
| where | ||
| rsaPaddingFlagsModel(function, keyArg, flagsArg, oaepFlag) and | ||
| crypt.getTarget().hasGlobalName(function) and | ||
| keyCreationFlowsTo(keyCreation, crypt, keyArg) and | ||
| crypt.getArgument(flagsArg).getValue().toInt().bitAnd(oaepFlag) = 0 and | ||
| selectsCryptoAlgorithm(keyCreation, rsa) and | ||
| rsa.getKind() = "rsa" and | ||
| not exists(FunctionCall otherKeyCreation | | ||
| otherKeyCreation != keyCreation and | ||
| keyCreationFlowsTo(otherKeyCreation, crypt, keyArg) | ||
| ) and | ||
| not ( | ||
| function = "CryptEncrypt" and | ||
| crypt.getArgument(4).getValue().toInt() = 0 | ||
| ) | ||
| select crypt, "Call to " + function + " uses an RSA key (created by $@) without OAEP padding.", | ||
| keyCreation, keyCreation.getTarget().getName() | ||
31 changes: 31 additions & 0 deletions
31
languages/cpp/custom/src/WinCryptWeakAlgorithm/WinCryptWeakAlgorithm.ql
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,31 @@ | ||
| /** | ||
| * @name Use of a weak cryptographic algorithm with the Windows CryptoAPI | ||
| * @description Selecting a broken or risky cipher (such as DES, 3DES, RC2 or RC4) | ||
| * or a broken hash algorithm (such as MD5 or SHA-1) when creating a | ||
| * CryptoAPI key or hash allows an attacker to compromise security. | ||
| * @kind problem | ||
| * @problem.severity error | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id cpp/custom/wincrypt-weak-algorithm | ||
| * @tags security | ||
| * external/cwe/cwe-327 | ||
| * external/cwe/cwe-328 | ||
| */ | ||
|
|
||
| import cpp | ||
| import WeakCryptoRandomness.WeakCryptoRandomness | ||
|
|
||
| from FunctionCall call, CryptoAlgorithmConstant constant, string description | ||
| where | ||
| selectsCryptoAlgorithm(call, constant) and | ||
| ( | ||
| constant.getKind() = "weak-cipher" and | ||
| description = "broken or risky cipher " + constant.getAlgorithm() | ||
| or | ||
| constant.getKind() = "weak-hash" and | ||
| description = "broken hash algorithm " + constant.getAlgorithm() | ||
| ) | ||
| select call, | ||
| "Call to " + call.getTarget().getName() + " uses the " + description + " (selected by $@).", | ||
| constant, constant.getConstantName() |
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 |
|---|---|---|
|
|
@@ -3,3 +3,5 @@ version: 0.0.2 | |
| library: false | ||
| dependencies: | ||
| codeql/cpp-all: "*" | ||
| dataExtensions: | ||
| - "**/*.model.yml" | ||
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,2 @@ | ||
| | test.c:9:14:9:17 | call to rand | Call to rand, a non-cryptographic pseudo-random number generator whose output is predictable. | | ||
| | test.c:14:10:14:15 | call to random | Call to random, a non-cryptographic pseudo-random number generator whose output is predictable. | |
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,2 @@ | ||
| query: WeakPrng/WeakPrng.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
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,26 @@ | ||
| #include "../WinCryptStubs/wincrypt.h" | ||
|
|
||
| int rand(void); | ||
| long random(void); | ||
| void srand(unsigned int seed); | ||
| void printIntLine(int value); | ||
|
|
||
| void rand_bad(void) { | ||
| int data = rand(); // $ Alert | ||
| printIntLine(data); | ||
| } | ||
|
|
||
| long random_bad(void) { | ||
| return random(); // $ Alert | ||
| } | ||
|
|
||
| void discarded_rand_good(void) { | ||
| srand(42); | ||
| rand(); | ||
| } | ||
|
|
||
| void crypt_gen_random_good(HCRYPTPROV hProv) { | ||
| unsigned int data; | ||
| CryptGenRandom(hProv, sizeof(data), (BYTE *)&data); | ||
| printIntLine(data); | ||
| } |
3 changes: 3 additions & 0 deletions
3
languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.expected
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 @@ | ||
| | test.c:6:3:6:14 | call to CryptEncrypt | Call to CryptEncrypt uses an RSA key (created by $@) without OAEP padding. | test.c:5:3:5:16 | call to CryptDeriveKey | CryptDeriveKey | | ||
| | test.c:12:3:12:14 | call to CryptDecrypt | Call to CryptDecrypt uses an RSA key (created by $@) without OAEP padding. | test.c:11:3:11:13 | call to CryptGenKey | CryptGenKey | | ||
| | test.c:52:3:52:14 | call to CryptEncrypt | Call to CryptEncrypt uses an RSA key (created by $@) without OAEP padding. | test.c:50:3:50:13 | call to CryptGenKey | CryptGenKey | |
2 changes: 2 additions & 0 deletions
2
languages/cpp/custom/test/WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.qlref
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,2 @@ | ||
| query: WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql | ||
| postprocess: utils/test/InlineExpectationsTestQuery.ql |
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,59 @@ | ||
| #include "../WinCryptStubs/wincrypt.h" | ||
|
|
||
| void rsa_encrypt_bad(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptDeriveKey(hProv, CALG_RSA_SIGN, hHash, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); // $ Alert | ||
| } | ||
|
|
||
| void rsa_decrypt_bad(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); | ||
| CryptDecrypt(hKey, 0, 1, 0, payload, payloadLen); // $ Alert | ||
| } | ||
|
|
||
| void rsa_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptDeriveKey(hProv, CALG_RSA_SIGN, hHash, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, CRYPT_OAEP, payload, payloadLen, 200); | ||
| } | ||
|
|
||
| void rsa_decrypt_good(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); | ||
| CryptDecrypt(hKey, 0, 1, CRYPT_OAEP, payload, payloadLen); | ||
| } | ||
|
|
||
| void aes_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); | ||
| } | ||
|
|
||
| void rsa_then_aes_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); | ||
| CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); | ||
| } | ||
|
|
||
| void aes_then_rsa_encrypt_good(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, 0, payload, payloadLen, 200); | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); | ||
| } | ||
|
|
||
| void copied_rsa_handle_bad(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY rsaKey; | ||
| HCRYPTKEY copiedKey; | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &rsaKey); | ||
| copiedKey = rsaKey; | ||
| CryptEncrypt(copiedKey, 0, 1, 0, payload, payloadLen, 200); // $ Alert | ||
| } | ||
|
|
||
| void rsa_encrypt_sizing_good(HCRYPTPROV hProv, BYTE *payload, DWORD *payloadLen) { | ||
| HCRYPTKEY hKey; | ||
| CryptGenKey(hProv, CALG_RSA_KEYX, 0, &hKey); | ||
| CryptEncrypt(hKey, 0, 1, 0, 0, payloadLen, 200); | ||
| } |
Oops, something went wrong.
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.