SOP & CORS

2022. 6. 30. 12:44ยท๐Ÿ–Œ๏ธ Theory/WEB

What is SOP(Same Origin Policy)?

HTTP ์š”์ฒญ์—์„œ ๊ฐ€์ ธ์˜จ ์•…์˜์ ์ธ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์„ ์ˆ˜ ์—†๊ฒŒ ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ณด์•ˆ

 

Origin ๊ตฌ๋ถ„ : Protocol, Port, Host

https://same-origin.com 

 

์ฃผ์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ Same Origin, Cross Origin ํŒ๋‹จ ์˜ˆ์‹œ

 

URL                                                                                                                                ๊ฒฐ๊ณผ                                 ์ด์œ 

https://same-origin.com/frame.html Same Origin Path๋งŒ ๋‹ค๋ฆ„
http://same-origin.com/frame.html Cross Origin Scheme์ด ๋‹ค๋ฆ„
https://cross.same-origin.com/frame.html Cross Origin Host๊ฐ€ ๋‹ค๋ฆ„
https://same-origin.com:1234/ Cross Origin Port๊ฐ€ ๋‹ค๋ฆ„

SOP Test

Origin์ด http://dreamhack.io์ธ SOP ๋ณดํ˜ธ ๊ธฐ๋ฒ•์ด ๊ฑธ๋ ค์žˆ๋Š” ์‚ฌ์ดํŠธ์—์„œ "window.open" ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด same origin์ธ ์‚ฌ์ดํŠธ์™€ cross origin ์‚ฌ์ดํŠธ๋ฅผ ์ง์ ‘ ์—ด์–ด๋ณด์ž.

 

Same Origin

sameNewWindow = window.open('https://dreamhack.io/lecture');
console.log(sameNewWindow.location.href);
๊ฒฐ๊ณผ: https://dreamhack.io/lecture

Cross Origin

crossNewWindow = window.open('https://theori.io');
console.log(crossNewWindow.location.href);
๊ฒฐ๊ณผ: Origin ์˜ค๋ฅ˜ ๋ฐœ์ƒ

๋‹ค๋งŒ, Cross Origin์ด read๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜์ง€๋งŒ Data์˜ write์€ ๊ฐ€๋Šฅํ•˜๋‹ค.

crossNewWindow = window.open('https://theori.io');
crossNewWindow.location.href = "https://dreamhack.io";

 

SOP Example

<iframe src="[Define Origin Site]" id="my-frame">

<script>
let myFrame = document.getElementById("my-frame");

