linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘建议留言网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > php >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·PHP生成静态页面的一些经验
·PHP无限分类与树型论坛的实现方
·php5学习笔记
·入门级PHP程序员面试题
·PHP 和 MySQL初学入门
·PHP入门速成
·vim的代码折叠
·文件下载统计php编程代码
·如何建立自己的新闻发布系统
·织梦CMS中文转换拼音函数研究
·Windows下Apache+Tomcat+MySQL
·PHP初学者头疼问题总结
·Cookie及其使用详细介绍
·生成sessionid和随机密码的例子
·使用无限生命期Session的方法
最新文章
·PHP入门速成
·用php实现广告轮播
·Zend Optimizer 问题浅析
·功能强大的CGI语言----PHP3
·用Session对Web页面进行保护
·PHP--进行模块化设计
·如何将PHP的结果输出到非PHP页
·如何开发一个虚拟域名系统
·PHP4调用自己编写的COM组件
·简单的页面缓冲技术(三)
·简单的页面缓冲技术(二)
·简单的页面缓冲技术(一)
·用Socket发送电子邮件(二)
·用Socket发送电子邮件(一)
·PHP/MySQL 购物车
Google
 
利用PHP编程防范XSS跨站脚本攻击
[ 作者:  加入时间:2008-02-13 11:35:44  来自:Linux联盟收集整理 ]
国内不少论坛都存在跨站脚本漏洞,国外也很多这样的例子,甚至Google也出现过,不过在12月初时修正了。跨站攻击很容易就可以构造,而且非常隐蔽,不易被查觉(通常盗取信息后马上跳转回原页面)。 bABLinux联盟
   bABLinux联盟
    如何攻击,在此不作说明(也不要问我),主要谈谈如何防范。首先,跨站脚本攻击都是由于对用户的输入没有进行严格的过滤造成的,所以我们必须在所有数据进入我们的网站和数据库之前把可能的危险拦截。针对非法的HTML代码包括单双引号等,可以使用htmlentities() 。 bABLinux联盟
   bABLinux联盟
  <?PHP bABLinux联盟
  $str = "A 'quote' is <b>bold</b>"; bABLinux联盟
   bABLinux联盟
  // Outputs: A 'quote' is <b>bold</b> bABLinux联盟
  echo htmlentities($str); bABLinux联盟
   bABLinux联盟
  // Outputs: A 'quote' is <b>bold</b> bABLinux联盟
  echo htmlentities($str, ENT_QUOTES); bABLinux联盟
  ?> bABLinux联盟
   bABLinux联盟
    这样可以使非法的脚本失效。 bABLinux联盟
   bABLinux联盟
    但是要注意一点,htmlentities()默认编码为 ISO-8859-1,如果你的非法脚本编码为其它,那么可能无法过滤掉,同时浏览器却可以识别和执行。这个问题我先找几个站点测试后再说。 bABLinux联盟
   bABLinux联盟
    这里提供一个过滤非法脚本的函数: bABLinux联盟
   bABLinux联盟
  function RemoveXSS($val) { bABLinux联盟
   // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed bABLinux联盟
   // this prevents some character re-spacing such as <Java\0script> bABLinux联盟
   // note that you have to handle splits with \n, \r, and \t later since they *are* allowed in some inputs bABLinux联盟
   $val = preg_replace('/([\x00-\x08][\x0b-\x0c][\x0e-\x20])/', '', $val); bABLinux联盟
   bABLinux联盟
   // straight replacements, the user should never need these since they're normal characters bABLinux联盟
   // this prevents like <IMG SRC=&#X40&#X61&#X76&#X61&#X73&#X63&#X72&#X69&#X70&#X74&bABLinux联盟
#X3A&#X61& bABLinux联盟
      _#X6C&#X65&#X72&#X74&#X28&#X27&#X58&#X53&#X53&#X27&#X29> bABLinux联盟
   $search = 'abcdefghijklmnopqrstuvwxyz'; bABLinux联盟
   $search .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; bABLinux联盟
   $search .= '1234567890!@#$%^&*()'; bABLinux联盟
   $search .= '~`";:?+/={}[]-_|\'\\'; bABLinux联盟
   for ($i = 0; $i < strlen($search); $i++) { bABLinux联盟
    // ;? matches the ;, which is optional bABLinux联盟
    // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars bABLinux联盟
   bABLinux联盟
    // &#x0040 @ search for the hex values bABLinux联盟
    $val = preg_replace('/(&#[x|X]0{0,8}'.dechex(ord($search[$i])).';?)/i', $search[$i], $val); // with a ; bABLinux联盟
    // @ @ 0{0,7} matches '0' zero to seven times bABLinux联盟
    $val = preg_replace('/(&#0{0,8}'.ord($search[$i]).';?)/', $search[$i], $val); // with a ; bABLinux联盟
   } bABLinux联盟
   bABLinux联盟
   // now the only remaining whitespace attacks are \t, \n, and \r bABLinux联盟
   $ra1 = Array('JavaScript', 'VBscript', 'expression', 'Applet', 'meta', 'XML', 'blink', 'link', 'style', 'script', 'embed', 'object', 'iframe', 'frame', 'frameset', 'ilayer', 'layer', 'bgsound', 'title', 'base'); bABLinux联盟
  $ra2 = Array('onabort', 'onactivate', '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'); bABLinux联盟
   $ra = array_merge($ra1, $ra2); bABLinux联盟
   bABLinux联盟
   $found = true; // keep replacing as long as the previous round replaced something bABLinux联盟
   while ($found == true) { bABLinux联盟
    $val_before = $val; bABLinux联盟
    for ($i = 0; $i < sizeof($ra); $i++) { bABLinux联盟
     $pattern = '/'; bABLinux联盟
     for ($j = 0; $j < strlen($ra[$i]); $j++) { bABLinux联盟
      if ($j > 0) { bABLinux联盟
       $pattern .= '('; bABLinux联盟
       $pattern .= '(&#[x|X]0{0,8}([9][a][b]);?)?'; bABLinux联盟
       $pattern .= '|(&#0{0,8}([9][10][13]);?)?'; bABLinux联盟
       $pattern .= ')?'; bABLinux联盟
      } bABLinux联盟
     $pattern .= $ra[$i][$j]; bABLinux联盟
    } bABLinux联盟
    $pattern .= '/i'; bABLinux联盟
    $replacement = substr($ra[$i], 0, 2).'<x>'.substr($ra[$i], 2); // add in <> to nerf the tag bABLinux联盟
    $val = preg_replace($pattern, $replacement, $val); // filter out the hex tags bABLinux联盟
    if ($val_before == $val) { bABLinux联盟
     // no replacements were made, so exit the loop bABLinux联盟
     $found = false; bABLinux联盟
    } bABLinux联盟
   } bABLinux联盟
  } bABLinux联盟
  } bABLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·UTF8下的中文PHP编程  (2007-11-22 16:28:59)
 ·用PHP编程语言开发动态WAP页面  (2007-11-21 15:48:14)
 ·文件下载统计php编程代码  (2007-10-11 12:42:23)
 ·用PHP编程读取汉字点阵数据  (2007-01-22 01:51:42)
 ·PHP编程常用技巧  (2007-01-03 21:08:56)
 ·最新的黑客技术:详解XSS跨站脚本攻击 (2)  (2006-06-24 13:20:37)
 ·最新的黑客技术:详解XSS跨站脚本攻击 (1)  (2006-06-24 13:17:23)
 ·PHP编程技巧:看实例学正则表达式  (2006-06-09 11:45:19)