๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ Web Hacking/Dreamhack

[Dreamhack] Mitigation : Same Origin Policy ( SOP ) + Cross Origin Resource Sharing ( CORS ) + JSON with Padding ( JSONP )

by A Lim Han 2022. 9. 18.

1 - 0 - 9. Mitigation : Same Origin Policy ( SOP ) + Cross Origin Resource Sharing ( CORS ) + JSON with Padding ( JSONP )

 

 

 

#  ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ… ( = Same Origin Policy ( SOP ) )

: ํ˜„ ํŽ˜์ด์ง€๊ฐ€ ์•„๋‹Œ ํƒ€ ์ถœ์ฒ˜์—์„œ ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด๋“ค์ด์ง€ ๋ชปํ•˜๊ฒŒ ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ๋ณด์•ˆ ๋ฉ”์ปค๋‹ˆ์ฆ˜

 

+ mitigation์ด๋ž€?

: '์™„ํ™”, ๊ฒฝ๊ฐ' ์ด๋ผ๋Š” ์˜๋ฏธ๋ฅผ ์ง€๋‹˜

 

 

 

- ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ…์˜ ์˜ค๋ฆฌ์ง„(origin) ๊ตฌ๋ถ„๋ฒ•

origin์˜ ๊ตฌ์„ฑ ์š”์†Œ
ํ”„๋กœํ† ์ฝœ(protocol, scheme) ํ˜ธ์ŠคํŠธ(host) ํฌํŠธ(port)

+ ์œ„ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋ชจ๋‘ ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„์ด๋ผ๊ณ  ์นญํ•œ๋‹ค.

 

 

ex 1) https://same-origin.com/๊ณผ ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„์ธ https://same-origin.com/frame.html

-->  path ๋งŒ ๋‹ค๋ฅด๊ธฐ์— ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„( Same Origin )

 

ex 2) https://same-origin.com/๊ณผ ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„์ด ์•„๋‹Œ http://same-origin.com/frame.html

-->  Scheme ์ด ๋‹ฌ๋ผ Cross Origin

 

ex 3) https://same-origin.com/๊ณผ ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„์ด ์•„๋‹Œ https://cross.same-origin.com/frame.html

--> Host ๊ฐ€ ๋‹ฌ๋ผ์„œ Cross Origin

 

ex 4) https://same-origin.com/๊ณผ ๋™์ผํ•œ ์˜ค๋ฆฌ์ง„์ด ์•„๋‹Œ https://same-origin.com:1234/

-->  Port๊ฐ€ ๋‹ฌ๋ผ์„œ Cross Origin

 

 

 

- SOP ์‹ค์Šต

1. ์‹ค์Šต ๋ชจ๋“ˆ ์ฝ”๋“œ ๋ถ„์„

<!-- iframe ๊ฐ์ฒด ์ƒ์„ฑ -->
<iframe src="" id="my-frame"></iframe>
<!-- Javascript ์‹œ์ž‘ -->
<script>
/* 2๋ฒˆ์งธ ์ค„์˜ iframe ๊ฐ์ฒด๋ฅผ myFrame ๋ณ€์ˆ˜์— ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. */
let myFrame = document.getElementById('my-frame')
/* iframe ๊ฐ์ฒด์— ์ฃผ์†Œ๊ฐ€ ๋กœ๋“œ๋˜๋Š” ๊ฒฝ์šฐ ์•„๋ž˜์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. */
myFrame.onload = () => {
    /* try ... catch ๋Š” ์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋กœ์ง ์ž…๋‹ˆ๋‹ค. */
    try {
        /* ๋กœ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด, secret-element ๊ฐ์ฒด์˜ ๋‚ด์šฉ์„ ์ฝ˜์†”์— ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. */
        let secretValue = myFrame.contentWindow.document.getElementById('secret-element').innerText;
        console.log({ secretValue });
    } catch(error) {
        /* ์˜ค๋ฅ˜ ๋ฐœ์ƒ์‹œ ์ฝ˜์†”์— ์˜ค๋ฅ˜ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. */
        console.log({ error });
    }
}
/* iframe๊ฐ์ฒด์— Same Origin, Cross Origin ์ฃผ์†Œ๋ฅผ ๋กœ๋“œํ•˜๋Š” ํ•จ์ˆ˜ ์ž…๋‹ˆ๋‹ค. */
const loadSameOrigin = () => { myFrame.src = 'https://same-origin.com/frame.html'; }
const loadCrossOrigin = () => { myFrame.src = 'https://cross-origin.com/frame.html'; }
</script>
<!--
๋ฒ„ํŠผ 2๊ฐœ ์ƒ์„ฑ (Same Origin ๋ฒ„ํŠผ, Cross Origin ๋ฒ„ํŠผ)
-->
<button onclick=loadSameOrigin()>Same Origin</button><br>
<button onclick=loadCrossOrigin()>Cross Origin</button>
<!--
frame.html์˜ ์ฝ”๋“œ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.
secret-element๋ผ๋Š” id๋ฅผ ๊ฐ€์ง„ div ๊ฐ์ฒด ์•ˆ์— treasure๋ผ๊ณ  ํ•˜๋Š” ๋น„๋ฐ€ ๊ฐ’์„ ๋„ฃ์–ด๋‘์—ˆ์Šต๋‹ˆ๋‹ค.
-->
<div id="secret-element">treasure</div>

+ << Line 2 >>

: iframe ์„ ์‚ฌ์šฉํ•˜์—ฌ ์›นํŽ˜์ด์ง€ ์•ˆ์— ๋‹ค๋ฅธ ์›นํŽ˜์ด์ง€๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” HTML ํƒœ๊ทธ๋ฅผ ๊ตฌํ˜„

