Dragon-v

PHP TP5 XSS攻击

1.什么是XSS攻击
  跨站脚本攻击(Cross Site Scripting),攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到恶意攻击用户的目的。
2.转化思想防范xss攻击
  修改application/config.php
  注:在框架配置文件中,配置的函数名称,如果写错,页面不会报错,只是所有接收的数据都是null.
    'default_filter' => 'htmlspecialchars',
3.过滤思想防范xss攻击
  (1)使用composer执行命令,安装 ezyang/htmlpurifier 扩展类库;
      项目目录下> composer require ezyang/htmlpurifier
  (2)在application/common.php中定义remove_xss函数
    if (!function_exists('remove_xss')) {
     //使用htmlpurifier防范xss攻击
     function remove_xss($string){
     //相对index.php入口文件,引入HTMLPurifier.auto.php核心文件
     //require_once './plugins/htmlpurifier/HTMLPurifier.auto.php';
     // 生成配置对象
     $cfg = HTMLPurifier_Config::createDefault();
     // 以下就是配置:
     $cfg -> set('Core.Encoding', 'UTF-8');
     // 设置允许使用的HTML标签
     $cfg -> set('HTML.Allowed','div,b,strong,i,em,a[href|title],ul,ol,li,br,p[style],span[style],img[width|height|alt|src]');
     // 设置允许出现的CSS样式属性
     $cfg -> set('CSS.AllowedProperties', 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
     // 设置a标签上是否允许使用target="_blank"
     $cfg -> set('HTML.TargetBlank', TRUE);
     // 使用配置生成过滤用的对象
     $obj = new HTMLPurifier($cfg);
     // 过滤字符串
     return $obj -> purify($string);
     }
    }
    说明:htmlpurifier插件,会过滤掉script标签以及标签包含的js代码。
  (3)设置全局过滤方法为封装的remove_xss函数: 
     修改application/config.php
      'default_filter' => 'remove_xss',
4.转化与过滤结合防范xss攻击
  (1)普通输入内容,使用转化的思想进行处理。
     设置全局过滤方法为封装的htmlspecialchars函数:
     修改application/config.php
        'default_filter' => 'htmlspecialchars',
  (2)富文本编辑器内容,使用过滤的思想进行处理。
     比如商品描述字段,处理如下:
       //商品添加或修改功能中
       $params = input();
      //单独处理商品描述字段
      goods_introduce$params['goods_desc'] = input('goods_desc', '', 'remove_xss');

 
 

posted on 2021-08-19 10:17  Dragon-v  阅读(101)  评论(0编辑  收藏  举报