Shang
Blog 👨‍💻
  • 🌸Introduction
  • 💻WEB SECURITY
    • Research Vulnerability
      • 📲Server-side topics
        • 🔏API Testing
        • 🔏Race conditions
        • 🔏XML external entity (XXE) injection
        • 🔏Server-side request forgery (SSRF)
        • 🔏File upload vulnerabilities
        • 🔏Access control vulnerabilities and privilege escalation
        • 🔏Business logic vulnerabilities
        • 🔏OS Command injection
        • 🔏Directory traversal
        • 🔏Authentication vulnerabilities
        • 🔏SQL injection
      • 📱Client-side topics
        • 🔏DOM-based vulnerabilities
        • 🔏Cross-origin resource sharing (CORS)
        • 🔏WebSockets
        • 🔏Clickjacking (UI redressing)
        • 🔏Cross-site request forgery (CSRF)
        • 🔏Cross-site scripting(XSS)
      • 🌀Advanced topics
        • 🔐Web cache poisoning
        • 🔐HTTP request smuggling
        • 🔐Prototype pollution
        • 🔐Server-side template injection(SSTI)
        • 🔐Insucure deserialization
    • Learn Java Vulnerability
      • Intro & Setup
      • Java Reflection Part 1
      • Java Reflection Part 2
    • Research Documents
      • 🎯DNS Rebinding
      • 🍪Remote Code Execution - Insecure Deserialization
      • 🍪Remote Code Execution on Jinja - SSTI Lab
      • 🍪Exploit cross-site request forgery (CSRF) - Lab
      • 🍪Exploit a misconfigured CORS - Lab
      • 🍪Same Origin Policy (SOP) - Lab
  • 📝WRITE-UP CTF
    • CTF Competitions
      • 🔰[WolvCTF 2023] Writeup Web
      • 🔰[M☆CTF Training 2023] Writeup Web
      • 🔰[HackTM CTF 2023] Writeup Web
      • 🔰[Incognito 4.0 2023] Writeup Web
      • 🔰[LA CTF 2023] Re-writeup Web
      • 🔰[Dice CTF 2023] Writeup Web
      • 🔰[ByteBandits CTF 2023] Writeup Web
      • 🔰[Knight CTF 2023] Writeup Web
      • 🔰[Sekai CTF 2022] Writeup Web
      • 🔰[WRECK CTF 2022] Writeup Web
      • 🔰[Maple CTF 2022] Writeup Web
    • CTF WarGame
      • ✏️[Root me] Writeup Sever Side
      • ✏️Websec.fr
      • ✏️[Root me] Writeup XSS Challenge
    • [tsug0d]-MAWC
      • 💉TSULOTT
      • 💉IQTEST
      • 🧬TooManyCrypto
      • 🧬NumberMakeup
    • Pwnable.vn
Powered by GitBook
On this page
  • TL;DR - show me the fun part❗
  • I don’t get it. Show me the flow 🔎
  • How did this happen❓
  • Analysing the attack 👀
  • Chúng ta sẽ xem xét ứng dụng có lỗ hổng sau:
  • Làm thế nào để kẻ tấn công exploit điều này ❓
  • Show me the source code of the backend❓
  • Conclusion
  1. WEB SECURITY
  2. Research Documents

Exploit a misconfigured CORS - Lab

PreviousExploit cross-site request forgery (CSRF) - LabNextSame Origin Policy (SOP) - Lab

Last updated 2 years ago

TL;DR - show me the fun part❗

  1. Nạn nhận vào app có hổng.

  2. Nạn nhân sẽ đi tới .

  3. Kẻ tấn công sẽ leak API key🎉.

I don’t get it. Show me the flow 🔎

  1. Người dùng được xác thực sẽ tới trang web tấn công.

  2. Trang web của kẻ tấn công chưa một đoạn JS độc hại cái mà sẽ yêu cầu trình duyệt gửi một cross-origin request được xác thực( qua XMLHttpRequest hoặc fetch) tới endpoint có lỗ hổng ở example.com.

  3. Endpoint có lỗ hổng reflect nhầm request của Origin header vàoAccess-Control-Allow-Origin response header.

  4. Trình duyệt thấy Origin của kẻ tấn công được cho phép. Do đó, nó cho phép JS read response.

How did this happen❓

Chúng ta đã nói trước đó rằng CORS là một cách để tạo ra một exception tới same origin policy. Nó cho phép đoạn JS để READ một cross-origin resource(Thực hiện đọc tài nguyên của trang web cross)

