溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在.net中防御core和 xss攻擊

發(fā)布時間:2021-05-18 17:29:37 來源:億速云 閱讀:264 作者:Leah 欄目:開發(fā)技術

今天就跟大家聊聊有關怎么在.net中防御core和 xss攻擊,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

XSS攻擊全稱跨站腳本攻擊 ,是為不和層疊樣式表(Cascading Style Sheets, CSS)的縮寫混淆,故將跨站腳本攻擊縮寫為XSS,XSS是一種在web應用中的計算機安全漏洞,它允許惡意web用戶將代碼植入到提供給其它用戶使用的頁面中。

比如我們在表單提交的時候插入腳本代碼

怎么在.net中防御core和 xss攻擊

如果不進行處理,那么就是這種效果,我這里只是演示一個簡單的彈窗

怎么在.net中防御core和 xss攻擊

下面給大家分享一下我的解決方案。

需要用到這個庫: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();
    }

  }

看完上述內容,你們對怎么在.net中防御core和 xss攻擊有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI