Repository navigation
Draft
[NEW DATA EXTENSION] C/C++: WinCrypt weak-algorithm, RSA-without-OAEP and weak-PRNG models + queries#326
Conversation
1 task done
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 stopped reviewing on behalf of
felickz due to an error
November 2, 2026 18:58
There was a problem hiding this comment.
🟡 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
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 task done
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


📝 Data Extension Information
languages/cpp/custom/src/WeakCryptoRandomness/weak-crypto-randomness.model.ymllanguages-cpp-custom-src(notcodeql/cpp-allMaD):cryptoAlgorithmArgumentModel,cryptoAlgorithmConstantModel,rsaPaddingFlagsModel,weakPrngFunctionModelwincrypt.h); C/POSIXrandfamilyCryptCreateHash,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-allMaD rows can't fix them:cpp/weak-cryptographic-algorithmfinds weak crypto by matching names. It has no MaD hooks for extension rows to feed.CALG_DESmacro 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.randis inArithmeticUncontrolled.ql(CWE-190), where it's an input source.So this PR adds model-driven extensible predicates plus three queries that read them:
cpp/custom/wincrypt-weak-algorithmweak-cipher/weak-hashCALG_*(local flow, soALG_ID a = CALG_3DES; CryptGenKey(p, a, ...)is caught)cpp/custom/wincrypt-rsa-without-oaepCryptEncrypt/CryptDecrypton a key created withCALG_RSA_*where the constant flags lack the OAEP bitcpp/custom/weak-prng@precision low: whether the use is security-relevant can't be decided, so expect noise outside test suitesAdding 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
Example Safe Code
📦 Extension Details
Extension YAML
languages/cpp/custom/src/qlpack.ymlnow declaresdataExtensions: ["**/*.model.yml"].Access Path Explanation
The columns are custom, not MaD access paths:
Algidis index 1;hKey/dwFlagsare 0/3).MacroInvocation.getExpr()) or by enum constant name.hasGlobalName(hasGlobalOrStdNamefor PRNGs).🧪 Testing
codeql database createorcodeql test extract)codeql query run --additional-packs=<model-pack-dir>)codeql test run --additional-packs=<model-pack-dir>)The extensions ship in the src pack, so
codeql test runpicks 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
languages/[language]/custom/src/)qlpack.ymlincludesdataExtensionsconfiguration🔗 References
cpp/weak-cryptographic-algorithm(overlaps on ciphers; it reports once per file, the new query reports each call)Note: This data extension was developed following CodeQL Models as Data best practices.