Cross Site Scripting
Cross-Site Scripting (XSS) is a common web application security vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. XSS occurs when an application fails to properly sanitize user-supplied input, allowing attackers to execute scripts in the context of a victim's browser. There are several types of XSS attacks, including reflected XSS, stored XSS, and DOM-based XSS.
1. Reflected XSS: Reflected XSS occurs when the malicious script is injected into the web application and then reflected back to the user by the server. The injected script is included in the response from the server and executed in the victim's browser. Reflected XSS attacks often involve sending a link containing the malicious payload to the victim, who then unknowingly executes the script when clicking on the link.
Example of Reflected XSS: Consider a search form on a website where the user input is directly echoed back in the response without proper validation:
If an attacker enters a malicious script in the search query parameter, such as <script>alert('XSS')</script>
, and the server echoes it back in the response, the script will be executed in the victim's browser when they view the search results.
2. Stored XSS: Stored XSS occurs when the malicious script is stored on the server, such as in a database, and then executed when other users access the affected page. Attackers typically exploit vulnerabilities in user input fields, comment sections, or message boards to store the malicious script on the server. When other users view the page containing the stored script, it is executed in their browsers.
Example of Stored XSS: Consider a web application with a comment section where users can submit comments:
If an attacker submits a comment containing a malicious script, such as <script>alert('XSS')</script>
, and the server stores it in the database without proper validation, the script will be executed for all users who view the comment section.
3. DOM-based XSS: DOM-based XSS occurs when the malicious script is executed in the victim's browser based on manipulation of the Document Object Model (DOM). Unlike reflected and stored XSS, the payload is not sent to the server but is instead processed client-side. Attackers exploit vulnerabilities in client-side scripts or user-controlled data within the DOM to execute the malicious script.
Example of DOM-based XSS: Consider a web application that uses JavaScript to dynamically update the page content based on user input:
If the application does not properly sanitize the query
parameter before updating the DOM, an attacker could craft a URL with a malicious script, such as http://example.com/search?query=<script>alert('XSS')</script>
, which would be executed in the victim's browser when they view the search results.
Prevention of XSS Attacks:
To prevent XSS attacks, web developers should implement the following best practices:
Input Validation and Sanitization: Always validate and sanitize user input before processing it. Use appropriate sanitization functions or libraries to remove or escape potentially malicious characters.
Output Encoding: Encode user input before outputting it to prevent it from being interpreted as HTML or JavaScript code. Use output encoding functions such as
htmlspecialchars()
or frameworks that automatically perform output encoding.Content Security Policy (CSP): Implement a Content Security Policy to restrict the types of content that can be executed on the page, such as scripts, stylesheets, and inline event handlers.
Use HTTPOnly and Secure Cookies: Set the HTTPOnly and Secure flags on cookies to prevent them from being accessed by client-side scripts and transmitted over insecure connections.
Regular Security Audits: Conduct regular security audits and penetration testing to identify and remediate XSS vulnerabilities and other security issues proactively.
Steal target session using xss
Start Netcat as listener
Malicious javascript
One-liner script
Result
Copy PHPSESID and paste it on your browser using cookie editor or developer tools.
Last updated