티스토리 뷰

study/web

[web] About CSP

hoppi 2021. 1. 14. 15:31

스물세 번째 글.

 

 

1. What is CSP?

1.1 정의

- CSP(Content Security Policy, 콘텐츠 보안 정책 ) : 브라우저에서 XSS 공격을 막고 공격 시도를 보고받을 수 있는 기술

-웹 사이트에 본래 있지 않던 스크립트를 삽입하거나 공격자에게 권한이 있는 서버 등에 요청을 보내지 못하도록 함

1.2 구성요소

- CSP Header는 1개 이상의 Policy directive가 세미콜론(;)으로 분리된 형태로 이루어짐

- Policy directive는 지시문 종류와 1개 이상의 출처가 스페이스로 분리된 형태로 지정돼야 함

- ex) Content-Security-Policy: script-src <source> <source>;

 

아래는 본인이 설정한 CSP가 XSS로 부터 얼마나 강력한지 체크해주는 사이트

csp-evaluator.withgoogle.com/

 

CSP Evaluator

 

csp-evaluator.withgoogle.com

 

2. List of Content Security Policy Fetch directives

지시문 내용(간단하게)
default-src 모든 지시문에 대한 정책을 정의
child-src <iframe>와 관련한 것을 제어
connect-src XHR, WebSockets 등을 통해 연결할 수 있는 것을 제어
script-src <script>와 관련한 것을 제어
img-src <img>와 관련한 것을 제어
report-uri CSP 위반시 보고서를 받을 URL 지정

더 자세한 내용들은 아래 사이트를 참조

developer.mozilla.org/ko/docs/Web/HTTP/Headers/Content-Security-Policy

 

Content-Security-Policy - HTTP | MDN

The HTTP Content-Security-Policy response header allows web site administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This hel

developer.mozilla.org

 

3. List of Content Security Policy source options

src 옵션 설명
'none' 어떤 Origin도 허용하지 않음
'self' 같은 Origin의 자원만 허용
'unsafe-eval' eval()등 안전하지 않은 함수를 허용
'unsafe-inline' inline script를 허용(유효한 nonce나 hash가 지정x)
'nonce-<base64>' Base64로 지정된 nonce를 지정
'<hashalg>-<base64>' Base64로 지정된 해시 값을 사용하여 추후 로드되는 자원의 해시와 비교

 

4. Example

#Content-Security-Policy: script-src 'nonce-bm9uY2V2YWx1ZQ=='#

<script nonce="bm9uY2V2YWx1ZQ=="> 
	alert('ok'); 
</script> 

<script> 
	alert('fail'); 
</script>

 

위처럼 CSP에 nonce 값을 설정해주고 그와 일치하는 nonce 값을 가진 script만 실행이 된다.

즉, alert('ok')만 실행이 된다.

 

Reference

defenit.kr/2020/02/11/Web/%E3%84%B4%20Research/CSP-Bypass-Tips/ //우회 참고시 보면 좋겠네요

simjaejin.tistory.com/31

developer.mozilla.org/ko/docs/Web/HTTP/Headers/Content-Security-Policy#directives

Dreamhack lecture - Client-side Advance

'study > web' 카테고리의 다른 글

[web] XXE Injection Advanced  (0) 2022.03.09
[web] XXE Injection  (0) 2022.03.06
[web] DOM Clobbering  (0) 2021.01.28
[web] python pickle module(deserialize vulnerability)  (0) 2021.01.07