基于代码修改的防御

和SQL注入防御一样,XSS攻击也是利用了Web页面的编写疏忽,所以还有一种方法就是从Web应用开发的角度来避免:

步骤1、对所有用户提交内容进行可靠的输入验证,包括对URL、查询关键字、HTTP头、POST数据等,仅接受指定长度范围内、采用适当格式、采用所预期的字符的内容提交,对其他的一律过滤。

步骤2、实现Session标记(session tokens)、CAPTCHA系统或者HTTP引用头检查,以防功能被第三方网站所执行。

步骤3、确认接收的的内容被妥善的规范化,仅包含最小的、安全的Tag(没有javascript),去掉任何对远程内容的引用(尤其是样式表和javascript),使用HTTP only的cookie。

当然,如上操作将会降低Web业务系统的可用性,用户仅能输入少量的制定字符,人与系统间的交互被降到极致,仅适用于信息发布型站点。并且考虑到很少有Web编码人员受过正规的安全培训,很难做到完全避免页面中的XSS漏洞(注 )。

附上防御代码(不是我写的): The goal of this function is to be a generic function that can be usedto parse almost any input and render it XSS safe. For more informationon  actual XSS attacks, check out http://ha.ckers.org/xss.html .  Another excellent site is the XSS Database which details each attack and how it works.

  1. <? php
  2. function RemoveXSS ( $val ) {
  3. // remove all non-printable characters. CR(0a) and LF(0b) and TAB(9) are allowed
  4. // this prevents some character re-spacing such as <java\0script>
  5. // note that you have to handle splits with n, r, and t later since they *are* allowed in some inputs
  6. $val = preg_replace ( ‘/([x00-x08,x0b-x0c,x0e-x19])/’ , ” , $val );
  7. // straight replacements, the user should never need these since they’re normal characters
  8. // this prevents like <IMG SRC=@avascript:alert(‘XSS’)>
  9. $search = ‘abcdefghijklmnopqrstuvwxyz’ ;
  10. $search .= ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’ ;
  11. $search .= ‘1234567890!@#$%^&amp;*()’ ;
  12. $search .= ‘~`&quot;;:?+/={}[]-_|’ \ ‘;
  13. for ($i = 0; $i < strlen($search); $i++) {
  14. // ;? matches the ;, which is optional
  15. // 0{0,7} matches any padded zeros, which are optional and go up to 8 chars
  16. // @ @ search for the hex values
  17. $val = preg_replace(‘/(& amp ; #[xX]0{0,8}’.dechex(ord($search[$i])).’;?)/i’, $search[$i], $val); // with a ;
  18. // @ @ 0{0,7} matches ’0′ zero to seven times
  19. $val = preg_replace ( ‘/(&amp;#0{0,8}’ . ord ( $search [ $i ]). ‘;?)/’ , $search [ $i ], $val ); // with a ;
  20. }
  21. // now the only remaining whitespace attacks are t, n, and r
  22. $ra1 = Array( ‘javascript’ , ‘vbscript’ , ‘expression’ , ‘applet’ , ‘meta’ , ‘xml’ , ‘blink’ , ‘link’ , ’style’ , ’script’ , ‘embed’ , ‘object’ , ‘iframe’ , ‘frame’ , ‘frameset’ , ‘ilayer’ , ‘layer’ , ‘bgsound’ , ‘title’ , ‘base’ );
  23. $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’ );
  24. $ra = array_merge ( $ra1 , $ra2 );
  25. $found = true ; // keep replacing as long as the previous round replaced something
  26. while ( $found == true ) {
  27. $val_before = $val ;
  28. for ($i = 0 ; $i < sizeof ( $ra ); $i ++) {
  29. $pattern = ‘/’ ;
  30. for ($j = 0 ; $j < strlen ( $ra [ $i ]); $j ++) {
  31. if ($j > 0 ) {
  32. $pattern .= ‘(‘ ;
  33. $pattern .= ‘(&amp;#[xX]0{0,8}([9ab]);)’ ;
  34. $pattern .= ‘|’ ;
  35. $pattern .= ‘|(&amp;#0{0,8}([9|10|13]);)’ ;
  36. $pattern .= ‘)*’ ;
  37. }
  38. $pattern .= $ra [ $i ][ $j ];
  39. }
  40. $pattern .= ‘/i’ ;
  41. $replacement = substr ( $ra [ $i ], 0 , 2 ). ‘<x>’ . substr ( $ra [ $i ], 2 ); // add in <> to nerf the tag
  42. $val = preg_replace ( $pattern , $replacement , $val ); // filter out the hex tags
  43. if ( $val_before == $val ) {
  44. // no replacements were made, so exit the loop
  45. $found = false ;
  46. }
  47. }
  48. }
  49. return $val ;
  50. }

XSS的防御的更多相关文章

  1. XSS攻击防御篇

    前言   上篇文章中提到了 XSS 攻击,而且,也从几个方面介绍了 XSS 攻击带来的严重影响.那么,这篇文章中,主要是针对 XSS 攻击做一个基本的防御,看看可以通过几种方式来修复这个特别常见的安全 ...

  2. Web安全系列(四):XSS 的防御

    简介 XSS 的防御很复杂,并不是一套防御机制就能就解决的问题,它需要具体业务具体实现. 目前来说,流行的浏览器内都内置了一些 XSS 过滤器,但是这只能防御一部分常见的 XSS,而对于网站来说,也应 ...

  3. 风炫安全WEB安全学习第二十七节课 XSS的防御措施

    风炫安全WEB安全学习第二十七节课 XSS的防御措施 XSS防御措施 总的原则 控制好输入/输出 过滤:根据业务需求进行过滤,对email,手机号码这样的输入框进行验证. 转义:所有输出到前端的数据都 ...

  4. XSS之防御与绕过

    很久之前的随笔讲过XSS的编码绕过的一些内容 本次侧重整理一下常见的防御思路,顺便补充一些针对性的绕过思路以及关于XSS个人想到的一些有趣的事情 开篇之前,先看一下XSS介绍(包括mXSS.uXSS. ...

  5. 安全测试 - XSS如何防御

    XSS主要是通过劫持用户COOKIE,执行JS脚本进行攻击 如何发现: 可以使用<script>alert(/yourname/)</script> script最具有代表性也 ...

  6. MVC WEB安全——XSS攻击防御

    XSS(跨站脚本攻击) 描述: 原理:攻击者往Web页面里插入恶意代码,当用户浏览该页之时,嵌入其中Web里面的代码会被执行,从而达到攻击用户的特殊目的. 类别: 1)被动注入(Passive Inj ...

  7. .Net Core 项目中添加统一的XSS攻击防御过滤器

    一.前言 最近公司内部在对系统的安全进行培训,刚好目前手里的一个.net core 项目中需要增加预防xss的攻击,本文将大概介绍下何为XSS攻击以及在项目中如何统一的预防XSS攻击. 二.XSS简介 ...

  8. .net core xss攻击防御

    XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞,它允许恶意 ...

  9. 前端XSS攻击和防御

    xss跨站脚本攻击(Cross Site Scripting),是一种经常出现在web应用中的计算机安全漏洞,指攻击者在网页中嵌入客户端脚本(例如JavaScript), 当用户浏览此网页时,脚本就会 ...

随机推荐

  1. Hadoop分布式系统的安装部署

    1.关于虚拟机的复制 新建一台虚拟机,系统为CentOS7,再克隆两台,组成一个三台机器的小集群.正常情况下一般需要五台机器(一个Name节点,一个SecondName节点,三个Data节点.) 此外 ...

  2. Oracle基础——学习笔记

    一[用户]sys\system\sysman\scott 1.查看数据库所有用户(dba_users数据字典): select username from dba_users; 2.查看当前用户: s ...

  3. JMeter常见问题集合

    前言 本文内容仅仅是针对Jmeter的部分功能名词的介绍和解释,以及初学者不易理解的问题的整理.部分内容来自别人做的整理,为了更好地整理自己的思路,所以可耻的整理一下发到博客上. 标题[1-6]和[参 ...

  4. nginx配置

    先在 cd /usr/local/nginx/conf 目录下找到 nginx.conf 文件 user www www; worker_processes 8; error_log /home/ww ...

  5. [LeetCode] Graph Valid Tree 图验证树

    Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), ...

  6. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  7. 网络切片在5G中的应用

    SDNLAB君 • 16-11-25 •1509 人围观 5G和网络切片 当5G被广泛提及的时候,网络切片是其中讨论最多的技术.像KT.SK Telecom.China Mobile.DT.KDDI. ...

  8. CSS优先级

    一.CSS代码出现的几个位置 多重样式(Multiple Styles):如果外部样式.内部样式和内联样式同时应用于同一个元素,就是使多重样式的情况. 一般情况下,优先级如下:(外部样式)Extern ...

  9. SQL语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...

  10. thinkcmf导航制作

    <?php $tree = sp_get_menu_tree('main'); ?> <foreach name="tree" item="vo&quo ...