X Tutup
The Wayback Machine - https://web.archive.org/web/20260321024230/https://github.com/github/codeql/pull/3977
Skip to content

[JS] cwe-327 (Weak or vulnerable cryptography usage) added#3977

Closed
monkey-junkie wants to merge 8 commits intogithub:mainfrom
monkey-junkie:master
Closed

[JS] cwe-327 (Weak or vulnerable cryptography usage) added#3977
monkey-junkie wants to merge 8 commits intogithub:mainfrom
monkey-junkie:master

Conversation

@monkey-junkie
Copy link
Contributor

Hi team!
I've wrote detection of usage of weak block cipher algorithms like block ciphers in ECB mode (known as chosen plaintext attack) and for common crypto flaw - static IV for block ciphers.
Either of such flaws could led to sensitive data leak.

More information:

@monkey-junkie monkey-junkie requested a review from a team as a code owner July 26, 2020 19:19
@monkey-junkie
Copy link
Contributor Author

Is this similar to https://github.com/github/codeql/blob/master/javascript/ql/src/Security/CWE-327/BrokenCryptoAlgorithm.qhelp and https://github.com/github/codeql/blob/master/javascript/ql/src/Security/CWE-327/BrokenCryptoAlgorithm.ql ?

Hi!
Not exactly. Proposed query could be used for detection of incorrect usage of secure algorithms (for example, AES-128 ECB which could be vulnerable for CPA), when mentioned query is about known-weak algorithms.
Maybe, it should be more appropriate to merge proposed query with https://github.com/github/codeql/blob/master/javascript/ql/src/Security/CWE-327/BrokenCryptoAlgorithm.ql

Copy link
Contributor

@asgerf asgerf left a comment

Choose a reason for hiding this comment

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

Hi @monkey-junkie, thanks for the contribution! This looks really interesting.

I'm not an expert in cryptography, but I think I understand the chosen-plaintext attack vector, and it would be great to have a query for that.

So far I just have a few initial comments and questions.

ivBlock = call.(InvokeNode).getArgument(2).getALocalSource().getBasicBlock() and
alg = call.(InvokeNode).getArgument(0).toString()
|
createCihperBlock.length() != ivBlock.length() and
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't looks right. The BasicBlock class has nothing to do with cryptography; it refers to the concept from compilers (see wikipedia page). I don't see why its length should matter.

Do I understand correctly that the OFB and CTR modes of AES become susceptible to chosen-plaintext attacks if subsequent encryption requests are made with the same IV? If so, you probably want to use getContainer instead of getBasicBlock:

call.getContainer() != call.getArgument(2).getALocalSource().getContainer()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi!
Yes, exactly, OFB and CTR with the static IV are vulnerable to CPA. I tried to use .legnth() method of BasicBlock to distinguish blocks with IV initialization and its actual usage. But now I can see, that I was wrong and I should use getContainer instead.
Thanks!

Copy link
Contributor Author

@monkey-junkie monkey-junkie Jul 30, 2020

Choose a reason for hiding this comment

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

I've updated pr according all your comments, @asgerf please review :)

monkey-junkie and others added 3 commits July 31, 2020 03:02
…cCrypto.ql

Co-authored-by: Asger F <asgerf@github.com>
…cCrypto.ql

Co-authored-by: Asger F <asgerf@github.com>
…cCrypto.ql

Co-authored-by: Asger F <asgerf@github.com>
@intrigus-lgtm
Copy link
Contributor

@monkey-junkie In case you didn't know:

^ Pro tip: in the "Files changed" view, you have the option of adding review suggestions to a batch and commit them all at once.

@esbena esbena added the JS label Aug 3, 2020
Copy link
Contributor

@asgerf asgerf left a comment

Choose a reason for hiding this comment

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

After studying this a little more I start to wonder if this should be moved to BrokenCryptoAlgorithm. The chosen-plaintext vector seemed really interesting at first, but now it seems to me these crypto algorithms are insecure even if the attacker can't choose the plaintext.

