A Machine Learning-based system to detect Cross-Site Scripting (XSS) attacks in URLs using multiple classifiers including Decision Trees, SVM, Naive Bayes, KNN, Random Forest, and Neural Networks.
This project uses Doc2Vec and various machine learning models to classify URLs as either malicious (containing XSS) or normal. The system achieves this by:
-
Converting URLs into feature vectors using Doc2Vec
-
Extracting additional features based on common XSS patterns
-
Using an ensemble of machine learning models for classification
Run the following command to install the required Python packages:
pip install numpy pickle nltk gensim urllib scikit-learn pandas sklearnXSS_urls.txt: One URL per line containing XSS payloadsnormal_urls.txt: One URL per line containing benign URLs
test.txt: One URL per line to be classified
-
Training the Models
- Open and run
IS_Project_Train.ipynb - This will:
- Load
XSS_urls.txtandnormal_urls.txtfromdata/directory - Process the training data
- Create feature vectors
- Train all models
- Save models to the
saved_models/directory
- Load
- Open and run
-
Testing/Prediction
- Open and run
IS_Project_Test.ipynb - This will:
- Load the trained models
- Process URLs from
test.txtindata/directory - Output classification results for each URL
- Provide a summary of XSS vs Normal URLs detected
- Open and run
The final classification uses a weighted ensemble of models:
- MLPClassifier: 30%
- RandomForestClassifier: 25%
- DecisionTreeClassifier: 17.5%
- SVC: 15%
- KNeighborsClassifier: 7.5%
- GaussianNB: 5%
A threshold of 0.5 is used to determine the final classification.
The system extracts the following features from URLs:
- Doc2Vec embeddings
- Count of HTML malicious tags
- Count of malicious methods/events keywords
- JavaScript-related patterns
- Special character frequencies
- URL length
- Script pattern frequencies
- HTTP occurrence count
The system will classify each URL and display results with color coding:
- Red: XSS detected
- Green: Normal URL
A summary will be provided showing:
- Total URLs classified
- Number of XSS URLs detected
- Number of Normal URLs detected