## 3 Proposed Solution
### Scope of Attacks Covered with the Approach  
As previously mentioned, we treat attacks as data outliers and deploy clustering techniques, namely SOM, unsupervised GA and Growing Neural Gas (GNG). Further details on the algorithm implementation can be found in [12, 14, 15], while their distributed organization will be described in more detail in Section 3.7. In the following we will explain the principles of the approach. It is important to mention here that the algorithms can be trained with both clean and \"unclean\" data (contains traces of attacks). Furthermore, the algorithms are constantly retrained in order to decrease time lags between model training and model application. The retraining frequency depends on the dynamics of the underlying sensor network. There are two approaches for detecting outliers using clustering techniques [16] depending on the following two possibilities: detecting outlying clusters or detecting outlying data that belong to non-outlying clusters. For the first case, we calculate the average distance of each cluster to the rest of the clusters (or its closest neighborhood) (_MD_). In the latter case, we calculate quantization error (_QE_) of each input as the distance from its corresponding cluster center. The attacks that can be detected with the proposed approach are those that introduce changes into either the sensed value that is forwarded to the base station or the routing paths. These changes will result in different distribution of the extracted n-grams. However, if we take frequencies as feature values, the sum of the feature values remain the same, _i.e._, 1, so we can write the following equation:  
\[\sum_{i=0}^{N}\Delta f_{i}=0 \tag{1}\]  
where N is the total number of the extracted n-grams and \(\Delta f_{i}\) is the change of the feature value of the n-gram \(i\). On the other hand, according to the distance function [13], the introduced change in distance between the attacked instance and any other is:\[\Delta D = \sum_{i=1}^{N}\left|\Delta f_{i}\right| \tag{2}\]  
In essence, this is the change introduced in the above defined \(QE\) or/and \(MD\) values. Thus, the following inequality defines the changes introduced by the attacks:  
\[\sum_{i=1}^{N}\left|\Delta f_{i}\right|>f_{th} \tag{3}\]  
where \(f_{th}\) is the threshold value used to distinguish attacks from normal situations. Now we will see how the changes introduced by the attacker affect the feature values. Bearing in mind that each sensed value or routing hop participates in n features, where n is the size of the n-gram, if the attacker changes one value, the values of \(2n\) (at most) features will be changed [the values of newly created n-grams (n at most) with the change will increase, while the values of those that existed before the change (again n at most) will decrease]. For example, the third element in the sequence...1 0 0 1 1... for n = 3 participates in three n-grams: 100, 001 and 011. However, if the attacker changes this value into 1, the sequence becomes...1 0 1 1 1..., in which case the third element participates in these n-grams: 101, 011 and 111. This results in decreased occurrences of the n-grams 100 and 001, while the occurrences of the 101 and 011 become increased (011 appears in both cases, so its total occurrence remains the same). In total, the occurrence of four n-grams is changed.