T09 · Insecure Skill Coding Practices
- Location
SKILL.md:87- Finding
SM4-CBC Encryption Uses a Fixed IV and Weak Key Normalization
- Content
View full analysis
Vulnerability Details
File Location:
SKILL.md:87-100
Vulnerability Type: Insecure cryptographic implementation
Risk Level: Mediumpython def sm4_encrypt(data: str, key: str) -> str: """SM4 encryption""" key_bytes = key.encode('utf-8')[:16].ljust(16, b'0') iv = b'5f5e5a247f544d77' data_bytes = data.encode('utf-8') padding_length = 16 - len(data_bytes) % 16 data_bytes += bytes([padding_length] * padding_length) cipher = Cipher(algorithms.SM4(key_bytes), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() encrypted = encryptor.update(data_bytes) + encryptor.finalize() return base64.b64encode(encrypted).decode('utf-8')Technical Analysis
The implementation truncates keys longer than 16 bytes and pads shorter keys with predictable zero bytes. This is not a secure key-derivation procedure and can substantially reduce effective key strength when operators provide short or low-entropy configuration values.
The CBC initialization vector is a constant value reused for every order. CBC encryption requires an unpredictable, unique IV for each encryption under the same key. Reuse causes identical plaintext prefixes to produce identical ciphertext prefixes, exposing relationships between encrypted payment records.
SM4-CBC also provides confidentiality only. No MAC or authenticated-encryption mechanism protects the ciphertext, IV, or associated order metadata. Base64 only serializes the ciphertext and does not provide integrity or additional secrecy.
Attack Path
- An attacker obtains multiple local order files or otherwise observes their
encrypted_datavalues. - Because the same configured key and fixed IV are reused, the attacker compares ciphertext blocks to identify repeated plaintext structures and payment-field relationships.
- If the configured key is short or predictable, zero-padding makes candidat ...[truncated 863 chars]
- An attacker obtains multiple local order files or otherwise observes their
- Remediation
View remediation
Remediation Suggestions
- Reject keys that are not exactly the protocol-required size and require cryptographically random key material.
- If passphrases must be supported, derive keys with a reviewed KDF such as Argon2id, scrypt, or PBKDF2 using a unique salt and suitable work factor.
- Generate a fresh cryptographically random IV for each encryption and serialize it alongside the ciphertext.
- Prefer an authenticated-encryption mode supported by the payment protocol. If SM4-CBC is mandatory, apply an independent MAC using an appropriately separated key and verify it before decryption.
- Authenticate immutable context such as order number, amount, payee, skill identifier, and protocol version.
- Ensure decryption fails closed when authentication, padding, schema, or field validation fails.
