This project demonstrates how to detect Cross-Site Scripting (XSS) attacks in HTML using a Convolutional Neural Network (CNN). It converts HTML content into ASCII matrices and feeds them to a CNN for binary classification (malicious vs. benign).
- Converts HTML code into normalized ASCII matrices (image-like format)
- Classifies input as XSS or benign using a trained CNN model
- Preprocessing pipeline with BeautifulSoup and OpenCV
- Model trained on labeled dataset from Kaggle
- XSS-Detection/
- ├── model/ # Trained model
- ├── data/ # Dataset
- ├── test_html/ # Sample HTML files
- ├── src/ # Source code
- ├── README.md # This File
- ├── ProjectReport.pdf # Detailed report of project
-
HTML Preprocessing:
- HTML is cleaned using
BeautifulSoup - Each character is converted to an ASCII value
- The ASCII array is padded and reshaped to a
100x100matrix - Matrix is normalized to values between 0 and 1
- HTML is cleaned using
-
CNN Architecture:
- 3 Convolutional layers with ReLU + MaxPooling
- Dense layers for decision-making
- Sigmoid output for binary classification
-
Prediction:
- Model outputs a probability between 0 and 1
- Threshold > 0.5 → XSS detected
| HTML Snippet | Prediction | Confidence |
|---|---|---|
<a href="http://example.com">Click</a> |
✅ Safe | 0.04 |
<script>alert('XSS')</script> |
❌ XSS Detected | 0.98 |
<img src="x" onerror="alert('XSS')"> |
❌ XSS Detected | 0.92 |
<div onclick="console.log('safe')">Click</div> |
✅ Safe | 0.12 |
- Clone the repo:
git clone https://github.com/IJBaig/XSS-Detection.git
cd xss-detection-
Place your HTML file in the
test/directory. -
Run the Python test script:
python src/test_xss_detection.pyThe training process is implemented entirely in a Jupyter Notebook:
📁 src/train_model.ipynb
- TensorFlow / Keras
- OpenCV
- NumPy / Pandas
- Matplotlib
- Data preprocessing
- Model architecture definition
- Training and validation visualization
- Model saving
For a detailed explanation of the project's methodology, implementation, and results, refer to:
📁 📄 Full Project Report