SlideShare a Scribd company logo
Cross Site Scripting
Detection and Prevention
~Aman Kumar
Cross Site Scripting
Detection and
Prevention
What is cross-site scripting?
• Cross-Site Scripting (referred to as XSS) is a type of web application attack
where malicious client-side script is injected into the application output and
subsequently executed by the user’s browser.
• It can be used to take over a user’s browser in a variety of ways
2
Why should I care about cross-site scripting?
•There was a time not too long ago when XSS was considered a low-risk
type of security issue, because when compared to a server-side exploit, it
seemed relatively low.
•As other issues like PHP remote file inclusions have become harder to
exploit, XSS attacks have increased in prominence and sophistication.
3
Who’s affected by cross-site scripting?
Everyone. No, really – almost every site you can think of has had XSS problems
at one time or another (and probably still does)
Don’t believe me?
• Universal XSS in Internet Explorer (2015) [1]
• Tweetdeck (2014) [2]
• PayPal (2013) – BONUS: discovered by a 17 year old kid [3]
• Google Finance (2013) [4]
• 25 “Verasign-secured” online stores (2012) [5]
• McAfee (2011) [6]
• Visa (2010) [7]
4
5
Some sites you might recognize
http://www.xssed.com/files/image/News/paypalevsslxss.PNG
Object Placeholder
6www.rackspace.com
Some sites you might recognize
http://3.bp.blogspot.com/-IpLMWEVPnRc/UmYV_19hnNI/AAAAAAAADfc/caJdmBEsyaE/s1600/1.png
Object Placeholder
7
Some sites you might recognize
https://isc.sans.edu/diaryimages/youtube.png
Boooooring…
The classic proof-of-concept for XSS is a little alert box with some arbitrary text in
it, or a picture of something silly. This doesn’t seem nearly dangerous enough to
warrant concern.
What else you got?
8
•Steal cookies
•Play a sound
•Get user-agent string
•See enabled plugins (e.g. Chrome PDF viewer, Java, etc.)
9
Basic Client-side Attacks
•Man-in-the-browser
•Forge user requests
•Get form values / HTML contents
•Fake notifications (Chrome plugin bar, LastPass login, etc.)
•Tabnabbing
10
More Advanced Client-Side Attacks
www.rackspace.com
•Man-in-the-browser
•Forge user requests
•Get form values / HTML contents
•Fake notifications (Chrome plugin bar, LastPass login, etc.)
•Tabnabbing
11
More Advanced Client-Side Attacks
www.rackspace.com
• Never trust the user
• Never trust the user
• Never trust the user
• Never trust the user
• Never trust the user
• Never trust the user
• Never trust the user
• Never trust the user
So what should I do to prevent XSS?
12
• Almost all client-side script injection comes down to the following characters:
< > ( ) { } [ ] " ' ; / 
• There are various ways to take care of these characters, but it is too context-
dependent to give a one-size-fits-all answer
• The shortest answer is, make sure you’re only getting characters you expect
when a user enters any kind of information - make sure you never display a
user-entered string without properly encoding it.
So what should I do to prevent XSS? (No, really)
13www.rackspace.com
Here’s some sample vulnerable JavaScript.
<html>
<script>
var lol = function () {
var a = document.getElementById('a').value;
document.write(a);
}
</script>
<input type="text" name="a" id="a">
<input type="submit" onclick="lol();">
</html>
14
Examples of XSS in code
Hmm, there’s the problem…
<html>
<script>
var lol = function () {
var a = document.getElementById('a').value;
document.write(a); // Too easy
}
</script>
<input type="text" name="a" id="a">
<input type="submit" onclick="lol();">
</html>
15
Examples of XSS in code
Now for something a little more interesting. Remember, we also have to
remember the third-party libraries you’re using.
Some innocent-looking jQuery code:
$(location.hash) // Wait, that’s it?
16
Examples of XSS in code
But you’re not only securing the code you write, but all the code you used…
$(location.hash) // WHERE’S THE VULNERABLE PART?!
Well, if we’re using jQuery 1.6.1 and we visit the page
http://app/#<img src=/ onerror=alert(1)>
…this will pop up one of those alert boxes [8].
17
Examples of XSS in code
Here are some examples of how to filter HTML characters in a few simple
scenarios in PHP (there should be similar functions in any language; check the
links at the end of the PPT)
$int = intval($_GET['a']); // This will never return anything other than an integer
$str = htmlentities($_GET['b']); // This will encode any character for which there is
// an HTML entity equivalent (e.g. &gt; &lt; &quot;)
// This is NOT always enough! [9]
18
Tips for filtering XSS
Pop quiz! What’s wrong with this PHP code:
echo('<a href="' . htmlentities($_GET['var']) . '">link</a>');
19
Getting around prevention measures
Pop quiz! What’s wrong with this PHP code:
echo('<a href="' . htmlentities($_GET['var']) . '">link</a>');
What if we set $_GET['var'] to javascript:alert(/xss/);
20
Getting around prevention measures
21
• OWASP Links
– Guide to Cross-site Scripting - https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
– XSS Prevention Cheat Sheet - https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
– DOM based XSS Prevention Cheat Sheet - https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet
22
Resources
• [1] http://seclists.org/fulldisclosure/2015/Feb/0
• [2] http://techcrunch.com/2014/06/11/tweetdeck-fixes-xss-vulnerability/
• [3] http://threatpost.com/paypal-site-vulnerable-to-xss-attack
• [4] http://miki.it/blog/2013/7/30/xss-in-google-finance/
• [5] http://nakedsecurity.sophos.com/2012/02/28/verisign-xss-holes/
• [6] http://www.scmagazine.com/mcafee-working-to-fix-xss-information-disclosure-flaws/article/199505/
• [7] http://news.softpedia.com/news/XSS-Weakness-Found-on-Visa-USA-Website-157115.shtml
• [8] http://ma.la/jquery_xss/
• [9] http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
23
References
Thank You

