当前位置: 移动技术网 > IT编程>开发语言>.net > .net core xss攻击防御的方法

.net core xss攻击防御的方法

2019年05月28日  | 移动技术网IT编程  | 我要评论

杂交仪,三河八中贴吧,王子变青蛙演员表

xss攻击全称跨站脚本攻击 ,是为不和层叠样式表(cascading style sheets, css)的缩写混淆,故将跨站脚本攻击缩写为xss,xss是一种在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。

比如我们在表单提交的时候插入脚本代码

如果不进行处理,那么就是这种效果,我这里只是演示一个简单的弹窗

下面给大家分享一下我的解决方案。

需要用到这个库:htmlsanitizer

https://github.com/mganss/htmlsanitizer

新建一个过滤类。

 public class xss
  {
    private htmlsanitizer sanitizer;
    public xss()
    {
      sanitizer = new htmlsanitizer();
      //sanitizer.allowedtags.add("div");//标签白名单
      sanitizer.allowedattributes.add("class");//标签属性白名单,默认没有class标签属性      
      //sanitizer.allowedcssproperties.add("font-family");//css属性白名单
    }

    /// <summary>
    /// xss过滤
    /// </summary>
    /// <param name="html">html代码</param>
    /// <returns>过滤结果</returns>
    public string filter(string html)
    {
      string str = sanitizer.sanitize(html);
      return str;
    }
  }

新建一个过滤器

 public class fieldfilterattribute : attribute,iactionfilter
  {
    private xss xss;
    public fieldfilterattribute()
    {
      xss = new xss();
    }

    //在action方法之回之后调用
    public void onactionexecuted(actionexecutedcontext context)
    {

    }

    //在调用action方法之前调用
    public void onactionexecuting(actionexecutingcontext context)
    {
      //获取action参数集合
      var ps = context.actiondescriptor.parameters;
      //遍历参数集合
      foreach (var p in ps)
      {
        if (context.actionarguments[p.name] != null)
        {
          //当参数等于字符串
          if (p.parametertype.equals(typeof(string)))
          {
            context.actionarguments[p.name] = xss.filter(context.actionarguments[p.name].tostring());
          }
          else if (p.parametertype.isclass)//当参数等于类
          {
            modelfieldfilter(p.name, p.parametertype, context.actionarguments[p.name]);
          }
        }          

      }
    }

    /// <summary>
    /// 遍历修改类的字符串属性
    /// </summary>
    /// <param name="key">类名</param>
    /// <param name="t">数据类型</param>
    /// <param name="obj">对象</param>
    /// <returns></returns>
    private object modelfieldfilter(string key, type t, object obj)
    {
      //获取类的属性集合
      var ats = t.getcustomattributes(typeof(fieldfilterattribute), false);


      if (obj != null)
      {
        //获取类的属性集合
        var pps = t.getproperties();

        foreach (var pp in pps)
        {
          if(pp.getvalue(obj) != null)
          {
            //当属性等于字符串
            if (pp.propertytype.equals(typeof(string)))
            {
              string value = pp.getvalue(obj).tostring();
              pp.setvalue(obj, xss.filter(value));
            }
            else if (pp.propertytype.isclass)//当属性等于类进行递归
            {
              pp.setvalue(obj, modelfieldfilter(pp.name, pp.propertytype, pp.getvalue(obj)));
            }
          }
          
        }
      }

      return obj;
    }
  }
  //属性过滤器
  [fieldfilter]
  public class notebookcontroller : managecontroller
  {
    //笔记操作接口
    private inotebookappservice _notebookapp;
    public notebookcontroller(inotebookappservice notebookapp)
    {
      this._notebookapp = notebookapp;
    }
    public iactionresult tab()
    {
      return view();
    }

  }

然后在需要过滤的控制器加上过滤控制器特性就可以了。这样所有string类型的参数就都会进行过滤了。如果不需要对整个控制器进行过滤,只需要在相应的action加上特性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网