Skip to content

Instantly share code, notes, and snippets.

@vctls
Last active November 18, 2022 12:27
Show Gist options
  • Save vctls/f99852d4aeaa8620357e21ec9e3f6be3 to your computer and use it in GitHub Desktop.
Save vctls/f99852d4aeaa8620357e21ec9e3f6be3 to your computer and use it in GitHub Desktop.
Slightly more ““secure”” parsing of php values through eval()
<?php
$phpString = '$values = ["test"];';
$code = <<<PHP
<?php
\$values = [];
\$string = <<<'STRING'
$phpString
STRING;
eval(\$string);
echo serialize([\$values]);
PHP;
// Get all enabled internal functions besides serialize
// in order to disable them before evaluating the script.
$functions = get_defined_functions()['internal'];
$serializeIndex = array_search('serialize', $functions, true);
array_splice($functions, $serializeIndex, 1);
$fnToDisable = implode(',', $functions);
// Put the code evaluating the raw php string into a temporary file,
// execute the file with a separate php process,
// retrieve the serialized data, and unserialize it.
// This, of course, does not disable constructs like eval() itself,
// but even functions within an eval wont work anyway.
$tmpFile = tmpfile();
fwrite($tmpFile, $code);
$path = stream_get_meta_data($tmpFile)['uri'];
$command = "php -d disable_functions=$fnToDisable $path";
$result = shell_exec($command);
fclose($tmpFile);
$data = @unserialize($result, [false]);
@vctls
Copy link
Author

vctls commented Nov 18, 2022

Use at your own risk lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment