douqu8828 2014-10-02 07:55
浏览 85

用解码的base64字符串替换受保护的成员

Let's say i have a class like this

class ClassA {
   public $publicMember;
   private $privateMember;
   protected $protected;
}

And i encoded the object ClassA like this:

$objectA = new ClassA();
$stringA = base64_encode(serialize($objectA));

I want to replace all protected member but i don't know how. I've tried like this:

$newString = str_replace('�*�', '', base64_decode($stringA));

I am sorry it this question is easy, but i really don't know how to handle this. Thank you!

  • 写回答

2条回答 默认 最新

  • dqz86173 2014-10-02 08:30
    关注

    Property name precedes with length.

    string(92) "O:6:"ClassA":3:{s:12:"publicMember";N;s:21:"\000ClassA\000privateMember";N;s:12:"\000*\000protected";N;}"
    | property name length        ^                     ^                                    ^
    | we will try to capture this part                                                      ^         ^    
    

    You ned reduce this length if you what to "convert" protected to public. For example.

    class ClassA
        {
    
        public $publicMember;
        private $privateMember;
        protected $protected;
    
        }
    
    $objectA = new ClassA();
    $stringA = serialize($objectA);
    $converted = preg_replace_callback('@:(\d+):"\x00\*\x00@', function($match)
    //                                     ^ catch the number
    //                                           ^     ^    null symbols        
        {
        $property_name_length = $match[1];
        return ':' . ($property_name_length - 3) . ':"'; // reduce catched number and do not return \x00*\x00
        }, $stringA);
    var_dump(unserialize($converted));
    

    Produces

    class ClassA#2 (4) {
      public $publicMember =>
      NULL
      private $privateMember =>
      NULL
      protected $protected =>
      NULL
      public $protected =>
      NULL
    }
    

    P.S. But this code will fail when you start to store serialized like data as members values.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?