简体   繁体   English

有什么好办法可以防止网站受到xss攻击

[英]What is a good way to prevent websites from xss attacks

I am using C# web forms with asp.net 4.0.我正在使用 C# web forms 和 asp.net。 This website needs to be secure and is going through a security audit now.该网站需要安全,现在正在通过安全审核。 I am just wondering what needs to be done to best prevent XSS attacks to my website.我只是想知道需要做些什么来最好地防止对我的网站的 XSS 攻击。

I know that there is encoding avaliable for the output of the site.我知道该站点的 output 有可用的编码。 I have also looked into antixsslibrary.dll from microsoft but this seems outdated to me.我还研究了来自微软的 antixsslibrary.dll 但这对我来说似乎已经过时了。 Does asp.net have any built in prevention of xss attacks? asp.net 是否有任何内置的防止 xss 攻击? I am lead to think that it does since I am trying to create and xss attack on my own website and I get the error:我被引导认为它确实如此,因为我试图在我自己的网站上创建和 xss 攻击并且我收到错误:

Server Error in '/GNB.DPS.MVAD.Presentation.Web' Application. “/GNB.DPS.MVAD.Presentation.Web”应用程序中的服务器错误。

A potentially dangerous Request.Form value was detected from the client (ctl00$cphListBody$tbXSS="alert('XSS V..."). Description: Request Validation has detected a potentially dangerous client input value, and processing of the request has been aborted. This value may indicate an attempt to compromise the security of your application, such as a cross-site scripting attack. To allow pages to override application request validation settings, set the requestValidationMode attribute in the httpRuntime configuration section to requestValidationMode="2.0". Example: . After setting this value, you can then disable request validation by setting validateRequest="false" in the Page directive or in the configuration section. However, it is strongly recommended that your application explicitly check all inputs in this case. For more information, see http://go.microsoft.com/fwlink/?LinkId=153133 .从客户端检测到一个潜在危险的 Request.Form 值 (ctl00$cphListBody$tbXSS="alert('XSS V...")。说明:请求验证检测到一个潜在危险的客户端输入值,并且对请求的处理已经已中止。此值可能表示试图破坏应用程序的安全性,例如跨站点脚本攻击。要允许页面覆盖应用程序请求验证设置,请将 httpRuntime 配置部分中的 requestValidationMode 属性设置为 requestValidationMode="2.0 "。示例:。设置此值后,您可以通过在 Page 指令或配置部分中设置 validateRequest="false" 来禁用请求验证。但是,强烈建议您的应用程序在这种情况下显式检查所有输入。有关详细信息,请参阅http://go.microsoft.com/fwlink/?LinkId=153133

Exception Details: System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client (ctl00$cphListBody$tbXSS="alert('XSS V...").异常详细信息:System.Web.HttpRequestValidationException:从客户端检测到潜在危险的 Request.Form 值 (ctl00$cphListBody$tbXSS="alert('XSS V...")。

There are tools provided by ASP.NET that allow you to defend against XSS attacks, but not automatically. ASP.NET 提供的工具可以让您防御 XSS 攻击,但不能自动防御。 You need to know what the attacks are, and how to use the tools to defend against them.您需要知道攻击是什么,以及如何使用这些工具来防御它们。

The error you showed is a default setting that attempts to block bad user input as it is coming in to the web app.您显示的错误是默认设置,它试图阻止错误的用户输入,因为它正在进入 web 应用程序。 That's a nice step, but don't let if give you a false sense of security .这是一个很好的步骤,但不要让 if 给你一种错误的安全感 There are other ways for bad input to get into your data and therefore make your site vulnerable.错误输入还有其他方法可以进入您的数据,从而使您的网站易受攻击。

For example, say your website is driven by user data.例如,假设您的网站是由用户数据驱动的。 The data entry screens you wrote may block "potentially dangerous content" but there's nothing to prevent someone from using another interface to enter the data - their own interface, SQL Server Management Studio, etc. If they get bad scripts in there, your website will happily present those dangerous scripts to users.您编写的数据输入屏幕可能会阻止“潜在危险内容”,但没有什么可以阻止某人使用其他界面输入数据 - 他们自己的界面,SQL Server Management Studio 等。如果他们在那里得到错误的脚本,您的网站将愉快地将那些危险的脚本呈现给用户。

The only way to protect against XSS is to sanitize data as it is being put out to the browser, NOT as it is coming in.防止 XSS 的唯一方法是在数据被发送到浏览器时对其进行清理,而不是在数据进入时对其进行清理。