myFrame.onload = () => {
	try {
    	let secretValue = myFrame.contentWindow.document.getElementById('secret-element').innerText;
        console.log({ secretValue });
    } catch() {
    	...
    }
 
const loadSameOrigin = () => { myFrame.src = 'https://same-origin.com/frame.html'; }
const loadCrossOrigin = () => { myFrame.src = 'https://cross-origin.com/frame.html'; }
</script>

<button onclick=loadSameOrigin()>Same Origin</button><br>
<button onclick=loadCrossOrigin()>Cross Origin</button>

<div id="secret-element">treasure</div>

iframe ํƒœ๊ทธ์˜ src๋ฅผ ์ด์šฉํ•ด Origin์„ ์ง€์ •ํ•œ๋‹ค. myFrame ๋ณ€์ˆ˜์—์„œ id๊ฐ€ "my-frame"์ธ Element๋ฅผ ์ €์žฅํ•˜๊ณ , onload๋ฅผ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚จ๋‹ค.  loadSameOrigin๊ณผ loadCrossOrigin ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ๊ฐ๊ฐ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

// Result
SameOrigin :
{"secretValue":"treasure"}

CrossOrigin :
{"error":{"stack":"Error: Blocked a frame with origin "https://same-origin.com/" from accessing a cross-origin frame.
at HTMLIFrameElement.myFrame.onload (https://same-origin.com/:6:51)"}}

What is CORS(Cross Origin Resource Sharing)?

HTTP ํ—ค๋”์— ๊ธฐ๋ฐ˜ํ•˜์—ฌ Cross Origin ๊ฐ„์˜ Resource๋ฅผ ๊ณต์œ ํ•˜๋Š” ๋ฐฉ์‹. Sender ์ธก์—์„œ CORS ํ—ค๋”๋ฅผ ์„ค์ •ํ•ด ์š”์ฒญํ•˜๋ฉด, Receiver ์ธก์—์„œ ํ—ค๋”๋ฅผ ๊ตฌ๋ถ„ํ•ด ๊ทœ์น™์— ๋งž๊ฒŒ Data๋ฅผ ๊ฐ€์ ธ๊ฐ€๋„๋ก ์„ค์ •. ์ด ๊ณผ์ •์„ ๋งˆ์นœ ํ›„, ๋ธŒ๋ผ์šฐ์ €๋Š” ์ˆ˜์‹ ์ธก์˜ ์‘๋‹ต์ด ๋ฐœ์‹ ์ธก์˜ ์š”์ฒญ๊ณผ ์ƒ์‘ํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ , ๊ทธ๋•Œ์•ผ ๋น„๋กœ์†Œ POST ์š”์ฒญ์„ ๋ณด๋‚ด ์ˆ˜์‹ ์ธก์˜ ์›น ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๋Š” HTTP ์š”์ฒญ์„ ๋ณด๋‚ธ๋‹ค.

 

Web Resource Request Code

/*
    XMLHttpRequest ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. 
    XMLHttpRequest๋Š” ์›น ๋ธŒ๋ผ์šฐ์ €์™€ ์›น ์„œ๋ฒ„ ๊ฐ„์— ๋ฐ์ดํ„ฐ ์ „์†ก์„
    ๋„์™€์ฃผ๋Š” ๊ฐ์ฒด ์ž…๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด HTTP ์š”์ฒญ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
*/
xhr = new XMLHttpRequest();
xhr.open('POST', 'https://theori.io/whoami');

/* HTTP ์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ, ์ฟ ํ‚ค ์ •๋ณด๋„ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ด์ค๋‹ˆ๋‹ค. */
xhr.withCredentials = true;
xhr.setRequestHeader('Content-Type', 'application/json');

/* xhr ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด HTTP ์š”์ฒญ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. */
xhr.send("{'data':'WhoAmI'}");

Sender's HTTP Request

OPTIONS /whoami HTTP/1.1
Host: theori.io
Connection: keep-alive
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Origin: https://dreamhack.io
Accept: */*
Referer: https://dreamhack.io/

Server's Response

HTTP/1.1 200 OK
Access-Control-Allow-Origin: https://dreamhack.io
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: Content-Type

 

Types of CORS's Header

 

Header                                                                             Description

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

 

Reasons for use

SOP ๋ณดํ˜ธ ๊ธฐ๋ฒ•์— ๊ตฌ์• ๋ฐ›์ง€ ์•Š๊ณ  ์™ธ๋ถ€ ์ถœ์ฒ˜์— ๋Œ€ํ•œ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌ. ํƒœ๊ทธ๋กœ๋Š” <img>, <style>, <script>๊ฐ€ ์žˆ๊ณ  ๊ทธ ์™ธ์—๋„  ์„œ๋น„์Šค์—์„œ ๋™์ผ ์ถœ์ฒ˜ ์ •์ฑ…์ธ SOP๋ฅผ ์™„ํ™”ํ•˜์—ฌ ๋‹ค๋ฅธ ์ถœ์ฒ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ CORS ๋ณดํ˜ธ ๊ธฐ๋ฒ•์„ ์ด์šฉํ•œ๋‹ค.

 

 

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ–Œ๏ธ Theory/WEB' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • Advanced Tips For SSRF Attack
  • Small Tip
  • Mass Assignment Vulnerability
  • CSP ( Content Security Policy )
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • ๐Ÿšฉ CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • ๐Ÿ’ป Security (5)
      • ๐Ÿ–Œ๏ธ Theory (22)
        • WEB (9)
        • PWN (13)
      • ๐Ÿ“„ Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    Remote Code Execution
    python
    TsukuCTF2022
    RCE
    Ubuntu ๊ธฐ์ดˆ ์…‹ํŒ…
    cache poisoning
    TFCCTF2022
    ubuntu ๋ช…๋ น์–ด
    bug report
    bug hunter
    Machine Learning
    cache
    Crypto
    Deep learning
    justCTF
    sqli
    GPNCTF
    Ubuntu ๊ธฐ์ดˆ
    Text Summarization
    pwntools
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

Cronus
SOP & CORS
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”