Two questions:

  • Is there ever a valid reason to use ECB when an outsider can't control the plaintext?
  • Is there ever a valid reason to reuse a given IV for multiple plaintexts?

If not, I think we can move these sinks into BrokenCryptoAlgorithm and just flag them regardless of whether the plaintext is tainted.

If there are valid reasons (and we could thus get FPs from BrokenCryptoAlgorithm), I'd appreciate it if the documentation is more specific to chosen-plaintext attacks rather than the generic "incorrect usage" term. For example, the @description could be:

Encrypting user-controlled plaintexts with ECB or with a reused IV enables an attacker to recover the secret key via chosen-plaintext attack.

(keep in mind I'm not a cryptographer so the above description may be wrong, in particular the part about actually recovering the key)

@erik-krogh
Copy link
Contributor

erik-krogh commented Aug 7, 2020

  • Is there ever a valid reason to reuse a given IV for multiple plaintexts?

I think this is the mistake that Sony made in the Playstation 3 (article).
I'm not sure if this was a chosen chosen-plaintext attack.

monkey-junkie and others added 2 commits August 9, 2020 01:56
…cCrypto.ql

Co-authored-by: Asger F <asgerf@github.com>
…cCrypto.ql

Co-authored-by: Asger F <asgerf@github.com>
@monkey-junkie
Copy link
Contributor Author

After studying this a little more I start to wonder if this should be moved to BrokenCryptoAlgorithm. The chosen-plaintext vector seemed really interesting at first, but now it seems to me these crypto algorithms are insecure even if the attacker can't choose the plaintext.

Two questions:

  • Is there ever a valid reason to use ECB when an outsider can't control the plaintext?
  • Is there ever a valid reason to reuse a given IV for multiple plaintexts?

If not, I think we can move these sinks into BrokenCryptoAlgorithm and just flag them regardless of whether the plaintext is tainted.

If there are valid reasons (and we could thus get FPs from BrokenCryptoAlgorithm), I'd appreciate it if the documentation is more specific to chosen-plaintext attacks rather than the generic "incorrect usage" term. For example, the @description could be:

Encrypting user-controlled plaintexts with ECB or with a reused IV enables an attacker to recover the secret key via chosen-plaintext attack.

(keep in mind I'm not a cryptographer so the above description may be wrong, in particular the part about actually recovering the key)

Hi!

Is there ever a valid reason to use ECB when an outsider can't control the plaintext?

No.

Is there ever a valid reason to reuse a given IV for multiple plaintexts?

According to RFC for CTR mode, "The same IV and key combination MUST NOT be used more than once" so, technically, it is safe to use static IV with unique encryption key per message, but unique encryption key is very rare case.

In sum, I think you are right and whe should move query to BrokenCryptoAlgorithm with additional check for key uniqueness (based on getContainer, like for unique IV in my ConstIVSink class).
So I'm going to add my updated pr to BrokenCryptoAlgorithm, if you dont mind.

@asgerf
Copy link
Contributor

asgerf commented Aug 10, 2020

So I'm going to add my updated pr to BrokenCryptoAlgorithm, if you dont mind.

Thanks. Please go ahead.

@adityasharad adityasharad changed the base branch from master to main August 14, 2020 18:33
@asgerf
Copy link
Contributor

asgerf commented May 9, 2022

Closing the PR due to inactivity, but feel free to reopen if you wish to continue work on it.

Overall I think the ECB and reused-IV stuff could be useful to have in query suite in some form, so I'd be happy to see it re-opened. In retrospect I wish I had not suggested moving it into the non-experimental BrokenCryptoAlgorithm query because the requirements are higher for such queries, and it's just more work to integrate with the abstraction we use internally. Merging as an experimental query would have been better than closing the PR unmerged like this.

@asgerf asgerf closed this May 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

X Tutup