在 php 中反序列化从 shell exec 接收到的数据时出错

errors while unserialize data received from shell exec in php

我有下面这行代码可以使用 shell_exec 在背景中创建不同大小的图像缩略图。它在网上运行良好,但在我的 xampp 本地主机上,我在日志文件中不断收到以下错误。

Notice: unserialize(): Error at offset 0 of 182 bytes in C:\xampp\htdocs\example.com\assets\createImgeThumbnils.php on line 4 bool(false) PHP Notice: unserialize(): Error at offset 0 of 182 bytes in C:\xampp\htdocs\example.com\assets\createImgeThumbnils.php on line 4

这是我的上传脚本中用于发送 shell 命令的内容

<?php
$arg = array ( 
    "dirname" => "gallery/p", 
    "basename" => "878513f88f048477029c8836438773ef-1.jpeg", 
    "extension" => "jpeg", 
    "tag" => 1, 
    "filename" => "878513f88f048477029c8836438773ef-1"
);
$arg = serialize($arg);
shell_exec(
    "C:\xampp\php\php.exe 
    " . __DIR__ . "/../../../assets/createImgeThumbnils.php 
    '".$arg."' 'alert' >> 
    " . __DIR__ . "/../../../server_assets/alert_log/paging.log 2>&1
");

这是我的 createImgeThumbnils.php

示例脚本
<?php
if(isset($argv[1]) && !empty($argv[1])){
    $data = $argv[1];
    //$data = preg_replace('!s:(\d+):"(.*?)";!e', "'s:'.strlen('').':\"\";'", $argv[1]);
    var_dump(unserialize($data));
}else{
    echo "NO is empty";
}

下面是我查看unserialize($argv)

时收到的数据
array(3) {
  [0]=>
  string(130) "C:\xampp\htdocs\example.com\app\test\file_path/../../../assets/createImgeThumbnils.php"
  [1]=>
  string(180) "'a:5:{s:7:dirname;s:9:gallery/p;s:8:basename;s:39:878513f88f048477029c8836438773ef-1.jpeg;s:9:extension;s:4:jpeg;s:3:tag;i:1;s:8:filename;s:34:878513f88f048477029c8836438773ef-1;}'"
  [2]=>
  string(7) "'alert'"
}

Unserialize 抱怨前导单引号。但是您的序列化字符串也缺少所有双引号。由于它是在线工作的,我认为您在 localhost 上的 shell 对待引号的方式不同。你可以避免这样的问题,如果你用例如编码你的参数base64。在您的第一个脚本中:

shell_exec(
  "C:\xampp\php\php.exe 
  " . __DIR__ . "/../../../assets/createImgeThumbnils.php 
  ".base64_encode($arg)." 'alert' >> 
  " . __DIR__ . "/../../../server_assets/alert_log/paging.log 2>&1
");

在你的第二个脚本中:

$data = base64_decode($argv[1]);