简体   繁体   English

php的本机函数与未经过滤的输入一起使用有多安全?

[英]How safe are php's native functions to use with unfiltered input?

Maybe I am being a bit paranoid, but as I am re-writing a contact module, the following question came to mind: 也许我有点偏执,但在我重新编写联系人模块时,我想到了以下问题:

Can I use unfiltered input in php's native functions? 我可以在php的原生函数中使用未过滤的输入吗?

It is easy to sanitize stuff to put in a database, output to the screen, etc. but I was wondering if for example the following statement could be dangerous: 很容易清理东西以放入数据库,输出到屏幕等,但我想知道例如以下语句是否可能是危险的:

    if (file_exists($_POST['brochure'])) {
        // do some stuff
    }

If someone somehow managed to post to that page, could the above code be exploited? 如果某人设法发布到该页面,上述代码是否可以被利用?

The above code is just an example, I can think of other functions I use when processing a form. 上面的代码只是一个例子,我可以想到我在处理表单时使用的其他函数。

Edit: Thanks everybody, the file_exists in the example is actually part of a sanitation function but when cleaning up, php functions are being used so it is rapidly becoming a chicken and egg story: To use functions, I have to clean up, but to clean up I have to use functions. 编辑:谢谢大家,示例中的file_exists实际上是卫生功能的一部分,但是当清理时,正在使用php函数,因此它正在迅速变成鸡蛋和鸡蛋的故事:要使用函数,我必须清理,但是清理我必须使用功能。

Anyway, I have got some fresh ideas now. 无论如何,我现在有了一些新鲜的想法。

Yep. 是的。 All I'd have to do is post "/etc/passwd", or "includes/dbconnection.php" (or anything) to your page, and depending on what //do some stuff actually is, I could possibly delete, modify or read sensitive information. 我所要做的就是将“/ etc / passwd”或“includes / dbconnection.php”(或任何内容)发布到您的页面,并根据//do some stuff实际上//do some stuff ,我可以删除,修改或阅读敏感信息。 The file_exists function itself won't do anything you wouldn't expect, but you can expect malicious users exploiting your logic. file_exists函数本身不会执行您不希望做的任何事情,但您可以预期恶意用户会利用您的逻辑。

Always sanitise your user input. 始终清理您的用户输入。 Always. 总是。 If you're expecting to only grab files from one particular folder, don't allow .. or / in the input 如果您希望仅从一个特定文件夹中获取文件,请不要在输入中允许..或/

By itself, that looks reasonably safe, but it could be used to reveal information. 它本身看起来相当安全,但它可以用来揭示信息。 It could allow an attack to check for the presence (or absence) of particular files (eg /etc/passwd , /proc/* , etc). 它可以允许攻击检查特定文件的存在(或不存在)(例如/etc/passwd/proc/*等)。

So in this example, you should ensure that $_POST['brochure'] is sanitised first to only accept inputs that match potentially valid file names. 因此,在此示例中,您应确保首先清理$_POST['brochure']以仅接受与潜在有效文件名匹配的输入。 Drop any input that contains .. , or that starts with a / . 删除包含..任何输入,或以/开头的输入。

Other functions could have potentially much worse side effects... 其他功能可能会产生更糟糕的副作用......

PHP's builtins won't do "unexpected" things on bad input (eg, file_exists("foo; rm -r /") will say "no, the file 'foo; rm -r /' doesn't exist")... And if they do, that's a bug you can file against Zend. PHP的内置函数不会在输入错误时执行“意外”操作(例如, file_exists("foo; rm -r /")会说“不,文件'foo; rm -r /'不存在”)。如果他们这样做,那就是你可以提交Zend的错误。

Of course, this doesn't stop people from exploiting your code (eg, file_exists("../hidden/shell.php") ), so you should still (actually, you should always ) be careful when passing user-supplied input around. 当然,这并不能阻止人们利用你的代码(例如, file_exists("../hidden/shell.php") ),所以在传递用户提供的输入时你仍然应该(实际上,你应该总是 )小心周围。

could 'brochure' = '../../../../.htaccess' 可以'宣传'='../../../../.htaccess'

that's an interesting question. 这是一个有趣的问题。

Apache on my computer is set to deny listing or viewing .ht* and .ini and .php.inc files, but you have me worried now. 我的计算机上的Apache设置为拒绝列出或查看.ht *和.ini和.php.inc文件,但你现在让我担心。

你应该养成过滤所有输入的习惯,但是你可能想查看http://www.hardened-php.net/ ,它分发了一个强化补丁和'Suhosin',它默认存在于许多二进制发行版中( OpenSUSE,Mandriva和Debian / Ubuntu)

The fact that you have to ask is your answer. 您必须要问的是您的答案。 It's not safe. 这不安全。

file_exists() is not as bad as others, but if you don't see the source code for the function you're passing data to, and know how it handles user input, then you're taking a chance. file_exists()没有其他人那么糟糕,但是如果你没有看到你传递数据的函数的源代码,并且知道它如何处理用户输入,那么你就有机会了。

It's not a good idea to pass unfiltered user data into any php filesystem command. 将未过滤的用户数据传递到任何php文件系统命令并不是一个好主意。 The key with security is that you never allow input to context switch. 具有安全性的关键是您永远不允许输入上下文切换。 In this case, your minimum sanitization should be removing path characters. 在这种情况下,您的最小清理应该是删除路径字符。

Always assume a hostile user and the worst they could possibly do if they saw your source code. 总是假设一个敌对用户,如果他们看到你的源代码,他们可能会做的最坏的事情。

多年来,在PHP本身的源代码中发现了安全漏洞,因此函数可以缓冲溢出流式攻击Suhosin是一个修补PHP以消除一些风险的项目。

I wouldn't trust those functions at all. 我根本不相信这些功能。

This may definitively sound strikingly, however when I closely watch the people and their C code quality in the commits, reverts, etc. over the last eight years, I'm just in constant fear. 这听起来确实很明显,但是当我在过去的八年中密切关注人们及其C代码的质量时,我还会不断担心。

What happens with the database string is that it could be retrieved and used somewhere that has a vulnerability. 数据库字符串的作用是可以检索并在有漏洞的地方使用它。 For the answer to your question, think about removing the step where you store the string and then retrieve it. 对于您的问题的答案,请考虑删除存储字符串的步骤,然后检索它。 You use it right away, and you have the same risks. 你马上使用它,你也有同样的风险。

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

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