More Related Content

PPTX
Vulnerabilities in modern web applications
PDF
Attacking and defending GraphQL applications: a hands-on approach
PPTX
Cross Site Scripting Defense Presentation
PDF
Web Application Security 101
PDF
Web vulnerabilities
PDF
Hacking and Defending APIs - Red and Blue make Purple.pdf
PPTX
Xss attack
Vulnerabilities in modern web applications
Attacking and defending GraphQL applications: a hands-on approach
Cross Site Scripting Defense Presentation
Web Application Security 101
Web vulnerabilities
Hacking and Defending APIs - Red and Blue make Purple.pdf
Xss attack

What's hot (20)

PDF
OWASP Top 10 - 2017
PPTX
XSS - Do you know EVERYTHING?
PPTX
Cross-Site Scripting (XSS)
PPT
Secure code practices
PPTX
A Forgotten HTTP Invisibility Cloak
PDF
Pentesting GraphQL Applications
PDF
PPTX
Anatomy of business logic vulnerabilities
PPT
Security Exploit of Business Logic Flaws, Business Logic Attacks
PDF
Cross site scripting attacks and defenses
PDF
Bug Bounty - Hackers Job
PPTX
Cross Site Scripting ( XSS)
PDF
Bug Bounty Hunter Methodology - Nullcon 2016
PPTX
Password cracking and brute force
PDF
Api security-testing
PDF
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
PPTX
Cross site scripting
PPTX
Rest API Security
PPTX
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
PPTX
OWASP Top 10 - 2017
XSS - Do you know EVERYTHING?
Cross-Site Scripting (XSS)
Secure code practices
A Forgotten HTTP Invisibility Cloak
Pentesting GraphQL Applications
Anatomy of business logic vulnerabilities
Security Exploit of Business Logic Flaws, Business Logic Attacks
Cross site scripting attacks and defenses
Bug Bounty - Hackers Job
Cross Site Scripting ( XSS)
Bug Bounty Hunter Methodology - Nullcon 2016
Password cracking and brute force
Api security-testing
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
Cross site scripting
Rest API Security
WAF Bypass Techniques - Using HTTP Standard and Web Servers’ Behaviour
Ad

Similar to Cross Site Scripting: Prevention and Detection(XSS) (20)

