• 安全漏洞系列(一)---XSS漏洞解决方案(C# MVC)


    参考地址:https://www.cnblogs.com/sagecheng/p/9462239.html

    测试项目:MVCDemo

    一、XSS漏洞定义

      XSS攻击全称跨站脚本攻击,它允许恶意web用户将代码(如:html代码)植入到页面上,当访问到该页面时,嵌入到页面的html代码会自动执行,从而达到恶意攻击的目的。

    二、解决方案

      1.新建立一个XSSHelper帮助类

     1     public static class XSSHelper
     2     {
     3         /// <summary>
     4         /// XSS过滤
     5         /// </summary>
     6         /// <param name="html">html代码</param>
     7         /// <returns>过滤结果</returns>
     8         public static string XssFilter(string html)
     9         {
    10             string str = HtmlFilter(html);
    11             return str;
    12         }
    13 
    14         /// <summary>
    15         /// 过滤HTML标记
    16         /// </summary>
    17         /// <param name="Htmlstring"></param>
    18         /// <returns></returns>
    19         public static string HtmlFilter(string Htmlstring)
    20         {
    21             string result = System.Web.HttpUtility.HtmlEncode(Htmlstring);
    22             return result;
    23         }
    24     }

      2.再建立一个XSSFilterAttribute过滤类

     1     /// <summary>
     2     /// XSS 过滤器
     3     /// </summary>
     4     public class XSSFilterAttribute : ActionFilterAttribute
     5     {
     6         /// <summary>
     7         /// OnActionExecuting
     8         /// </summary>
     9         /// <param name="context"></param>
    10         public override void OnActionExecuting(ActionExecutingContext context)
    11         {
    12             //获取参数集合
    13             var ps = context.ActionDescriptor.GetParameters();
    14             if (ps.Count() == 0)
    15             {
    16                 return;
    17             }
    18             //遍历参数集合
    19             foreach (var p in ps)
    20             {
    21                 if (context.ActionParameters[p.ParameterName] != null)
    22                 {
    23                     //当参数是str
    24                     if (p.ParameterType.Equals(typeof(string)))
    25                     {
    26                         context.ActionParameters[p.ParameterName] = XSSHelper.XssFilter(context.ActionParameters[p.ParameterName].ToString());
    27                     }
    28                     else if (p.ParameterType.Equals(typeof(Int64)))
    29                     {
    30 
    31                     }
    32                     else if (p.ParameterType.Equals(typeof(Int32)))
    33                     {
    34 
    35                     }
    36 
    37                     else if (p.ParameterType.IsClass)//当参数是一个实体
    38                     {
    39                         PostModelFieldFilter(p.ParameterType, context.ActionParameters[p.ParameterName]);
    40                     }
    41                 }
    42 
    43             }
    44         }
    45         /// <summary>
    46         /// 遍历实体的字符串属性
    47         /// </summary>
    48         /// <param name="type">数据类型</param>
    49         /// <param name="obj">对象</param>
    50         /// <returns></returns>
    51         private object PostModelFieldFilter(Type type, object obj)
    52         {
    53             if (obj != null)
    54             {
    55                 foreach (var item in type.GetProperties())
    56                 {
    57                     if (item.GetValue(obj) != null)
    58                     {
    59                         //当参数是str
    60                         if (item.PropertyType.Equals(typeof(string)))
    61                         {
    62                             string value = item.GetValue(obj).ToString();
    63                             item.SetValue(obj, XSSHelper.XssFilter(value));
    64                         }
    65                         else if (item.PropertyType.Equals(typeof(Int64)))
    66                         {
    67 
    68                         }
    69                         else if (item.PropertyType.Equals(typeof(Int32)))
    70                         {
    71 
    72                         }
    73                         else if (item.PropertyType.Equals(typeof(Int16)))
    74                         {
    75 
    76                         }
    77                         else if (item.PropertyType.IsClass)//当参数是一个实体
    78                         {
    79                             //   item.SetValue(obj, PostModelFieldFilter(item.PropertyType, item.GetValue(obj)));
    80                         }
    81                     }
    82 
    83                 }
    84             }
    85             return obj;
    86         }
    87     }

      3.在控制器上加上该属性,就可对传递过来的参数数值进行过滤(记得要引入对应的命名空间)

      说明:为什么要加入[ValidateInput(false)],因为用户如果加入类似<script>的话,直接就报错了,界面不友好,所以就修改为后台对输入的内容进行过滤处理。如果压根不希望用户输入类似的字符,需要也在前端进行一下验证就可以了。

  • 相关阅读:
    【实践】mysql数据库表设计及存储过程设计
    # Java类链接模型
    java gc
    Spring Data Jpa
    Spring Security
    Amazon SQS 消息队列服务
    JMS概述
    jdk 7&8 new features
    java jri null
    java.lang.OutOfMemoryError: PermGen space
  • 原文地址:https://www.cnblogs.com/jas0203/p/11044629.html
Copyright © 2020-2023  润新知