http://www./test_form.php/%22%3E%3Cscript%3Ealert(‘hacked‘)%3C/script%3E
以上的 URL 中,将被解析为如下代码并执行:
<form method="post" action="test_form.php/"><script>alert(‘hacked‘)</script>
代码中添加了 script 标签,并添加了alert命令。 当页面载入时会执行该Javascript代码(用户会看到弹出框)。 这仅仅只是一个简单的实例来说明PHP_SELF变量会被黑客利用。
请注意, 任何JavaScript代码可以添加在<script>标签中! 黑客可以利用这点重定向页面到另外一台服务器的页面上,页面 代码文件中可以保护恶意代码,代码可以修改全局变量或者获取用户的表单数据。
如何避免 $_SERVER["PHP_SELF"] 被利用?
$_SERVER["PHP_SELF"] 可以通过 htmlspecialchars() 函数来避免被利用。
form 代码如下所示:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
htmlspecialchars() 把一些预定义的字符转换为 HTML 实体。现在如果用户想利用 PHP_SELF 变量, 结果将输出如下所示:
<form method="post" action="test_form.php/"><script>alert(‘hacked‘)</script>">
尝试该漏洞失败!
使用 PHP 验证表单数据
首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数处理。
当我们使用 htmlspecialchars() 函数时,在用户尝试提交以下文本域:
<script>location.href(‘http://www)</script>
该代码将不会被执行,因为它会被保存为HTML转义代码,如下所示:
<script>location.href(‘http://www.‘)</script>
以上代码是安全的,可以正常在页面显示或者插入邮件中。
当用户提交表单时,我们将做以下两件事情,:
- 使用 PHP trim() 函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)。
- 使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\)
接下来让我们将这些过滤的函数写在一个我们自己定义的函数中,这样可以大大提高代码的复用性。
将函数命名为 test_input()。
现在,我们可以通过test_input()函数来检测 $_POST 中的所有变量, 脚本代码如下所示:
实例
<?php
// 定义变量并默认设置为空值
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data); \\去除没用的换行、空格
$data = stripslashes($data); \\将反斜杠去除
// 定义变量并默认设置为空值
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$website = test_input($_POST["website"]);
$comment = test_input($_POST["comment"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data)
{
$data = trim($data); \\去除没用的换行、空格
$data = stripslashes($data); \\将反斜杠去除
$data = htmlspecialchars($data); \\将特殊字符转码,防XSS
/*
- & (和号) 成为 &
- " (双引号) 成为 "
- ‘ (单引号) 成为 '
- < (小于) 成为 <
- > (大于) 成为 >
*/
return $data;
}
?>
}
?>
对表单输入时做验证(白名单方法)
<?php// 定义变量并默认设置为空值$nameErr = $emailErr = $genderErr = $websiteErr = "";$name = $email = $gender = $comment = $website = "";if ($_SERVER["REQUEST_METHOD"] == "POST") { ??if (empty($_POST["name"])) { ?????$nameErr = "Name is required"; ?????} else { ????????$name = test_input($_POST["name"]); ????????// 检测名字是否只包含字母跟空格 ????????if (!preg_match("/^[a-zA-Z ]*$/",$name)) { ????????$nameErr = "只允许字母和空格"; ?????????} ????} ?????if (empty($_POST["email"])) { ?????$emailErr = "Email is required"; ??} else { ?????$email = test_input($_POST["email"]); ?????// 检测邮箱是否合法 ?????if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { ????????$emailErr = "非法邮箱格式"; ??????} ??} ???????if (empty($_POST["website"])) { ?????$website = ""; ??} else { ?????$website = test_input($_POST["website"]); ?????// 检测 URL 地址是否合法 ????if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { ????????$websiteErr = "非法的 URL 的地址"; ??????} ??} ??if (empty($_POST["comment"])) { ?????$comment = ""; ??} else { ?????$comment = test_input($_POST["comment"]); ??} ??if (empty($_POST["gender"])) { ?????$genderErr = "性别是必需的"; ??} else { ?????$gender = test_input($_POST["gender"]); ??}}?>
php前端做过滤校验
原文地址:https://www.cnblogs.com/drkang/p/8462954.html