简体   繁体   English

序列化和反序列化$ _POST数组是否安全?

[英]Is it safe to serialize and unserialize the $_POST array?

TLDR: Is it safe to serialize the $_POST array to a file and then read it back into the $_POST variable on a different request or a different script? TLDR:将$ _POST数组序列化为文件,然后根据不同的请求或不同的脚本将其读回到$ _POST变量中是否安全?

I realize this is unusual. 我意识到这很不正常。 There is a reason for it, and it would take a dozen pages of text to explain why I am considering doing it in a special case, at least in the meantime. 这是有原因的,至少要在此期间,需要十几页的文字来解释为什么我要考虑在特殊情况下进行此操作。

Boiled down process: 水煮过程:

file_put_contents('sample.post', serialize($_POST));

$_POST = unserialize(file_get_contents('sample.post'));

I already have extensive filtering in place for the actual contents of the post variable. 我已经对post变量的实际内容进行了广泛的过滤。 My question is whether or not the process of serializing and unserializing the entire $_POST array is giving a malicious user a method of attack. 我的问题是,整个$ _POST数组的序列化和反序列化过程是否为恶意用户提供了一种攻击方法。

The PHP doc says "Warning. Do not pass untrusted user input to unserialize(). Unserialization can result in code being loaded and executed due to object instantiation and autoloading, and a malicious user may be able to exploit this." PHP文档说:“警告。请勿将不受信任的用户输入传递给unserialize()。反序列化可能会由于对象实例化和自动加载而导致代码被加载和执行,并且恶意用户可能会利用它。”

I found these articles that describe this method of attack. 我发现这些文章描述了这种攻击方法。 But they seem to depend on the user being able to specify the string to unserialize directly, IE unserialize($_POST['value']). 但是它们似乎取决于用户能否指定要直接反序列化的字符串,即IE unserialize($ _ POST ['value'])。

https://www.notsosecure.com/remote-code-execution-via-php-unserialize/ https://heine.familiedeelstra.com/security/unserialize https://www.notsosecure.com/remote-code-execution-via-php-unserialize/ https://heine.familiedeelstra.com/security/unserialize

Am I correct that as long as I am serializing and unserializing, objects can't be created in the unserializing process, right? 我是否正确,只要我要序列化和反序列化,就不能在反序列化过程中创建对象,对吗?

I am under the impression that the $_POST array will only ever contain strings (though I couldn't find that explicitly mentioned in the PHP docs). 我的印象是$ _POST数组将仅包含字符串(尽管我找不到PHP文档中明确提到的字符串)。

As I understand it, even if someone supplies a string matching the format of a serialized object, it will be 'stored' as a string during serialization, with an explicit length (byte length). 据我了解,即使有人提供与序列化对象格式匹配的字符串,它也将在序列化过程中以字符串的形式“存储”为一个明确的长度(字节长度)。 So it will just be assigned as a string when unserializing. 因此,将在反序列化时将其分配为字符串。 It seems like since the lengths of the strings are stored along with them during serialization, you can't break the structure of a serialized string from the input like you might with SQL injection. 看起来,由于字符串的长度在序列化过程中与它们一起存储,因此您无法像使用SQL注入那样从输入中破坏序列化字符串的结构。 I tried to trick it with some invalid multi-byte characters, but no luck. 我试图用一些无效的多字节字符来欺骗它,但是没有运气。 However, me being unable to do it and experienced hackers being able to do it are 2 different things. 但是,我无法做到这一点和经验丰富的黑客能够做到这一点是两件事。

I couldn't find anything else about other attack methods. 我找不到其他有关其他攻击方法的信息。

Please let me know if I'm missing something! 如果我缺少什么,请告诉我! I just read several comments saying 'you should never do this', so I'm nervous that I'm misunderstanding something. 我刚刚读了几句话,说“你永远都不要这样做”,所以我担心自己误会了一些东西。

I think it is not possible without further attacks to send something as a POST variable to exploit the unserialize() call later in your scenario, but others may probably have an idea. 我认为如果没有进一步的攻击,就不可能在以后的情况下发送某些内容作为POST变量来利用unserialize()调用,但是其他人可能会有一个主意。 It could be a problem if $_POST had something unserializable, but I think that may not happen. 如果$ _POST具有无法序列化的内容,则可能是一个问题,但我认为可能不会发生。 Whatever was in $_POST, it was already in memory once, so assuming serialize() and unserialize() work correctly, it should be secure to do something like 无论$ _POST中的内容是什么,它都已经在内存中一次,因此,假设serialize()unserialize()可以正常工作,则执行类似以下操作应是安全的

$serialized = serialize($userinput);
unserialize($serialized);

However, you are saving this data to disk inbetween. 但是,您正在将该数据保存到它们之间的磁盘上。 After exploiting a different flaw and having access to your files (or having access "by design", like IT ops staff), an attacker may be able to modify saved serialized data, and may inject his attack there. 在利用了不同的漏洞并可以访问您的文件之后(或像IT操作人员一样“通过设计方式”访问了该文件),攻击者可能能够修改保存的序列化数据,并可能向其发起攻击。 That way it would obviously be vulnerable. 这样,它显然会很脆弱。

So it is indeed a risk, albeit maybe not a very high one. 因此,这确实是一种风险,尽管可能不是很高。 Be aware that the security of this solution depends a lot on security of your saved serialized content. 请注意,此解决方案的安全性在很大程度上取决于您保存的序列化内容的安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM