Skip to content

About

Machine learning for malicious JavaScript detection using static, dynamic and hybrid features. AI for Cybersecurity university project (University of Pisa, 2026).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

AI-based Detection of Malicious JavaScript

A machine learning project that compares static, dynamic and hybrid features for detecting malicious JavaScript in web applications.

Course: Artificial Intelligence for Cybersecurity
University: University of Pisa
Academic year: 2025–2026
Author: Gabriele Donno

Project goal

The goal is to classify JavaScript samples as benign (class 0) or malicious (class 1) and to evaluate which feature representation works best.

The project compares three representations:

  • Static: features extracted from source code without executing it.
  • Dynamic: runtime-observable intent and API-related features supplied by the dataset author.
  • Hybrid: a combination of static and dynamic features.

This is an academic classification experiment, not a browser extension or a production malware detection tool.

Dataset

The project uses version 1 of Static, Dynamic, and Hybrid Feature Dataset for Lightweight Detection of Malicious JavaScript in Web Applications, by Naimish Sojitra (2026), published on Mendeley Data under the CC BY 4.0 license.

The original dataset contains 52,317 rows in each representation. After removing repeated sample IDs, 48,358 unique sample IDs remain: 22,167 benign and 26,191 malicious. The notebook excludes sample IDs and batch IDs from the predictive features, removes two non-informative dynamic features, and maps the original text labels (benign, malicious) to integers.

Representation Features after preprocessing
Static 172
Dynamic 15
Hybrid 187

The CSV files are not included in this repository. Download the original files and follow the instructions in data/README.md. No raw malicious JavaScript is required or included. Feature extraction is inherited from the dataset; this project starts from the provided CSV files.

Methodology

The notebook includes:

  1. Dataset exploration, duplicate removal and preprocessing.
  2. Feature distributions, correlations and dynamic pattern analysis.
  3. Decision Tree, Random Forest and Linear SVM classification.
  4. Stratified 10-fold cross-validation with a fixed random seed (42) and a random dummy baseline.
  5. Evaluation using accuracy, precision, recall, F1-score and ROC-AUC. F1 for the malicious class is the primary metric.
  6. Pairwise Wilcoxon signed-rank tests on fold-level F1 scores.
  7. Out-of-fold ROC curves, confusion matrix, feature importance and error analysis.

Standardization for the Linear SVM is performed inside a scikit-learn pipeline. No extensive hyperparameter tuning is performed.

Results

Mean F1-scores from the original notebook (rounded to four decimal places):

Model Static Dynamic Hybrid
Decision Tree 0.9973 0.4571 0.9974
Random Forest 0.9990 0.4571 0.9990
Linear SVM 0.9906 0.5857 0.9906

Random Forest with Static features has the highest unrounded mean F1-score. Its out-of-fold predictions contain 43 false negatives and 5 false positives. The static and hybrid representations perform similarly, while the dynamic representation performs substantially worse. The reported Wilcoxon tests do not find a significant improvement from adding dynamic features to static features at the 0.05 threshold for any of the three models.

See the full notebook and project presentation for the original outputs and discussion. The F1 comparison plot, ROC curves and confusion matrix are also available.

Limitations

  • Results are based on internal cross-validation on one dataset, not an independent external test set.
  • The origin of individual JavaScript samples is not available as metadata, so source bias cannot be excluded.
  • The supplied dynamic features have limited diversity and do not represent all possible runtime behavior.
  • Hyperparameter tuning was limited, and feature extraction was not implemented in this project.

The high F1-score should not be interpreted as guaranteed performance on real-world or previously unseen malware from other sources.

How to run

The original notebook used Python 3.11.16. Python 3.11 is recommended. The dependency list is provided for convenience; the exact original package versions were not recorded in the supplied files.

  1. Download or clone this repository and open a terminal in its root directory.

  2. Create a virtual environment:

    python -m venv .venv
  3. Activate it.

    Windows (PowerShell):

    .\.venv\Scripts\Activate.ps1

    macOS/Linux:

    source .venv/bin/activate
  4. Install the dependencies:

    python -m pip install -r requirements.txt
  5. Download the three CSV files from the original dataset page and place them in data/ as described in data/README.md.

  6. Start Jupyter Notebook from the repository root:

    python -m notebook
  7. Open malware_detection.ipynb, select the Python kernel from your virtual environment (rather than the original custom kernel if prompted), then select Run All.

The notebook includes the original saved outputs. Running all cells will train the models again and regenerate the plots in images/. It may take several minutes or longer depending on the computer. Package versions and hardware may affect results and execution time.

Repository structure

javascript-malware-detection/
├── malware_detection.ipynb
├── requirements.txt
├── README.md
├── .gitignore
├── data/
│   └── README.md
├── docs/
│   └── project-presentation.pdf
└── images/
    └── ... (original plots)

Credits

  • Gabriele Donno — university project, model evaluation and presentation.
  • Naimish Sojitra — original feature dataset, Mendeley Data (2026), version 1, DOI: 10.17632/nnr45bvxzg.1.

The dataset has its own CC BY 4.0 license. No separate software license is specified for the project code.

About

Machine learning for malicious JavaScript detection using static, dynamic and hybrid features. AI for Cybersecurity university project (University of Pisa, 2026).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors