王新阳

wangxinyang

禁止网站被别人通过iframe引用嵌套套入

转自:https://aisoa.cn/post-2117.html

由于嵌入iframe的嵌入 使得网站变的更不安全, 如何能防止网页禁止被iframe嵌入呢?

首先看下知名网站使用的方法

<script type="text/javascript">
document.domain="qq.com";var _s_=new Date(),g_T={},siDomain="ctc.qzonestyle.gtimg.cn",g_iUin=499469859,g_iLoginUin=499469859;g_T.fwp=[_s_];document.namespaces&&document.namespaces.add&&(document.namespaces.add('qz', 'http://qzone.qq.com/'),document.namespaces.add('x', 'http://qzone.qq.com/'));var QZFL={};QZFL.event={};QZFL.event.getEvent=function(evt){var evt=window.event||evt,c,cnt;if(!evt&&window.Event){c=arguments.callee;cnt=0;while(c){if((evt=c.arguments[0])&&typeof(evt.srcElement)!="undefined"){break;}else if(cnt>9){break;}c=c.caller;++cnt;}}return evt;};QZFL.event.getTarget=function(evt){var e=QZFL.event.getEvent(evt);if(e){return e.srcElement||e.target;}else{return null;}};var QZFF_M_img_ribr=[];QZFL.media={reduceImgByRule:function(ew,eh,opts,cb){QZFF_M_img_ribr.push(QZFL.event.getTarget());},adjustImageSize:function(w,h,trueSrc,cb,errCallback){QZFF_M_img_ribr.push(QZFL.event.getTarget());},reduceImage:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());},getImageInfo:function(){QZFF_M_img_ribr.push(QZFL.event.getTarget());}};g_T.fwp[1] = new Date();</script>

这个是腾讯QQ空间使用的防嵌套方法

if(window.top !== window.self){ window.top.location = window.location;}

这个是淘宝使用的方法

这个要说下,其实今天要介绍,四种防iframe的方法

第一种要说的就是淘宝使用的办法

解决方案一:js方法

<script type="text/javascript">
if(self != top) { top.location = self.location; }
</script>
if (self == top) {
    var theBody = document.getElementsByTagName('body')[0];
    theBody.style.display = "block";
} else {
    top.location = self.location;
}

上面两种方法都是可以的

把上面的JS代码片段放到你页面的 head 中即可。

要特别说明下这种方法不是很靠谱,可以很轻松使这种方法失效。

只需要添加下面代码使JS代码失效,这种方法就没用了。

<script type="text/javascript" charset="utf-8">
document.write('<iframe seamless sandbox security="restricted" id="url_mainframe" frameborder="0" scrolling="yes" name="main" src="http://aisoa.cn" style="height:100%; visibility: inherit; width: 100%; z-index: 1;overflow: visible;"></iframe>');
</script>//把里面的http://aisoa.cn换成要嵌套的网址

解决方案二:Meta标签方法

在需要禁用iframe嵌套的网页head中添加下面代码

<meta http-equiv="X-FRAME-OPTIONS" content="DENY">

以上两种为前端处理方法,就我个人来说不推荐使用,不过这个也是因人而异的,没有绝对的好与差。

解决方案三:PHP方法

<?php header('X-Frame-Options:Deny'); ?>

在需要禁用iframe嵌套的PHP网页中添加上面代码

上面这种是后端程序处理方法。

解决方案四:Apache主机方法

配置 Apache 在所有页面上发送 X-Frame-Options 响应头,需要把下面这行添加到 'site' 的配置中:

Header always append X-Frame-Options SAMEORIGIN

解决方案五:Nginx主机方法

配置 nginx 发送 X-Frame-Options 响应头,把下面这行添加到 'http', 'server' 或者 'location' 的配置中:

add_header X-Frame-Options "SAMEORIGIN";

配置 IIS 发送 X-Frame-Options 响应头

添加下面的配置到 Web.config 文件中:

<system.webServer>
  ...
  <httpProtocol>
    <customHeaders>
      <add name="X-Frame-Options" value="SAMEORIGIN" />
    </customHeaders>
  </httpProtocol>
  ...
</system.webServer>

解决方案六:.htaccess方法

在网站根目录下的.htaccess文件中中加一句

Header append X-FRAME-OPTIONS "SAMEORIGIN"

以上几种解决方案为服务器端解决方案。

CodeIgniter把找不到的页面301跳转到指定页

问题:网站改版后页面链接地址发生变化,百度收录的原内容打开后出现404。

解决办法:

