asp.net mvc 安全測試漏洞 "跨站點請求僞造" 問題解決

IBM Security Appscan漏洞篩查-跨站請求僞造,該漏洞的產生,有多種狀況:html

1.WebApi的跨站請求僞造,須要對WebApi的請求頭部作限制(此文不作詳細介紹);跨域

2.MVC Action Post接口的跨站請求僞造,具體解決方案,請查看mvc 當中 [ValidateAntiForgeryToken] 的做用安全

3. MVC Action Get接口,例如: 跳轉頁面,數據查詢等接口,使用驗證HTTP Referer字段 防止跨站請求僞造攻擊。mvc

具體實現思路ide

定義RefererAttribute繼承自ActionFilterAttribute,在Action執行以前,進行攔截;spa

  1. /// <summary>
  2. /// Referer(安全)攔截組件
  3. /// </summary>
  4. public class RefererAttribute : ActionFilterAttribute
  5. {
  6.     private ExcuteMode _customMode;
  7.     /// <summary>默認構造</summary>
  8.     public RefererAttribute(ExcuteMode Mode)
  9.     {
  10.         _customMode = Mode;
  11.     }
  12.  
  13.     /// <summary>
  14.     /// 安全認證
  15.     /// </summary>
  16.     /// <param name="filterContext"></param>
  17.     public override void OnActionExecuting(ActionExecutingContext filterContext)
  18.     {
  19.         //是否忽略
  20.         if (_customMode == ExcuteMode.Ignore)
  21.         {
  22.             return;
  23.         }
  24.         var request = filterContext.HttpContext.Request;
  25.  
  26.         if (request.Headers.Get("Referer").IndexOf(Config.GetValue("WebUrl")) > -1
  27.             || request.Headers.Get("Referer").IndexOf(Config.GetValue("NwWebUrl")) > -1
  28.             )
  29.         {
  30.             return;
  31.         }
  32.         else
  33.         {
  34.             throw new Exception("跨域防僞攻擊:" + request.Headers.Get("Referer"));
  35.         }
  36.     }
  37. }

在Controller層增長特性,則全部Action在執行以前都會進行攔截。.net

參考:htm

IBM Security Appscan漏洞--跨站點請求僞造blog

相關文章
相關標籤/搜索