Home  >  Article  >  Backend Development  >  How Ajax implements website hijacking detection

How Ajax implements website hijacking detection

小云云
小云云Original
2017-12-25 10:37:421505browse

Ajax如何实现网站劫持的检测?https可以彻底解决劫持的问题。但是一般虚拟主机都不支持 https,难道http只能任流氓们恶意劫持么?下面通过本文给大家介绍Ajax 实现网站劫持的检测方法,需要的朋友可以参考下,希望能帮助到大家。

既然只有第一次访问时才会出现抽奖链接,通过JS在浏览器中检测,如果发现 被植入的 代码,则自动刷新网页,就可以解决被劫持的问题了。

现在要做的就是得到 被植入的代码。找了一圈,没有找到检查的工具。网站传输到客户的浏览器,需要三个步骤:【1】服务器 -> 【2】运行商 -> 【3】客户浏览器。

劫持出现在第【2】步,因为离开了服务器,已经不受控制了。但是第【3】部的浏览器可以通过JS来控制。通过 Ajax 上传 客户最终获取到的代码,就可以对进行分析了。

运行效果如下:


<?php
 //根据访问域名,创建不同的目录
 $log_path = $_SERVER[&#39;SERVER_NAME&#39;];
 if( !is_dir($log_path) ){
 mkdir($log_path);
 }
 //根据访客IP,分别记录
 $log_path .= &#39;/&#39; . $_SERVER[&#39;REMOTE_ADDR&#39;];
 if( !is_dir($log_path) ){
 mkdir($log_path);
 }
 //按天分组
 $log_path .= &#39;/&#39; . date(&#39;Y-m-d&#39;, time());
 if( !is_dir($log_path) ){
 mkdir($log_path);
 }
 //根据时间生成文件名
 $log_file = $log_path . &#39;/&#39; . date(&#39;His&#39;, time()) . &#39;_&#39; . rand() . &#39;.html&#39;;
 //保存日志
 $html = &#39;&#39;;
 $html .= &#39;URL:/&#39; . post(&#39;url&#39;) . &#39;<hr>&#39;;
 $html .= &#39;HEAD:<br><textarea cols="200" rows="40">&#39; . post(&#39;head&#39;) . &#39;</textarea><hr>&#39;;
 $html .= &#39;BODY:<br><textarea cols="200" rows="40">&#39; . post(&#39;body&#39;) . &#39;</textarea>&#39;;
 file_put_contents($log_file, $html);
 die(&#39;{"help":"http://www.miaoqiyuan.cn/p/browser-page-tracert/","log_file":"&#39; . $log_file . &#39;"}&#39;);
 //调用的函数
 function post($input){
 $post_str = isset($_POST[$input]) ? $_POST[$input] : &#39;&#39;;
 $post_str = str_replace(&#39;\\&#39;, &#39;&#39;, $post_str);
 $post_str = iconv_substr($post_str, 0, 50000); //防止恶意上传假日志
 return $post_str;
 }
?>

调用很简单,比如将 上边的php代码保存到了 /log/page_tracert.php,然后在整站页面中都加入 以下的代码,就可以获取到 所有的访问记录了。根据访问记录,得到 被植入的代码后,就可以进行检测脚本的开发了。


$(function(){
 $.post(&#39;/log/page_tracert.php&#39;,{
 url : location.href,
 head : $(&#39;head&#39;).html(),
 body : $(&#39;body&#39;).html(),
 },function(){});
});

注意:得到数据后,一定要尽快删除 测试代码。因为他会产生大量的日志,产生虚拟主机双倍的流量消耗。

相关推荐:

PHP安全-会话劫持

php session劫持和防范的方法_PHP

javascript 设置cookie 有效期 检测cookie

The above is the detailed content of How Ajax implements website hijacking detection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn