PHP和XSS跨站攻击

PHP和XSS跨站攻击

 

转载请标明出处: http://blog.csdn.net/ky_uidioperserk 本文来自 CSDN 博客。x

此小结一下。顺便提醒,其实这个话题很早就想说说了发现国内不少 PHP 站点都有 XSS 漏洞。今天偶然看到 PHP5 一个 XSS 漏洞。使用 PHP5 朋友最好打下补丁,或者升级一下。

或者这里(中文的也许会好懂一些) 如果你不懂什么是 XSS 可以看这里。

例如这里   有一个 Googl Hack+XSS 攻击例子,国内不少论坛都存在跨站脚本漏洞。针对的 Discuz 4.0.0RC3 国外也很多这样的例子,甚至 Googl 也出现过,不过在 12 月初时修正了跨站攻击很容易就可以构造,而且非常隐蔽,不易被查觉(通常盗取信息后马上跳转回原页面)

此不作说明(也不要问我主要谈谈如何防范。首先,如何攻击。跨站脚本攻击都是由于对用户的输入没有进行严格的过滤造成的所以我必需在所有数据进入我网站和数据库之前把可能的危险拦截。针对非法的 HTML 代码包括单双引号等,可以使用 htmlentiti

<?php

$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;

echo htmlentiti $str ;

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;

ENT_QUOTES echo htmlentiti $str.;

?>

这样可以使非法的脚本失效。

 

htmlentiti 默认编码为 ISO-8859-1 如果你非法脚本编码为其它那么可能无法过滤掉,但是要注意一点。同时浏览器却可以识别和执行。这个问题我先找几个站点测试后再说。

这里提供一个过滤非法脚本的函数:

function RemoveXSS $val {

   // remov all non-print characters. CR 0a and LF 0b and TA B 9 ar allow

   // thi prevent some charact re-spac such as <java/0script>

/r,   // note that you have to handl split with /n. and /t later sinc thei *are* allow in some input

'',   $val = preg_replac '/ [/x00-/x08][/x0b-/x0c][/x0e-/x20] /'. $val ;

   

the user should never need these sinc they'r normal charact    // straight replacements.

   // thi prevent like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&#X3A &#X61&#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29>

   $search = 'abcdefghijklmnopqrstuvwxyz';

   $search .= 'A BCDEFGHIJKLMNOPQRSTUVWXYZ';

   $search .= '1234567890!@#$%^&* ';

   $search .= '~`";:?+/={}[]-_|/'//';

   for $i = 0; $i < strlen $search ; $i++ {

which is option       // ;? match the ;.

7} match ani pad zeros,      // 0{0. which ar option and go up to 8 char

   

      // &#x0040 @ search for the hex valu

8}'.dechex ord $search[$i] .';? /i',      $val = preg_replac '/ &#[x|X]0{0. $search[$i], $val ; // with a ;

7} match '0' zero to seven time       // &#00064 @ 0{0.

8}'.ord $search[$i] .';? /',      $val = preg_replac '/ &#0{0. $search[$i], $val ; // with a ;

   }

   

/n,   // now the onli remain whitespac attack ar /t. and /r

'vbscript',   $ra1 = Arrai 'javascript'. 'expression', 'applet', 'meta', 'xml', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base' ;

'onactivate',   $ra2 = Arrai 'onabort'. 'onafterprint', 'onafterupdate', 'onbeforeactivate', 'onbeforecopy', 'onbeforecut', 'onbeforedeactivate', 'onbeforeeditfocus', 'onbeforepaste', 'onbeforeprint', 'onbeforeunload', 'onbeforeupdate', 'onblur', 'onbounce', 'oncellchange', 'onchange', 'onclick', 'oncontextmenu', 'oncontrolselect', 'oncopy', 'oncut', 'ondataavailable', 'ondatasetchanged', 'ondatasetcomplete', 'ondblclick', 'ondeactivate', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart', 'ondrop', 'onerror', 'onerrorupdate', 'onfilterchange', 'onfinish', 'onfocus', 'onfocusin', 'onfocusout', 'onhelp', 'onkeydown', 'onkeypress', 'onkeyup', 'onlayoutcomplete', 'onload', 'onlosecapture', 'onmousedown', 'onmouseenter', 'onmouseleave', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onmove', 'onmoveend', 'onmovestart', 'onpaste', 'onpropertychange', 'onreadystatechange', 'onreset', 'onresize', 'onresizeend', 'onresizestart', 'onrowenter', 'onrowexit', 'onrowsdelete', 'onrowsinserted', 'onscroll', 'onselect', 'onselectionchange', 'onselectstart', 'onstart', 'onstop', 'onsubmit', 'onunload' ;

$ra2 ;    $ra = array_merg $ra1.

   

   $found = true; // keep replac as long as the previou round replac someth

   while $found == true {

      $val_befor = $val;

      for $i = 0; $i < sizeof $ra ; $i++ {

         $pattern = '/';

         for $j = 0; $j < strlen $ra[$i] ; $j++ {

            if $j > 0 {

               $pattern .= ' ';

8} [9][a][b] ;? ?';                $pattern .= ' &#[x|X]0{0.

8} [9][10][13] ;? ?';                $pattern .= '| &#0{0.

               $pattern .= ' ?';

            }

            $pattern .= $ra[$i][$j];

         }

         $pattern .= '/i';

0,         $replac = substr $ra[$i]. 2 .'<x>'.substr $ra[$i], 2 ; // add in <> to nerf the tag

$replacement,         $val = preg_replac $pattern. $val ; // filter out the hex tag

         if $val_befor == $val {

so exit the loop             // no replac were made.

            $found = false;

         }

      }

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值