1.前言:
这个是我做CG-CTF-Web-单身二十年时遇到的,这里就该题简单总结一下
2.window.location介绍
获取当前页面地址(URL)并把浏览器重定向到新页面
搭建环境测试
test.html代码:
<html>
<head>
<script type="text/javascript">
function wanzhengurl()
{
alert(window.location)
}
function chongdingxiangurl()
{
window.location="/no_key_is_here_forever.php"
}
</script>
</head>
<body>
<input type="button" onclick="wanzhengurl()" value="Get URL">
<input type="button" onclick="chongdingxiangurl()" value="Change URL">
</body>
</html>
(1)获取到该页面完整url:
(2)进行重定向URL(no_key_is_here_forever.php不在根目录下,在根目录的下一级window.location_test目录下,因此报错)
总结:由上面测试可以看出,window.location="/no_key_is_here_forever.php"
,是将获取到的完整的URL后,仅保留根目录,也就是将no_key_is_here_forever.php
替换根目录后面的所有目录,即最后重定向的URL为http://127.0.0.1:8008/no_key_is_here_forever.php
3.做CTF题时遇到的代码
<script>window.location="./no_key_is_here_forever.php"; </script>
上面的js代码也是将页面重定向到no_key_is_here_forever.php
,只不过原先的/变成了./,这又是什么意思呢
于是我又在本地搭了个环境用做测试,环境搭建:
index.html:
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<a href="./search_key.php">重定向URL</a>
</body>
</html>
search_key.php:
<script>window.location="./no_key_is_here_forever.php"; </script>
key is : nctf{yougotit_script_now}
no_key_is_here_forever.php:
<?php
echo "这里真的没有KEY,土土哥哥说的,土土哥哥从来不坑人,PS土土是闰土,不是谭神";
?>
上面index.html获取当前页面完整的URL也是http://127.0.0.1:8008/window.location_test/search_key.php
,之后进行重定向,多了个./
,我们来看看是个什么情况:
哎?它这里怎么没有用no_key_is_here_forever.php
替代除根目录外的所有目录,而是只替换了当前URL的最后一个目录,最后重定向的URL为http://127.0.0.1:8008/window.location_test/no_key_is_here_forever.php
,原来这就是./
的作用!!
注:如果是window.location="no_key_is_here_forever.php"
,没有/
呢?经测试,没有/
和./
效果一样,都是只替换完整URL的最后一个目录。
4.总结
window.location="/no_key_is_here_forever.php"是用no_key_is_here_forever.php替换完整路径除根目录外的所有目录
window.location="./no_key_is_here_forever.php"和window.location="no_key_is_here_forever.php"是用no_key_is_here_forever.php替换完整路径的最后一个目录
注:
想要了解它的属性的可以看以下几个网址:
http://www.w3school.com.cn/js/js_window_location.asp
http://www.w3school.com.cn/jsref/dom_obj_location.asp