首页 > 解决方案 > 反序列化从 php 中的 shell exec 接收的数据时出错

问题描述

我有下面的代码行来使用 shell_exec 在后台创建不同大小的图像缩略图。它在网上运行良好,但在我的 xampp localhost 上,我在日志文件中不断收到错误消息。

注意:unserialize():在 C:\xampp\htdocs\example.com\assets\createImgeThumbnils.php 第 4 行中的 182 字节偏移 0 处出错 bool(false) PHP 注意:unserialize():在 182 的偏移 0 处出错第 4 行 C:\xampp\htdocs\example.com\assets\createImgeThumbnils.php 中的字节

这是我在上传脚本中发送 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('$2').':\"$2\";'", $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'"
}

标签: phpshell-exec

解决方案


反序列化正在抱怨前导单引号。但是您的序列化字符串也错过了所有双引号。由于它是在线工作的,我认为您在 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]);

推荐阅读