Tô Đỉnh Nguyên
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # WEB13: Web Cache Poisoning ## Lab: Web cache poisoning with an unkeyed header ```! This lab is vulnerable to web cache poisoning because it handles input from an unkeyed header in an unsafe way. An unsuspecting user regularly visits the site's home page. To solve this lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser. ``` Thử gửi một request bất kì hai lần, ta thấy response header thay đổi như sau `X-Cache: miss` -> `X-Cache: hit` => Server có sử dụng cache, và bên cạnh đó còn dựa vào request header `X-Forwarded-Host` để tạo ra các resource 1 cách tự động, như trong hình dưới thuộc tính `src` của tag `script` được thay đổi dựa trên header này. ![](https://i.imgur.com/2gAjYjC.png) Setup lại exploit server ![](https://i.imgur.com/H3ku1qT.png) Send request 2 lần để poison cache ![](https://i.imgur.com/8EwhWE5.png) Kết quả ![](https://i.imgur.com/bdkoyxc.png) ## Lab: Web cache poisoning with an unkeyed cookie ```! This lab is vulnerable to web cache poisoning because cookies aren't included in the cache key. An unsuspecting user regularly visits the site's home page. To solve this lab, poison the cache with a response that executes alert(1) in the visitor's browser. ``` Ta thấy cookie `fehost` được refect trong field `frontend` của biến `data`. ![](https://i.imgur.com/UasC0BP.png) Thử break tag script và xss: ![](https://i.imgur.com/1m6qsvd.png) kết quả: ![](https://i.imgur.com/iysqCsR.png) Chỉnh request line lại thành `GET /` và send request 2 lần ![](https://i.imgur.com/MvaOuBq.png) Solve bài lab ![](https://i.imgur.com/HnST3XS.png) ## Lab: Web cache poisoning with multiple headers ```! This lab contains a web cache poisoning vulnerability that is only exploitable when you use multiple headers to craft a malicious request. A user visits the home page roughly once a minute. To solve this lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser. ``` Ban đầu thử set giá trị bất kì cho header `X-Forwarded-Host` thì nhận thấy k có sự thay đổi nào trong respone. Sau một hồi fuzzing, phát hiện ra nếu set giá trị cho h`X-Forwarded-Scheme` là `http` thì server response với status 302 đến trang https ![](https://i.imgur.com/EDHE8fC.png) Và nếu ta set hai giá trị này đồng thời đối với request line là `/resources/js/tracking.js` thì ![](https://i.imgur.com/Ir6ooNE.png) -> XSS thành công và solve bài lab ![](https://i.imgur.com/5VVcha8.png) ![](https://i.imgur.com/YE7juO9.png) ## Lab: Targeted web cache poisoning using an unknown header ```! This lab is vulnerable to web cache poisoning. A victim user will view any comments that you post. To solve this lab, you need to poison the cache with a response that executes alert(document.cookie) in the visitor's browser. However, you also need to make sure that the response is served to the specific subset of users to which the intended victim belongs. ``` Trong trang comment, view source ta thấy có sử dụng domPurify để sanitize html input tuy nhiên version này khá cũ ![](https://i.imgur.com/Wld7JQK.png) Và từ post [này](https://portswigger.net/research/bypassing-dompurify-again-with-mutation-xss) có thể bypass bằng payload sau: ``` <math><mtext><table><mglyph><style><!--</style><img title="--&gt;&lt;img src=1 onerror=alert(1)&gt;"> ``` Kết quả: ![](https://i.imgur.com/DKXB8hJ.png) Dựa theo yêu cầu bài lab, tiếp đến ta cần tìm User-Agent của victim => XSS redirect đến exploit server và check Access log ![](https://i.imgur.com/OdfejqC.png) Chỉnh lại payload XSS alert(document.domain) theo như yêu cầu ![](https://i.imgur.com/RXTqEq0.png) Poisoning cache attack các request với user-agent của victim: ![](https://i.imgur.com/noRwD26.png) Kết quả: ![](https://i.imgur.com/uo2LUJx.png) ## Lab: Web cache poisoning via an unkeyed query string ```! This lab is vulnerable to web cache poisoning because the query string is unkeyed. A user regularly visits this site's home page using Chrome. To solve the lab, poison the home page with a response that executes alert(1) in the victim's browser. ``` ![](https://i.imgur.com/WqPP1kn.png) ![](https://i.imgur.com/TIsaTz4.png) Từ hai hình trên có thể thấy toàn bộ query string của ta bị exclude và không được coi là cache key, nhưng response vẫn bao gồm chúng vì vậy có thể khai thác bằng cách chờ response trong cache expire và poison cache với payload xss thông qua query string ![](https://i.imgur.com/BrJ0lf9.png) Solve bài lab ![](https://i.imgur.com/4ZTNn5D.png) ## Lab: Web cache poisoning via an unkeyed query parameter ```! This lab is vulnerable to web cache poisoning because it excludes a certain parameter from the cache key. A user regularly visits this site's home page using Chrome. To solve the lab, poison the cache with a response that executes alert(1) in the victim's browser. ``` Dùng header `Pragma: x-get-cache-key`, ta thấy cache key bao gồm các query parameter ![](https://i.imgur.com/VvWCuYN.png) Tuy nhiên đối với param `utm_content` thì lại không được đưa vào cache key ![](https://i.imgur.com/YcecArn.png) Và một điều quan trọng nữa đó là query string sẽ được reflect hoàn toàn vào response: ![](https://i.imgur.com/mnPCXvx.png) Vậy chỉnh lại query string và exploit thôi ![](https://i.imgur.com/Y2H4FYS.png) kết quả ![](https://i.imgur.com/uj5qFyB.png) ## Lab: Parameter cloaking ```! This lab is vulnerable to web cache poisoning because it excludes a certain parameter from the cache key. There is also inconsistent parameter parsing between the cache and the back-end. A user regularly visits this site's home page using Chrome. To solve the lab, use the parameter cloaking technique to poison the cache with a response that executes alert(1) in the victim's browser. ``` ![a](https://i.imgur.com/D7osqj9.png) ![a](https://i.imgur.com/6ZaVLDN.png) Từ hai hình trên có thể thấy được các param sẽ được include trong cache key ngoại trừ utm_content Tiếp theo, dựa vào lí thuyết ta send request sau ![](https://i.imgur.com/FLZgTsW.png) ``` ?callback=setCountryCookie&utm_content=123;callback=alert(1)// ``` - cache server sẽ thấy chỉ có một cache key là `/js/geolocate.js?callback=setCountryCookie$$` (xem `utm_content=...` như một param cần được exclude) - tuy nhiên bên phía backend (maybe xài rails) parse query string và thấy có tới tận 3 param `callback`, `utm_content` và `callback` -> chỉ giá trị của `callback` cuối cùng được lấy Kết quả ![](https://i.imgur.com/qZNPtG3.png) ## Lab: Web cache poisoning via a fat GET request ```! This lab is vulnerable to web cache poisoning. It accepts GET requests that have a body, but does not include the body in the cache key. A user regularly visits this site's home page using Chrome. To solve the lab, poison the cache with a response that executes alert(1) in the victim's browser. ``` Vẫn ở endpoint JSONP cũ, ta thử thêm body cho GET request như sau và nhận thấy, body k được đưa vào cache key và đồng thời server sử dụng giá trị trong body để trả về response: ![](https://i.imgur.com/0lcD6L1.png) Solve ![](https://i.imgur.com/ESbJt0J.png) ## Lab: URL normalization ```! This lab contains an XSS vulnerability that is not directly exploitable due to browser URL-encoding. To solve the lab, take advantage of the cache's normalization process to exploit this vulnerability. Find the XSS vulnerability and inject a payload that will execute alert(1) in the victim's browser. Then, deliver the malicious URL to the victim. ``` Thử truy cập đến đường dẫn không tồn tại trên server ![](https://i.imgur.com/e3KoSmF.png) -> Có thể khai thác xss ở đây. Đề bài cho ta gửi link đến victim, tuy nhiên nếu chỉ đơn thuần gửi gửi link là `https://0a7300b70472dc6bc2770dc3005b005d.web-security-academy.net/abcd<script>alert(1)</script>` thì sẽ chỉ nhận lại chuỗi đã bị url encode bởi trình duyệt: ![](https://i.imgur.com/xiuUkQD.png) Vì vậy ta cần lợi dụng cơ chế normalize cache key của cache server. Poison cache với burp repeater ![](https://i.imgur.com/LR05yM3.png) Và sau đó XSS bằng browser ![](https://i.imgur.com/GbuTzwT.png) Kết quả ![](https://i.imgur.com/YowNMrR.png) ## Lab: Cache key injection ```! This lab contains multiple independent vulnerabilities, including cache key injection. A user regularly visits this site's home page using Chrome. To solve the lab, combine the vulnerabilities to execute alert(1) in the victim's browser. Note that you will need to make use of the Pragma: x-get-cache-key header in order to solve this lab. ``` `GET /login?lang=en` sẽ redirect đến `/login/?lang=en` ![](https://i.imgur.com/Vr4TCMV.png) Và ở request này, ta thấy server import file js với `src='/js/localize.js?lang=en&cors=0'` ![](https://i.imgur.com/oEArYlm.png) Và request ở `GET /login?lang=en` + `GET /js/localize.js?lang=en&cors=0` đều được cache Bên cạnh đó `Origin` header cũng nằm trong cache key, và ta có thể inject vào header này để chỉnh response trả về nếu `cors=1` ![](https://i.imgur.com/paY6UbB.png) Poison cache và sau đó gửi một request line như sau để lấy response lưu trong cache ![](https://i.imgur.com/AxyBXbw.png) Vậy việc còn lại chỉ là cần làm cho thuộc tính `src` của `script` trỏ đến `/js/localize.js?lang=en&cors=1&x=bla$$origin=x%0d%0aContent-Length:%208%0d%0a%0d%0aalert(1)$$` Tiếp tục fuzzing, ta thấy `utm_content` vẫn sẽ không được include vào cache key ![](https://i.imgur.com/mmLpDMf.png) Vậy ta chỉnh lại injected cache key để tí nữa có thể poinson cache ở `GET /login?lang=en` ![](https://i.imgur.com/TdmdKbN.png) ![](https://i.imgur.com/T6dekHd.png) Poin cache ở `/loign`, ở đây cần dùng `?utm_content` bởi vì server chỉ lấy param `lang` từ request và nhét vào thuộc tính `src` ở dạng `<lang_param_value>&cors=0`, nên nếu bỏ đi trường này thì request line sẽ bị cắt bỏ. ![](https://i.imgur.com/8koquj8.png) (`%23` ở cuối để "cắt" đi `&cors=0` - [HTTP parameter pollution](https://owasp.org/www-pdf-archive/AppsecEU09_CarettoniDiPaola_v0.8.pdf) ) Kết quả ![](https://i.imgur.com/5yxPGM1.png) ## Lab: Internal cache poisoning ```! This lab is vulnerable to web cache poisoning. It uses multiple layers of caching. A user regularly visits this site's home page using Chrome. To solve the lab, poison the internal cache so that the home page executes alert(document.cookie) in the victim's browser. ``` Dùng param miner, ta tìm được header `X-Forwared-Host` là đặc biệt ![](https://i.imgur.com/wmxyZWn.png) Ở đây để ý nếu ta send `GET /` thì giá trị của header được reflect trong response - khi fetch `analytis.js` tuy nhiên đối với `geolocate.js` thì vẫn giữ nguyên => chúng được cache bởi hai fragment khác nhau. Tiếp tục send request, lúc này đã thành công thay đổi được hostname ứng với `geolocate.js` ![](https://i.imgur.com/nxVtPbR.png) Thử bỏ đi header này và send lại request ![](https://i.imgur.com/BtD6Bnl.png) -> Rút ra kết luận rằng `X-Forwared-Host` là một keyed input đối với external cache, nhưng lại là một unkeyed input đối với internal cache. Setup exploit server ![](https://i.imgur.com/kPRRzbh.png) Kết quả ![](https://i.imgur.com/x8ryJAh.png) ## Lab: Web cache poisoning to exploit a DOM vulnerability via a cache with strict cacheability criteria ```! This lab contains a DOM-based vulnerability that can be exploited as part of a web cache poisoning attack. A user visits the home page roughly once a minute. Note that the cache used by this lab has stricter criteria for deciding which responses are cacheable, so you will need to study the cache behavior closely. To solve the lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser. ``` Ta thấy ở trang home có gọi hàm initGeoLocate với tham số thứ nhất được concat bởi data.hót ![](https://i.imgur.com/SsWOioW.png) Nội dung hàm này như sau: fetch đến jsonUrl, lấy response -> parse json và đưa vào `innerHTML` của thẻ div để render => sink DOM based XSS tại đây ![](https://i.imgur.com/8CLUt3s.png) Poison giá trị của host trong `data` thành exploit server ![](https://i.imgur.com/Z33tVjT.png) Setup exploit server ![](https://i.imgur.com/5GMQ6hy.png) kết quả ![](https://i.imgur.com/2EribG0.png) ## Lab: Combining web cache poisoning vulnerabilities ```! This lab is susceptible to web cache poisoning, but only if you construct a complex exploit chain. A user visits the home page roughly once a minute and their language is set to English. To solve this lab, poison the cache with a response that executes alert(document.cookie) in the visitor's browser. ``` Request tại home page như sau: ![](https://i.imgur.com/D9MgpsJ.png) Ta thấy về mặt logic thì vẫn tương tự như bài lab trước. Trường Cookie được dùng để chỉ định language của trang web và sử dụng hàm `initTranslations` để translate Hàm này được định nghĩa như sau: ```js= function initTranslations(jsonUrl) { const lang = document.cookie.split(';') .map(c => c.trim().split('=')) .filter(p => p[0] === 'lang') .map(p => p[1]) .find(() => true); const translate = (dict, el) => { for (const k in dict) { if (el.innerHTML === k) { el.innerHTML = dict[k]; } else { el.childNodes.forEach(el_ => translate(dict, el_)); } } } fetch(jsonUrl) .then(r => r.json()) .then(j => { const select = document.getElementById('lang-select'); if (select) { for (const code in j) { const name = j[code].name; const el = document.createElement("option"); el.setAttribute("value", code); el.innerText = name; select.appendChild(el); if (code === lang) { select.selectedIndex = select.childElementCount - 1; } } } lang in j && lang.toLowerCase() !== 'en' && j[lang].translations && translate(j[lang].translations, document.getElementsByClassName('maincontainer')[0]); }); } ``` Và file json: ![](https://i.imgur.com/J1WZ8IU.png) Cookie language được set thông qua endpoint `GET /setlang/en?` và có thể rewrite bằng `X-Original-Url` ![](https://i.imgur.com/KiXEZys.png) Vậy ý tưởng sẽ là poison cache ở `GET /` thành `GET /setlang/es`, lúc này victim access đến home page sẽ bị redirect đến `/?localized=1`, và tiếp tục poison `GET /?localized=1` với `X-Forwarded-Host` để trỏ đến exploit server. Setup exploit server ![](https://i.imgur.com/KZPQipa.png) *(Vì trong đoạn mã js trên ta cần `lang` phải khác `en` => có thể dùng `es`)* Poison `GET /` thành `GET /setlang/es` ![](https://i.imgur.com/ci8uhy6.png) vì response bao gồm set-cookie header [nên](https://cdnsun.com/knowledgebase/cdn-static/setting-a-set-cookie-ignore-or-accept) response này không được cache, thay vào đó ta có thể làm như sau ![](https://i.imgur.com/3FQ2heL.png) *(chỉnh giá trị của `X-Original-Url` lại thành `/setlang\es` -> redirect đến `/setlang/es`)* Poison `GET ?localized=1` ![](https://i.imgur.com/a0p4IBp.png) Solve bài lab ![](https://i.imgur.com/6ARapbv.png) ###### tags: `portswigger`

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully