C#


CSHARP.HARDCODED.PASSWD : Hardcoded Password (C#)

要旨

A hardcoded password is used.

Password manipulation can be risky, for instance if passwords are stored in plain in a property file. Moreover, passwords stored as program constants expose to the risk of inferring the password by simply looking at the binary executable of the code. This checker identifies situations when password manipulation is done in an unsafe way.

プロパティ

クラス名 Hardcoded Password (C#)
日本語クラス名 Hardcoded Password (C#)
クラス分類 セキュリティ (security)
ニーモニック CSHARP.HARDCODED.PASSWD
カテゴリー
CWE CWE:259 Use of Hard-coded Password
OWASP-2017 OWASP-2017:A2 Broken authentication
OWASP-2021 OWASP-2021:A7 Identification and authorization failures
対応言語 C# で利用可能です。
有効/無効設定 このワーニングクラスのチェックはデフォルトで有効になっています。チェックを無効にするにはプロジェクト設定ファイル (configuration file)に以下の WARNING_FILTER ルールを追加してください。
WARNING_FILTER += discard class="Hardcoded Password (C#)"

Example 1

using System;
using System.Net;


namespace DocumentationExamples
{

    public class Passwords
    {
        private static void Main(string[] args)
        { }

       private static readonly string pwd = "password";
        public void Test()
        {
            string s = "password";
            Environment.GetEnvironmentVariable("password");                                                  // Password in Property File (C#) warning issued here
            Environment.GetEnvironmentVariable(pwd);                                                         // Password in Property File (C#) warning issued here
            Environment.GetEnvironmentVariable(s);                                                           // Password in Property File (C#) warning issued here
            Environment.GetEnvironmentVariable("www.juliasoft.password");                                    // Password in Property File (C#) warning issued here
            Environment.GetEnvironmentVariable("password", EnvironmentVariableTarget.Machine);               // Password in Property File (C#) warning issued here
            Environment.GetEnvironmentVariable("www.juliasoft.password", EnvironmentVariableTarget.Process); // Password in Property File (C#) warning issued here
            Environment.SetEnvironmentVariable("com.julia.password", "goofy mouse");                         // Hardcoded Password (C#) warning issued here
            Environment.SetEnvironmentVariable("com.julia.password", s);                                     // Hardcoded Password (C#) warning issued here
            Environment.SetEnvironmentVariable("com.julia.password", s + " and " + pwd);
            NetworkCredential nc = new NetworkCredential("www.juliasoft.com", "silvio");                     // Hardcoded Password (C#) warning issued here
        }
    }
}

Example 2

public PasswordPropagation
{

    public void CheckPassword(string password)
    {
    
        if (password.Trim().Equals("SpidermanIsTheBest")) // "Hardcoded Password (C#)" warning issued here
        {
            Console.WriteLine("Hi Peter!");
            SavePassword(password.Trim());
        }
                    
                
        if (password.Substring(1).Equals("BlackPearl"))   // "Hardcoded Password (C#)" warning issued here
        {
            Console.WriteLine("Hi Jack!");
            SavePassword(password.Substring(1));
        }
    
    }

    private void SavePassword(string v)
    {
        // ...
    }
}

解決法

Avoid storing passwords in property files and use encrypted files instead. Do not use program constants for passwords, store them in encrypted files instead.

関連のある設定ファイルパラメータ

設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。