简体   繁体   English

如何保护或保护 ASP.NET MVC 免受 XSS 攻击?

[英]How to protect or secured ASP.NET MVC from XSS?

I'm creating a web application using the latest version of ASP.NET MVC 5.2.3.我正在使用最新版本的 ASP.NET MVC 5.2.3 创建一个 Web 应用程序。 I just concern in XSS attack.我只关心 XSS 攻击。 I figure out in ASP.NET Core is perfectly working protecting from this attack the XSS and this framework totally amazing but it lacked third party I need to my project.我发现在 ASP.NET Core 中可以完美地防止这种攻击,XSS 和这个框架非常棒,但它缺乏我的项目所需的第三方。 Here's my concern.这是我的担忧。 I already enabled the custom error too but I disabled it currently for testing.我也已经启用了自定义错误,但我目前禁用了它以进行测试。

But I want to make sure this will catch also.但我想确保这也能抓住。

  1. Input Validation is passed.输入验证通过。 To avoid this exception or error.为了避免这种异常或错误。

A potentially dangerous Request.Form value was detected from the client (Name="").从客户端检测到潜在危险的 Request.Form 值 (Name="")。

using, the [AllowHtml] attribute this is fine or using the AntiXss library.使用 [AllowHtml] 属性这很好,或者使用 AntiXss 库。

  1. But, from the URL.但是,从 URL。 Example URLs,示例网址,

    http://localhost:54642/Employees/ http://localhost:54642/Employees/

     http://localhost:54642/Employees/?a=<script>

link or url链接或网址

this error should like,这个错误应该是,

A potentially dangerous Request.Path value was detected from the client (<).从客户端 (<) 检测到潜在危险的 Request.Path 值。

So my solution is enabling this from Web.config then it works!所以我的解决方案是从 Web.config 启用它,然后它就可以工作了!

But Troy Hunt said from his tutorial this is not a good or better practice for this error.但是 Troy Hunt 在他的教程中说,对于这个错误,这不是一个好的或更好的做法。 So I decided to look the best solution from this XSS attack.所以我决定从这次 XSS 攻击中寻找最佳解决方案。

In my form I normally add this anti-forgery token在我的表单中,我通常添加这个防伪令牌

 @Html.AntiForgeryToken()

then on my controller I made sure validate the token然后在我的控制器上我确保验证令牌

[ValidateAntiForgeryToken] 

also when passing the variable or data, I always declare correct variable.同样在传递变量或数据时,我总是声明正确的变量。 Anyways if its member area page you can always restrict access to correct member roles example like无论如何,如果它的会员区页面你总是可以限制对正确的成员角色的访问,例如

  [Authorize] // for registered user

  or more filtered

  [Authorize(Roles = "SUBSCRIBER.VIEW")]

Below is only applicable for .net 4.5 and above以下仅适用于 .net 4.5 及以上

  // web.config 
  <system.Web> 
     <httpRuntime targetFramework="4.5" />
  </system.Web>

 // enabling anti-xss 
   <httpRuntime targetFramework="4.5" encoderType="System.Web.Security.AntiXss.AntiXssEncoder,System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Request validation Lazy validation was introduced in ASP.NET 4.5, I just did some testing on it and it seems that lazy validation is the enabled regardless of how you set the "requestValidationMode", after you've installed the 4.5 framework.请求验证 延迟验证是在 ASP.NET 4.5 中引入的,我只是对其进行了一些测试,并且在安装 4.5 框架后,无论您如何设置“requestValidationMode”,似乎都启用了延迟验证。

Check out OWASP site.查看 OWASP 网站。 Here is the common ones I add in system.web in web.config file of a webapi app.这是我在 webapi 应用程序的 web.config 文件中的 system.web 中添加的常见内容。

<httpProtocol>
  <customHeaders>
    <remove name="Server" />
    <remove name="X-Powered-By" />
    <remove name="X-Frame-Options" />
    <remove name="X-XSS-Protection" />
    <remove name="X-Content-Type-Options" />
    <remove name="Cache-Control" />
    <remove name="Pragma" />
    <remove name="Expires" />
    <remove name="Content-Security-Policy"/>
    <clear />
    <add name="X-Frame-Options" value="DENY" />
    <add name="X-XSS-Protection" value="1; mode=block"/>
    <add name="X-Content-Type-Options" value="nosniff" />
    <add name="Cache-Control" value="no-cache, no-store" />
    <add name="Pragma" value="no-cache" />
    <add name="Expires" value="Sun, 1 Jan 2017 00:00:00 UTC" />
    <add name="Content-Security-Policy" value="default-src 'self' 'unsafe-inline' data; img-src https://*;"/>
  </customHeaders>
</httpProtocol>
Steps:
1. Disables input validation
2. Encodes all the input that is coming from the user
3. Finally we selectively replace, the encoded html with the HTML elements that we want to allow.

[HttpPost]
// Input validation is disabled, so the users can submit HTML
[ValidateInput(false)]
public ActionResult Create(Comment comment)
{
    StringBuilder sbComments = new StringBuilder();
    
    // Encode the text that is coming from comments textbox
    sbComments.Append(HttpUtility.HtmlEncode(comment.Comments));
    
    // Only decode bold and underline tags
    sbComments.Replace("&lt;b&gt;", "<b>");
    sbComments.Replace("&lt;/b&gt;", "</b>");
    sbComments.Replace("&lt;u&gt;", "<u>");
    sbComments.Replace("&lt;/u&gt;", "</u>");
    comment.Comments = sbComments.ToString();

    // HTML encode the text that is coming from name textbox
    string strEncodedName = HttpUtility.HtmlEncode(comment.Name);
    comment.Name = strEncodedName;

    if (ModelState.IsValid)
    {
        db.Comments.AddObject(comment);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(comment);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM