๐ XSS Tutorial โ Flask Lab + WebGoat + Payloads¶
๐ What is XSS?¶
Cross-Site Scripting (XSS) is a web security vulnerability that allows attackers to inject malicious JavaScript into webpages. These scripts can:
-
Steal cookies / sessions
-
Deface websites
-
Redirect users
-
Execute actions as other users
โ ๏ธ Types of XSS¶
| Type | Description |
|---|---|
| Stored | Script is saved on the server (e.g., in a DB) and shown to others later. |
| Reflected | Script is reflected off the server (e.g., in URLs, search, error pages). |
| DOM-based | Script is run entirely on the client via insecure JS manipulations. |
๐งช Practice XSS โ Two Ways¶
โ 1. Dockerized Vulnerable Flask App¶
This app is intentionally vulnerable to stored XSS and is portable with Docker.
๐ Project Structure¶
๐ app.py¶
from flask import Flask, request, render_template_string
app = Flask(__name__)
comments = []
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
comment = request.form.get("comment")
comments.append(comment)
html = '''
<!DOCTYPE html>
<html>
<head>
<title>XSS Lab</title>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
</head>
<body>
<h2>Leave a Comment</h2>
<form method="post">
<textarea name="comment" rows="4" cols="40"></textarea><br>
<button type="submit">Submit</button>
</form>
<h3>Comments</h3>
<ul>
{% for comment in comments %}
<li>{{ comment | safe }}</li> <!-- vulnerable -->
{% endfor %}
</ul>
</body>
</html>
'''
return render_template_string(html, comments=comments)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
๐ requirements.txt¶
๐ Dockerfile¶
FROM python:3.11-slim
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]
โถ๏ธ Build & Run¶
Visit โ http://localhost:5000
Try payloads like:
โ Fix XSS¶
Replace:
With:
โ Also set strict CSP headers like:
โ 2. Practice with OWASP WebGoat + WebWolf¶
๐ณ Run with Docker:¶
Access:¶
-
WebGoat: http://localhost:8080/WebGoat
-
WebWolf: http://localhost:9090/WebWolf
Login:
๐ What You Can Learn¶
-
Stored XSS
-
Reflected XSS
-
DOM-based XSS
-
Secure coding practices
๐ง XSS Payload Cheat Sheet¶
๐น Basic¶
<script>alert(1)</script>
<img src=x onerror=alert(1)>
<svg/onload=alert(1)>
<iframe src="javascript:alert(1)">
๐น Filter Evasion¶
๐น Events¶
๐น Reflected via URL¶
๐ Prevention Tips¶
-
Escape all user input (
{{ comment }}instead of|safe) -
Set CSP headers
-
Use frameworks that auto-escape (Jinja2, React, Vue)
-
Validate input on both client and server
๐ Summary¶
| Tool | Description |
|---|---|
| Flask App | Simple vulnerable app for stored XSS practice |
| WebGoat | Full XSS lab with guided lessons via Docker |
| WebWolf | Used alongside WebGoat for advanced challenges |
| Payloads | Use cheat sheet to simulate real-world attacks |