【问题标题】:How to protect or secured ASP.NET MVC from XSS?如何保护 ASP.NET MVC 免受 XSS 攻击?
【发布时间】:2017-08-16 02:41:38
【问题描述】:

我正在使用最新版本的 ASP.NET MVC 5.2.3 创建一个 Web 应用程序。我只关心 XSS 攻击。我发现 ASP.NET Core 可以完美地保护 XSS 免受这种攻击,这个框架非常棒,但它缺少我的项目所需的第三方。这是我的担忧。我也已经启用了自定义错误,但我目前禁用它以进行测试。

但我想确保这也能赶上。

  1. 输入验证已通过。为避免此异常或错误。

从客户端检测到一个具有潜在危险的 Request.Form 值(Name="")。

使用 [AllowHtml] 属性就可以了,或者使用 AntiXss 库。

  1. 但是,从 URL。示例网址,

    http://localhost:54642/Employees/

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

link or url

这个错误应该是,

检测到来自客户端 (

所以我的解决方案是从 Web.config 启用它,然后它就可以工作了!

但是 Troy Hunt 在他的教程中说,对于这个错误,这不是一个好的或更好的做法。所以我决定从这次 XSS 攻击中寻找最好的解决方案。

【问题讨论】:

    标签: c# asp.net-mvc security asp.net-mvc-5


    【解决方案1】:

    在我的表单中,我通常会添加这个防伪令牌

     @Html.AntiForgeryToken()
    

    然后在我的控制器上我确保验证令牌

    [ValidateAntiForgeryToken] 
    

    在传递变量或数据时,我总是声明正确的变量。无论如何,如果它的成员区域页面,您始终可以限制对正确成员角色的访问,例如

      [Authorize] // for registered user
    
      or more filtered
    
      [Authorize(Roles = "SUBSCRIBER.VIEW")]
    

    以下仅适用于 .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" />
    

    请求验证延迟验证是在 ASP.NET 4.5 中引入的,我 刚刚对其进行了一些测试,似乎惰性验证是 无论您如何设置“requestValidationMode”,都启用,之后 你已经安装了 4.5 框架。

    【讨论】:

    • 谢谢,但这是针对 CSRF 攻击的,我需要 XSS 安全性。
    • 在您的输入中没有标签 [AllowHtml] 它应该已经保护了您的表单,但如果您仍然不信任您的表单,您可以使用这个 AntiXss.AntiXssEncoder。
    • 对于 url 保护示例,localhost:8080/Employees/
    • 它应该自动检测你的 web.config 上是否有这个
    【解决方案2】:

    查看 OWASP 网站。这是我在 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>
    

    【讨论】:

      【解决方案3】:
      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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-07
        • 1970-01-01
        • 2011-10-02
        • 2019-06-26
        • 2020-05-12
        • 2016-10-28
        相关资源
        最近更新 更多