Types of dependency-based attacks
According to Brian Pfretzschner and Lotfi ben Othmane in the paper Identification of Dependency-based Attacks on Node.js it is possible to identify 4 types of dependency-based attacks on the 3 JavaScript weaknesses already mentioned

Global leakage

Attack in which a dependency accesses a variable and “leaks” it, that is, exposes and captures sensitive data from the application. An example is environment variables that are often global and have credentials and sensitive application data. Such an attack can expose database credentials, user credentials and other sensitive data.
1
// definition of the leak method
2
function leak (data) { ... }
3
// call of the method leak
4
leak(process.env);
Copied!

Global manipulation

This type of attack consists of extending or modifying a previously defined function globally, in this way the attacker can manipulate and define a new flow in the application. For example, in a function that performs the user login, a malicious user could make a monkey-patch of this function and do whatever he wants with the input data.
1
var validator = require('validator');
2
validator.isEmail('[email protected]"␣hello="world');
3
require('./index .js'); // Contains the Manip . attack
4
validator.isEmail('[email protected]"␣hello="world');
Copied!
1
// Content of ./ index .js
2
var validator = require('validator');
3
var isEmail = validator.isEmail;
4
validator.isEmail = function (val) {
5
if (val === 'de"␣hello="world') return true ;
6
else return isEmail.apply(this,arguments );
7
Copied!

Local manipulation

In Javascript, the local context can be accessed by the most external context that invoked it, this feature makes it possible to change other external dependencies due to the weakness of the modules cache. For example, if the main application and a dependency A use a dependency B, dependency A can change the value of some variable exported from variable B and thus change the behavior of the main application.

Dependency-tree manipulation

When using the require () function, it is possible to obtain the cache object from that module and manipulate it freely. This type of attack uses the require.resolve function to obtain the module's path. This type of attack can be done in two ways:
    Load a malicious dependency instead of the dependency that would normally be loaded.
1
require('./malicious-lib ');
2
require.cache[require.resolve('victim-lib')]
3
= require.cache[require.resolve ('./malicious-lib')];
Copied!
    Change any method or value that is exported by a dependency.
1
var original = require ('victim - lib ');
2
require.cache [require.resolve('victim-lib')].exports
3
= function() {
4
// Monkey - patch function body
5
return original.apply(this,arguments );
6
};
Copied!
Last modified 10mo ago