# VNX-1240: Use of a Cryptographic Primitive with a Risky Implementation

A Java javax.crypto.Cipher is created for a block cipher in ECB mode.

| Field | Value |
|---|---|
| Rule ID | `VNX-1240` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-1240](https://cwe.mitre.org/data/definitions/1240.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `risky-primitive` `ecb` `java` `cwe-1240` |
| Source | [`internal/sast/rules/vnx-1240.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-1240.rego) |

## Overview

VNX-1240 maps to [CWE-1240: Use of a Cryptographic Primitive with a Risky Implementation](https://cwe.mitre.org/data/definitions/1240.html).

## Why this matters

ECB encrypts each block independently, so equal plaintext blocks give equal ciphertext blocks. The structure of the data shows through the encryption, and an attacker can cut, replay and reorder blocks without knowing the key. In Java, asking for a bare algorithm name such as `"AES"` selects ECB through the provider default, so the mode is often chosen by accident.

## What gets flagged

Most ECB in Java is reported by [VNX-CRYPTO-003](/sast/rules/vnx-crypto-003.md) (AES in ECB mode, CWE-327): an explicit `/ECB/` transformation, a bare block-cipher name such as `"AES"` whose provider default is ECB, a constant or a `getProperty` default holding one, and a call split after `getInstance(`. This rule does not report those calls again, so a Java ECB cipher is one finding. It reports the forms VNX-CRYPTO-003 does not:

```java
Cipher cipher = Cipher.getInstance(alg + "/ECB/PKCS5Padding");   // mode concatenated on

String t = conf.getOrDefault("vault.cipher", "AES/ECB/PKCS5Padding");
Cipher c = Cipher.getInstance(t);                                // getOrDefault default

// src/test/java/...: ECB in test sources, which VNX-CRYPTO-003 leaves out
Cipher fixture = Cipher.getInstance("AES/ECB/PKCS5Padding");
```

Not flagged: `"AES/GCM/NoPadding"`, `"RSA/ECB/OAEPWithSHA-256AndMGF1Padding"` (RSA has no chaining), `KeyGenerator.getInstance("AES")`, `new SecretKeySpec(raw, "AES")`, and `Cipher.getInstance("AES/ECB/PKCS5Padding")` in production code, which VNX-CRYPTO-003 reports.

## Remediation

1. Use an authenticated mode: `Cipher.getInstance("AES/GCM/NoPadding")` with a fresh 12-byte IV for every encryption.
2. Always spell out the full transformation; never rely on the provider default.

## References

- [CWE-1240: Use of a Cryptographic Primitive with a Risky Implementation](https://cwe.mitre.org/data/definitions/1240.html)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-1240
// nosec vnx-1240
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-1240 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
