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
-
/// <summary>
-
/// Referer(安全)攔截組件
-
/// </summary>
-
public class RefererAttribute : ActionFilterAttribute
-
{
-
private ExcuteMode _customMode;
-
/// <summary>默認構造</summary>
-
public RefererAttribute(ExcuteMode Mode)
-
{
-
_customMode = Mode;
-
}
-
-
/// <summary>
-
/// 安全認證
-
/// </summary>
-
/// <param name="filterContext"></param>
-
public override void OnActionExecuting(ActionExecutingContext filterContext)
-
{
-
//是否忽略
-
if (_customMode == ExcuteMode.Ignore)
-
{
-
return;
-
}
-
var request = filterContext.HttpContext.Request;
-
-
if (request.Headers.Get("Referer").IndexOf(Config.GetValue("WebUrl")) > -1
-
|| request.Headers.Get("Referer").IndexOf(Config.GetValue("NwWebUrl")) > -1
-
)
-
{
-
return;
-
}
-
else
-
{
-
throw new Exception("跨域防僞攻擊:" + request.Headers.Get("Referer"));
-
}
-
}
-
}
在Controller層增長特性,則全部Action在執行以前都會進行攔截。.net
參考:htm
IBM Security Appscan漏洞--跨站點請求僞造blog