界面

关键代码
- 处理用户输入和输出的部分,很简单
if(isset($_GET["firstname"]) && isset($_GET["lastname"]))
{
$firstname = $_GET["firstname"];
$lastname = $_GET["lastname"];
if($firstname == "" or $lastname == "")
{
//firstname和lastname有一个为空就输出这句话
echo "<font color=\"red\">Please enter both fields...</font>";
}
else
{
//firstname和lastname都不为空才输出过滤后的firstname和lastname
echo "Welcome " . htmli($firstname) . " " . htmli($lastname);
}
}
- html字符过滤级别
function htmli($data)
{
switch($_COOKIE["security_level"])
{
//对应security_level为low
case "0" :
$data = no_check($data);
break;
//对应security_level为medium
case "1" :
$data = xss_check_1($data);
break;
//对应security_level为high
case "2" :
$data = xss_check_3($data);
break;
default :
$data = no_check($data);
break;
}
return $data;
}
对应方法
- 安全级别为low
从函数名字no_check()
可以看出应该是不做任何过滤和替换,事实也的确如此。
源码:
function no_check($data)
{
return $data;//原样返回
}
我们输入任意的html代码都能原样显示出来,例如我们在firstname中输入:
<script> alert("123"); </script>
lastname中随意输入一点内容,然后点击Go按钮。

- 安全级别为medium
源码:
function xss_check_1($data)
{
// Converts only "<" and ">" to HTLM entities
$input = str_replace("<", "<", $data);
$input = str_replace(">", ">", $input);
// Failure is an option
// Bypasses double encoding attacks
// <script>alert(0)</script>
// %3Cscript%3Ealert%280%29%3C%2Fscript%3E
// %253Cscript%253Ealert%25280%2529%253C%252Fscript%253E
$input = urldecode($input);
return $input;
}
作用就是将<
替换成<
,将>
替换成>
。
相关资料:HTML 字符实体
现在html的script标签被显示出来了,没有没浏览器解释执行。

注意$input = urldecode($input);
,此处进行了url解码,只要我们将<>
转换成url编码后的形式就可以避免被str_replace
给替换掉,最后还能被url解码还原成原来的形式。
相关资料:为什么要进行URL编码
将刚才的html代码进行url编码,相关工具:在线url编码/解码
%3cscript%3e+alert(%22123%22)%3b++%3c%2fscript%3e
然后输入到firstname中,同样lastname随便输入。
同样弹出了123,不再截图了。
- 安全级别为high
源码:
function xss_check_3($data, $encoding = "UTF-8")
{
// htmlspecialchars - converts special characters to HTML entities
// '&' (ampersand) becomes '&'
// '"' (double quote) becomes '"' when ENT_NOQUOTES is not set
// "'" (single quote) becomes ''' (or ') only when ENT_QUOTES is set
// '<' (less than) becomes '<'
// '>' (greater than) becomes '>'
return htmlspecialchars($data, ENT_QUOTES, $encoding);//ENT_QUOTES - 编码双引号和单引号。
}
相关资料:PHP htmlentities() 函数
TMD单引号也给我过滤了,本人小白,完全没思路。
看了某乎上大佬们的回答以后也没思路。
相关链接:XSS 攻击时怎么绕过 htmlspecialchars 函数呢?
最后给Google Chrome一个好评

网友评论