Ở đoạn JS, bạn có thể thực hiện các request CORSS-Origin được xác thực. Có nghĩa là, một JS có thể yêu cầu trình duyệt tự động gửi cookie cùng với yêu cầu.

Để cho phép loại request được xác thực này, điểm đến endpoint phải cho phép rõ ràng request được xác thực qua HTTP header

Access-Control-Allow-Credentials: true

Khi header trên được sử dụng, sau đó endpoint cũng phải chỉ định rõ ràng một origin trong giá trị của Access-Control-Allow-Origin header, ví dụ:

Access-Control-Allow-Origin: https://origin.secure-cookie.io

Vì các nhà phát triển phải chỉ định một origin. Các nhà phát triển nhầm khi cho phép bất kỳ orign nào thông qua việc reading the origin của request đến và sau đó "reflect" nó trong response header

Access-Control-Allow-Origin: <REQUEST ORIGIN HERE>

Điều này gây ra lỗ hổng bảo mật vì request có thể đến từ origin của kẻ tấn công và nó sẽ được reflect trong header được phép. Do đó, JS từ trang web của kẻ tấn công sẽ được phép read response và rò rỉ dữ liệu của người dùng.

Analysing the attack 👀

Chúng ta sẽ xem xét ứng dụng có lỗ hổng sau:

  1. Một người dùng được xác thực có thể generate API key bằng cách nhấn vào "Generate API key".

  2. Khi kích hoạt nút generate sẽ trigger đoạn JS này:

    function get_api_key(){
    var options = {
    method:"GET",
    credentials: 'include'
    }
    var users = fetch(url="https://demo-api.secure-cookie.io/getKey",options);
    users.then(resp => {
               return resp.json()
    }).then(data => {
            update_key_html(data);
         })
    }
  3. Tùy chọn credentials:'include', sẽ cho trình duyệt thực hiện request được xác thực (thêm cookie dọc theo bên request). Theo mặc định mà không có tùy chọn này, trình duyệt sẽ không bao gồm cookie.

  4. Endpoint có lỗ hổng sẽ response header như này:

#Reflects orgin(https://csrf.secure-cookie.io) của request ở “Access-Control-Allow-Origin” header
Access-Control-Allow-Origin: https://csrf.secure-cookie.io 

#Cho phép GET method
Access-Control-Request-Method: GET

#Cho phép người gọi gửi request xác thực kèm cookie 
Access-Control-Allow-Credentials: true

Làm thế nào để kẻ tấn công exploit điều này ❓

  1. Lưu ý rằng ngay khi bạn/nạn nhân mở trang, khóa API bị hack

  2. Trang web của kẻ tấn công chứa đoạn JS mà thực hiện cùng một công việc chính xác mà chúng ta đã giải thích trong các bước (4, 5, 6) ở trên.

Access-Control-Allow-Origin: https://s3-eu-west-1.amazonaws.com
Access-Control-Request-Method: GET
Access-Control-Allow-Credentials: true

Vì orgin của kẻ tấn công được cho phép, JS trong trang kẻ tấn công có thể đọc API Key🎉.

Show me the source code of the backend❓

Cho ví dụ này, chúng ta sẽ sử dụng Python cho backend. Đoạn code chứa lỗ hổng ở endpoint /getKey

def get_key():
    #Đọc request header tới và láy giá trị của Origin
    rcvd_origin = request.headers.get("Origin")
    resp = make_response(jsonify(key=GetRandom.session(size=16)))
    
    #Reflect origin tới ở response header Access-Control-Allow-Origin
    resp.headers['Access-Control-Allow-Origin'] = str(rcvd_origin)
    resp.headers['Access-Control-Allow-Credentials'] = 'true'
    resp.headers['Access-Control-Request-Method'] = 'GET'
    
    #Gửi response
    return resp

Conclusion

Đây là một lab đơn giản phân tích rõ hổng hổng liên quan tới CORS phần mở rộng cho SOP. Cảm ơn các bạn đã đọc bài..

Mở dev tool rồi chuyển qua tab ở trình duyệt của bạn

Login tới

Đoạn JS này sẽ yêu cầu trình duyệt tạo GET request tới

Ở lab này, hãy mở :

Endpoint có lỗ hổng sẽ reflect tới origin bị tấn công trong respone header

💻
🍪
network monitor
https://csrf.secure-cookie.io/login.
https://demo-api.secure-cookie.io/getKey.
trang của kẻ tấn công
https://s3-eu-west-1.amazonaws.com
login
page của kẻ tấn công
Attacker exploiting misconfigured cors
Attacker gets API keys due to misconfigured cors