如何“防篡改” php中的$_SERVER变量是什么?

发布于 2024-10-04 03:48:13 字数 78 浏览 0 评论 0原文

通过信任 $_SERVER 变量数组的内容来使用 $_SERVER['PHP_SELF'] 获取 php 文件的名称,我会冒很大的安全风险吗?

Would I be taking a big security risk by trusting the content of the $_SERVER variable array to get the name of php file using $_SERVER['PHP_SELF']?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海风掠过北极光 2024-10-11 03:48:13

许多但并非全部 $_SERVER 变量都受到攻击者控制。例如,$_SERVER['SCRIPT_NAME'] 是安全的,而 $_SEVER['PHP_SELF'] 是一个不同的危险变量,并且通常是 xss 的来源:

<?php
echo $_SEVER['PHP_SELF'];
?>

PoC:

http://localhost/self.php/<script>alert(/xss/)</script>

它通过查看 phpinfo

Many but not all of the $_SERVER variables are attacker controlled. For instance $_SERVER['SCRIPT_NAME'] is safe where as $_SEVER['PHP_SELF'] is a vary dangerous variable and is often the source of xss:

<?php
echo $_SEVER['PHP_SELF'];
?>

PoC:

http://localhost/self.php/<script>alert(/xss/)</script>

It is easy to see this vulnerability in action by looking at phpinfo.

柠檬心 2024-10-11 03:48:13

没有有效的特殊机制来保护该变量。您可以像写入任何其他变量一样写入它。因此,您必须像任何其他变量一样保护它免遭篡改(禁用 register_globals、避免变量变量等)。那么你就可以相信它。

作为一种解决方法,您可以在程序的早期定义自己的常量:

define('SCRIPT_FILENAME',$_SERVER['SCRIPT_FILENAME']);

并在可用的情况下使用预定义常量,例如 __FILE__

There is no special mechanism in effect to protect this variable. You can write to it as you can to any other variable. So you have to protect it against tampering like any other variable (disable register_globals, avoid variable variables, etc.). Then you can trust it.

As a workaround to be sure, you can define your own constants early in your program:

define('SCRIPT_FILENAME',$_SERVER['SCRIPT_FILENAME']);

and use predefined constants where available, e.g. __FILE__.

燃情 2024-10-11 03:48:13

来自 php.net 手册

此数组中的条目由 Web 服务器创建。无法保证每个网络服务器都会提供其中任何一个;服务器可能会省略一些,或提供此处未列出的其他内容。

因此,如果您了解有权更改服务器配置的所有用户(以及会话中可能修改变量内容的所有脚本),您就可以合理地确定 $_SERVER 变量的数据。

From the php.net manual:

The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here.

So, if you are aware of all users who have access to change server configuration, (and all scripts in your session that may modify the contents of the variable) you can be reasonably sure of the $_SERVER variable's data.

清引 2024-10-11 03:48:13

完全不,只要您不使用用户的数据,这实际上根本就不会构成风险。也就是说,使用以下之一:

echo __FILE__;
// is the same as
echo $_SERVER["SCRIPT_FILENAME"];

echo $_SERVER["SCRIPT_NAME"];
// SCRIPT_NAME contains just the path

Not at all, this can not actually be a risk at all as long as you don't use data from user. That is, use one of these:

echo __FILE__;
// is the same as
echo $_SERVER["SCRIPT_FILENAME"];

echo $_SERVER["SCRIPT_NAME"];
// SCRIPT_NAME contains just the path
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文