Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
)
}
}
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"]
22 changes: 22 additions & 0 deletions languages/cpp/custom/src/WeakPrng/WeakPrng.ql
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."
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
Comment thread
felickz marked this conversation as resolved.
Resolved
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()
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()
2 changes: 2 additions & 0 deletions languages/cpp/custom/src/qlpack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ version: 0.0.2
library: false
dependencies:
codeql/cpp-all: "*"
dataExtensions:
- "**/*.model.yml"
2 changes: 2 additions & 0 deletions languages/cpp/custom/test/WeakPrng/WeakPrng.expected
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. |
2 changes: 2 additions & 0 deletions languages/cpp/custom/test/WeakPrng/WeakPrng.qlref
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
query: WeakPrng/WeakPrng.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
26 changes: 26 additions & 0 deletions languages/cpp/custom/test/WeakPrng/test.c
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);
}
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 |
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
query: WinCryptRsaWithoutOaep/WinCryptRsaWithoutOaep.ql
postprocess: utils/test/InlineExpectationsTestQuery.ql
59 changes: 59 additions & 0 deletions languages/cpp/custom/test/WinCryptRsaWithoutOaep/test.c
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);
}
Loading
Loading