-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Java Padding Oracle encryption vulnerability rule (#93)
Co-authored-by: David Roe <[email protected]>
- Loading branch information
Showing
7 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,40 @@ | ||
patterns: | ||
- pattern: $<CIPHER>.getInstance($<PADDING_ORACLE>) | ||
filters: | ||
- variable: CIPHER | ||
regex: ^(javax.)?(crypto.)?Cipher | ||
- variable: PADDING_ORACLE | ||
string_regex: \/CBC\/PKCS5Padding\z | ||
- not: | ||
variable: PADDING_ORACLE | ||
string_regex: \A(RSA|ECIES) | ||
languages: | ||
- java | ||
metadata: | ||
description: "Padding Oracle encryption vulnerability detected." | ||
remediation_message: | | ||
## Description | ||
Using a block cipher algorithm mode, such as CBC, together with a padding scheme is vulnerable to Padding Oracle attacks. | ||
## Remediations | ||
❌ Do not use CBC (Cipher Block Chaining) mode with padding | ||
```java | ||
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
``` | ||
✅ Prefer GCM (Galois/Counter Mode) instead | ||
```java | ||
Cipher c = Cipher.getInstance("AES/GCM/PKCS5Padding"); | ||
``` | ||
## Resources | ||
- [Java Cipher class](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/javax/crypto/Cipher.html) | ||
- [Java Security Standard Algorithm Names](https://docs.oracle.com/en/java/javase/20/docs/specs/security/standard-names.html) | ||
cwe_id: | ||
- 327 | ||
id: java_lang_padding_oracle_encryption_vulnerability | ||
documentation_url: https://docs.bearer.com/reference/rules/java_lang_padding_oracle_encryption_vulnerability |
52 changes: 52 additions & 0 deletions
52
java/lang/padding_oracle_encryption_vulnerability/.snapshots/block_cipher.yml
This file contains 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,52 @@ | ||
low: | ||
- rule: | ||
cwe_ids: | ||
- "327" | ||
id: java_lang_padding_oracle_encryption_vulnerability | ||
title: Padding Oracle encryption vulnerability detected. | ||
description: | | ||
## Description | ||
Using a block cipher algorithm mode, such as CBC, together with a padding scheme is vulnerable to Padding Oracle attacks. | ||
## Remediations | ||
❌ Do not use CBC (Cipher Block Chaining) mode with padding | ||
```java | ||
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
``` | ||
✅ Prefer GCM (Galois/Counter Mode) instead | ||
```java | ||
Cipher c = Cipher.getInstance("AES/GCM/PKCS5Padding"); | ||
``` | ||
## Resources | ||
- [Java Cipher class](https://docs.oracle.com/en/java/javase/20/docs/api/java.base/javax/crypto/Cipher.html) | ||
- [Java Security Standard Algorithm Names](https://docs.oracle.com/en/java/javase/20/docs/specs/security/standard-names.html) | ||
documentation_url: https://docs.bearer.com/reference/rules/java_lang_padding_oracle_encryption_vulnerability | ||
line_number: 3 | ||
full_filename: /tmp/scan/block_cipher.java | ||
filename: . | ||
source: | ||
location: | ||
start: 3 | ||
end: 3 | ||
column: | ||
start: 14 | ||
end: 69 | ||
sink: | ||
location: | ||
start: 3 | ||
end: 3 | ||
column: | ||
start: 14 | ||
end: 69 | ||
content: javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding") | ||
parent_line_number: 3 | ||
snippet: javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding") | ||
fingerprint: 368142b7ce98efcf47351ef4a2c2dab7_0 | ||
old_fingerprint: 83661fbb9de19d845a95fe1686f115bb_0 | ||
|
2 changes: 2 additions & 0 deletions
2
java/lang/padding_oracle_encryption_vulnerability/.snapshots/ok_gcm.yml
This file contains 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,2 @@ | ||
{} | ||
|
2 changes: 2 additions & 0 deletions
2
java/lang/padding_oracle_encryption_vulnerability/.snapshots/ok_not_a_block_cipher.yml
This file contains 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,2 @@ | ||
{} | ||
|
8 changes: 8 additions & 0 deletions
8
java/lang/padding_oracle_encryption_vulnerability/testdata/block_cipher.java
This file contains 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 @@ | ||
public byte[] signToken(byte[] payload) throws GeneralSecurityException | ||
{ | ||
Cipher c = javax.crypto.Cipher.getInstance("AES/CBC/PKCS5Padding"); | ||
|
||
c.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); | ||
|
||
return c.doFinal(payload); | ||
} |
8 changes: 8 additions & 0 deletions
8
java/lang/padding_oracle_encryption_vulnerability/testdata/ok_gcm.java
This file contains 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 @@ | ||
public byte[] signToken(byte[] payload) throws GeneralSecurityException | ||
{ | ||
Cipher c = javax.crypto.Cipher.getInstance("AES/GCM/PKCS5Padding"); | ||
|
||
c.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); | ||
|
||
return c.doFinal(payload); | ||
} |
8 changes: 8 additions & 0 deletions
8
java/lang/padding_oracle_encryption_vulnerability/testdata/ok_not_a_block_cipher.java
This file contains 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 @@ | ||
public byte[] signToken(byte[] payload) throws GeneralSecurityException | ||
{ | ||
Cipher c = javax.crypto.Cipher.getInstance("RSA/CBC/PKCS5Padding"); | ||
|
||
c.init(Cipher.ENCRYPT_MODE, keyPair.getPrivate()); | ||
|
||
return c.doFinal(payload); | ||
} |