Dependencies and Malicious Code Inclusion

Why It's Critical

Loading untrusted code gives attackers complete control:

  • Code execution: Malicious code runs with application privileges

  • Data theft: Steal credentials, keys, customer data

  • Malware distribution: Use infected system to spread malware

  • Persistence: Malicious code survives application restarts

  • Supply chain attack: Single compromised dependency affects thousands

A single untrusted dependency can compromise entire applications and networks.


Real-World Attack Scenarios

Scenario 1: NPM Package Dependency Vulnerability

JavaScript application uses NPM package with vulnerability:

{
  "dependencies": {
    "lodash": "4.17.15",
    "express": "4.17.1",
    "axios": "0.19.0"
  }
}

The attack:

  1. Attacker discovers vulnerability in axios 0.19.0

  2. Creates exploit that steals API keys from environment

  3. Publishes as update to same version or patches version

  4. When npm install runs, malicious version downloaded

Result:

  • Credentials stolen during installation

  • Attacker gains cloud access

  • Database compromise

  • Complete infrastructure takeover

Finding it: Check dependency versions. Look for suspicious install scripts. Monitor outbound network requests during npm install.


Web application loads JavaScript from CDN without verification:

The attack:

  1. Attacker compromises CDN or redirects traffic

  2. Serves malicious JavaScript instead

  3. JavaScript steals session tokens, credentials, form data

When user submits form → credentials sent to attacker.

Result:

  • Session token theft

  • Credential capture

  • Form data exfiltration

  • Account takeover

Finding it: Check for SRI (Subresource Integrity) hashes. Look for external script sources. Verify HTTPS for all resources.


Scenario 3: Plugin/Extension Vulnerability

Application loads third-party plugins without verification:

The attack:

  1. Attacker places malicious plugin in plugins directory

  2. Application loads and executes it

  3. Malicious plugin has full application access

Result:

  • Remote code execution

  • Full application compromise

  • Attacker command execution

Finding it: Check plugin loading mechanisms. Look for plugins without signature verification. Test loading malicious plugins.


Scenario 4: Package Manager Supply Chain Attack

Attacker registers similar package name to popular library (typosquatting):

The attack:

Result:

  • Credentials stolen during development

  • Developer machine compromised

  • All projects affected

Finding it: Verify package names carefully. Use lock files (package-lock.json). Monitor packages for suspicious behavior.


Scenario 5: Unverified Code Download

Application downloads executable code at runtime without integrity check:

The attack:

  1. Attacker intercepts download (MITM attack)

  2. Replaces legitimate code with malware

  3. Malware executes with application privileges

Or attacker compromises download server:

Result:

  • Malware execution

  • System compromise

  • Credential theft

  • Persistence

Finding it: Check for downloads without HTTPS. Look for missing integrity checks. Test with MITM proxy.


How to Identify Untrusted Code Loading During Testing

1. Audit dependencies

2. Test for missing integrity checks

3. Monitor network requests

Watch for:

  • Unencrypted downloads (HTTP instead of HTTPS)

  • Missing SRI hashes on script tags

  • External script sources

  • Unexpected outbound connections

4. Check for signature verification

5. Test plugin/extension loading

Try loading malicious plugins. Check if executed without validation.


Mitigation Strategies

Use SRI (Subresource Integrity) for external scripts

Pin dependency versions

Use lock files

Verify package integrity

Don't execute install scripts

Use allowlist for external resources

Verify code signatures

Monitor for suspicious behavior

  • Unexpected network requests

  • Spawning child processes

  • File system modifications

  • Environment variable access

  • Credential theft attempts


Last updated