PDF
Rich Web App Security - Keeping your application safe
PPTX
Website hacking and prevention (All Tools,Topics & Technique )
PPTX
04. xss and encoding
PPTX
Browser Security 101
PDF
Ch 12 Attacking Users - XSS
PDF
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
PDF
Session7-XSS & CSRF
PPT
Andrews whitakrer lecture18-security.ppt
PDF
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
DOCX
Continuing in your role as a human service provider for your local.docx
PDF
The Cross Site Scripting Guide
PPT
Xss talk, attack and defense
PPTX
Web Hacking Series Part 4
PPTX
Understanding dom based xss
PDF
BsidesDelhi 2018: DomGoat - the DOM Security Playground
PDF
PPTX
XSS (Cross Site Scripting)
PDF
Tsc summit #2 - HTTP Header Security
PPT
Owasp Top 10 - Owasp Pune Chapter - January 2008
Rich Web App Security - Keeping your application safe
Website hacking and prevention (All Tools,Topics & Technique )
04. xss and encoding
Browser Security 101
Ch 12 Attacking Users - XSS
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
Session7-XSS & CSRF
Andrews whitakrer lecture18-security.ppt
CNIT 129S: Ch 12: Attacking Users: Cross-Site Scripting
Continuing in your role as a human service provider for your local.docx
The Cross Site Scripting Guide
Xss talk, attack and defense
Web Hacking Series Part 4
Understanding dom based xss
BsidesDelhi 2018: DomGoat - the DOM Security Playground
XSS (Cross Site Scripting)
Tsc summit #2 - HTTP Header Security
Owasp Top 10 - Owasp Pune Chapter - January 2008
Ad

Recently uploaded (20)

PPTX
Welding lecture in detail for understanding
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Sustainable Sites - Green Building Construction
PDF
Well-logging-methods_new................
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
composite construction of structures.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
Welding lecture in detail for understanding
Foundation to blockchain - A guide to Blockchain Tech
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Mechanical Engineering MATERIALS Selection
Sustainable Sites - Green Building Construction
Well-logging-methods_new................
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT-1 - COAL BASED THERMAL POWER PLANTS
composite construction of structures.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
bas. eng. economics group 4 presentation 1.pptx
OOP with Java - Java Introduction (Basics)
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Arduino robotics embedded978-1-4302-3184-4.pdf

