Hard-coded Credentials

Summary

The product contains hard-coded credentials, such as a password, a user name, a certification, a cryptographic key, an API key, or an authentication token, etc.

Description

Including unencrypted hard-coded authentication credentials in source code is dangerous because the credentials may be easily discovered. For example, the code may be open source, or it may be leaked or accidentally revealed, making the credentials visible to an attacker. This, in turn, might enable them to gain unauthorized access, or to obtain privileged information.

There are two main variations:

  • Inbound: the product contains an authentication mechanism that checks the input credentials against a hard-coded set of credentials. In this variant, a default administration account is created, and a simple password is hard-coded into the product and associated with that account. This hard-coded password is the same for each installation of the product, and it usually cannot be changed or disabled by system administrators without manually modifying the program, or otherwise patching the product. It can also be difficult for the administrator to detect.

  • Outbound: the product connects to another system or component, and it contains hard-coded credentials for connecting to that component. This variant applies to front-end systems that authenticate with a back-end service. The back-end service may require a fixed password that can be easily discovered. The programmer may simply hard-code those back-end credentials into the front-end product.

Risk

How Can It Happen?

  • Bypass Protection Mechanism. If hard-coded passwords are used, it is almost certain that malicious users will gain access to the account in question. Any user of the product that hard-codes passwords may be able to extract the password. Client-side systems with hard-coded passwords pose even more of a threat, since the extraction of a password from a binary is usually very simple.

  • Read Application Data; Gain Privileges or Assume Identity; Execute Unauthorized Code or Commands; and Other. This weakness can lead to the exposure of resources or functionality to unintended actors, possibly providing attackers with sensitive information or even execute arbitrary code. If the password is ever discovered or published (a common occurrence on the Internet), then anybody with knowledge of this password can access the product. Finally, since all installations of the product will have the same password, even across different organizations, this enables massive attacks such as worms to take place.

Example

In 2022, the OT:ICEFALL study examined products by 10 different Operational Technology (OT) vendors. The researchers reported 56 vulnerabilities and said that the products were “insecure by design”. If exploited, these vulnerabilities often allowed adversaries to change how the products operated, ranging from denial of service to changing the code that the products executed. Since these products were often used in industries such as power, electrical, water, and others, there could even be safety implications. Multiple vendors used hard-coded credentials in their OT products.

Example Code (Bad Code)

  1. The following code uses a hard-coded password to connect to a database:
     ...
     DriverManager.getConnection(url, "scott", "tiger");
     ...
    

    This is an example of an external hard-coded password on the client-side of a connection. This code will run successfully, but anyone who has access to it will have access to the password. Once the program has shipped, there is no going back from the database user “scott” with a password of “tiger” unless the program is patched. A devious employee with access to this information can use it to break into the system. Even worse, if attackers have access to the bytecode for application, they can use the javap -c command to access the disassembled code, which will contain the values of the passwords used. The result of this operation might look something like the following for the example above:

     $ javap -c ConnMngr.class
     ...
     22: ldc #36; //String jdbc:mysql://ixne.com/rxsql
     24: ldc #38; //String scott
     26: ldc #17; //String tiger
     ...
    
  2. The following code is an example of an internal hard-coded password in the back-end:

     int VerifyAdmin(String password) {
       if (!password.equals("Mew!")) {
         return(0)
       }
       //Diagnostic Mode
       return(1);
     }
    

    Every instance of this program can be placed into diagnostic mode with the same password. Even worse is the fact that if this program is distributed as a binary-only distribution, it is very difficult to change that password or disable this “functionality.”

  3. The following code examples attempt to verify a password using a hard-coded cryptographic key.

     public boolean VerifyAdmin(String password) {
       if (password.equals("68af404b513073584c4b6f22b6c63e6b")) {
         System.out.println("Entering Diagnostic Mode...");
         return true;
       }
       System.out.println("Incorrect Password!");
       return false;
     }
    

    The cryptographic key is within a hard-coded string value that is compared to the password. It is likely that an attacker will be able to read the key and compromise the system.

  4. The following examples show a portion of properties and configuration files for Java and ASP.NET applications. The files include username and password information but they are stored in cleartext.

    This Java example shows a properties file with a cleartext username / password pair.

     # Java Web App ResourceBundle properties file
     ...
     webapp.ldap.username=secretUsername
     webapp.ldap.password=secretPassword
     ...
    

    The following example shows a portion of a configuration file for an ASP.Net application. This configuration file includes username and password information for a connection to a database but the pair is stored in cleartext.

     ...
     <connectionStrings>
     <add name="ud_DEV" connectionString="connectDB=uDB; uid=db2admin; pwd=password; dbalias=uDB;" providerName="System.Data.Odbc" />
     </connectionStrings>
     ...
    

    Username and password information should not be included in a configuration file or a properties file in cleartext as this will allow anyone who can read the file access to the resource. If possible, encrypt this information.

  5. The following code example shows a hard-coded API key in a JavaScript file. The API key is used to access a third-party service.

     const API_KEY = "1234567890abcdef";
     const API_URL = "https://api.example.com/data";
    

    It makes the keys easily accessible to attackers, leading to potential unauthorized access and misuse

Addressing Hard-Coded Credentials

Remove hard-coded credentials, such as user names, passwords, certificates, API keys, and other sensitive information from the source code. Recommended solutions are as follows:

  1. Place them in configuration files or other data stores if necessary. If possible, store configuration files including credential data separately from the source code, in a secure location with restricted access.
  2. Use environment variables.
  3. If the credentials are a placeholder value, make sure the value is obviously a placeholder by using a name such as “SampleToken” or “MyPassword”.

Below are examples how we address this vulnerability.

  1. The following code example connects to an HTTP request using an hard-codes authentication header:

     let base64 = require('base-64');
    
     let url = 'http://example.org/auth';
     let username = 'user';    // hard-coded user name
     let password = 'passwd';  // hard-coded password
    
     let headers = new Headers();
    
     headers.append('Content-Type', 'text/json');
     headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password));
    
     fetch(url, {
               method:'GET',
               headers: headers
           })
     .then(response => response.json())
     .then(json => console.log(json))
     .done();
    

    Instead, user name and password can be supplied through the environment variables username and password, which can be set externally without hard-coding credentials in the source code.

     let base64 = require('base-64');
    
     let url = 'http://example.org/auth';
     let username = process.env.USERNAME; // use environment variable
     let password = process.env.PASSWORD; // use environment variable
    
     let headers = new Headers();
    
     headers.append('Content-Type', 'text/json');
     headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password));
    
     fetch(url, {
             method:'GET',
             headers: headers
         })
     .then(response => response.json())
     .then(json => console.log(json))
     .done();
    
  2. The following code example connects to a Postgres database using the pg package and hard-codes user name and password:

     const pg = require("pg");
    
     const client = new pg.Client({
       user: "bob",
       host: "database.server.com",
       database: "mydb",
       password: "correct-horse-battery-staple",
       port: 3211
     });
     client.connect();
    

    Instead, user name and password can be supplied through the environment variables PGUSER and PGPASSWORD, which can be set externally without hard-coding credentials in the source code.

Acknowledgement

This page is derived from CWE-798, CWE-259, CWE-321, and the CodeQL project.