π₯Cross-Site Scripting (XSS)
Complete XSS exploitation guide covering all attack types, payloads, and HTB Academy lab solutions
π Table of Contents
Core Concepts
Overview - XSS fundamentals and impact
Types of XSS - Stored, Reflected, DOM-based
Basic Payloads - Standard testing payloads
Alternative Payloads - Bypass techniques when
<script>is blocked
Attack Techniques
XSS Discovery - Automated and manual detection
Session Hijacking - Cookie theft and account takeover
Credential Harvesting - Phishing attacks via XSS
Blind XSS - Admin panel and hidden XSS exploitation
Practical Labs
HTB Academy Labs - Complete lab solutions and walkthroughs
Troubleshooting - Common issues and fixes
Tools & Resources - Professional XSS testing toolkit
Defense
Prevention - Secure coding and mitigation techniques
Quick Reference
π― Essential XSS Payloads
π XSS Detection Flow
Find input points β Forms, URL params, headers
Test basic payload β
<script>alert(1)</script>Check page source β Look for payload reflection
Verify execution β Confirm JavaScript runs
Exploit discovered XSS β Session hijacking, phishing
π― HTB Academy Coverage
β All XSS Types - Stored, Reflected, DOM-based XSS
β Session Hijacking - Cookie stealing and account takeover
β Phishing Attacks - Credential harvesting via fake forms
β Blind XSS - Admin panel exploitation techniques
β Bypass Techniques - Filter evasion and encoding methods
β Complete Labs - Step-by-step HTB Academy solutions
Overview
Cross-Site Scripting (XSS) is a web application vulnerability that allows attackers to inject malicious scripts into web pages viewed by other users. XSS occurs when user input is not properly sanitized and gets executed as JavaScript code in the victim's browser.
Impact:
Session hijacking - Stealing authentication cookies
Credential theft - Capturing login credentials via fake forms
Data exfiltration - Accessing sensitive information
Website defacement - Modifying page content
Phishing attacks - Redirecting users to malicious sites
Malware distribution - Downloading malicious files
Types of XSS Vulnerabilities
1. Stored XSS (Persistent XSS)
Most Critical Type - The injected payload gets stored in the back-end database and affects all users who visit the page.
Characteristics:
Persistent - Payload remains after page refresh
Wide impact - Affects all users visiting the page
Database storage - Payload stored in backend database
Hard to remove - Requires database cleanup
Example Scenario:
Testing Method:
Submit XSS payload through input form
Refresh page to confirm persistence
Check if other users see the same payload
2. Reflected XSS (Non-Persistent XSS)
Temporary XSS - Input gets processed by back-end server and returned without proper sanitization.
Characteristics:
Non-persistent - Only affects targeted user
Server processing - Input reaches back-end server
URL parameters - Often exploited through GET requests
Temporary messages - Common in error messages
Example Scenario:
Attack Vector:
3. DOM-based XSS (Client-Side XSS)
Client-side processing - Completely processed on the browser through JavaScript, never reaches back-end server.
Characteristics:
Client-side only - Never reaches backend server
JavaScript processing - Uses Document Object Model (DOM)
No HTTP requests - Processing happens in browser
URL fragments - Often uses # parameters
Source and Sink Concept:
Source - JavaScript object that takes user input:
URL parameters
Input fields
Hash fragments
localStorage/sessionStorage
Sink - Function that writes to DOM objects:
Example Vulnerable Code:
DOM XSS Payload:
Basic XSS Testing Payloads
Standard Payloads
Basic Alert Payload:
Cookie Stealing:
Phishing Login Form Injection:
Session Hijacking (Cookie Stealing):
Blind XSS Detection Payloads:
Alternative Payloads
Bypass techniques when
<script>tags are blocked by filters
When <script> is Blocked
<script> is BlockedImage onerror event:
SVG payload:
Input onfocus:
Iframe JavaScript:
Other HTML5 elements:
Advanced Payloads
Event Handlers:
Without Parentheses:
Encoding Bypass Techniques
URL Encoding:
HTML Entities:
Unicode Encoding:
Double Encoding:
Mixed Case:
XSS Discovery Methods
1. Automated Discovery
Open-Source Tools:
Commercial Scanners:
Burp Suite Professional
Nessus
OWASP ZAP
Acunetix
2. Manual Discovery
Testing Approach:
Identify input points - All user inputs, not just forms
Submit test payload - Use basic
<script>alert(1)</script>Analyze response - Check page source for payload
Verify execution - Confirm JavaScript execution
Test variations - Try different payload types
Input Points to Test:
HTML form fields
URL parameters (GET)
HTTP headers (User-Agent, Cookie, Referer)
JSON/XML parameters
File upload fields
Search functionality
3. Code Review
Frontend Code Review:
Backend Code Review:
Common XSS Attack Scenarios
1. Session Hijacking & Cookie Stealing
π Critical Attack: Steal authentication cookies to hijack user sessions without knowing passwords
Overview: Session hijacking allows attackers to steal user authentication cookies through XSS, gaining unauthorized access to victim accounts without knowing their credentials.
Blind XSS Detection
What is Blind XSS? Blind XSS occurs when the vulnerability is triggered on a page we don't have access to (e.g., Admin panels, contact forms, support tickets).
Common Blind XSS Targets:
Contact Forms
Reviews
User Details
Support Tickets
HTTP User-Agent header
Remote Script Loading for Detection:
Blind XSS Detection Payloads:
Complete Session Hijacking Workflow
Step 1: Setup Server for Detection
Step 2: Test Blind XSS Payloads
Step 3: Create Cookie Stealing Script Create script.js:
Step 4: Create Cookie Harvesting Server Create index.php:
Step 5: Deploy Working Payload
Step 6: Collect Stolen Cookies
Step 7: Use Stolen Cookies
Navigate to target login page
Open Firefox Developer Tools (Shift+F9)
Go to Storage tab
Click + to add new cookie
Set Name and Value from stolen cookie
Refresh page to access victim account
Alternative Cookie Stealing Methods
Direct Navigation Method:
Image Loading Method (Stealthy):
Fetch API Method:
XMLHttpRequest Method:
2. Credential Harvesting & Phishing Attack
π£ Social Engineering: Create fake login forms to steal usernames and passwords
Basic Fake Login Form:
Advanced Phishing Attack (HTB Academy Style)
Complete Phishing Payload with Form Removal:
URL Encoded Phishing Payload:
Complete Attack Workflow:
Setup Credential Harvesting Server:
Create index.php for credential capture:
Start PHP listener:
Craft malicious URL (example):
Check captured credentials:
Attack Breakdown:
'>- Escapes from image URL attributedocument.write()- Injects fake login formgetElementById('urlform').remove()- Removes original form to avoid suspicion<!--- Comments out remaining HTML to prevent rendering issuesForm redirects victims back to original site after credential theft
3. Keylogger
JavaScript Keylogger:
4. Page Defacement
Modifying Page Content:
XSS Prevention and Bypass Techniques
Common Filters and Bypasses
Filter: Blocking <script> tags
Filter: Blocking alert()
Filter: Blocking quotes
Filter: Case-sensitive filtering
Filter: Blocking form injection
HTML Context Escaping
Escaping from different contexts:
Tools and Resources
Testing Tools
Session Hijacking Tools
Payload Repositories
PayloadAllTheThings - XSS section
PayloadBox - XSS payloads
OWASP XSS Filter Evasion - Bypass techniques
PortSwigger XSS Cheat Sheet - Browser-specific payloads
Vulnerable Practice Sites
DVWA - Damn Vulnerable Web Application
bWAPP - Buggy Web Application
WebGoat - OWASP WebGoat
XSS Game - Google XSS Challenge
Detection and Mitigation
Security Headers
Secure Coding Practices
HTB Academy Lab Solutions
Question Examples
Cookie Stealing Payload:
DOM XSS with innerHTML:
Reflected XSS in URL parameter:
Phishing Attack (HTB Academy Labs):
Session Hijacking Lab (HTB Academy):
XSS Discovery Exercise Solutions:
XSS Troubleshooting & Common Mistakes
Phishing Attack Issues
Problem: Payload not working
Problem: PWNIP:PWNPO placeholders
Problem: Server not receiving credentials
Problem: Form not appearing
Session Hijacking Issues
Problem: No requests to server during blind XSS testing
Problem: Script.js not loading
Problem: Cookies not being captured
Problem: Cookie injection not working
Common Payload Encoding Issues
URL Encoding Problems:
JavaScript String Escaping:
Debugging XSS Payloads
Step-by-step debugging:
Test basic XSS:
<script>alert(1)</script>Test with URL parameter:
?url=<script>alert(1)</script>Check payload encoding with online tools
Verify server listening:
sudo php -S 0.0.0.0:8080Test credential capture with manual form submission
This XSS guide covers the fundamental concepts and practical techniques from HTB Academy's Cross-Site Scripting module, providing a comprehensive resource for penetration testing and web application security assessment.
Last updated