Repository navigation
Unified: Add weak password hashing query #23339
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
geoffw0
wants to merge
9
commits into
github:main
Choose a base branch
from
geoffw0:unifiedpasswordhash
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.
+1,031
−4
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10eb907
unified: Copy Swift weak password hashing query and tests unchanged
geoffw0 cbf265f
unified: Collect original Swift weak password hashing configuration a…
geoffw0 5fa9a02
unified: Port weak password hashing query to unified Swift
geoffw0 951a41d
unified: Fix SHA3 sink kind in legacy Swift models
geoffw0 dde9daf
unified: Add barrier and metatype test cases for CWE-328
geoffw0 da89b66
unified: Fix libsodium anchor link in WeakPasswordHashing.qhelp
geoffw0 689a0ee
Refactor barrier predicate for weak password hashing
geoffw0 15d8fca
Unified: Add observeDiffInformedIncrementalMode predicate.
geoffw0 b3277c3
Merge branch 'main' into unifiedpasswordhash
geoffw0 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
Some comments aren't visible on the classic Files Changed page.
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
95 changes: 95 additions & 0 deletions
95
unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.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,95 @@ | ||
| <!DOCTYPE qhelp PUBLIC | ||
| "-//Semmle//qhelp//EN" | ||
| "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p> | ||
| Hash functions that are not sufficiently computationally hard can leave data vulnerable. You should not use such functions for password hashing. | ||
| </p> | ||
|
|
||
| <p> | ||
| A strong cryptographic hash function should be resistant to: | ||
| </p> | ||
| <ul> | ||
| <li> | ||
| <strong>Pre-image attacks</strong>. If you know a hash value <code>h(x)</code>, | ||
| you should not be able to easily find the input <code>x</code>. | ||
| </li> | ||
| <li> | ||
| <strong>Collision attacks</strong>. If you know a hash value <code>h(x)</code>, | ||
| you should not be able to easily find a different input | ||
| <code>y</code> | ||
| with the same hash value <code>h(x) = h(y)</code>. | ||
| </li> | ||
| <li> | ||
| <strong>Brute force</strong>. If you know a hash value <code>h(x)</code>, | ||
| you should not be able to find an input <code>y</code> that computes to that hash value | ||
| using brute force attacks without significant computational effort. | ||
| </li> | ||
| </ul> | ||
|
|
||
| <p> | ||
| All of MD5, SHA-1, SHA-2 and SHA-3 are weak against offline brute forcing, since they are not sufficiently computationally hard. This includes SHA-224, SHA-256, SHA-384 and SHA-512, which are in the SHA-2 family. | ||
| </p> | ||
|
|
||
| <p> | ||
| Password hashing algorithms should be slow and/or memory intensive to compute, to make brute force attacks more difficult. | ||
| </p> | ||
|
|
||
| </overview> | ||
| <recommendation> | ||
|
|
||
| <p> | ||
| For password storage, you should use a sufficiently computationally hard cryptographic hash function, such as one of the following: | ||
| </p> | ||
|
|
||
| <ul> | ||
| <li> | ||
| Argon2 | ||
| </li> | ||
| <li> | ||
| scrypt | ||
| </li> | ||
| <li> | ||
| bcrypt | ||
| </li> | ||
| <li> | ||
| PBKDF2 | ||
| </li> | ||
| </ul> | ||
|
|
||
| </recommendation> | ||
| <example> | ||
|
|
||
| <p> | ||
| The following examples show two versions of the same function. In both cases, a password is hashed using a cryptographic hashing algorithm. | ||
|
|
||
| In the first case, the SHA-512 hashing algorithm is used. It is vulnerable to offline brute force attacks: | ||
| </p> | ||
| <sample src="WeakPasswordHashingBad.swift"/> | ||
| <p> | ||
|
|
||
| Here is the same function using Argon2, which is suitable for password hashing: | ||
| </p> | ||
| <sample src="WeakPasswordHashingGood.swift"/> | ||
|
|
||
| </example> | ||
| <references> | ||
| <li> | ||
| OWASP: | ||
| <a href="https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html">Password Storage | ||
| Cheat Sheet | ||
| </a> | ||
| </li> | ||
| <li> | ||
| GitHub: <a href="https://github.com/krzyzanowskim/CryptoSwift/blob/main/README.md#password-based-key-derivation-function">CryptoSwift README - Password-Based Key Derivation Function</a> | ||
| </li> | ||
| <li> | ||
| libsodium: <a href="https://doc.libsodium.org/bindings_for_other_languages#bindings-for-programming-languages">libsodium bindings for other languages</a> | ||
| </li> | ||
| <li> | ||
| GitHub: <a href="https://github.com/tmthecoder/Argon2Swift">Argon2Swift</a> | ||
| </li> | ||
| </references> | ||
|
|
||
| </qhelp> |
79 changes: 79 additions & 0 deletions
79
unified/ql/src/queries/security/CWE-328/WeakPasswordHashing.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,79 @@ | ||
| /** | ||
| * @name Use of an inappropriate cryptographic hashing algorithm on passwords | ||
| * @description Using inappropriate cryptographic hashing algorithms with passwords can compromise security. | ||
| * @kind path-problem | ||
| * @problem.severity warning | ||
| * @security-severity 7.5 | ||
| * @precision high | ||
| * @id unified/swift/weak-password-hashing | ||
| * @tags security | ||
| * external/cwe/cwe-327 | ||
| * external/cwe/cwe-328 | ||
| * external/cwe/cwe-916 | ||
| */ | ||
|
|
||
| import unified | ||
| import codeql.unified.security.SensitiveData | ||
|
|
||
| /** | ||
| * Holds if `node` is a sink for weak password hashing using `algorithm`. | ||
| */ | ||
| private predicate isWeakPasswordHashingSink(DataFlow::Node node, string algorithm) { | ||
| exists(string kind | Models::isSink(node, kind) | | ||
|
geoffw0 marked this conversation as resolved.
Resolved
|
||
| // match the following sinks: | ||
| // - `weak-hash-input-<algorithm>` (sinks that apply to both this query and `unified/swift/weak-sensitive-data-hashing`) | ||
| // - `weak-password-hash-input-<algorithm>` (sinks that apply to just this query) | ||
| algorithm = kind.regexpCapture("weak-(?:password-)?hash-input-(.+)", 1) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * A taint tracking configuration from password expressions to inappropriate | ||
| * hashing sinks. | ||
| */ | ||
| module WeakPasswordHashingConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node node) { | ||
| node.(SensitiveData).getClassification() = SensitiveDataClassification::password() | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node node) { isWeakPasswordHashingSink(node, _) } | ||
|
|
||
| predicate isBarrier(DataFlow::Node node) { | ||
| // A barrier for weak password hashing, when it occurs inside of | ||
| // certain cryptographic algorithms as part of their design. | ||
| exists(ClassLikeDeclaration cls, string name | | ||
| node.asExpr().getParent+() = cls and | ||
| ( | ||
| name = cls.resolve().getName() | ||
| or | ||
| name = cls.getExtensionTarget().(Identifier).getValue() | ||
| ) and | ||
| name = ["HMAC", "PBKDF1", "PBKDF2"] | ||
| ) | ||
| } | ||
|
|
||
| predicate isBarrierIn(DataFlow::Node node) { | ||
| // make sources barriers so that we only report the closest instance | ||
| isSource(node) | ||
| } | ||
|
|
||
| predicate isBarrierOut(DataFlow::Node node) { | ||
| // make sinks barriers so that we only report the closest instance | ||
| isSink(node) | ||
| } | ||
|
|
||
| predicate observeDiffInformedIncrementalMode() { any() } | ||
| } | ||
|
|
||
| module WeakPasswordHashingFlow = TaintTracking::Global<WeakPasswordHashingConfig>; | ||
|
|
||
| import WeakPasswordHashingFlow::PathGraph | ||
|
|
||
| from | ||
| WeakPasswordHashingFlow::PathNode source, WeakPasswordHashingFlow::PathNode sink, string algorithm | ||
| where | ||
| WeakPasswordHashingFlow::flowPath(source, sink) and | ||
| isWeakPasswordHashingSink(sink.getNode(), algorithm) | ||
| select sink.getNode(), source, sink, | ||
| "Insecure hashing algorithm (" + algorithm + ") depends on $@.", source.getNode(), | ||
| "password (" + source.getNode().toString() + ")" | ||
8 changes: 8 additions & 0 deletions
8
unified/ql/src/queries/security/CWE-328/WeakPasswordHashingBad.swift
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,8 @@ | ||
| let passwordData = Data(passwordString.utf8) | ||
| let passwordHash = Crypto.SHA512.hash(data: passwordData) // BAD: SHA-512 is not suitable for password hashing. | ||
|
|
||
| // ... | ||
|
|
||
| if Crypto.SHA512.hash(data: Data(passwordString.utf8)) == passwordHash { | ||
| // ... | ||
| } |
11 changes: 11 additions & 0 deletions
11
unified/ql/src/queries/security/CWE-328/WeakPasswordHashingGood.swift
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,11 @@ | ||
| import Argon2Swift | ||
|
|
||
| let salt = Salt.newSalt() | ||
| let result = try! Argon2Swift.hashPasswordString(password: passwordString, salt: salt) // GOOD: Argon2 is suitable for password hashing. | ||
| let passwordHash = result.encodedString() | ||
|
|
||
| // ... | ||
|
|
||
| if try! Argon2Swift.verifyHashString(password: passwordString, hash: passwordHash) { | ||
| // ... | ||
| } |
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.