Cross Site Scripting Tutorial

HacksLand | The computer science playground

Posted by Thilan Dissanayaka on Aug 12, 2019

Hello and welcome all. In our web application hacking tutorials now we have learned about SQLI and RCE. So today we are going to add one more vulnerability to that list. Yes this is a great and awesome thing . we call it XSS. Hear we have a simple website.Actually it does nothing other than getting name as an input from user and print it back to screen.

<html>
<title>XSS Tutorial - HacksLand</title>
<body>
  <h2>XSS Tutorial - HacksLand</h2>

  <form action = "xss.php" method = "GET" >
   <input type = "text" name = "name" ><br>
   <button type = "submit"><br>
  </form>
</body>
</html>
I think you can understand how front-end application is look like. HTML form takes input from user and make a HTTP request to xss.php via GET method.   Any way hear is the PHP code which handle this processes.
<?php

if( $_GET[ 'name' ] != NULL ) {
    $name = $_GET['name']
    
    echo '<pre>Hello ' . $name . '</pre>';
}

?>
You can see what is going under the hood. In PHP we can fetch HTTP parameters in this way.
$_GET[ 'name' ]
After that input is echoed with a <pre> element. Did you notice that this script don't check what type of data is being submitting.It happily gives out whatever user has typed. Let's take an example .Think I give an input as Thilan Dissanayaka . Now what happens? Output from php code will be.
<pre> Hello Thilan Danushka</pre>
How it looks like in the graphically ? This is the time that things getting interesting. Now think what if I enter following?
<script>alert("XSS")</script>
Since our PHP code does not check the input it will echo back this string as normal inside of a <pre> element.   Final output is hear.
<pre> Hello <script>alert("XSS")</script></pre>
  But when our browser see <script> and </script> it thinks that there is a java script and I should execute it as a script. So what ever code inside the <script> tag will get executed. So guys this is how XSS working.In next tutorials  of this serious we are going to see what is SOP(Same Origin Policy) and how its related to xss, How to bypass XSS filters What's DOM (Document Object Model) etc.Also we need to know what we can do with this XSS ataack and how we can secure web applications from XSS. So wait and watch . Thank you for reading.

Hi, I'm Thilan. An engineering student from SriLanka. I love to code with Python, JavaScript PHP and C.

Also read

Aug 20
CIA law of cryptography

Today I selected an important topic in cryptography. We can call or as the base of cryptography.....

Sep 09
SQL Injection Explained

Hello all, I hope you know how to do a SQL injection and have used it .In this tutorial we are....

May 04
C programming strings

In our "Manipulating data with C" article we saw how we can store data in memory. We used the....

Comments