ASP.NET MVC CSRF (XSRF) security

CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站的恶意利用。尽管听起来像跨站脚本(XSS),但它与XSS非常不同,XSS利用站点内的信任用户,而CSRF则通过伪装成受信任用户的请求来利用受信任的网站。与XSS攻击相比,CSRF攻击往往不大流行(因此对其进行防范的资源也相当稀少)和难以防范,所以被认为比XSS更具危险性。

----  摘自《百度百科》   

那我们如何防御呢?

一、在页面添加:@Html.AntiForgeryToken()

生成代码如下:

1
<input name="__RequestVerificationToken" type="hidden" value="-ixNsp4avtyr2m0rXsEshzmZcQAoitTNqqqrKn5UeGEJSTir7YgD7HrZ3hr6WnFQKcCnKhR4cFr6DvTqSSioX9YVc4ynLmFi19jCvvVaUQhE3E2j1sT0JwLeIYmeoujB0">

 

二、在我们的过滤器里添加过滤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class AntiForgeryAttribute : FilterAttribute, IAuthorizationFilter
{
    private readonly bool _ignore;
 
    /// <summary>
    /// Anti-forgery security attribute
    /// </summary>
    /// <param name="ignore">Pass false in order to ignore this security validation</param>
    public AdminAntiForgeryAttribute(bool ignore = false)
    {
        this._ignore = ignore;
    }
    public virtual void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
            throw new ArgumentNullException("filterContext");
 
        if (_ignore)
            return;
 
        //don't apply filter to child methods
        if (filterContext.IsChildAction)
            return;
 
        //only POST requests
        if (!String.Equals(filterContext.HttpContext.Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase))
            return;
 
        if (!DataSettingsHelper.DatabaseIsInstalled())
            return;
        var securitySettings = EngineContext.Current.Resolve<SecuritySettings>();
        if (!securitySettings.EnableXsrfProtectionForAdminArea)
            return;
         
        var validator = new ValidateAntiForgeryTokenAttribute();
        validator.OnAuthorization(filterContext);
    }
}

 三、然后在控制器上添加过滤:

1
2
[AntiForgery]
public class BaseTController : BaseController

 四、在我们的页面的js里发送post前添加:

1
2
3
4
5
6
7
8
9
10
11
12
addAntiForgeryToken(postData);
$.ajax({
        type: "POST",
        url: "@(Url.Action("actionx", "controllerx"))",
        data: postData,
        complete: function(data) {
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError);
        },
        traditional: true
    });

 五、调用的这个addAntiForgeryToken方法是什么呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
// CSRF (XSRF) security
function addAntiForgeryToken(data) {
    //if the object is undefined, create a new one.
    if (!data) {
        data = {};
    }
    //add token
    var tokenInput = $('input[name=__RequestVerificationToken]');
    if (tokenInput.length) {
        data.__RequestVerificationToken = tokenInput.val();
    }
    return data;
};

 这样我们就能防御跨站请求伪造了。

 

下面我们实现一个简单的xss攻击

首先我们的js是这样的:

$(function(){
  var msg='@ViewBag.Message';  
  $('#xss').html(msg)
})

html:

<div id="xss"></div>
@using (Html.BeginForm("Index", "contacts", FormMethod.Post))
{
    <input type="text" name="msg" value="" />
    <input type="submit" value="提交" />
}

 

控制器呢是这样的:

[HttpPost]
public ActionResult Index(string msg="")
{
    ViewBag.Message = msg;
    return View();
}

好了,就是这么一个简单的页面;

然后我们在输入框里填写:\x3cscript\x3ealert(\x27pwnd\x27)\x3c/script\x3e

然后提交,竟然能弹出提示框?是不是很奇怪!

razor明明默认是HTML编码,怎么这里会失败呢?

因为尽管msg进行了htm编码,但是仍然具有潜在的XSS脆弱性。在js里应该使用@Ajax.JavascriptStringEncode方法对msg的内容进行编码,这样就能阻止xss攻击了。

 

posted @   mantishell  阅读(269)  评论(0编辑  收藏  举报
编辑推荐:
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
阅读排行:
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· Ollama系列05:Ollama API 使用指南
· 为什么AI教师难以实现
· 如何让低于1B参数的小型语言模型实现 100% 的准确率
点击右上角即可分享
微信分享提示