/**
Copyright: 2024 Contrast Security, Inc
  Contact: support@contrastsecurity.com
  License: Commercial

  NOTICE: This Software and the patented inventions embodied within may only be
  used as part of Contrast Security’s commercial offerings. Even though it is
  made available through public repositories, use of this Software is subject to
  the applicable End User Licensing Agreement found at
  https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
  between Contrast Security and the End User. The Software may not be reverse
  engineered, modified, repackaged, sold, redistributed or otherwise used in a
  way not consistent with the End User License Agreement.
*/
/**
 * Searches an input string looking for chained command patterns.
 * @module lib/rules/protect/cmd-injection/custom-searchers/ChainedCommandSearcher
 */

'use strict';

const Builder = require('../../../attack-patterns');
const { CustomSearcher } = require('../../signature');
const config = require('../config');

const common = require('../../../common');
const { SCORES } = common;

const commandLiterals = [
  'wget',
  'uname',
  'dir',
  'netstat',
  'nc',
  'echo',
  'ping',
  'whoami'
];

const builderPatternNames = [
  'anyUsrBin',
  'echoToFile',
  'catToFile',
  'netcat',
  'netstat',
  'ping',
  'uname',
  'whoami',
  'wget'
];

/**
 * Returns a function that tests whether an input string
 * matches chained command patterns. The regular expressions
 * are built once and used for each test function via closure.
 * @param {string} value The string to test
 * @returns {boolean} Whether any patterns match
 */
const matchesCommandPattern = (value) =>
  builderPatternNames.some((name) => {
    const rx = Builder()
      .anyOf(config.exploitChars)
      .whitespaces()
      .CMD()
      [name]()
      .build();

    return rx.test(value);
  });

/**
 * Returns a function that tests whether an input string
 * matches chained command patterns starting with <code>&&</code>.
 * The regular expressions are built once and used for each test
 * function via closure.
 * @param {string} value The string to test
 * @returns {boolean} Whether any patterns match
 */
const matchesDoubleAndPattern = (value) =>
  commandLiterals.some((command) => {
    const rx = Builder()
      .literally('&&', true)
      .whitespaces()
      .literally(command, true)
      .build();

    return rx.test(value);
  });

/**
 * Tests whether an input string matches any chained command patterns.
 * All patterns score HIGH.
 */
class ChainedCommandSearcher extends CustomSearcher {
  constructor() {
    super();
    this.description =
      'Tests whether an input string matches chained command patterns.';
    this.id = 'CMDI-CCS-1';
    this.name = 'ChainedCommandSearcher';
  }

  /**
   * @param {Object} userInput
   * @param {string} userInput.value
   * @returns {string}
   */
  getImpact(userInput) {
    const isMatch =
      matchesCommandPattern(userInput.value) ||
      matchesDoubleAndPattern(userInput.value);

    return isMatch ? SCORES.HIGH : SCORES.NONE;
  }
}

module.exports = new ChainedCommandSearcher();
