train = read.csv('training.csv')
test = read.csv('testing.csv')
str(train)
## 'data.frame':    175341 obs. of  45 variables:
##  $ id               : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ dur              : num  0.121 0.65 1.623 1.682 0.449 ...
##  $ proto            : chr  "tcp" "tcp" "tcp" "tcp" ...
##  $ service          : chr  "-" "-" "-" "ftp" ...
##  $ state            : chr  "FIN" "FIN" "FIN" "FIN" ...
##  $ spkts            : int  6 14 8 12 10 10 10 10 10 10 ...
##  $ dpkts            : int  4 38 16 12 6 6 8 8 8 6 ...
##  $ sbytes           : int  258 734 364 628 534 534 534 534 534 534 ...
##  $ dbytes           : int  172 42014 13186 770 268 268 354 354 354 268 ...
##  $ rate             : num  74.1 78.5 14.2 13.7 33.4 ...
##  $ sttl             : int  252 62 62 62 254 254 254 254 254 254 ...
##  $ dttl             : int  254 252 252 252 252 252 252 252 252 252 ...
##  $ sload            : num  14159 8395 1572 2740 8561 ...
##  $ dload            : num  8495 503571 60929 3359 3987 ...
##  $ sloss            : int  0 2 1 1 2 2 2 2 2 2 ...
##  $ dloss            : int  0 17 6 3 1 1 1 1 1 1 ...
##  $ sinpkt           : num  24.3 49.9 231.9 152.9 47.8 ...
##  $ dinpkt           : num  8.38 15.43 102.74 90.24 75.66 ...
##  $ sjit             : num  30.2 61.4 17179.6 259.1 2415.8 ...
##  $ djit             : num  11.8 1387.8 11420.9 4991.8 115.8 ...
##  $ swin             : int  255 255 255 255 255 255 255 255 255 255 ...
##  $ stcpb            : num  6.22e+08 1.42e+09 2.12e+09 1.11e+09 2.44e+09 ...
##  $ dtcpb            : num  2.20e+09 3.08e+09 2.96e+09 1.05e+09 1.98e+09 ...
##  $ dwin             : int  255 255 255 255 255 255 255 255 255 255 ...
##  $ tcprtt           : num  0 0 0.112 0 0.128 ...
##  $ synack           : num  0 0 0.0615 0 0.0711 ...
##  $ ackdat           : num  0 0 0.0504 0 0.0572 ...
##  $ smean            : int  43 52 46 52 53 53 53 53 53 53 ...
##  $ dmean            : int  43 1106 824 64 45 45 44 44 44 45 ...
##  $ trans_depth      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ response_body_len: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_srv_src       : int  1 43 7 1 43 43 43 43 43 43 ...
##  $ ct_state_ttl     : int  0 1 1 1 1 1 1 1 1 1 ...
##  $ ct_dst_ltm       : int  1 1 2 2 2 2 1 3 3 3 ...
##  $ ct_src_dport_ltm : int  1 1 1 1 2 2 1 3 3 3 ...
##  $ ct_dst_sport_ltm : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ ct_dst_src_ltm   : int  1 2 3 3 40 40 40 40 40 40 ...
##  $ is_ftp_login     : int  0 0 0 1 0 0 0 0 0 0 ...
##  $ ct_ftp_cmd       : int  0 0 0 1 0 0 0 0 0 0 ...
##  $ ct_flw_http_mthd : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_src_ltm       : int  1 1 2 2 2 2 1 3 3 3 ...
##  $ ct_srv_dst       : int  1 6 6 1 39 39 39 39 39 39 ...
##  $ is_sm_ips_ports  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ attack_cat       : chr  "Normal" "Normal" "Normal" "Normal" ...
##  $ label            : int  0 0 0 0 0 0 0 0 0 0 ...
str(test)
## 'data.frame':    82332 obs. of  45 variables:
##  $ id               : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ dur              : num  1.1e-05 8.0e-06 5.0e-06 6.0e-06 1.0e-05 3.0e-06 6.0e-06 2.8e-05 0.0 0.0 ...
##  $ proto            : chr  "udp" "udp" "udp" "udp" ...
##  $ service          : chr  "-" "-" "-" "-" ...
##  $ state            : chr  "INT" "INT" "INT" "INT" ...
##  $ spkts            : int  2 2 2 2 2 2 2 2 1 1 ...
##  $ dpkts            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ sbytes           : int  496 1762 1068 900 2126 784 1960 1384 46 46 ...
##  $ dbytes           : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ rate             : num  90909 125000 200000 166667 100000 ...
##  $ sttl             : int  254 254 254 254 254 254 254 254 0 0 ...
##  $ dttl             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ sload            : num  1.80e+08 8.81e+08 8.54e+08 6.00e+08 8.50e+08 ...
##  $ dload            : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ sloss            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ dloss            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ sinpkt           : num  0.011 0.008 0.005 0.006 0.01 ...
##  $ dinpkt           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ sjit             : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ djit             : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ swin             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ stcpb            : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ dtcpb            : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ dwin             : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ tcprtt           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ synack           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ ackdat           : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ smean            : int  248 881 534 450 1063 392 980 692 46 46 ...
##  $ dmean            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ trans_depth      : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ response_body_len: int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_srv_src       : int  2 2 3 3 3 2 2 3 2 2 ...
##  $ ct_state_ttl     : int  2 2 2 2 2 2 2 2 2 2 ...
##  $ ct_dst_ltm       : int  1 1 1 2 2 2 2 1 2 2 ...
##  $ ct_src_dport_ltm : int  1 1 1 2 2 2 2 1 2 2 ...
##  $ ct_dst_sport_ltm : int  1 1 1 1 1 1 1 1 2 2 ...
##  $ ct_dst_src_ltm   : int  2 2 3 3 3 2 2 3 2 2 ...
##  $ is_ftp_login     : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_ftp_cmd       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_flw_http_mthd : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ ct_src_ltm       : int  1 1 1 2 2 2 2 1 2 2 ...
##  $ ct_srv_dst       : int  2 2 3 3 3 2 2 3 2 2 ...
##  $ is_sm_ips_ports  : int  0 0 0 0 0 0 0 0 1 1 ...
##  $ attack_cat       : chr  "Normal" "Normal" "Normal" "Normal" ...
##  $ label            : int  0 0 0 0 0 0 0 0 0 0 ...

