forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 21
Use of weak hmac alg #348
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
Open
chanel-y
wants to merge
2
commits into
main
Choose a base branch
from
users/chanely/weak-hmac
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.
Open
Use of weak hmac alg #348
Changes from all commits
Commits
Show all changes
2 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
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
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 @@ | ||
| <!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p> | ||
| HMAC (Hash-based Message Authentication Code) algorithms are used to verify both the | ||
| integrity and authenticity of messages. Using weak HMAC algorithms such as HMACMD5, | ||
| HMACSHA1, or HMACRIPEMD160 can compromise message authentication, as the underlying | ||
| hash functions have known cryptographic weaknesses. | ||
| </p> | ||
| </overview> | ||
| <recommendation> | ||
| <p> | ||
| Use a strong HMAC algorithm such as HMACSHA256, HMACSHA384, or HMACSHA512. These are | ||
| based on the SHA-2 family of hash functions and provide adequate security for message | ||
| authentication. | ||
| </p> | ||
| </recommendation> | ||
|
|
||
| <references> | ||
| <li>NIST, SP 800-131A: <a href="https://csrc.nist.gov/publications/detail/sp/800-131a/rev-2/final">Transitioning the Use of Cryptographic Algorithms and Key Lengths</a>.</li> | ||
| <li>CWE-327: <a href="https://cwe.mitre.org/data/definitions/327.html">Use of a Broken or Risky Cryptographic Algorithm</a>.</li> | ||
| <li>CWE-328: <a href="https://cwe.mitre.org/data/definitions/328.html">Use of Weak Hash</a>.</li> | ||
| </references> | ||
| </qhelp> |
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,21 @@ | ||
| /** | ||
| * @name Use of weak HMAC algorithm | ||
| * @description Using weak HMAC algorithms like HMACMD5 or HMACSHA1 can compromise message authentication. | ||
| * @kind problem | ||
| * @problem.severity warning | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id powershell/microsoft/security/weak-hmac | ||
| * @tags security | ||
| * external/cwe/cwe-327 | ||
| * external/cwe/cwe-328 | ||
| */ | ||
|
|
||
| import powershell | ||
| import semmle.code.powershell.ApiGraphs | ||
| import semmle.code.powershell.dataflow.DataFlow | ||
| import semmle.code.powershell.security.cryptography.Concepts | ||
|
|
||
| from HmacAlgorithm hmacAlg | ||
| where not hmacAlg.getHmacName() = ["hmacsha256", "hmacsha384", "hmacsha512"] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will also raise an alert if no name can be found (i.e.,
|
||
| select hmacAlg, "Use of weak HMAC algorithm: " + hmacAlg.getHmacName() + ". Use HMACSHA256 or stronger." | ||
4 changes: 4 additions & 0 deletions
4
powershell/ql/test/query-tests/security/cwe-327/WeakHmac/WeakHmac.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,4 @@ | ||
| | test.ps1:6:9:6:55 | Call to new-object | Use of weak HMAC algorithm: hmacmd5. Use HMACSHA256 or stronger. | | ||
| | test.ps1:9:9:9:56 | Call to new-object | Use of weak HMAC algorithm: hmacsha1. Use HMACSHA256 or stronger. | | ||
| | test.ps1:12:9:12:56 | Call to create | Use of weak HMAC algorithm: hmacmd5. Use HMACSHA256 or stronger. | | ||
| | test.ps1:15:9:15:54 | Call to new | Use of weak HMAC algorithm: hmacsha1. Use HMACSHA256 or stronger. | |
1 change: 1 addition & 0 deletions
1
powershell/ql/test/query-tests/security/cwe-327/WeakHmac/WeakHmac.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 @@ | ||
| queries/security/cwe-327/WeakHmac.ql |
28 changes: 28 additions & 0 deletions
28
powershell/ql/test/query-tests/security/cwe-327/WeakHmac/test.ps1
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,28 @@ | ||
| # =================================================================== | ||
| # ========== TRUE POSITIVES (should trigger alert) ================== | ||
| # =================================================================== | ||
|
|
||
| # --- Case 1: HMACMD5 via New-Object --- | ||
| $hmac = New-Object System.Security.Cryptography.HMACMD5 # BAD | ||
|
|
||
| # --- Case 2: HMACSHA1 via New-Object --- | ||
| $hmac = New-Object System.Security.Cryptography.HMACSHA1 # BAD | ||
|
|
||
| # --- Case 3: HMACMD5 via static Create --- | ||
| $hmac = [System.Security.Cryptography.HMACMD5]::Create() # BAD | ||
|
|
||
| # --- Case 4: HMACSHA1 via ::new() --- | ||
| $hmac = [System.Security.Cryptography.HMACSHA1]::new() # BAD | ||
|
|
||
| # =================================================================== | ||
| # ========== TRUE NEGATIVES (should NOT trigger alert) ============== | ||
| # =================================================================== | ||
|
|
||
| # --- Safe: HMACSHA256 --- | ||
| $hmac = New-Object System.Security.Cryptography.HMACSHA256 # GOOD | ||
|
|
||
| # --- Safe: HMACSHA384 --- | ||
| $hmac = [System.Security.Cryptography.HMACSHA384]::new() # GOOD | ||
|
|
||
| # --- Safe: HMACSHA512 --- | ||
| $hmac = [System.Security.Cryptography.HMACSHA512]::Create() # GOOD |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the class further below (i.e., XXX) we do this:
could we not do a similarly pretty thing here?