# Securing Network Traffic Classification Models against Adversarial Examples Using Derived Variables
## 4 DVars for Adversarial Attack Defense
### DVar Algorithms
#### 4.2.2 The Absolute Difference in Adjacent Values  
In contrast to the sum of adjacent values, this process computes the absolute difference in adjacent values in each set of features in the baseline dataset. To keep the DVars in the same feature space with the baseline, if any resulting value is below the minimum value of the baseline data, a condition is set to retain the minimum value. This process is  
\begin{table}
\begin{tabular}{c c c c c} \hline \hline
**Bytes** & **Dbytes** & **Spkts** & **Dpkts** & **C** \\ \hline \(\text{sbytes}_{1}\) & \(\text{dbytes}_{1}\) & \(\text{spkts}_{1}\) & \(\text{dpkts}_{1}\) & — \\ \(\dots\) & \(\dots\) & \(\dots\) & \(\dots\) & — \\ \(\text{sbytes}_{n+1}\) & \(\text{dbytes}_{n+1}\) & \(\text{spkts}_{n+1}\) & \(\text{dpkts}_{n+1}\) & Equation (7) \\ \(|\text{spkts}_{n-1}|\) & \(|\text{dpytes}_{n-1}|\) & \(|\text{spkts}_{n-1}|\) & \(|\text{dpkts}_{n-1}|\) & Equation (8) \\ \(\text{sbytes}_{n}\) & \(\text{dbytes}_{n}\) & \(\text{spkts}_{n}\) & \(\text{dpkts}_{n}\) & — \\ \hline \hline \end{tabular}
\end{table}
Table 1: A representation of DVars using both the sum and absolute difference in adjacent flow features; C indicates the condition.  
repeated for each adjacent feature value until the final feature value of the selected features is obtained. Representative feature \(Y_{ij}\) can be calculated using Equation (8).  
\[Y_{ij}=\begin{cases}|X_{ij}-X_{i(j+1)}|,&\text{if }|Y_{ij}|<\min(|X_{i,:}|)\\ \min(|X_{i,:}|),&\text{otherwise}\end{cases} \tag{8}\]  
where \(X\) represents the baseline dataset; \(X_{ij}\) denotes the value of the \(j\)-th feature in the \(i\)-th sample of the baseline dataset; \(Y\) is the matrix containing the representative features; \(Y_{ij}\) represents the value of the representative feature for the \(j\)-th feature in the \(i\)-th sample; \(|X_{i,:}|\) denotes the absolute values of all elements in the \(i\)-th row of the baseline dataset; \(\min(|X_{i,:}|)\) represents the minimum absolute difference between all values in the \(i\)-th row of the baseline dataset; and \(|Y_{ij}|\) represents the absolute value of \(Y_{ij}\), which is the calculated absolute difference. The process of deriving variables using the absolute difference between the values of the adjacent flow features is shown in Algorithm 2.  
```
Data: Baseline dataset \(X\) Result: Matrix of representative features \(Y\) for each sample \(i\) in \(X\)do
1 for\(j=1\)to number of featuresdo
2 if\(j+1\leq number\) of featuresthen
3\(Y_{ij}=|X_{ij}-X_{i(j+1)}|\);
4 if\(|Y_{ij}|<\min(|X_{i,:}|)\)then
5\(Y_{ij}=\min(|X_{i,:}|)\);
6
7 end if
8
9 end for
10
11 else
12\(Y_{ij}=|X_{ij}|\);
13
14 end if
15
16 end for
```  
**Algorithm 2**Derivation of representative features using the absolute difference in adjacent values.  
After acquiring a new set of representative features through the process described in the equations (Equations (7) and (8)), they are merged with the baseline dataset. Subsequently, this combined dataset is used to train the ML models for network traffic classification. The primary objective of this training is to enhance the ability of the models to withstand adversarial attacks while also improving their overall accuracy.