# Detecting Web-Based Attacks with SHAP and Tree Ensemble Machine Learning Methods
## 3 Proposed Method
### Preprocessing
#### 3.1.2 Sample JS Attack Codes
\begin{table}
\begin{tabular}{c c c c} \hline \hline
**Model** & **Recall** & **Precision** & **F1** \\ \hline XGBoost & 0.9989 \(\pm\) 0.0004 & 0.9832 \(\pm\) 0.0024 & 0.9909 \(\pm\) 0.0014 \\ LightGBM & 0.9986 \(\pm\) 0.0007 & 0.9820 \(\pm\) 0.0027 & 0.9902 \(\pm\) 0.0015 \\ RandomForest & 0.9985 \(\pm\) 0.0006 & 0.9840 \(\pm\) 0.0024 & 0.9912 \(\pm\) 0.0014 \\ DecisionTree & 0.9986 \(\pm\) 0.0005 & 0.9815 \(\pm\) 0.0024 & 0.9900 \(\pm\) 0.0013 \\ LogisticRegression & 0.9895 \(\pm\) 0.0013 & 0.9632 \(\pm\) 0.0037 & 0.9762 \(\pm\) 0.0017 \\ KNeighbors & 0.8779 \(\pm\) 0.0056 & 0.9995 \(\pm\) 0.0003 & 0.9347 \(\pm\) 0.0031 \\ GaussianNB & 0.9373 \(\pm\) 0.0056 & 0.6889 \(\pm\) 0.0031 & 0.7941 \(\pm\) 0.0030 \\ \hline \hline \end{tabular}
\end{table} 
Table 4: Performance comparison using SHAP selected AST-JS features. Table 5 presents precision, recall, and F1-scores obtained using SHAP selected features compared to the other feature selection methods; Boruta, ELI5, RandomForest, and SelectKBest. Our proposed detection model performed better than the other feature selection methods in all three evaluation metrics, with notable differences in precision and F1. The other feature selection methods have limitations because different model iterations assign different feature values to the AST-JS features. Additionally, Boruta assigns a _True_ or _False_ value to each AST-JS feature, as shown in Figure 5a. The permutation-based methods are computationally expensive and can have problems with highly-correlated AST-JS features, resulting in loss of important information. SHAP-selected AST-JS features have consistency in the feature values assigned to each feature. Unlike Boruta, SHAP values show the degree and manner of each AST-JS feature's contribution to the model prediction and are model-agnostic. These features also provide interaction graphs that are instrumental in getting information on AST-JS feature combinations, further boosting the model's detection performance. Table 6 shows the training and detection times for the various classifiers on the JS code dataset using SHAP selected features. XGBoost yielded the highest detection performance and the third-fastest detection time, making it the best classifier for JS-based attack detection. KNeighbors yielded the lowest training time; however, it achieved the lowest recall rate, rendering it ineffective for this detection task. DecisionTree yielded the lowest detection time; however, XGBoost outperformed DecisionTree in all three evaluation metrics. ## 5 Discussion

Blocklists, and heuristic- and signature-based systems are widely used to detect malicious URLs and JS codes. Current detection methods have various challenges stemming from fast-flux, cloaking, and zero-day attacks. Some services that can be used for copyright and privacy reasons in benign JS codes, such as obfuscation, can also create new or variant JS-based attacks. Therefore, it is biased to classify transformed URLs and JS codes as malicious without further detailed analysis. A system to analyze and accurately detect web-based attacks is needed. As part of our research findings, feature selection using SHAP values resulted in a better prediction model. Other researchers limited their scope by studying specific malicious URLs and JS-based attacks, such as those containing phishing attacks, social engineering, malvertising attacks, and denial of service attacks, among others. For this, \begin{table}
\begin{tabular}{c c c} \hline \hline
**Model** & **Training Time (s)** & **Detection Time (s)** \\ \hline XGBoost & 0.8685 & \(1.498\times 10^{-6}\) \\ LightGBM & 0.5716 & \(2.114\times 10^{-6}\) \\ RandomForest & 1.6350 & \(1.035\times 10^{-5}\) \\ DecisionTree & 0.0895 & \(1.179\times 10^{-6}\) \\ LogisticRegression & 0.6384 & \(1.871\times 10^{-6}\) \\ KNeighbors & 0.0037 & 0.0015 \\ GaussianNB & 0.0425 & \(1.334\times 10^{-6}\) \\ \hline \hline \end{tabular}
\end{table} 
Table 6: Training and detection time.