Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arbitrary PHP code execution in M2.0.2 #3233

Closed
scholtz opened this issue Feb 1, 2016 · 3 comments
Closed

Arbitrary PHP code execution in M2.0.2 #3233

scholtz opened this issue Feb 1, 2016 · 3 comments
Labels
bug report Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development

Comments

@scholtz
Copy link

scholtz commented Feb 1, 2016

I have looked over file Phrase.php and it seems it allows arbitrary php code execution!

Anyone can exploit it by setting the php into language csv file, and with the execution of command 'php bin/magento i18n:collect-phrases -o "lang.csv" -m .' it will execute.

I have modified a bit the Phrase.php to speed up testing:

    /**
     * Compile PHP string based on quotes type it enclosed with
     *
     * @param string $string
     * @return string
     *
     * @SuppressWarnings(PHPMD.EvalExpression)
     */
    private function getCompiledString($string)
    {
        $string = "'.file_put_contents('/tmp/o.html','test');'";
        var_dump($string);
        $encloseQuote = $this->getQuote() == Phrase::QUOTE_DOUBLE ? Phrase::QUOTE_DOUBLE : Phrase::QUOTE_SINGLE;
        //find all occurrences of ' and ", with no \ before it.
        preg_match_all('/(?<!\\\\)[\'"]/', $string, $matches);
        if (count($matches[0]) % 2 !== 0) {
            $string = addslashes($string);
        }
        $evalString = 'return ' . $encloseQuote . $string . $encloseQuote . ';';
        var_dump($evalString);
        $result = eval($evalString);
        var_dump($string);
        var_dump(file_get_contents("/tmp/o.html"));
        exit;
        return is_string($result) ? $result :  $string;
    }

Please do not use eval to evaluate the phrase :)

scholtz added a commit to scholtz/magento2 that referenced this issue Feb 1, 2016
i hope this is what this function was intended to do
@scholtz
Copy link
Author

scholtz commented Feb 1, 2016

There seems to be very similar arbitrary php execution bug in this file https://github.com/magento/magento2/blob/077584c99ebb8007cad176c3b9a0144a05c259cd/lib/internal/Magento/Framework/Data/Collection/Filesystem.php , but this one needs some products to be listed in the filesystem.. With the filtering the attacker should be able to run php script on server from remote host.

    /**
     * The filters renderer and caller
     * Applies to each row, renders once.
     *
     * @param array $row
     * @return bool
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     * @SuppressWarnings(PHPMD.EvalExpression)
     */
    protected function _filterRow($row)
    {
        // render filters once
        if (!$this->_isFiltersRendered) {
            $eval = '';
            for ($i = 0; $i < $this->_filterIncrement; $i++) {
                if (isset($this->_filterBrackets[$i])) {
                    $eval .= $this->_renderConditionBeforeFilterElement(
                        $i,
                        $this->_filterBrackets[$i]['is_and']
                    ) . $this->_filterBrackets[$i]['value'];
                } else {
                    $f = '$this->_filters[' . $i . ']';
                    $eval .= $this->_renderConditionBeforeFilterElement(
                        $i,
                        $this->_filters[$i]['is_and']
                    ) .
                        ($this->_filters[$i]['is_inverted'] ? '!' : '') .
                        '$this->_invokeFilter(' .
                        "{$f}['callback'], array({$f}['field'], {$f}['value'], " .
                        '$row))';
                }
            }
            $this->_filterEvalRendered = $eval;
            $this->_isFiltersRendered = true;
        }
        $result = false;
        if ($this->_filterEvalRendered) {
            eval('$result = ' . $this->_filterEvalRendered . ';');
        }
        return $result;
    }

There does not seem to be any verification agains code execution on this variable:

$this->_filterBrackets[$i]['value']

please do not use eval or similar functions, or read the PHP documentation:

The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand.

@i-daszkowski
Copy link

Thank you for reporting this issue. We have created an internal ticket APPSEC-1337 to fix the problem.

@i-daszkowski i-daszkowski added Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development PS bug report labels Feb 5, 2016
magento-team pushed a commit that referenced this issue Mar 1, 2016
- Removed eval statement from phrase parser.
magento-team pushed a commit that referenced this issue Mar 1, 2016
magento-team pushed a commit that referenced this issue Mar 1, 2016
- No need to add slashes now that we do not do eval.
@okorshenko
Copy link
Contributor

@scholtz this issue is fixed and merged to develop branch. Thank you for reporting this issue

magento-engcom-team pushed a commit that referenced this issue Oct 5, 2018
[thunder] MAGETWO-94844: Add the ability to install Magento without creating an administrator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development
Projects
None yet
Development

No branches or pull requests

3 participants