1、如果原网站也是 /product/view/id/12.html 这样的访问方式,只需要在自定义路由中设置下即可。如 $route['product/view/id/(:any)']='new_product/new_list';  把原产品详情页路由到新产品列表页;可以全部路由到一个新控制器,然后再跳转。

2、如果原网站是 product.asp?id=12 这种而新网站又是linux不支持asp时,可以使用 .htaccess 直接进行301跳转。

RewriteRule ^(news)\.asp$ http://domain/$1/gongsixinwen.html [R=301,L]
RewriteRule ^(products)\.asp$ http://domain/$1/jixie.html [R=301,L]

3、跳转到一个单独的控制器再进行精确跳转。

RewriteRule ^(news|art|files)\/ https://domain/baidu/index/$1 [R=301,L]


因网站漏洞被上传木马的解决方法

网站被上传木马,导致页面被植入恶意链接,如果找不到漏洞,可以用 .htaccess 或 web.config 屏蔽木马程序的运行,确保网站的"相对"安全。

.htaccess

RewriteEngine on
RewriteRule (uploadfile.+\.(php|asp|aspx|jsp))$ /rec.php?uri=$1 [R,NC,L]

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="规则 1" stopProcessing="true">
                    <match url="(uploadfile.+\.(php|asp|aspx|jsp))$" />
                    <action type="Redirect" url="/rec.php?uri={R:1}" appendQueryString="false" redirectType="Found" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

上面规则是阻止上传文件目录 uploadfile 中 php/asp/aspx/jsp 等类型文件的运行,并跳转到 /rec.php 进行记录(也可以重定向到百度……)。虽然没有解决漏洞,至少确保了木马文件不能运行。

CodeIgniter框架中网站英文版以 /en/index.php 方式访问的设置

网址: http://www.baidu.com/en/about.html
实际请求地址: /en/index.php/about

实现步骤:

一、新建 /en 文件夹,把原 index.php 复制进来, 修改以下内容:
 1、 $system_path = '../system';
 2、 $application_folder = '../application/en_web';
 3、define('FCPATH', realpath(dirname(__FILE__).'/../').DIRECTORY_SEPARATOR);

二、在 /application 目录下新建英文版app的文件夹 en_web,把原中文版的app内容复制进来,修改 config.php
 1、$config['index_page'] = 'en/';

三、修改 .htaccess 文件,在原内容前面添加:
    RewriteRule ^en\/(.*)$ en/index.php/$1 [L]

OK!
另外,可以在 /index.php 中使用 $_SERVER['HTTP_ACCEPT_LANGUAGE'] 判断客户语言并自动转入相应版本。

附我的 .htaccess

RewriteEngine On

RewriteRule ^en\/m\.php(.*)$ en/m.php/$1 [L]
RewriteRule ^en\/manage\.php(.*)$ en/manage.php/$1 [L]
RewriteRule ^en\/(.*)$ en/index.php/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^.+\.(ico|js|css|jpg|jpe|jpeg|png|bmp|gif|mp4|flv|swf|txt|pdf|zip|rar|htm|doc|docx|xls|xlsx)$
RewriteRule ^(.*)$ index.php/$1 [L]

使用.htaccess/web.config文件实现图片防盗链

.htaccess

<Files ~ "^.(htaccess|htpasswd)$">
deny from all
</Files>
order deny,allow
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://.+\.baidu\.com/.+$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://(.+\.)*我的域名\.com/.*$ [NC]
RewriteRule .*\.(gif|jpg|jpeg|bmp|png)$ http://imgcache.qq.com/qzone/client/photo/swf/no.gif [R,NC,L]

web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="图片防盗链" stopProcessing="true">
                    <match url=".*\.(gif|jpg|jpeg|bmp|png)$" ignoreCase="true" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{HTTP_REFERER}" pattern="^$" ignoreCase="false" negate="true" />
                        <add input="{HTTP_REFERER}" pattern="^https://.+\.baidu\.com/.+$" negate="true" />
                        <add input="{HTTP_REFERER}" pattern="^https?://(.+\.)?我的域名\.com/.*$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://imgcache.qq.com/qzone/client/photo/swf/no.gif" redirectType="Found" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

我的.htaccess

RewriteEngine On
#以old开头的请求301跳转到www.XXX.com首页
RewriteCond %{request_uri} ^/old [NC]
RewriteRule ^(.*)$ http://www.XXX.com/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(robots\.txt|favicon\.ico)$
RewriteCond $1 !^.+\.(ico|js|css|jpg|jpe|jpeg|png|bmp|gif|mp4|flv|swf|txt|pdf|zip|rar|htm|doc|docx|xls|xlsx)$
#部分IIS中!-f无效,需要下面一行以防止手机站被index.php解析
RewriteCond $1 !^m\.php
RewriteRule ^(.*)$ index.php/$1 [L]

