简体   繁体   English

逃避<和>是否足以阻止XSS攻击?

[英]Is escaping < and > sufficient to block XSS attacks?

I'm sure that the answer to this question is No, but I can't seem to find a way that simply transforming < and > to &lt; 我确信这个问题的答案是否定的,但我似乎找不到简单地将<>转换为&lt; and &gt; &gt; doesn't completely block reflected and persistent XSS. 不会完全阻止反射和持久的XSS。

I'm not talking about CSRF. 我不是在谈论CSRF。

If this doesn't block XSS, can you provide an example of how to bypass this defence? 如果这不能阻止XSS,你能提供一个如何绕过这种防御的例子吗?

When using an untrusted string in an attribute (quoted with " ) you need to escape " as &quot . 在属性中使用不受信任的字符串(引用" )时,您需要转义为" as &quot

Otherwise you could easily inject javascript. 否则你可以轻松注入javascript。 For example, <a href="{{str}}"> with str being, for example, " onmouseover='something-evil'" . 例如, <a href="{{str}}"> str就是" onmouseover='something-evil'"

No. Here are a couple of examples where escaping < , > , ' , " and & is not enough: 不。以下是一些逃避<>'"&不够的例子:

Example 1: 例1:

<a href="{{myUrl}}">

XSS Attack: XSS攻击:

myUrl = "javascript:alert(1)"

Example 2: 例2:

<script>var page = {{myVar}};</script>

XSS Attack: XSS攻击:

myVar = "1;alert(1)"

See https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet for ways of preventing these attacks. 有关防止这些攻击的方法,请参阅https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet

No, it's not sufficient. 不,这还不够。 Remember that XSS isn't just about untrusted data in HTML, you'll also find it in JavaScript and CSS. 请记住,XSS不仅仅是关于HTML中不受信任的数据,您还可以在JavaScript和CSS中找到它。 Think about a situation such as "var myVar = [input];" 考虑一下诸如“var myVar = [input];”之类的情况。 There are all sorts of malicious things you can do with that [input] value without going anywhere near angle brackets. 你可以用这个[输入]值做各种各样的恶意事情而不需要靠近尖括号。 There's many more examples over in the XSS cheat sheet: http://ha.ckers.org/xss.html 在XSS备忘单中有更多的例子: http//ha.ckers.org/xss.html

You've mentioned ASP.NET in the tag; 你在标签中提到过ASP.NET; what you want to be looking at is the [AntiXSS library][1] . 你想看的是[AntiXSS library][1] Grab this and use the appropriate output encoding: 抓住这个并使用适当的输出编码:

Encoder.CssEncode()
Encoder.HtmlEncode()
Encoder.HtmlAttributeEncode()
Encoder.JavaScriptEncode()

etc. etc. There's absolutely no reason to try and do your own character substitution in .NET. 等等。完全没有理由尝试在.NET中进行自己的字符替换。

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

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