Skip to content

[NEW DATA EXTENSION] C/C++: WinCrypt weak-algorithm, RSA-without-OAEP and weak-PRNG models + queries - #326

Draft
felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/cppdata-extension-createweak-crypto-randomness
Draft

felickz with Copilot wants to merge 4 commits into
mainfrom
copilot/cppdata-extension-createweak-crypto-randomness

Conversation

Copilot AI commented Oct 31, 2026 •

Copy link
Copy Markdown

📝 Data Extension Information

  • Language: cpp
  • Extension Name(s): languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.yml
  • Extension Types: Custom extensible predicates in languages-cpp-custom-src (not codeql/cpp-all MaD): cryptoAlgorithmArgumentModel, cryptoAlgorithmConstantModel, rsaPaddingFlagsModel, weakPrngFunctionModel
  • Target Library/Framework: Windows CryptoAPI (wincrypt.h); C/POSIX rand family
  • Library Modules Covered: CryptCreateHash, CryptDeriveKey, CryptGenKey, CryptEncrypt, CryptDecrypt; rand, rand_r, random, *rand48

🎯 Description

What This Data Extension Models

AI Scan found four Juliet C/C++ weak-crypto/randomness findings that default setup did not report (CWE-327/328/338/780). Reproduction showed that codeql/cpp-all MaD rows can't fix them:

  • cpp/weak-cryptographic-algorithm finds weak crypto by matching names. It has no MaD hooks for extension rows to feed.
  • CWE-327 (DES) is already reported by that query when the CALG_DES macro is present. The Juliet miss is likely an extraction problem: <windows.h> probably can't be resolved on a Linux scan. The new queries also need those macros.
  • CWE-328, CWE-338 and CWE-780 have no standard cpp query. The only other match for rand is in ArithmeticUncontrolled.ql (CWE-190), where it's an input source.

So this PR adds model-driven extensible predicates plus three queries that read them:

Query CWE Flags
cpp/custom/wincrypt-weak-algorithm 327, 328 Algorithm argument receives a weak-cipher/weak-hash CALG_* (local flow, so ALG_ID a = CALG_3DES; CryptGenKey(p, a, ...) is caught)
cpp/custom/wincrypt-rsa-without-oaep 780 CryptEncrypt/CryptDecrypt on a key created with CALG_RSA_* where the constant flags lack the OAEP bit
cpp/custom/weak-prng 338 Non-void-context call to a modeled PRNG. @precision low: whether the use is security-relevant can't be decided, so expect noise outside test suites

Adding new APIs (for example BCrypt or OpenSSL) means adding rows to the YAML. The queries don't need to change.

Threat Model

N/A. These are API-misuse checks, not taint tracking.

Example Vulnerable Code

CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);           // weak hash
CryptDeriveKey(hProv, CALG_DES, hHash, 0, &hKey);         // weak cipher
CryptDeriveKey(hProv, CALG_RSA_SIGN, hHash, 0, &hRsa);
CryptEncrypt(hRsa, 0, 1, 0, buf, &len, 200);              // RSA without OAEP
int data = rand();                                        // weak PRNG

Example Safe Code

CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash);
CryptDeriveKey(hProv, CALG_AES_256, hHash, 0, &hKey);
CryptEncrypt(hRsa, 0, 1, CRYPT_OAEP, buf, &len, 200);
CryptGenRandom(hProv, sizeof(v), (BYTE *)&v);
rand();                                                   // result discarded

📦 Extension Details

Extension YAML

extensions:
  - addsTo: { pack: languages-cpp-custom-src, extensible: cryptoAlgorithmArgumentModel }
    data:
      - ["CryptCreateHash", 1]
      - ["CryptDeriveKey", 1]
      - ["CryptGenKey", 1]
  - addsTo: { pack: languages-cpp-custom-src, extensible: cryptoAlgorithmConstantModel }
    data:   # weak-cipher: DES, DESX, 3DES, 3DES_112, RC2, RC4, RC5
            # weak-hash:   MD2, MD4, MD5, SHA, SHA1
            # rsa:         RSA_KEYX, RSA_SIGN
      - ["CALG_MD5", "MD5", "weak-hash"]
  - addsTo: { pack: languages-cpp-custom-src, extensible: rsaPaddingFlagsModel }
    data:
      - ["CryptEncrypt", 0, 3, 64]   # keyArg, flagsArg, CRYPT_OAEP
      - ["CryptDecrypt", 0, 3, 64]
  - addsTo: { pack: languages-cpp-custom-src, extensible: weakPrngFunctionModel }
    data:
      - ["rand"]   # + rand_r, random, drand48, erand48, lrand48, nrand48, mrand48, jrand48

languages/cpp/custom/src/qlpack.yml now declares dataExtensions: ["**/*.model.yml"].

Access Path Explanation

The columns are custom, not MaD access paths:

  • Arguments are 0-based (Algid is index 1; hKey/dwFlags are 0/3).
  • Constants are matched by macro name (MacroInvocation.getExpr()) or by enum constant name.
  • Functions are matched with hasGlobalName (hasGlobalOrStdName for PRNGs).

