CWE-494: Download of Code Without Integrity Check

Description:
The product downloads source code or an executable from a remote location and executes the code without sufficiently verifying the origin and integrity of the code.

Extended Description:
An attacker can execute malicious code by compromising the host server, performing DNS spoofing, or modifying the code in transit.

Affected languages: Not Language-Specific
Parent weaknesses: CWE-345, CWE-669, CWE-669

Consequences:
  - Integrity: Execute Unauthorized Code or Commands

Detection methods:
  - Manual Analysis: This weakness can be detected using tools and techniques that require manual (human) analysis, such as penetration testing, threat modeling, and interactive tools that allow the tester to record and m
  - Black Box: Use monitoring tools that examine the software's process as it interacts with the operating system and the network. This technique is useful in cases when source code is unavailable, if the software w
  - Automated Static Analysis: Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without havin

Mitigations:
  - [Implementation] Perform proper forward and reverse DNS lookups to detect DNS spoofing.
  - [Architecture and Design] Encrypt the code with a reliable encryption scheme before transmitting. 
                   This will only be a partial solution, since it will not detect DNS spoofing and it will not prevent your code from being modified on the hosting site.
  - [Architecture and Design] Use a vetted library or framework that does not allow this weakness to occur or provides constructs that make this weakness easier to avoid [REF-1482]. 
                   Speficially, it may be helpful to use tools or frameworks to perform integrity checking on the transmitted code. 
              
  - [Architecture and Design] Run your code using the lowest privileges that are required to accomplish the necessary tasks [REF-76]. If possible, create isolated accounts with limited privileges that are only used for a single task. That way, a successful attack will not immediately give the attacker access to the rest of the s

Demonstrative examples:
This example loads an external class from a local subdirectory.
This code does not ensure that the class loaded is the intended one, for example by verifying the class's checksum. An attacker may be able to modify the class file to execute malicious code.
URL[] classURLs= new URL[]{ new URL("file:subdir/") }; URLClassLoader loader = new URLClassLoader(classURLs); Class loadedClass = Class.forName("loadMe", true, loader);

This code includes an external script to get database credentials, then authenticates a user against the database, allowing access to the application.
This code does not verify that the external domain accessed is the intended one. An attacker may somehow cause the external domain name to resolve to an attack server, which would provide the information for a false database. The attacker may then steal the usernames and encrypted passwords from real user login attempts, or simply allow themself to access the application without a real user account.
//assume the password is already encrypted, avoiding CWE-312 
                      
                      function authenticate($username,$password){ 
                         include("http://external.example.com/dbInfo.php"); 
                 

Real-world examples:
  - CVE-2019-9534: Satellite phone does not validate its firmware image.
  - CVE-2021-22909: Chain: router's firmware update procedure uses curl with "-k" (insecure) option that disables certificate validation (CWE-295), allowing adversary-in-the-middle (AITM) compromise with a malicious firm
  - CVE-2008-3438: OS does not verify authenticity of its own updates.
  - CVE-2008-3324: online poker client does not verify authenticity of its own updates.
  - CVE-2001-1125: anti-virus product does not verify automatic updates for itself.