POSTED BY SUNNY JAAN[SHOAIB]
Cross Site Scripting(XSS) Complete Tutorial for Beginners~ Web Application Vulnerability
What is XSS?
Cross Site Scripting also known as XSS, is one of the most common web appliction vulnerability that allows an attacker to run his own client side scripts(especially Javascript) into web pages viewed by other users.
In a typical XSS attack, a hacker inject his malicious javascript code in the legitimate website . When a user visit the specially-crafted link , it will execute the malicious javascript. A successfully exploited XSS vulnerability will allow attackers to do phishing attacks, steal accounts and even worms.
In a typical XSS attack, a hacker inject his malicious javascript code in the legitimate website . When a user visit the specially-crafted link , it will execute the malicious javascript. A successfully exploited XSS vulnerability will allow attackers to do phishing attacks, steal accounts and even worms.
Quote:Example :Let us imagine, a hacker has discovered XSS vulnerability in Gmail and inject malicious script. When a user visit the site, it will execute the malicious script. The malicious code can be used to redirect users to fake gmail page or capture cookies. Using this stolen cookies, he can login into your account and change password.It will be easy to understand XSS , if you have the following prerequisite:
- Strong Knowledge in HTML,javascript(Reference).
- Basic Knowledge in HTTP client-Server Architecure(Reference)
- [optional]Basic Knowledge about server side programming(php,asp,jsp)
XSS Attack:
Step 1: Finding Vulnerable Website
Hackers use google dork for finding the vulnerable sites for instance "?search= or ".php?q= . 1337 target specific sites instead of using google search. If you are going to test your own site, you have to check every page in your site for the vulnerability.
Step 2: Testing the Vulnerability:
First of all, we have to find a input field so that we can inject our own script, for example: search box, username,password or any other input fields.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipSGh8QKK5K0lxIhzDOuukbuqFf57q1VZ5DnKmNcfg98SJP_AcDdPoZbNMvonGa1R_ZGY722Aj0XfEVNVziTOa1u6wI8D4l56xREq1Qct4ExSFAciMVt0X1-oZEpY6uXi4sNbFQs1-OCY/s1600/search+box.jpg"imageanchor=1][/url]
First of all, we have to find a input field so that we can inject our own script, for example: search box, username,password or any other input fields.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEipSGh8QKK5K0lxIhzDOuukbuqFf57q1VZ5DnKmNcfg98SJP_AcDdPoZbNMvonGa1R_ZGY722Aj0XfEVNVziTOa1u6wI8D4l56xREq1Qct4ExSFAciMVt0X1-oZEpY6uXi4sNbFQs1-OCY/s1600/search+box.jpg"imageanchor=1][/url]
Test 1 :
Once we found the input field, let us try to put some string inside the field, for instance let me input"BTS". It will display the result .
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtmEdb2aw3w8j6KHP8X54LIwz1pgVK9Me2FwH_X4rr1D1k6FqDvrwVuS1BURyqjOKOL4JgGJ0igEjzYgYyYOqBFPjkMSN9HNP_aiPhlKQv56qQvFH1nsxsvfWoxnTOxXsFIzbKLtANlk4/s1600/XSS_input.gif" imageanchor=1][/url]
Once we found the input field, let us try to put some string inside the field, for instance let me input"BTS". It will display the result .
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhtmEdb2aw3w8j6KHP8X54LIwz1pgVK9Me2FwH_X4rr1D1k6FqDvrwVuS1BURyqjOKOL4JgGJ0igEjzYgYyYOqBFPjkMSN9HNP_aiPhlKQv56qQvFH1nsxsvfWoxnTOxXsFIzbKLtANlk4/s1600/XSS_input.gif" imageanchor=1][/url]
Now right click on the page and select view source. search for the string "BTS" which we entered in the input field. Note the location where the input is placed.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBc3tFZ97IvajTfAM3WCWn-jdroKLPL2CS3TXsrFSDtLTHMuT8Yaqy2xcwNmXYpRyybxPnsEL4zEl0zo3_j-8qQ20YcuULRV9fGgTUw2qBCNA9-eTzeuLI4HaTHK_y6S2xXudBJHlHO2o/s1600/BTS_XSS.gif" imageanchor=1][/url]
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjBc3tFZ97IvajTfAM3WCWn-jdroKLPL2CS3TXsrFSDtLTHMuT8Yaqy2xcwNmXYpRyybxPnsEL4zEl0zo3_j-8qQ20YcuULRV9fGgTUw2qBCNA9-eTzeuLI4HaTHK_y6S2xXudBJHlHO2o/s1600/BTS_XSS.gif" imageanchor=1][/url]
Test 2:
Now we are going to check whether the server sanitize our input or not. In order to do this , let usinput the <script> tag inside the input field.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1QKenme70HysIgN1vh1KAOJ10o_dJOOdruw4f41QURz30zreBcD7AWwUln0R17YjgGIErB-nucnhGSZlfFnShQdJ_5XNKZ0oHo2hQF4QxfbZZZd9UtjdjsoepBnJ0dnoo6oS_SwetOSw/s1600/Xss_script_tag.gif" imageanchor=1][/url]
View the source of the page . Find the location where input displayed place in previous test.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVtGQIDw7cUT8XL-Dqa5EMjwfePJL5P8DqdIRuZRAMCxKe7LLZC97Z0RrIPGTkvTPk_2xiL4nZmwkLmrS_jOUEoOL0oFH-qANEG2kWKtwl5p0Ibt64XH9ESdK5pw6F_7bNYtTczlYBAkM/s1600/xss-SUCCESS_Noparsing.gif" imageanchor=1][/url]
Thank god, our code is not being sanitized by the server and the code is just same as what we entered in the field. If the server sanitize our input, the code may look like this &lt;script&gt;. This indicates that the website vulnerable to XSS attack and we can execute our own scripts .
Step 3: Exploiting the vulnerability
Now we know the site is somewhat vulnerable to XSS attack. But let us make sure whether the site is completely vulnerable to this attack by injecting a full javascript code.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWc0l0fJ7yR265hv69k4WY-kWlYwK6QYskcym9EGsTROitbaoZqMrJFT6n1DgS2Z0_oYlvwvwIsfbWbUnx4oVFjhoIJWbUuMf7_DpK4tf5cZttGpxwCB5ZVkSCQ1ndqcQS6Jk-vMd6Cq0/s1600/injecting-XSS.gif" imageanchor=1][/url]
Now it will display pop-up box with 'BTS' string. Finally, we successfully exploit the XSS . By extending the code with malicious script, a hacker can do steal cookies or deface the site and more.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd_oeR_CP-WL8O-fVo_H-lvxv43y4Y1xoLeN3iQceEGSz3UpbgZ5uLtL4rtQwWREx74GhqlEwNQ2QWgxJuM80yRU81b3JczQ7FEKvRMm2GNAgV2jbtNgW6yUJ3QmDa7qKZmTlzLbqhiZ4/s1600/alert-box-xss.gif" imageanchor=1][/url]
Types of XSS Based on persisting capability:
Based one Persistence capability, we can categorize the XSS attack into two types namely Persistent and Non-Persistent.
Persistent XSS:
The Persistent or Stored XSS attack occurs when the malicious code submitted by attacker is saved by the server in the database, and then permanently it will be run in the normal page.
For Example:
Many websites host a support forum where registered users can ask their doubts by posting message , which are stored in the database. Let us imagine , An attacker post a message containing malicious javascript code instead. If the server fail to sanitize the input provided, it results in execution of injected script. The code will be executed whenever a user try to read the post. If suppose the injected code is cookie stealing code, then it will steal cookie of users who read the post. Using the cookie, attacker can take control of your account.
Non-Persistent XSS:
Non-Persistent XSS, also referred as Reflected XSS , is the most common type of XSS found now a days. In this type of attack, the injected code will be send to the server via HTTPrequest. The server embedd the input with the html file and return the file(HTTPResponse) to browser. When the browser executes the HTML file, it also execute the embedded script. This kind of XSS vulnerability frequently occur in search fields.
Example:
Let us consider a project hosting website. To find our favorite project, we will just input the related-word in the search box . When searching is finished, it will display a message like this "search results for yourword " . If the server fail to sanitize the input properly, it will results in execution of injected script.
In case of reflected XSS attacks, attacker will send the specially-crafted link to victims and trick them into click the link. When user click the link, the browser will send the injected code to server, the server reflects the attack back to the users' browser. The browser then executes the code .
In addition to these types, there is also third type of attack called DOM Based XSS attack, i will explain about this attack in later posts.
What can an attacker do with this Vulnerability?
Now we are going to check whether the server sanitize our input or not. In order to do this , let usinput the <script> tag inside the input field.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi1QKenme70HysIgN1vh1KAOJ10o_dJOOdruw4f41QURz30zreBcD7AWwUln0R17YjgGIErB-nucnhGSZlfFnShQdJ_5XNKZ0oHo2hQF4QxfbZZZd9UtjdjsoepBnJ0dnoo6oS_SwetOSw/s1600/Xss_script_tag.gif" imageanchor=1][/url]
View the source of the page . Find the location where input displayed place in previous test.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVtGQIDw7cUT8XL-Dqa5EMjwfePJL5P8DqdIRuZRAMCxKe7LLZC97Z0RrIPGTkvTPk_2xiL4nZmwkLmrS_jOUEoOL0oFH-qANEG2kWKtwl5p0Ibt64XH9ESdK5pw6F_7bNYtTczlYBAkM/s1600/xss-SUCCESS_Noparsing.gif" imageanchor=1][/url]
Thank god, our code is not being sanitized by the server and the code is just same as what we entered in the field. If the server sanitize our input, the code may look like this &lt;script&gt;. This indicates that the website vulnerable to XSS attack and we can execute our own scripts .
Step 3: Exploiting the vulnerability
Now we know the site is somewhat vulnerable to XSS attack. But let us make sure whether the site is completely vulnerable to this attack by injecting a full javascript code.
Code:
For instance, let us input<script>alert('BTS')</script> .
Now it will display pop-up box with 'BTS' string. Finally, we successfully exploit the XSS . By extending the code with malicious script, a hacker can do steal cookies or deface the site and more.
[url=https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd_oeR_CP-WL8O-fVo_H-lvxv43y4Y1xoLeN3iQceEGSz3UpbgZ5uLtL4rtQwWREx74GhqlEwNQ2QWgxJuM80yRU81b3JczQ7FEKvRMm2GNAgV2jbtNgW6yUJ3QmDa7qKZmTlzLbqhiZ4/s1600/alert-box-xss.gif" imageanchor=1][/url]
Types of XSS Based on persisting capability:
Based one Persistence capability, we can categorize the XSS attack into two types namely Persistent and Non-Persistent.
Persistent XSS:
The Persistent or Stored XSS attack occurs when the malicious code submitted by attacker is saved by the server in the database, and then permanently it will be run in the normal page.
For Example:
Many websites host a support forum where registered users can ask their doubts by posting message , which are stored in the database. Let us imagine , An attacker post a message containing malicious javascript code instead. If the server fail to sanitize the input provided, it results in execution of injected script. The code will be executed whenever a user try to read the post. If suppose the injected code is cookie stealing code, then it will steal cookie of users who read the post. Using the cookie, attacker can take control of your account.
Non-Persistent XSS:
Non-Persistent XSS, also referred as Reflected XSS , is the most common type of XSS found now a days. In this type of attack, the injected code will be send to the server via HTTPrequest. The server embedd the input with the html file and return the file(HTTPResponse) to browser. When the browser executes the HTML file, it also execute the embedded script. This kind of XSS vulnerability frequently occur in search fields.
Example:
Let us consider a project hosting website. To find our favorite project, we will just input the related-word in the search box . When searching is finished, it will display a message like this "search results for yourword " . If the server fail to sanitize the input properly, it will results in execution of injected script.
In case of reflected XSS attacks, attacker will send the specially-crafted link to victims and trick them into click the link. When user click the link, the browser will send the injected code to server, the server reflects the attack back to the users' browser. The browser then executes the code .
In addition to these types, there is also third type of attack called DOM Based XSS attack, i will explain about this attack in later posts.
What can an attacker do with this Vulnerability?
- Stealing the Identity and Confidential Data(credit card details).
- Bypassing restriction in websites.
- Session Hijacking(Stealing session)
- Malware Attack
- Website Defacement
- Denial of Service attacks(Dos)
1 comments :
Write commentsIf you ever want to change or up your university grades contact cybergolden hacker he'll get it done and show a proof of work done before payment. He's efficient, reliable and affordable. He can also perform all sorts of hacks including text, whatsapp, password decrypt,hack any mobile phone, Escape Bancruptcy, Delete Criminal Records and the rest
ReplyEmail: cybergoldenhacker at gmail dot com
Emoticon Emoticon