# Detecting Web-Based Attacks with SHAP and Tree Ensemble Machine Learning Methods
## 3 Proposed Method
### Preprocessing
#### 3.1.2 Sample JS Attack Codes
We divided the AST-JS features into those that enhanced the performance of the malicious JS code detection model and those that did not by determining whether their SHAP values moved the predicted value toward or away from the true value. Algorithm 4 shows how the SHAP values were used to select relevant AST-JS features. For each AST-JS feature (line 1), we checked whether the true feature value given by the input JS code was greater than the predicted value (line 2); a positive SHAP value indicates a contributing feature (line 3) and vice versa (line 4). If the predicted feature value was less than the input value for a JS code (line 5), the contributing features is indicated by a negative SHAP value and vice versa. This algorithm returned two lists, the \(SHAPSelected\) and \(SHAPnotSelected\) features, containing the contributing and non-contributing AST-JS features, respectively, along with the SHAP values for each of the AST-JS \(M\) features. ```
0:\(Z\)--a malicious or benign JS code we want to explain, \(Z_{1..}\)--AST-JS instances that tree SHAP uses as background examples, the \(g\)--tree ensemble model. 0:\(SHAPsortedM\) features - SHAP values for each AST-JS feature in JS code dataset sorted in descending order of their importance. 1:\(M\) features \(\leftarrow\) feature values based on AST-JS. 2:for each\(i\in M\) features do
3: explainer \(\leftarrow\) shap.TreeExplainer(\(g\),\(Z_{1..j}\))
4:\(SHAPsortedM\) features\([i]\leftarrow\) explainer.shapvalues(\(Z\),\(i\))
5:endfor
6:return \(SHAPsortedM\) features
```
**Algorithm 2** Calculating tree SHAP values for AST-JS \(M\) features. ```
0:\(Z\)--a malicious or benign JS code to explain, \(i\)--the AST-JS feature to get prediction for, \(g\)--tree ensemble model. 0:\(z^{\prime}_{i}\)--the value of the \(i\)th AST-JS feature in \(Z^{\prime}\). 1:\(z^{\prime}_{i}\gets g\).predict(\(Z\))[i]
2:return \(z^{\prime}_{i}\)
```
**Algorithm 3**\(g(Z,i)\)--predicting the \(i\)th AST-JS feature. ```
0:\(SHAPsortedM\) features--SHAP values for each AST-JS feature in JS code dataset sorted in descending order of their importance, \(Z\)--a malicious or benign JS code we want to explain, \(Z^{\prime}\)--the prediction for \(Z\). 0:\(SHAPselected\) features and \(SHAPnotSelected\) features. 1:for each\(i\in SHAPsortedM\) features do
2:if\(z_{i}>z^{\prime}_{i}\)then
3:\(SHAPselected[i]\gets SHAPsortedM\) features\([i]>0\)
4:\(SHAPnotSelected[i]\gets SHAPsortedM\) features\([i]<0\)
5:else
6:\(SHAPselected[i]\gets SHAPsortedM\) features\([i]<0\)
7:\(SHAPnotSelected[i]\gets SHAPsortedM\) features\([i]>0\)
8:endif
9:endfor
10:return \(SHAPselected\) features, \(SHAPnotSelected\) features
```

**Algorithm 4** Selecting contributing AST-JS features' SHAP values. The benefits of computing SHAP values are global and local interpretability, which shows the manner and degree of each AST-JS feature's contribution to the prediction, and each observation obtains its own set of SHAP values, enabling the evaluation of the features' impacts. ### Machine Learning Classifiers

For the classification task for web-based attack detection, seven machine learning classifiers from the Scikit-learn library [47; 48; 49; 50] were used in the experiments, i.e., XGBoost, LightGBM, RandomForest, DecisionTree, LogisticRegression, KNeighbors, and GaussianNB. The best algorithm was selected based on the k-fold cross-validation evaluation results. ## 4 Experiments

This section presents the experimental setup and performance comparison results for web-based attack detection. ### Experimental Setup

We used a dataset of 39,443 malicious and 40,000 benign JS codes in the experimental setup. The malicious JS codes were obtained from [PERSON]'s dataset [51], and the benign codes were obtained from the Majestic Million Service [7]. We used a JS code parser to preprocess the dataset into AST-JS nodes, resulting in 32,430 malicious and 38,891 benign JS codes. This resulted in 25 _expression_ and _pattern_ and 23 _statement_ and _declaration_ AST-JS features.