🧪 Testing

  • Extension YAML resolves without errors
  • Database created with sample code (codeql database create or codeql test extract)
  • Single query verified with extension applied (codeql query run --additional-packs=<model-pack-dir>)
  • Unit tests pass with extension applied (codeql test run --additional-packs=<model-pack-dir>)
  • Positive test cases (vulnerable code detected)
  • Negative test cases (safe code not flagged)

The extensions ship in the src pack, so codeql test run picks them up without --additional-packs. Tests use inline expectations with a shared stub, test/WinCryptStubs/wincrypt.h, which uses the real Windows constant values.

📋 Checklist

  • Extension YAML is valid and properly formatted
  • Extension placed in correct location (languages/[language]/custom/src/)
  • qlpack.yml includes dataExtensions configuration
  • Access paths verified via API graph queries (N/A for cpp custom predicates)
  • No false positives in test cases
  • Documentation/comments included in YAML

🔗 References


Note: This data extension was developed following CodeQL Models as Data best practices.

Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>

void des_bad(HCRYPTPROV hProv, HCRYPTHASH hHash, BYTE *data, DWORD *len) {
HCRYPTKEY hKey;
CryptDeriveKey(hProv, CALG_DES, hHash, 0, &hKey); // $ Alert
Copilot AI changed the title [WIP] Add data extension models for weak cryptography and randomness [NEW DATA EXTENSION] C/C++: WinCrypt weak-algorithm, RSA-without-OAEP and weak-PRNG models + queries Oct 31, 2026
Copilot AI requested a review from felickz October 31, 2026 05:26
@felickz
felickz requested a balanced review from Copilot November 2, 2026 18:37
Copilot stopped reviewing on behalf of felickz due to an error November 2, 2026 18:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The query produces false positives and misses RSA operations by matching variable accesses rather than handle value flow, and it incorrectly reports size-only CryptEncrypt calls.

2 open issues
What changed in this PR

Adds model-driven C/C++ security queries for weak cryptography and randomness, addressing the coverage gaps identified in #276 through custom extensible predicates.

Changes:

  • Adds queries for weak WinCrypt algorithms, RSA without OAEP, and non-cryptographic PRNG use.
  • Defines YAML models and enables data-extension loading in the C++ source pack.
  • Adds positive and negative test cases with shared WinCrypt stubs.
File Description
languages/​cpp/​custom/​test/​WinCryptWeakAlgorithm/​WinCryptWeakAlgorithm.qlref Connects algorithm tests to the query and inline expectations.
languages/​cpp/​custom/​test/​WinCryptWeakAlgorithm/​WinCryptWeakAlgorithm.expected Records expected weak-algorithm alerts.
languages/​cpp/​custom/​test/​WinCryptWeakAlgorithm/​test.c Covers weak algorithms, local variable flow, and safe alternatives.
languages/​cpp/​custom/​test/​WinCryptStubs/​wincrypt.h Provides shared CryptoAPI declarations and constants.
languages/​cpp/​custom/​test/​WinCryptRsaWithoutOaep/​WinCryptRsaWithoutOaep.qlref Connects RSA padding tests to the query.
languages/​cpp/​custom/​test/​WinCryptRsaWithoutOaep/​WinCryptRsaWithoutOaep.expected Records expected missing-OAEP alerts.
languages/​cpp/​custom/​test/​WinCryptRsaWithoutOaep/​test.c Covers RSA padding and AES negative cases.
languages/​cpp/​custom/​test/​WeakPrng/​WeakPrng.qlref Connects PRNG tests to the query.
languages/​cpp/​custom/​test/​WeakPrng/​WeakPrng.expected Records expected PRNG alerts.
languages/​cpp/​custom/​test/​WeakPrng/​test.c Covers consumed and discarded results and a secure alternative.
languages/​cpp/​custom/​src/​WinCryptWeakAlgorithm/​WinCryptWeakAlgorithm.ql Detects modeled weak cipher and hash selections.
languages/​cpp/​custom/​src/​WinCryptRsaWithoutOaep/​WinCryptRsaWithoutOaep.ql Detects RSA operations with constant flags lacking OAEP.
languages/​cpp/​custom/​src/​WeakPrng/​WeakPrng.ql Reports modeled PRNG calls outside void contexts.
languages/​cpp/​custom/​src/​WeakCryptoRandomness/​WeakCryptoRandomness.qll Defines shared model predicates and matching logic.
languages/​cpp/​custom/​src/​WeakCryptoRandomness/​weak-crypto-randomness.model.yml Models WinCrypt algorithms, padding flags, and PRNG functions.
languages/​cpp/​custom/​src/​qlpack.yml Enables loading of model YAML files.

Review effort: Balanced

Copilot AI and others added 2 commits November 2, 2026 21:40
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Data Extension Create]: CWE-327, CWE-328, CWE-338, CWE-780 MEDIUM - C/C++: data extension models for weak cryptography and randomness

4 participants