我正在使用最新版本的ASP.NET MVC 5.2.3创建一个Web应用程序。我只关注XSS攻击。我认为在ASP.NET Core中完全可以防止这种攻击XSS和这个框架完全令人惊讶,但它缺少我需要的第三方项目。这是我的担忧。我已经启用了自定义错误,但我目前已禁用它进行测试。
但我想确保这也会抓住。
从客户端检测到一个潜在危险的Request.Form值(Name =“”)。
使用,[AllowHtml]属性,这很好或使用AntiXss库。
但是,从URL。示例网址
http://localhost:54642/Employees/
http://localhost:54642/Employees/?a=<script>
这个错误应该是,
从客户端(&lt;)中检测到潜在危险的Request.Path值。
所以我的解决方案是从Web.config启用它然后它可以工作!
但特洛伊·亨特从他的教程中说,对于这个错误,这不是一个好的或更好的做法。所以我决定从这次XSS攻击中寻找最佳解决方案。
答案 0 :(得分:3)
在我的表格中,我通常会添加这个防伪标记
@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框架。
答案 1 :(得分:1)
查看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>
答案 2 :(得分:0)
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("<b>", "<b>");
sbComments.Replace("</b>", "</b>");
sbComments.Replace("<u>", "<u>");
sbComments.Replace("</u>", "</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);
}