CWE-798: Use of Hard-coded Credentials

Description:
The product contains hard-coded credentials, such as a password or cryptographic key.

Extended Description:
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 produc

Affected languages: Not Language-Specific
Affected technologies: Mobile, ICS/OT
Parent weaknesses: CWE-1391, CWE-287, CWE-344, CWE-671

Consequences:
  - Access Control: Bypass Protection Mechanism
  - Integrity: Read Application Data

Detection methods:
  - Black Box: Credential storage in configuration files is findable using black box methods, but the use of hard-coded credentials for an incoming authentication routine typically involves an account that is not vi
  - Automated Static Analysis: Automated white box techniques have been published for detecting hard-coded credentials for incoming authentication, but there is some expert disagreement regarding their effectiveness and applicabili
  - Manual Static Analysis: This weakness may be detectable using manual code analysis. Unless authentication is decentralized and applied throughout the product, there can be sufficient time for the analyst to find incoming aut
  - Manual Dynamic Analysis: For hard-coded credentials in incoming authentication: use monitoring tools that examine the product's process as it interacts with the operating system and the network. This technique is useful in ca

Mitigations:
  - [Architecture and Design] For outbound authentication: store passwords, keys, and other credentials outside of the code in a strongly-protected, encrypted configuration file or database that is protected from access by all outsiders, including other local users on the same system. Properly protect the key (CWE-320). If you c
  - [Architecture and Design] For inbound authentication: Rather than hard-code a default username and password, key, or other authentication credentials for first time logins, utilize a "first login" mode that requires the user to enter a unique strong password or key.
  - [Architecture and Design] If the product must contain hard-coded credentials or they cannot be removed, perform access control checks and limit which entities can access the feature that requires the hard-coded credentials. For example, a feature might only be enabled through the system console instead of through a network c
  - [Architecture and Design] For inbound authentication using passwords: apply strong one-way hashes to passwords and store those hashes in a configuration file or database with appropriate access control. That way, theft of the file/database still requires the attacker to try to crack the password. When handling an incoming pa

Demonstrative examples:
The following code uses a hard-coded password to connect to a database:
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:
... DriverManager.getConnec

The following code is an example of an internal hard-coded password in the back-end:
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."
int VerifyAdmin(char *password) { 
                         if (strcmp(password, "Mew!")) { 
                               
                                 printf("Incorrect Password!\n"); return(0) 
                            } printf("Entering Diagnostic Mode...\n"); return(1); 
                      }
int VerifyAdmin(String password) { if (!password.equals("Mew!")) { return(0) } //Diagnostic Mode return(1); }

Real-world examples:
  - CVE-2022-40263: Software for biological cell analysus has hard-coded credentials, leading to leak of Protected Health Information (PHI)
  - CVE-2022-29953: Condition Monitor firmware has a maintenance interface with hard-coded credentials
  - CVE-2022-29960: Engineering Workstation uses hard-coded cryptographic keys that could allow for unathorized filesystem access and privilege escalation
  - CVE-2022-29964: Distributed Control System (DCS) has hard-coded passwords for local shell access
  - CVE-2022-30997: Programmable Logic Controller (PLC) has a maintenance service that uses undocumented, hard-coded credentials