πŸ”₯Cross-Site Scripting (XSS)

Complete XSS exploitation guide covering all attack types, payloads, and HTB Academy lab solutions

πŸ“š Table of Contents

Core Concepts

Attack Techniques

Practical Labs

Defense

  • Prevention - Secure coding and mitigation techniques


Quick Reference

🎯 Essential XSS Payloads

πŸ” XSS Detection Flow

  1. Find input points β†’ Forms, URL params, headers

  2. Test basic payload β†’ <script>alert(1)</script>

  3. Check page source β†’ Look for payload reflection

  4. Verify execution β†’ Confirm JavaScript runs

  5. 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:

  1. Submit XSS payload through input form

  2. Refresh page to confirm persistence

  3. 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

Image 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:

  1. Identify input points - All user inputs, not just forms

  2. Submit test payload - Use basic <script>alert(1)</script>

  3. Analyze response - Check page source for payload

  4. Verify execution - Confirm JavaScript execution

  5. 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

πŸ’€ 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

  1. Navigate to target login page

  2. Open Firefox Developer Tools (Shift+F9)

  3. Go to Storage tab

  4. Click + to add new cookie

  5. Set Name and Value from stolen cookie

  6. Refresh page to access victim account

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:

  1. Setup Credential Harvesting Server:

  1. Create index.php for credential capture:

  1. Start PHP listener:

  1. Craft malicious URL (example):

  1. Check captured credentials:

Attack Breakdown:

  • '> - Escapes from image URL attribute

  • document.write() - Injects fake login form

  • getElementById('urlform').remove() - Removes original form to avoid suspicion

  • <!-- - Comments out remaining HTML to prevent rendering issues

  • Form 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:

  1. Test basic XSS: <script>alert(1)</script>

  2. Test with URL parameter: ?url=<script>alert(1)</script>

  3. Check payload encoding with online tools

  4. Verify server listening: sudo php -S 0.0.0.0:8080

  5. Test 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