允许跨域

Header add Access-Control-Allow-Origin "http://aaa.example"

中文-未验证

RewriteRule ([\x80-\xffa-zA-Z]{1,})-([0-9]{1,}).html$ test.php?action=$1&id=$2

根据域名跳转,下面把abc.com的所有访问临时跳转到def.com(含querystring)

Rewritecond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
Rewriterule ^(.*)$ http://def.com/$1 [R=302,L]

RewriteCond %{request_uri} ^/old [NC]
RewriteRule ^(.*)$ http://www.XXX.com/ [L,R=301]
以上会把 http://abc.com/old?kw=text 跳转到 http://www.xxx.com/?kw=text
如果要舍弃QUERY_STRING,只需在跳转网址后加问号(正则结尾)即可,如下:
RewriteCond %{request_uri} ^/old [NC]
RewriteRule ^(.*)$ http://www.XXX.com/? [L,R=301]

下面可以用于不支持asp的服务器,把百度收录的原asp网页跳转到指定页面。

RewriteRule ^(.*)\.asp http://www.abc.com/baidu/index/$1 [R=301,L]

nginx伪静态

if (!-e $request_filename) {
	rewrite ^/manage\.php /manage.php last;
	rewrite ^.*$ /index.php last;
}

不是ico/jpg/png/gif中的一种时才进行伪静态重写

location ~* .*(?<!\.(ico|jpg|png|gif))$ {
	if (!-e $request_filename) {
		rewrite ^/manage\.php /manage.php last;
		rewrite ^.*$ /index.php last;
	}
}

CodeIgniter使用rewrite时出现404的原因

.htaccess中有如下一条,本意是要把/m重写为/m.php,结果总是 404错误

RewriteRule ^m/(.*)$ m.php/$1 [L]

原因:
config/config.php

$config['uri_protocol'] = 'REQUEST_URI';

要改为

$config['uri_protocol'[ = 'PATH_INFO';

否则CI无法根据重写后的uri获取请求的class和method,从而导致出错。
当访问地址为 http://abc.com/m   没有以 / 结尾时仍不能正确重写到 m.php,需要在.htaccess中增加如下一行(加在 RewriteRule ^m/(.*)$ m.php/$1 [L] 的前面):

RewriteRule ^m$ /m/ [R=301,NC,L]

参考:http://blog.csdn.net/newjueqi/article/details/12014673

如果服务器不支持$_SERVER['PATH_INFO'],可以在服务器上输入$_SERVER数组,查看是否支持$_SERVER['ORIG_PATH_INFO'],支持的话以ORIG_PATH_INFO代替PATH_INFO,当前要观察两个值是否相同,如果不相同,需要同时修改 system/core/URI.php 中关于$uri获取的部分。我本地电脑中ORIG_PATH_INFO和PATH_INFO的值不一样。阿里云虚拟主机不支持PATH_INFO,但ORIG_PATH_INFO的值却与我本地的PATH_INFO一样。

IIS10安装url重写工具2.0

http://www.microsoft.com/web/downloads/

https://www.microsoft.com/web/downloads/platform.aspx

下载并安装"web 平台安装程序",目前最新版本为5.0,双击,下一步到底即可。

打开Internet Information Services(IIS)管理器,在管理中已经多了一个"Web 平台安装程序",双击打开。

搜索"url",搜索结果第一个"URL 重写工具2.0",点击该记录后面的添加,再点击下方的安装按钮,还是一步到底,完成之后,重启下iis管理器。

iis管理器下发现多了一个"url重写"即安装成功了。接下来就是如何使用.htaccess文件了。找一个需要伪静态的项目,例如888,然后再双击"url重写"。

在IIS管理器左侧》网站,打到要支持url重写的站点,点击url重写,点击右侧导入规则,导入.htaccess文件,应用。导入.htaccess本质是在网站根目录下生成web.config文件,如果会写web.config,可不需要导入。

注:URL重写工具安装时出现需要IIS7.0以上版本,安装失败时,打开注册表编辑器,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp位置把MajorVersion的值改为9之后,就可以安装了,安装完成之后,再把MajorVersion的值改回10,重启一下iis。

又注:如果安装 URL重写工具2.0 还是失败,可以直接下载 rewrite_amd64.msi 安装。可百度“Download MicroSoft URL Rewrite Module 2.0",https://www.microsoft.com/en-us/download/details.aspx?id=47337

参考:

http://jingyan.baidu.com/article/b0b63dbff07fec4a48307083.html

http://zhidao.baidu.com/question/328627522371876565.html

2024-04-27 星期六 农历三月十九