: src ์š”์†Œ๋กœ ์‚ฝ์ž…๋˜๋Š” ์›น ํŽ˜์ด์ง€ ์ฃผ์†Œ ์„ค์ •

 

+ << Line 10 >>

: ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ onload๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ๋กœ๋“œ ์„ฑ๊ณต ์‹œ์— ๋™์ž‘ํ•˜๋„๋ก ํ•จ

 

+ << Line 10 ~ 23 >>

: iframe ๊ฐ์ฒด์— ํŽ˜์ด์ง€ ๋กœ๋“œ ์‹œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ

 

+ << Line 14 ~ 15 >>

: ํŽ˜์ด์ง€ ๋กœ๋“œ ์™„๋ฃŒ ์‹œ secret-element ๊ฐ์ฒด์˜ ๊ฐ’์ธ tresure์„ ์ฝ์–ด์™€ ์ฝ˜์†”์— ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ

 

 

 

2. ์‹ค์Šต ํƒญ์„ ํ†ตํ•ด ์ฝ”๋“œ ์ •์ƒ ๋™์ž‘ ์—ฌ๋ถ€ ์‹ค์Šต

Same Origin์ธ ๊ฒฝ์šฐ&nbsp; --> ๋ฐ์ดํ„ฐ ์ ‘๊ทผ์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ˆ˜ํ–‰๋จ

 

 

Cross Origin์ธ ๊ฒฝ์šฐ&nbsp; -->&nbsp; tresure ๊ฐ์ฒด๊ฐ’ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•˜์—ฌ ์ฝ์–ด์˜ค๋Š” ๊ณผ์ •์—์„œ ์—๋Ÿฌ ๋ฐœ์ƒ

 

 

 

 

# ๊ต์ฐจ ์ถœ์ฒ˜ ๋ฆฌ์†Œ์Šค ๊ณต์œ  ( = Cross Origin Resource Sharing ( CORS ) )

: SOP์˜ ์ œํ•œ์„ ๋ฐ›์ง€ ์•Š๊ณ  Cross Origin์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ, HTTP ํ—ค๋”๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ Cross Origin ๊ฐ„ ๋ฆฌ์†Œ์Šค๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

 

- CORS ๋™์ž‘ ์ˆœ์„œ

1. ๋ฐœ์‹ ์ž ์ธก์—์„œ CORS ํ—ค๋” ์„ค์ • ํ›„ ์š”์ฒญ

์›น ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๋Š” ์ฝ”๋“œ

 

๋ฐœ์‹  ์ธก์—์„œ ๋ณด๋‚ธ HTTP ์š”์ฒญ&nbsp; -->&nbsp; ํ—ค๋”์™€ ๋ฉ”์†Œ๋“œ์˜ ์ถ”๊ฐ€ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•œ์ง€ ์งˆ์˜

+ CORS preflight

: ์ˆ˜์‹  ์ธก์— ์›น ๋ฆฌ์†Œ์Šค ์š”์ฒญ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ์งˆ์˜ํ•˜๋Š” ๊ณผ์ •

 

 

 

2. ์ˆ˜์‹  ์ธก์—์„œ ํ—ค๋”๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ž‘์—… ์ˆ˜ํ–‰

 

 

 

3. ๊ตฌ๋ถ„ ํ›„ ์ •ํ•ด์ง„ ๊ทœ์น™์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ์„ค์ •

์„œ๋ฒ„์˜ ์‘๋‹ต ์ฝ”๋“œ

Access-Control-
Allow-Origin
Access-Control-
Allow-Methods
Access-Control-
Allow-Credentials
Access-Control-
Allow-Headers
ํ—ค๋” ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š”
Origin์—์„œ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ํ—ค๋”
ํ—ค๋” ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์†Œ๋“œ์˜ ์š”์ฒญ๋งŒ ์ฒ˜๋ฆฌํ•˜๋Š” ํ—ค๋” ์ฟ ํ‚ค์˜ ์‚ฌ์šฉ ์—ฌ๋ถ€๋ฅผ
ํŒ๋‹จํ•˜๋Š” ํ—ค๋”
ํ—ค๋” ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ํ—ค๋”์˜
์‚ฌ์šฉ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํ—ค๋”

 

 

 

4. ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ˆ˜์‹  ์ธก์˜ ์‘๋‹ต๊ณผ ๋ฐœ์‹  ์ธก์˜ ์š”์ฒญ์˜ ์ƒ์‘ ์—ฌ๋ถ€ ํ™•์ธ

 

 

 

5. ์ƒ์‘ํ•จ์ด ํ™•์ธ๋  ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ €์—์„œ POST ์š”์ฒญ์„ ๋ณด๋‚ด ์ˆ˜์‹  ์ธก์˜ ์›น ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๋Š” HTTP ์š”์ฒญ ์ „์†ก

 

 

 

 

# JSON with Padding ( JSONP )

: CORS์˜ ๋Œ€์ฒด๋ฒ• ์ค‘ ํ•˜๋‚˜๋กœ, <script> ํƒœ๊ทธ๋กœ Cross origin์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  Callback ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

 

 

ex) 

์›น ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๋Š” ์ฝ”๋“œ

+ << Line 13 >>

: Cross origin์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ฝ”๋“œ

: ๋ฐ์ดํ„ฐ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ callback ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ myCallback ๋„ ํ•จ๊ป˜ ์ „๋‹ฌ

 

๋ฆฌ์†Œ์Šค ์š”์ฒญ์— ๋”ฐ๋ฅธ ์‘๋‹ต