简体   繁体   English

跨站点脚本(XSS)可能吗?

[英]Cross site scripting (XSS) possible?

I found following code in my application: 我在我的应用程序中找到以下代码:

eval( 'window.opener.' + fct );

The variable fct is coming from a GET-Parameter (so it can be changed by the user). 变量fct来自GET参数(因此可以由用户更改)。

Is it possible to pass some evil value to execute JavaScript? 是否有可能传递一些邪恶的价值来执行JavaScript? I am not sure, because if you change the URL and send the link to the user, and he clicks on it, "window.opener" will be null, so an error will be thrown: 我不确定,因为如果您更改URL并将链接发送给用户,并且他点击它,“window.opener”将为null,因此将抛出错误:

eval( 'window.opener.x; alert(1);' ); // Uncaught TypeError: Cannot read property 'x' of null

Is there any attack vector, that could cause a security problem? 是否存在可能导致安全问题的攻击媒介? I know, that you should never use eval() - but I also would try to find a proof of concept. 我知道,你永远不应该使用eval() - 但我也会尝试找到一个概念证明。

Thank you! 谢谢!

If fct is x = alert('yes') then it'll result in: 如果fctx = alert('yes')那么它将导致:

window.opener.x = alert('yes')

and the right-hand side of the expression will be evaluated first, allowing code execution. 并且将首先评估表达式的右侧,允许代码执行。

If you don't want any error triggered, you can do: 如果您不希望触发任何错误,您可以执行以下操作:

window.opener.x = (window.opener={pwnd:confirm('game over')})

Is there any attack vector, that could cause a security problem? 是否存在可能导致安全问题的攻击媒介?

Yes. 是。 You are executing arbitrary JavaScript. 您正在执行任意JavaScript。 Any code can be ran. 任何代码都可以运行。 This is a serious security issue. 这是一个严重的安全问题。

Firstly, any variable data used in the context of JavaScript should be JSON-encoded, which is compatible with JavaScript. 首先,JavaScript上下文中使用的任何变量数据都应该是JSON编码的,它与JavaScript兼容。 You didn't say what server-side language you were using, so here's a PHP example to illustrate: 您没有说出您使用的是哪种服务器端语言,因此这里有一个PHP示例来说明:

var fct = <?php echo json_encode($_GET['fct']); ?>;

If fct is parsed from the query-string client side, this immediate issue doesn't exist because you aren't parsing arbitrary strings as JavaScript at this point. 如果从查询字符串客户端解析fct ,则不存在这个直接问题,因为此时您没有将任意字符串解析为JavaScript。 However, you still have the eval problem. 但是,您仍然存在eval问题。

You should rewrite your code as this: 你应该重写你的代码:

window.opener[fct]

That way, you are only referencing what you need without injecting script. 这样,您只需引用所需内容而无需注入脚本。

Now, you also need to whitelist what's in this variable, should opener contain things you don't want to expose. 现在,您还需要将此变量中的内容列入白名单, opener是否应包含您不想公开的内容。

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

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