all are ints or nums aside from ‘proto’, ‘service’, ‘state’ and ‘attact_cat’

summary(test)
##        id             dur              proto             service         
##  Min.   :    1   Min.   : 0.00000   Length:82332       Length:82332      
##  1st Qu.:20584   1st Qu.: 0.00001   Class :character   Class :character  
##  Median :41167   Median : 0.01414   Mode  :character   Mode  :character  
##  Mean   :41167   Mean   : 1.00676                                        
##  3rd Qu.:61749   3rd Qu.: 0.71936                                        
##  Max.   :82332   Max.   :59.99999                                        
##     state               spkts              dpkts              sbytes        
##  Length:82332       Min.   :    1.00   Min.   :    0.00   Min.   :      24  
##  Class :character   1st Qu.:    2.00   1st Qu.:    0.00   1st Qu.:     114  
##  Mode  :character   Median :    6.00   Median :    2.00   Median :     534  
##                     Mean   :   18.67   Mean   :   17.55   Mean   :    7994  
##                     3rd Qu.:   12.00   3rd Qu.:   10.00   3rd Qu.:    1280  
##                     Max.   :10646.00   Max.   :11018.00   Max.   :14355774  
##      dbytes              rate                sttl          dttl       
##  Min.   :       0   Min.   :      0.0   Min.   :  0   Min.   :  0.00  
##  1st Qu.:       0   1st Qu.:     28.6   1st Qu.: 62   1st Qu.:  0.00  
##  Median :     178   Median :   2650.2   Median :254   Median : 29.00  
##  Mean   :   13234   Mean   :  82410.9   Mean   :181   Mean   : 95.71  
##  3rd Qu.:     956   3rd Qu.: 111111.1   3rd Qu.:254   3rd Qu.:252.00  
##  Max.   :14657531   Max.   :1000000.0   Max.   :255   Max.   :253.00  
##      sload               dload              sloss              dloss         
##  Min.   :0.000e+00   Min.   :       0   Min.   :   0.000   Min.   :   0.000  
##  1st Qu.:1.120e+04   1st Qu.:       0   1st Qu.:   0.000   1st Qu.:   0.000  
##  Median :5.770e+05   Median :    2113   Median :   1.000   Median :   0.000  
##  Mean   :6.455e+07   Mean   :  630547   Mean   :   4.754   Mean   :   6.309  
##  3rd Qu.:6.514e+07   3rd Qu.:   15858   3rd Qu.:   3.000   3rd Qu.:   2.000  
##  Max.   :5.268e+09   Max.   :20821108   Max.   :5319.000   Max.   :5507.000  
##      sinpkt             dinpkt              sjit                djit         
##  Min.   :    0.00   Min.   :    0.00   Min.   :      0.0   Min.   :     0.0  
##  1st Qu.:    0.01   1st Qu.:    0.00   1st Qu.:      0.0   1st Qu.:     0.0  
##  Median :    0.56   Median :    0.01   Median :     17.6   Median :     0.0  
##  Mean   :  755.39   Mean   :  121.70   Mean   :   6363.1   Mean   :   535.2  
##  3rd Qu.:   63.41   3rd Qu.:   63.14   3rd Qu.:   3219.3   3rd Qu.:   128.5  
##  Max.   :60009.99   Max.   :57739.24   Max.   :1483830.9   Max.   :463199.2  
##       swin           stcpb               dtcpb                dwin      
##  Min.   :  0.0   Min.   :0.000e+00   Min.   :0.000e+00   Min.   :  0.0  
##  1st Qu.:  0.0   1st Qu.:0.000e+00   1st Qu.:0.000e+00   1st Qu.:  0.0  
##  Median :255.0   Median :2.789e+07   Median :2.857e+07   Median :255.0  
##  Mean   :133.5   Mean   :1.085e+09   Mean   :1.073e+09   Mean   :128.3  
##  3rd Qu.:255.0   3rd Qu.:2.171e+09   3rd Qu.:2.144e+09   3rd Qu.:255.0  
##  Max.   :255.0   Max.   :4.295e+09   Max.   :4.295e+09   Max.   :255.0  
##      tcprtt             synack             ackdat            smean       
##  Min.   :0.000000   Min.   :0.000000   Min.   :0.00000   Min.   :  24.0  
##  1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.:0.00000   1st Qu.:  57.0  
##  Median :0.000551   Median :0.000441   Median :0.00008   Median :  65.0  
##  Mean   :0.055925   Mean   :0.029256   Mean   :0.02667   Mean   : 139.5  
##  3rd Qu.:0.105541   3rd Qu.:0.052596   3rd Qu.:0.04882   3rd Qu.: 100.0  
##  Max.   :3.821465   Max.   :3.226788   Max.   :2.92878   Max.   :1504.0  
##      dmean         trans_depth        response_body_len   ct_srv_src    
##  Min.   :   0.0   Min.   :  0.00000   Min.   :      0   Min.   : 1.000  
##  1st Qu.:   0.0   1st Qu.:  0.00000   1st Qu.:      0   1st Qu.: 2.000  
##  Median :  44.0   Median :  0.00000   Median :      0   Median : 5.000  
##  Mean   : 116.3   Mean   :  0.09428   Mean   :   1595   Mean   : 9.547  
##  3rd Qu.:  87.0   3rd Qu.:  0.00000   3rd Qu.:      0   3rd Qu.:11.000  
##  Max.   :1500.0   Max.   :131.00000   Max.   :5242880   Max.   :63.000  
##   ct_state_ttl     ct_dst_ltm     ct_src_dport_ltm ct_dst_sport_ltm
##  Min.   :0.000   Min.   : 1.000   Min.   : 1.000   Min.   : 1.000  
##  1st Qu.:1.000   1st Qu.: 1.000   1st Qu.: 1.000   1st Qu.: 1.000  
##  Median :1.000   Median : 2.000   Median : 1.000   Median : 1.000  
##  Mean   :1.369   Mean   : 5.745   Mean   : 4.929   Mean   : 3.663  
##  3rd Qu.:2.000   3rd Qu.: 6.000   3rd Qu.: 4.000   3rd Qu.: 3.000  
##  Max.   :6.000   Max.   :59.000   Max.   :59.000   Max.   :38.000  
##  ct_dst_src_ltm    is_ftp_login        ct_ftp_cmd       ct_flw_http_mthd 
##  Min.   : 1.000   Min.   :0.000000   Min.   :0.000000   Min.   : 0.0000  
##  1st Qu.: 1.000   1st Qu.:0.000000   1st Qu.:0.000000   1st Qu.: 0.0000  
##  Median : 3.000   Median :0.000000   Median :0.000000   Median : 0.0000  
##  Mean   : 7.456   Mean   :0.008283   Mean   :0.008381   Mean   : 0.1297  
##  3rd Qu.: 6.000   3rd Qu.:0.000000   3rd Qu.:0.000000   3rd Qu.: 0.0000  
##  Max.   :63.000   Max.   :2.000000   Max.   :2.000000   Max.   :16.0000  
##    ct_src_ltm       ct_srv_dst     is_sm_ips_ports    attack_cat       
##  Min.   : 1.000   Min.   : 1.000   Min.   :0.00000   Length:82332      
##  1st Qu.: 1.000   1st Qu.: 2.000   1st Qu.:0.00000   Class :character  
##  Median : 3.000   Median : 5.000   Median :0.00000   Mode  :character  
##  Mean   : 6.468   Mean   : 9.164   Mean   :0.01113                     
##  3rd Qu.: 7.000   3rd Qu.:11.000   3rd Qu.:0.00000                     
##  Max.   :60.000   Max.   :62.000   Max.   :1.00000                     
##      label       
##  Min.   :0.0000  
##  1st Qu.:0.0000  
##  Median :1.0000  
##  Mean   :0.5506  
##  3rd Qu.:1.0000  
##  Max.   :1.0000
dim(train)
## [1] 175341     45
head(test)
##   id     dur proto service state spkts dpkts sbytes dbytes      rate sttl dttl
## 1  1 1.1e-05   udp       -   INT     2     0    496      0  90909.09  254    0
## 2  2 8.0e-06   udp       -   INT     2     0   1762      0 125000.00  254    0
## 3  3 5.0e-06   udp       -   INT     2     0   1068      0 200000.01  254    0
## 4  4 6.0e-06   udp       -   INT     2     0    900      0 166666.66  254    0
## 5  5 1.0e-05   udp       -   INT     2     0   2126      0 100000.00  254    0
## 6  6 3.0e-06   udp       -   INT     2     0    784      0 333333.32  254    0
##        sload dload sloss dloss sinpkt dinpkt sjit djit swin stcpb dtcpb dwin
## 1  180363632     0     0     0  0.011      0    0    0    0     0     0    0
## 2  881000000     0     0     0  0.008      0    0    0    0     0     0    0
## 3  854400000     0     0     0  0.005      0    0    0    0     0     0    0
## 4  600000000     0     0     0  0.006      0    0    0    0     0     0    0
## 5  850400000     0     0     0  0.010      0    0    0    0     0     0    0
## 6 1045333312     0     0     0  0.003      0    0    0    0     0     0    0
##   tcprtt synack ackdat smean dmean trans_depth response_body_len ct_srv_src
## 1      0      0      0   248     0           0                 0          2
## 2      0      0      0   881     0           0                 0          2
## 3      0      0      0   534     0           0                 0          3
## 4      0      0      0   450     0           0                 0          3
## 5      0      0      0  1063     0           0                 0          3
## 6      0      0      0   392     0           0                 0          2
##   ct_state_ttl ct_dst_ltm ct_src_dport_ltm ct_dst_sport_ltm ct_dst_src_ltm
## 1            2          1                1                1              2
## 2            2          1                1                1              2
## 3            2          1                1                1              3
## 4            2          2                2                1              3
## 5            2          2                2                1              3
## 6            2          2                2                1              2
##   is_ftp_login ct_ftp_cmd ct_flw_http_mthd ct_src_ltm ct_srv_dst
## 1            0          0                0          1          2
## 2            0          0                0          1          2
## 3            0          0                0          1          3
## 4            0          0                0          2          3
## 5            0          0                0          2          3
## 6            0          0                0          2          2
##   is_sm_ips_ports attack_cat label
## 1               0     Normal     0
## 2               0     Normal     0
## 3               0     Normal     0
## 4               0     Normal     0
## 5               0     Normal     0
## 6               0     Normal     0
table(train$label)
## 
##      0      1 
##  56000 119341
table(train$label)/175341
## 
##         0         1 
## 0.3193777 0.6806223
dim(test)
## [1] 82332    45
table(test$label)
## 
##     0     1 
## 37000 45332
table(test$label)/82332
## 
##      0      1 
## 0.4494 0.5506
glm.fit = glm(train$label ~ dpkts + sbytes + dbytes + rate + synack + sqrt(dur) + service, data = train, family=binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
glm.probs=predict(glm.fit, newdata = test, type = "response", na.action = na.pass)
glm.pred = rep("0", 252)
glm.pred[glm.probs > 0.5] = "1"
cm = table(glm.pred, test$label)
cm
##         
## glm.pred     0     1
##        0     5     4
##        1 19299 40844
correct = (cm[1,1] + cm[2,2])/sum(cm)
print(correct)
## [1] 0.6790963
error = 1 - correct
print(error)
## [1] 0.3209037

Roughly a 68% accuracy by removing the state factor from the suggested set of features. It was initially removed because it was causing some odd errors in the code.

glm.fit = glm(train$label ~ service + sbytes + sttl + smean + ct_dst_sport_ltm, data = train, family = binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred

This set of features was derived from this paper: https://shura.shu.ac.uk/15662/1/Feature%20Selection%20TJ-SZ-ISIE%202017-Camera%20Ready1.pdf which explored the best sets of features determined by machine learning.

glm.probs=predict(glm.fit, newdata = test, type = "response", na.action = na.pass)
glm.pred = rep("0", 252)
glm.pred[glm.probs > 0.5] = "1"
cm = table(glm.pred, test$label)
cm
##         
## glm.pred     0     1
##        0     9     0
##        1 17936 43435
correct = (cm[1,1] + cm[2,2])/sum(cm)
print(correct)
## [1] 0.7077876
error = 1 - correct
print(error)
## [1] 0.2922124

Slightly more accurate than the last set of features but is short of what the paper found in it’s accuracy. They saw around 75% accuracy, but this may have been because of the difference between the testing and training sets used.

glm.fit = glm(train$label ~ sttl + ct_dst_src_ltm + spkts + dload + sloss+ dloss + ct_src_ltm + ct_srv_dst, data = train, family = binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred

This is a list derived from the most frequently appeared features in each of the attack types.

glm.probs=predict(glm.fit, newdata = test, type = "response", na.action = na.pass)
glm.pred = rep("0", 252)
glm.pred[glm.probs > 0.5] = "1"
cm = table(glm.pred, test$label)
cm
##         
## glm.pred     0     1
##        0    31     0
##        1 15777 38848
correct = (cm[1,1] + cm[2,2])/sum(cm)
print(correct)
## [1] 0.71134
error = 1 - correct
print(error)
## [1] 0.28866

This is the best one yet and finds about a 71% accuracy. This may be because it focuses on the attack types themselves and taking those factors in helps to accurately predict them within the data set. There are obviously many more ways to use factors in these, but this is highest that I have been able to get from just searching around and testing myself.

Conclusion

As said before, the third model was the best one that I was able to find. It was derived from finding a resources that showed the factors that were represented most commonly in the different attack types that showed up in the data set. I picked these specifically because I thought that this may lead to the model being able to more accurately predict the attacks themselves. This did end up producing the best overall results with about a 71% accuracy to the testing set.