C Sharp Hardcoded Symmetric Key
Description
Detects hardcoded symmetric cryptographic keys used with SymmetricSecurityKey in C# code. This is a security risk because hardcoding cryptographic keys in source code makes them easily discoverable and prevents key rotation, potentially leading to compromised encryption.
Detection Strategy
• Look for instantiations of SymmetricSecurityKey class (including various namespace variations)
• Check if the first constructor argument (the key material) is a hardcoded string value
• Report a vulnerability if a SymmetricSecurityKey is created with a hardcoded key rather than retrieving it from secure configuration or key management systems
Vulnerable code example
using Microsoft.IdentityModel.Tokens;
using System.Text;
public class TokenGenerator
{
public SigningCredentials CreateCredentials()
{
var secretKey = "hardcoded_secret_1234"; // Vulnerable: Hardcoded secret key...✅ Secure code example
using Microsoft.IdentityModel.Tokens;
using System.Security.Cryptography;
public class TokenGenerator
{
public SigningCredentials CreateCredentials()
{
byte[] keyBytes = new byte[32]; // 256-bit key...Search for vulnerabilities in your apps for free with Fluid Attacks' automated security testing! Start your 21-day free trial and discover the benefits of the Continuous Hacking Essential plan. If you prefer the Advanced plan, which includes the expertise of Fluid Attacks' hacking team, fill out this contact form.