The most basic method is to use the buil-in Server.HtmlEncode() function to clean all strings that come from untrusted sources.最基本的方法是使用内置的 Server.HtmlEncode() function 来清除所有来自不受信任来源的字符串。 (Untrusted meaning anything other than strings you've hard-coded yourself. Databases, files, and web services are NOT trusted sources.) (不受信任的意思是除您自己硬编码的字符串之外的任何内容。数据库、文件和 web 服务不是受信任的来源。)

There are limitations to what Server.HtmnlEncode can do. Server.HtmnlEncode 可以做的事情是有限制的。 I personally like to use the Microsoft Anti-Xss library , as it does a better job (whitelist vs. blacklist filtering) and offers more functions (like GetSafehtmlFragment()).我个人喜欢使用Microsoft Anti-Xss 库,因为它做得更好(白名单与黑名单过滤)并提供更多功能(如 GetSafehtmlFragment())。

There are tons of resources on the web for this. web 上有大量资源用于此目的。 I'd start here:我将从这里开始:

https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet https://www.owasp.org/index.php/XSS_%28Cross_Site_Scripting%29_Prevention_Cheat_Sheet

and then specifically here for .NET:然后专门针对 .NET:

http://msdn.microsoft.com/en-us/library/aa973813.aspx http://msdn.microsoft.com/en-us/library/aa973813.aspx

Edit - added编辑 - 添加

The primary issue at hand here seems to be a misunderstanding about the difference between input filtering and output filtering.这里的主要问题似乎是对输入过滤和 output 过滤之间的区别的误解。 This article does a good job of explaining the difference and uses better than I could here:这篇文章很好地解释了差异并且比我在这里使用得更好:

http://jeremiahgrossman.blogspot.com/2007/01/input-validation-or-output-filtering.html http://jeremiahgrossman.blogspot.com/2007/01/input-validation-or-output-filtering.html

Final edit and then I'll stop.最后编辑,然后我会停下来。

Even Microsoft acknowledges what I'm saying above.甚至微软也承认我上面所说的。 See this article: http://msdn.microsoft.com/en-us/library/ms998274.aspx请参阅这篇文章: http://msdn.microsoft.com/en-us/library/ms998274.aspx

from the article:来自文章:

Do Not Rely on Input Sanitization不要依赖输入清理

A common practice is for code to attempt to sanitize input by filtering out known unsafe characters.一种常见的做法是让代码尝试通过过滤掉已知的不安全字符来清理输入。 Do not rely on this approach because malicious users can usually find an alternative means of bypassing your validation.不要依赖这种方法,因为恶意用户通常可以找到绕过验证的替代方法。 Instead, your code should check for known secure, safe input.相反,您的代码应该检查已知的安全输入。 Table 1 shows various safe ways to represent some common characters.表 1 显示了表示一些常见字符的各种安全方法。

Input validaiton is one tool in your defence arsenal, but alone it is not sufficient.输入验证是您防御库中的一种工具,但仅此还不够。

You could take a look at the Microsoft Web Protection Library on CodePlex.您可以查看 CodePlex 上的Microsoft Web 保护库 You can also have a look at the XSS page on OWASP as well as the cheat sheet .您还可以查看OWASP 上的 XSS 页面以及备忘单

In addition to the previous answers: The message you got was produced by the RequestValidation feature of ASP.NET.除了之前的答案:您收到的消息是由 ASP.NET 的 RequestValidation 功能生成的。 It tries to filter malicious content for example in the QueryString.它尝试过滤恶意内容,例如在 QueryString 中。

While it is a good start, it is not secure enough for you not to worry about scripting attacks anymore.虽然这是一个好的开始,但它还不够安全,您不必再担心脚本攻击。

Also note that RequestValidation is disabled by default in ASP.NET 4.0: http://geekswithblogs.net/bbastiaensen/archive/2010/12/13/request-validation-in-asp.net-4.0.aspx另请注意,在 ASP.NET 4.0 中默认禁用 RequestValidation: http://geekswithblogs.net/bbastiaensen/archive/2010/12/13/request-validation-in-asp.net-4.0.x

Personally, I don't like the RequestValidation feature.就个人而言,我不喜欢 RequestValidation 功能。 Basically, it's guessing whether or not a string of text entered by the user might be harmful.基本上,它是在猜测用户输入的一串文本是否有害。 You can't count on it catching every possible attack string.你不能指望它捕捉到每一个可能的攻击字符串。 This could also result in errors when the user types in legitimate values in your form.当用户在您的表单中输入合法值时,这也可能导致错误。

Here's the problem.这就是问题所在。 Let's suppose that your web site displays rows of text that are input by visitors.假设您的 web 站点显示访问者输入的文本行。

I, as a malicious user, type in <script>window.location='http://www.grossout.com'></script> .作为恶意用户,我输入<script>window.location='http://www.grossout.com'></script>

If this text is output directly to the browser the next time someone visits your web site, they will be redirected to www.grossout.com.如果此文本是 output 直接发送到浏览器,则下次有人访问您的 web 站点时,他们将被重定向到 www.grossout.com。

On the other hand, a friendly visitor might type in This is a cool web site <:-) .另一方面,友好的访问者可能会输入This is a cool web site <:-)

The next time someone visits your web site, they will receive malformed html because of the < character.下次有人访问您的 web 站点时,由于<字符,他们将收到格式错误的 html。

In this example, the best solution is probably to use the Server.HtmlEncode() method which will escape the < character so that it will display correctly without breaking your page.在此示例中,最好的解决方案可能是使用Server.HtmlEncode()方法,该方法将转义<字符,以便在不破坏页面的情况下正确显示。

If you are using JavaScript, you can use the jQuery .text method to escape the < character.如果您使用的是 JavaScript,您可以使用 jQuery .text方法来转义<字符。

I believe that both solutions are superior to using the RequestValidation because they allow for any text to be displayed without causing the user to see an error message.我相信这两种解决方案都优于使用 RequestValidation,因为它们允许显示任何文本而不会导致用户看到错误消息。

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

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