Cross Site Scripting: Prevention and Detection(XSS)

  • 1. Cross Site Scripting Detection and Prevention ~Aman Kumar Cross Site Scripting Detection and Prevention
  • 2. What is cross-site scripting? • Cross-Site Scripting (referred to as XSS) is a type of web application attack where malicious client-side script is injected into the application output and subsequently executed by the user’s browser. • It can be used to take over a user’s browser in a variety of ways 2
  • 3. Why should I care about cross-site scripting? •There was a time not too long ago when XSS was considered a low-risk type of security issue, because when compared to a server-side exploit, it seemed relatively low. •As other issues like PHP remote file inclusions have become harder to exploit, XSS attacks have increased in prominence and sophistication. 3
  • 4. Who’s affected by cross-site scripting? Everyone. No, really – almost every site you can think of has had XSS problems at one time or another (and probably still does) Don’t believe me? • Universal XSS in Internet Explorer (2015) [1] • Tweetdeck (2014) [2] • PayPal (2013) – BONUS: discovered by a 17 year old kid [3] • Google Finance (2013) [4] • 25 “Verasign-secured” online stores (2012) [5] • McAfee (2011) [6] • Visa (2010) [7] 4
  • 5. 5 Some sites you might recognize http://www.xssed.com/files/image/News/paypalevsslxss.PNG
  • 6. Object Placeholder 6www.rackspace.com Some sites you might recognize http://3.bp.blogspot.com/-IpLMWEVPnRc/UmYV_19hnNI/AAAAAAAADfc/caJdmBEsyaE/s1600/1.png
  • 7. Object Placeholder 7 Some sites you might recognize https://isc.sans.edu/diaryimages/youtube.png
  • 8. Boooooring… The classic proof-of-concept for XSS is a little alert box with some arbitrary text in it, or a picture of something silly. This doesn’t seem nearly dangerous enough to warrant concern. What else you got? 8
  • 9. •Steal cookies •Play a sound •Get user-agent string •See enabled plugins (e.g. Chrome PDF viewer, Java, etc.) 9 Basic Client-side Attacks
  • 10. •Man-in-the-browser •Forge user requests •Get form values / HTML contents •Fake notifications (Chrome plugin bar, LastPass login, etc.) •Tabnabbing 10 More Advanced Client-Side Attacks www.rackspace.com
  • 11. •Man-in-the-browser •Forge user requests •Get form values / HTML contents •Fake notifications (Chrome plugin bar, LastPass login, etc.) •Tabnabbing 11 More Advanced Client-Side Attacks www.rackspace.com
  • 12. • Never trust the user • Never trust the user • Never trust the user • Never trust the user • Never trust the user • Never trust the user • Never trust the user • Never trust the user So what should I do to prevent XSS? 12
  • 13. • Almost all client-side script injection comes down to the following characters: < > ( ) { } [ ] " ' ; / • There are various ways to take care of these characters, but it is too context- dependent to give a one-size-fits-all answer • The shortest answer is, make sure you’re only getting characters you expect when a user enters any kind of information - make sure you never display a user-entered string without properly encoding it. So what should I do to prevent XSS? (No, really) 13www.rackspace.com
  • 14. Here’s some sample vulnerable JavaScript. <html> <script> var lol = function () { var a = document.getElementById('a').value; document.write(a); } </script> <input type="text" name="a" id="a"> <input type="submit" onclick="lol();"> </html> 14 Examples of XSS in code
  • 15. Hmm, there’s the problem… <html> <script> var lol = function () { var a = document.getElementById('a').value; document.write(a); // Too easy } </script> <input type="text" name="a" id="a"> <input type="submit" onclick="lol();"> </html> 15 Examples of XSS in code
  • 16. Now for something a little more interesting. Remember, we also have to remember the third-party libraries you’re using. Some innocent-looking jQuery code: $(location.hash) // Wait, that’s it? 16 Examples of XSS in code
  • 17. But you’re not only securing the code you write, but all the code you used… $(location.hash) // WHERE’S THE VULNERABLE PART?! Well, if we’re using jQuery 1.6.1 and we visit the page http://app/#<img src=/ onerror=alert(1)> …this will pop up one of those alert boxes [8]. 17 Examples of XSS in code
  • 18. Here are some examples of how to filter HTML characters in a few simple scenarios in PHP (there should be similar functions in any language; check the links at the end of the PPT) $int = intval($_GET['a']); // This will never return anything other than an integer $str = htmlentities($_GET['b']); // This will encode any character for which there is // an HTML entity equivalent (e.g. &gt; &lt; &quot;) // This is NOT always enough! [9] 18 Tips for filtering XSS
  • 19. Pop quiz! What’s wrong with this PHP code: echo('<a href="' . htmlentities($_GET['var']) . '">link</a>'); 19 Getting around prevention measures
  • 20. Pop quiz! What’s wrong with this PHP code: echo('<a href="' . htmlentities($_GET['var']) . '">link</a>'); What if we set $_GET['var'] to javascript:alert(/xss/); 20 Getting around prevention measures
  • 21. 21
  • 22. • OWASP Links – Guide to Cross-site Scripting - https://www.owasp.org/index.php/Cross-site_Scripting_(XSS) – XSS Prevention Cheat Sheet - https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet – DOM based XSS Prevention Cheat Sheet - https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet 22 Resources
  • 23. • [1] http://seclists.org/fulldisclosure/2015/Feb/0 • [2] http://techcrunch.com/2014/06/11/tweetdeck-fixes-xss-vulnerability/ • [3] http://threatpost.com/paypal-site-vulnerable-to-xss-attack • [4] http://miki.it/blog/2013/7/30/xss-in-google-finance/ • [5] http://nakedsecurity.sophos.com/2012/02/28/verisign-xss-holes/ • [6] http://www.scmagazine.com/mcafee-working-to-fix-xss-information-disclosure-flaws/article/199505/ • [7] http://news.softpedia.com/news/XSS-Weakness-Found-on-Visa-USA-Website-157115.shtml • [8] http://ma.la/jquery_xss/ • [9] http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references 23 References