-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject - Copy.Rmd
1378 lines (1126 loc) · 58.8 KB
/
project - Copy.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: 'BDA Project: Malicious and Benign Website URL detections'
author: "Nguyen Xuan Binh"
date: "January 2023"
output:
word_document:
toc: yes
toc_depth: '3'
pdf_document:
toc: yes
toc_depth: 3
fig_caption: yes
bibliography: bibliography.bib
---
# Introduction
## Central problem
Detection of malicious URLs among the benign ones is a crucial goal of modern-day cybersecurity as it helps prevent individuals and organizations from falling victim to phishing, data breaching, malware infections, and other types of cyber threats. The most common type is phishing, where the URLs are disguised as valid sites to trick users into revealing their credentials. Some other types even install harmful softwares or redirect users to other malicious sites. With the rapid growth of the internet and the increasing dependence on technology, black-hat hackers and thieves have found innovative ways to spread their malicious content through fake URLs. A 2017 report from Cybersecurity Ventures predicted ransomware damages would cost the world $5 billion in 2017, up from $325 million in 2015 — a 15X increase in just two years. The damages for 2018 were predicted to reach $8 billion, and for 2019 the figure is $11.5 billion @cybersec. Therefore, it is an urgent task to automate the process of detecting and blocking malicious URLs floating around the internet.
## Motivation
In order to protect against these threats, it is important to detect malicious URLs and to prevent individuals and organizations from accessing them. This can be accomplished through the use of various techniques, including URL reputation analysis, machine learning algorithms, and network security solutions. By detecting and blocking malicious URLs, individuals and organizations can better protect themselves and their sensitive information from cyber attacks. In this report, I aim to detect malicious URLs among the benign or safe ones based on various features of the URLs and the websites associated with them. The method will be based on Bayesian inference approach to take into account the past data.
## Main modeling idea
# Dataset
## Data Description
The dataset described in this manuscript is meant for such machine learning based analysis of malicious and benign webpages. The data has been collected from Internet using a specialized focused web crawler named MalCrawler [1]. The dataset comprises of various extracted attributes, and also raw webpage content including JavaScript code. It supports both supervised and unsupervised learning. For supervised learning, class labels for malicious and benign webpages have been added to the dataset using the Google Safe Browsing API.1 The most relevant attributes within the scope have already been extracted and included in this dataset. However, the raw web content, including JavaScript code included in this dataset supports further attribute extraction, if so desired. Also, this raw content and code can be used as unstructured data input for text-based analytics. This dataset consists of data from approximately 1.5 million webpages, which makes it suitable for deep learning algorithms. This article also provides code snippets used for data extraction and its analysis.
The dataset contains extracted attributes from websites that can be used for Classification of webpages as malicious or benign. The dataset also includes raw page content including JavaScript code that can be used as unstructured data in Deep Learning or for extracting further attributes. The data has been collected by crawling the Internet using MalCrawler [1]. The labels have been verified using the Google Safe Browsing API [2]. Attributes have been selected based on their relevance [3]. The details of dataset attributes is as given below:
'url' - The URL of the webpage.
'ip_add' - IP Address of the webpage.
'geo_loc' - The geographic location where the webpage is hosted.
'url_len' - The length of URL.
'js_len' - Length of JavaScript code on the webpage.
'js_obf_len - Length of obfuscated JavaScript code.
'tld' - The Top Level Domain of the webpage.
'who_is' - Whether the WHO IS domain information is compete or not.
'https' - Whether the site uses https or http.
'content' - The raw webpage content including JavaScript code.
'label' - The class label for benign or malicious webpage.
Python code for extraction of the above listed dataset attributes is attached.
The Visualisation of this dataset and it python code is also attached. This visualisation can be seen online on Kaggle
## Data source and analysis difference
Kaggle: https://www.kaggle.com/datasets/aksingh2411/dataset-of-malicious-and-benign-webpages
Data source: https://data.mendeley.com/datasets/gdx3pkwp47/2
https://www.researchgate.net/publication/347936136_Malicious_and_Benign_Webpages_Dataset
# Data cleaning
# Feature selection and transformation
```{r, include=FALSE}
library(rstan)
library(cmdstanr)
library(ggplot2)
library(dplyr)
library(tidyr)
library(grid)
library(gridExtra)
library(scales)
library(loo)
library(sentimentr)
library(stringr)
library(gridExtra)
library(MASS)
library(Metrics)
library(caret)
library(cvms)
library(tibble)
library(posterior)
library(purrr)
options(dplyr.summarise.inform = FALSE)
```
```{r setup, include = FALSE}
#knitr::opts_chunk$set(eval = TRUE)
#knitr::opts_chunk$set(eval = FALSE)
```
```{r}
train_websites <- read.csv("websites/train_websites.csv")
test_websites <- read.csv("websites/test_websites.csv")
train_websites_top_3 <- read.csv("websites/train_websites_top_3.csv")
test_websites_top_3 <- read.csv("websites/test_websites_top_3.csv")
```
```{r}
cat("Number of training data:",nrow(train_websites_top_3))
cat("\nNumber of testing data:",nrow(test_websites_top_3))
head(train_websites_top_3)
```
```{r,echo=FALSE}
# Count the number of rows for each combination of https and who_is
train_websites_count <- train_websites %>%
filter(label == "good")
numberOfMaliciousURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "good") %>%
group_by(https) %>%
summarize(count = n())
# Create a pie chart with a legend
p1 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(https))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = -1) +
scale_fill_manual(values = c("red", "green"), labels=c("no","yes")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("HTTPS in",numberOfMaliciousURLs,"benign URLs\n(yes/no)")) +
guides(fill=guide_legend(title=""))
# Count the number of rows for each combination of https and who_is
train_websites_count <- train_websites %>%
filter(label == "good")
numberOfBenignURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "good") %>%
group_by(who_is) %>%
summarize(count = n()) %>%
arrange(desc(count))
# Create a pie chart with a legend
p2 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(who_is))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = 1) +
scale_fill_manual(values = c("green", "red"), labels=c("complete","incomplete")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("WHOIS in",numberOfBenignURLs,"benign URLs\n(complete/incomplete)")) +
guides(fill=guide_legend(title=""))
train_websites_count <- train_websites %>%
filter(label == "good")
numberOfBenignURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "good") %>%
group_by(https, who_is) %>%
summarize(count = n())
# Create a pie chart with a legend
p3 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(https, who_is))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = 1) +
scale_fill_manual(values = c("blue", "green", "red", "orange")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("(HTTPS.WHOIS) pair combination\nin",numberOfBenignURLs,"benign URLs")) +
guides(fill=guide_legend(title="", nrow=2))
grid.arrange(p1, p2, p3, ncol = 3)
```
```{r, echo=FALSE}
# Count the number of rows for each combination of https and who_is
train_websites_count <- train_websites %>%
filter(label == "bad")
numberOfMaliciousURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "bad") %>%
group_by(https) %>%
summarize(count = n())
# Create a pie chart with a legend
p1 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(https))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = 1) +
scale_fill_manual(values = c("red", "green"), labels=c("no","yes")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("HTTPS in",numberOfMaliciousURLs,"malicious URLs\n(yes/no)")) +
guides(fill=guide_legend(title=""))
# Count the number of rows for each combination of https and who_is
train_websites_count <- train_websites %>%
filter(label == "bad")
numberOfMaliciousURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "bad") %>%
group_by(who_is) %>%
summarize(count = n()) %>%
arrange(desc(count))
# Create a pie chart with a legend
p2 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(who_is))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = -1) +
scale_fill_manual(values = c("green", "red"), labels=c("complete","incomplete")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("WHOIS in",numberOfMaliciousURLs,"malicious URLs\n(complete/incomplete)")) +
guides(fill=guide_legend(title=""))
# Count the number of rows for each combination of https and who_is
train_websites_count <- train_websites %>%
filter(label == "bad")
numberOfMaliciousURLs <- nrow(train_websites_count)
train_websites_count <- train_websites %>%
filter(label == "bad") %>%
group_by(https, who_is) %>%
summarize(count = n())
# Create a pie chart with a legend
p3 <- ggplot(train_websites_count, aes(x = "", y = count, fill = interaction(https, who_is))) +
geom_bar(width = 1, stat = "identity") +
coord_polar("y", start = 0, direction = 1) +
scale_fill_manual(values = c("blue", "green", "red", "orange")) +
theme(legend.position = "bottom") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10), legend.text = element_text(size = 8)) +
ggtitle(paste("(HTTPS.WHOIS) pair combination\nin",numberOfMaliciousURLs,"malicious URLs")) +
guides(fill=guide_legend(title="")) +
guides(fill=guide_legend(title="", nrow=2))
grid.arrange(p1, p2, p3, ncol = 3)
```
```{r, echo=FALSE}
ggplot(data = train_websites, aes(x = js_len, y = js_obf_len, color = label)) +
geom_point() +
scale_color_manual(values = c("red", "blue"),
labels = c("malicious", "benign"),
guide = guide_legend(title = "Label")) +
ggtitle("js_len vs js_obf_len") +
xlab("js_len") +
ylab("js_obf_len") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_vline(xintercept = 250, linetype = "dashed", color = "black") +
geom_hline(yintercept = 100, linetype = "dashed", color = "black") +
annotate("text", x = 260, y = Inf, label = "js_len = 250", hjust = 0, vjust = 1) +
annotate("text", x = Inf, y = 60, label = "js_ofs_len = 100", hjust = 1, vjust = 0)
#guides(color = guide_legend(title = "Label"))
```
```{r, include=FALSE}
# Set up the plotting grid for the coefficients
par(mfrow = c(2,1))
hist(train_websites$js_len, main = "JS length histogram of the recorded URLs", xlab = "Javascript length", breaks = 100)
hist(train_websites$js_obf_len, main ="Obfuscated JS length histogram of the recorded URLs", xlab = "Obfuscated Javascript length",breaks = 100)
```
```{r, echo=FALSE}
# Group the dataframe by geo_loc and count the number of rows for each country
train_websites_count <- train_websites %>%
group_by(geo_loc) %>%
summarize(count = n() ) %>%
top_n(3, count) %>%
slice_tail(n=3)
# Count the number of benign and malicious URLs for each country
train_websites_count_label <- train_websites %>%
filter(geo_loc %in% train_websites_count$geo_loc) %>%
group_by(geo_loc, label) %>%
summarize(count = n())
# Plot the bar chart
ggplot(train_websites_count_label, aes(x = geo_loc, y = count, fill = label)) +
geom_bar(stat = "identity", position = "stack") +
scale_fill_manual(values = c("red", "blue"),
labels = c("malicious","benign")) +
xlab("Country") +
ylab("Number of URLs") +
ggtitle("Distribution of benign and malicious URLs\n of top 3 recorded countries") +
guides(fill = guide_legend(title = "Label")) +
theme(plot.title = element_text(hjust = 0.5)) +
theme(axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.5, size = 10,
margin = margin(r = -20, unit = "pt"),
#family = "serif",
lineheight = 0.9, color = "black"))
```
```{r, echo=FALSE}
# Get unique country names
countries <- unique(train_websites_top_3$geo_loc)
# Get number of countries
K <- length(countries)
# Number of URLs per country, varying length of vector element
N_list = list()
# For each country, saving the number of URLs
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
N_list <- c(N_list, nrow(train_websites_country))
}
# Maximum length of training URLs for all countries
Nmax <- N_list[[which.max(N_list)]]
M_list = list()
# For each country, saving the number of URLs
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
M_list <- c(M_list, nrow(test_websites_country))
}
# Maximum length of training URLs for all countries
Mmax <- M_list[[which.max(M_list)]]
# The matrix of Javascript code
js_len_list = list()
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
js_len_list <- c(js_len_list, list(train_websites_country$js_len_bin))
}
js_len_list <- map(js_len_list, function(x) {return(c(x, rep(0, Nmax - length(x))))})
# The matrix of Javascript obfuscated code
js_obf_len_list = list()
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
js_obf_len_list <- c(js_obf_len_list, list(train_websites_country$js_obf_len_bin))
}
js_obf_len_list <- map(js_obf_len_list, function(x) {return(c(x, rep(0, Nmax - length(x))))})
# The matrix of safety level of the URL, varying length of vector element
https_list = list()
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
https_list <- c(https_list, list(train_websites_country$https_bin))
}
https_list <- map(https_list, function(x) {return(c(x, rep(0, Nmax - length(x))))})
# The matrix of safety level of the URL, varying length of vector element
whois_list = list()
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
whois_list <- c(whois_list, list(train_websites_country$whois_bin))
}
whois_list <- map(whois_list, function(x) {return(c(x, rep(0, Nmax - length(x))))})
# The matrix of Javascript code, varying length of vector element
js_len_test_list = list()
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
js_len_test_list <- c(js_len_test_list, list(test_websites_country$js_len_bin))
}
js_len_test_list <- map(js_len_test_list, function(x) {return(c(x, rep(0, Mmax - length(x))))})
# The matrix of Javascript code, varying length of vector element
js_obf_len_test_list = list()
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
js_obf_len_test_list <- c(js_obf_len_test_list, list(test_websites_country$js_obf_len_bin))
}
js_obf_len_test_list <- map(js_obf_len_test_list, function(x) {return(c(x, rep(0, Mmax - length(x))))})
# The matrix of safety level of the URL, varying length of vector element
https_test_list = list()
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
https_test_list <- c(https_test_list, list(test_websites_country$https_bin))
}
https_test_list <- map(https_test_list, function(x) {return(c(x, rep(0, Mmax - length(x))))})
# The matrix of safety level of the URL, varying length of vector element
whois_test_list = list()
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
whois_test_list <- c(whois_test_list, list(test_websites_country$whois_bin))
}
whois_test_list <- map(whois_test_list, function(x) {return(c(x, rep(0, Mmax - length(x))))})
# The matrix of label of the URL (malicious/benign), varying length of vector element
label_list = list()
for (country in countries) {
train_websites_country <- train_websites_top_3 %>%
filter(geo_loc == country)
label_list <- append(label_list, list(train_websites_country$label_bin))
}
label_list <- map(label_list, function(x) {return(c(x, rep(0, Nmax - length(x))))})
# The matrix of label of the URL (malicious/benign), varying length of vector element
label_test_list = list()
for (country in countries) {
test_websites_country <- test_websites_top_3 %>%
filter(geo_loc == country)
label_test_list <- append(label_test_list, list(test_websites_country$label_bin))
}
label_test_list <- map(label_test_list, function(x) {return(c(x, rep(0, Mmax - length(x))))})
```
```{r, echo=FALSE}
stan_data <- list(
Nmax = Nmax,
Mmax = Mmax,
K = K,
N_list = N_list,
M_list = M_list,
js_len_list = as.matrix(do.call(rbind, js_len_list)),
js_obf_len_list = as.matrix(do.call(rbind, js_obf_len_list)),
https_list = as.matrix(do.call(rbind, https_list)),
whois_list = as.matrix(do.call(rbind, whois_list)),
js_len_pred_list = as.matrix(do.call(rbind, js_len_test_list)),
js_obf_len_pred_list = as.matrix(do.call(rbind, js_obf_len_test_list)),
https_pred_list = as.matrix(do.call(rbind, https_test_list)),
whois_pred_list = as.matrix(do.call(rbind, whois_test_list)),
label_list = as.matrix(do.call(rbind, label_list))
)
```
# Separate model
## Model description
## Prior choice and justifications
Default protocol https is used by 81.5% of all the websites. @https://w3techs.com/technologies/details/ce-httpsdefault
There are 1.24 billion with complete WHOIS registration, while there are currently 1.7 billion websites. So the ratio of complete WHOIS website is 0.73.
## Stan code and running options
The Stan model code:
```{r, echo = TRUE, results = 'hide'}
"
data {
int<lower=1> Nmax; // Number of maximum URLs among all countries (training)
int<lower=1> Mmax; // Number of maximum URLs among all countries (testing)
int<lower=1> K; // Number of countries
array[K] int<lower=1> N_list; // Number of URLs of each country (training)
array[K] int<lower=1> M_list; // Number of URLs of each country (testing)
// The training features
array[K, Nmax] int<lower=0,upper=1> js_len_list;
array[K, Nmax] int<lower=0,upper=1> js_obf_len_list;
array[K, Nmax] int<lower=0,upper=1> https_list;
array[K, Nmax] int<lower=0,upper=1> whois_list;
// The testing predicting features
array[K, Mmax] int<lower=0,upper=1> js_len_pred_list;
array[K, Mmax] int<lower=0,upper=1> js_obf_len_pred_list;
array[K, Mmax] int<lower=0,upper=1> https_pred_list;
array[K, Mmax] int<lower=0,upper=1> whois_pred_list;
// label for each URL: benign(0) or malicious(1)
array[K, Nmax] int<lower=0,upper=1> label_list;
}
parameters {
array[K] real<lower=0, upper=1> theta_js_len; // probability for js_len
array[K] real<lower=0, upper=1> theta_js_obf_len; // probability for js_obf_len
array[K] real<lower=0, upper=1> theta_https; // probability for https
array[K] real<lower=0, upper=1> theta_whois; // probability for whois
array[K] real js_len_coeff; // Slope coefficient for js_len
array[K] real js_obf_len_coeff; // Slope coefficient for js_obf_len
array[K] real https_coeff; // Slope coefficient for https_coeff
array[K] real whois_coeff; // Slope coefficient for whois_coeff
array[K] real intercept; // Intercept coefficient
}
model {
// Prior probabilities of the features
for (k in 1:K){
theta_js_len[k] ~ beta(1,10);
theta_js_obf_len[k] ~ beta(1,10);
theta_https[k] ~ beta(8,10);
theta_whois[k] ~ beta(7,10);
}
// likelihood for the features
for (k in 1:K){
js_len_list[k, 1:N_list[k]] ~ bernoulli(theta_js_len[K]);
js_obf_len_list[k, 1:N_list[k]] ~ bernoulli(theta_js_obf_len[K]);
https_list[k, 1:N_list[k]] ~ bernoulli(theta_https[K]);
whois_list[k, 1:N_list[k]] ~ bernoulli(theta_whois[K]);
}
// priors of the coefficients
for (k in 1:K){
js_len_coeff[k] ~ cauchy(1,1);
js_obf_len_coeff[k] ~ cauchy(1,1);
https_coeff[k] ~ cauchy(-1,1);
whois_coeff[k] ~ cauchy(-1,1);
intercept[k] ~ normal(0,20);
}
// Modelling of the label based on bernoulli logistic regression by
// multiple variable linear regression
for (k in 1:K){
for (i in 1:N_list[k]){
label_list[k, i] ~ bernoulli(inv_logit(intercept[k]
+ https_coeff[k] * https_list[k, i]
+ whois_coeff[k] * whois_list[k, i]
+ js_len_coeff[k] * js_len_list[k, i]
+ js_obf_len_coeff[k] * js_obf_len_list[k, i]));
}
}
}
generated quantities {
array[K, Nmax] real label_train_pred;
array[K, Mmax] real label_test_pred;
array[Nmax] real log_likelihood;
// Predictions for the training data
for (k in 1:K){
for (i in 1:N_list[k]){
label_train_pred[k, i] = bernoulli_rng(inv_logit(intercept[k]
+ https_coeff[k] * https_list[k, i]
+ whois_coeff[k] * whois_list[k, i]
+ js_len_coeff[k] * js_len_list[k, i]
+ js_obf_len_coeff[k] * js_obf_len_list[k, i]));
}
}
// Predictions for the testing data
for (k in 1:K){
for (i in 1:M_list[k]){
label_test_pred[k, i] = bernoulli_rng(inv_logit(intercept[k]
+ https_coeff[k] * https_pred_list[k, i]
+ whois_coeff[k] * whois_pred_list[k, i]
+ js_len_coeff[k] * js_len_pred_list[k, i]
+ js_obf_len_coeff[k] * js_obf_len_pred_list[k, i]));
}
}
for (k in 1:K) {
if (N_list[k] == Nmax){
for (i in 1:Nmax){
log_likelihood[i] = bernoulli_lpmf(label_list[k, i] | inv_logit(intercept[k]
+ https_coeff[k] * https_list[k, i]
+ whois_coeff[k] * whois_list[k, i]
+ js_len_coeff[k] * js_len_list[k, i]
+ js_obf_len_coeff[k] * js_obf_len_list[k, i]));
}
}
}
}
"
```
```{r, echo=FALSE}
# Compiling the separate Stan model
file_separate <- file.path("models/model_separate.stan")
model_separate <- cmdstan_model(file_separate)
model_separate$compile(quiet = FALSE)
```
The sampling running options
```{r}
separate_sampling <- model_separate$sample(data = stan_data, chains=3, iter_warmup = 1000, iter_sampling = 500, refresh=0)
```
```{r, echo=FALSE}
# Set up the plotting grid
par(mfrow = c(3,5))
row_labels <- c("intercept", "HTTPS coefficient", "WHOIS coefficient","JS length coefficient","JS obf. length coefficient")
row_names <- c("intercept","https_coeff","whois_coeff","js_len_coeff","js_obf_len_coeff")
#separate_sampling$summary()
# Loop through the countries
for (j in 1:3){
for(i in 1:5){
# Create the subplot
hist(separate_sampling$draws(paste(row_names[i],"[",j,"]", sep="")),main = countries[j], xlab=row_labels[i])
# Add the country name to the top of the column
mtext(countries[j], side = 3, line = 0.2, outer = TRUE)
}
}
```
## Convergence diagnostics
MCMC convergence chains visualization
```{r, echo=FALSE}
plotConvergence <- function (draws, paramName){
chain1 <- as.vector(draws[1:500, 1])
chain2 <- as.vector(draws[501:1000, 1])
chain3 <- as.vector(draws[1001:1500, 1])
#chain4 <- as.vector(draws[3001:4000, 1])
iters = length(chain1)
indices <- 1:iters
data <- data.frame(indices, chain1, chain2,
chain3)
p <- ggplot(data, aes(x=indices)) +
ggtitle(paste("Separate model - Convergence of",paramName,"\n",iters,"sampling iterations, no warm-up")) +
xlab("iteration") +
ylab(paramName) +
theme(plot.title = element_text(hjust = 0.5, size=10), legend.position = "right") +
geom_line(aes(y = chain1, color = "chain1")) +
geom_line(aes(y = chain2, color = "chain2")) +
geom_line(aes(y = chain3, color = "chain3")) +
#geom_line(aes(y = chain4, color = "chain4")) +
scale_color_manual(name = "MCMC", values = c("chain1" = "red", "chain2" = "blue", "chain3" = "black"))
return(p)
}
intercept_draws <- separate_sampling$draws("intercept[1]", format = "matrix")
p1 <- plotConvergence(intercept_draws, "intercept")
https_draws <- separate_sampling$draws("https_coeff[1]", format = "matrix")
p2 <- plotConvergence(https_draws, "https coefficient")
grid.arrange(p1, p2, ncol = 2)
# whois_draws <- separate_sampling$draws("whois_coeff[1]", format = "matrix")
# plotConvergence(whois_draws, "whois coefficient")
#js_len_draws <- separate_sampling$draws("js_len_coeff[1]", format = "matrix")
#plotConvergence(js_len_draws, "JS length coefficient")
#js_obf_len_draws <- separate_sampling$draws("js_obf_len_coeff[1]", format = "matrix")
#plotConvergence(js_obf_len_draws, "JS obfuscated length coefficient")
```
HMC specific convergence diagnostics
```{r, eval=TRUE, include=TRUE}
separate_sampling$diagnostic_summary()
```
$\hat{R}$-values and effective sample size
```{r, echo=FALSE}
summaryDiagnostics = data.frame()
for (i in 1:K){
summaryDiagnosticsCountry <- separate_sampling$summary(c(paste("intercept[",i,"]", sep=""), paste("https_coeff[",i,"]", sep=""),paste("whois_coeff[",i,"]", sep=""),paste("js_len_coeff[",i,"]", sep=""),paste("js_obf_len_coeff[",i,"]", sep="")))[, c("variable", "rhat", "ess_bulk", "ess_tail")]
summaryDiagnostics <- rbind(summaryDiagnostics, summaryDiagnosticsCountry)
}
summaryDiagnostics$variable <- 1:15
summaryDiagnostics <- summaryDiagnostics %>% rename(index = variable)
p1 <- ggplot(summaryDiagnostics, aes(x = index, y = rhat)) +
geom_point() +
geom_hline(yintercept = 1.05, color = "red") +
annotate("text", x = 15, y = 1.055, label = "critical Rhat value = 1.05",
hjust = 1, color = "red") +
xlab("Index") +
ylab("Rhat values") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10)) +
ggtitle("Rhat values\n of all coefficients")
p2 <- ggplot(summaryDiagnostics, aes(x = index, y = ess_bulk)) +
geom_point() +
geom_hline(yintercept = 1500, color = "red") +
annotate("text", x = 17, y = 1400, label = "Samples = 1500",
hjust = 1, color = "red") +
xlab("Index") +
ylab("Bulk ESS values") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.title = element_text(size = 10)) +
ggtitle("Bulk effective sample size\n of all coefficients")
#p3 <- ggplot(summaryDiagnostics, aes(x = index, y = ess_tail)) +
# geom_point() +
# geom_hline(yintercept = 1500, color = "red") +
# annotate("text", x = 17, y = 1400, label = "Samples = 1500",
# hjust = 1, color = "red") +
# xlab("Index") +
# ylab("Tail ESS values") +
# theme(plot.title = element_text(hjust = 0.5)) +
# theme(plot.title = element_text(size = 10)) +
# ggtitle("Tail effective sample size\n of all coefficients")
grid.arrange(p1, p2, ncol = 2)
```
## Posterior predictive checks
```{r, echo=FALSE}
inv_logit <- function(vec){
return(1/(1+exp(-vec)))
}
bernoulli_logit <- function (intercept, js_len_coeff, js_obf_len_coeff, https_coeff, whois_coeff, js_len_truncated, js_obf_len_truncated, https_truncated, whois_truncated){
probability <- inv_logit(intercept + js_len_coeff * js_len_truncated + js_obf_len_coeff * js_obf_len_truncated + https_coeff * https_truncated + whois_coeff * whois_truncated)
classification <- ifelse(probability >= 0.5, 1, 0)
return(classification)
}
```
```{r, echo=FALSE}
metricsName <- c("Accuracy", "Precision", "Recall", "F1")
metricsSummary <- data.frame(Metrics=metricsName)
metricsSummary$Accuracy <- NULL
metricsSummary$Precision <- NULL
metricsSummary$Recall <- NULL
metricsSummary$F1 <- NULL
#print(metricsSummary)
sumTP <- 0
sumTN <- 0
sumFP <- 0
sumFN <- 0
listTrue <- c()
listPred <- c()
for (k in 1:K){
#predicted_train = c()
#for (i in 1:N_list[[k]]){
# draws <- separate_sampling$draws(paste("label_train_pred[",k,",",i,"]", sep=""), format = "matrix")
# predicted_train <- c(predicted_train, as.vector(draws[1, ]))
#}
#true_train = label_list[[k]][1:N_list[[k]]]
#confusion_matrix <- table(predicted_train, true_train)
true_train = label_list[[k]][1:N_list[[k]]]
js_len_truncated <- js_len_list[[k]][1:N_list[[k]]]
js_obf_len_truncated <- js_obf_len_list[[k]][1:N_list[[k]]]
https_truncated <- https_list[[k]][1:N_list[[k]]]
whois_truncated <- whois_list[[k]][1:N_list[[k]]]
intercept <- mean(as.vector(separate_sampling$draws(paste("intercept[",k,"]",sep=""), format = "matrix")[, 1]))
js_len_coeff <- mean(as.vector(separate_sampling$draws(paste("js_len_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
js_obf_len_coeff <- mean(as.vector(separate_sampling$draws(paste("js_obf_len_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
https_coeff <- mean(as.vector(separate_sampling$draws(paste("https_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
whois_coeff <- mean(as.vector(separate_sampling$draws(paste("whois_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
predicted_train <- bernoulli_logit(intercept, js_len_coeff, js_obf_len_coeff, https_coeff, whois_coeff, js_len_truncated, js_obf_len_truncated, https_truncated, whois_truncated)
listTrue <- c(listTrue, true_train)
listPred <- c(listPred, predicted_train)
confusion_matrix <- table(predicted_train, true_train)
TP <- confusion_matrix[2,2]
TN <- confusion_matrix[1,1]
FP <-confusion_matrix[1,2]
FN <-confusion_matrix[2,1]
#cat(TP,TN,FP,FN)
sumTP <- sumTP + TP
sumTN <- sumTN + TN
sumFP <- sumFP + FP
sumFN <- sumFN + FN
accuracy <- (TP+TN)/(TP+FP+FN+TN)
precision <- TP/(TP+FP)
recall <- TP/(TP+FN)
f1 <- 2*(precision*recall)/(precision+recall)
metricsSummary[,countries[k]] <- c(accuracy, precision,recall,f1)
}
accuracy <- (sumTP+sumTN)/(sumTP+sumFP+sumFN+sumTN)
prevalence <- (sumTP+sumFN)/(sumTP+sumFP+sumFN+sumTN)
sensitivity <- sumTP/(sumTP+sumFN)
specificity <- sumFN/(sumFN+sumFP)
precision <- sumTP/(sumTP+sumFP)
recall <- sumTP/(sumTP+sumFN)
f1 <- 2*(precision*recall)/(precision+recall)
metricsSummary[,"All countries"] <- c(accuracy, precision,recall,f1)
metricsSummary
```
```{r, warning = FALSE}
confusion_matrix <- tibble("actual" = listTrue,
"prediction" = listPred)
basic_table <- table(confusion_matrix)
cfm <- as_tibble(basic_table)
plot_confusion_matrix(cfm,
target_col = "actual",
prediction_col = "prediction",
counts_col = "n", palette = "Oranges")
```
## Predictive performance assessment
```{r, echo=FALSE}
metricsName <- c("Accuracy", "Precision", "Recall", "F1")
metricsSummary <- data.frame(Metrics=metricsName)
metricsSummary$Accuracy <- NULL
metricsSummary$Precision <- NULL
metricsSummary$Recall <- NULL
metricsSummary$F1 <- NULL
#print(metricsSummary)
sumTP <- 0
sumTN <- 0
sumFP <- 0
sumFN <- 0
listTrue <- c()
listPred <- c()
for (k in 1:K){
# predicted_test = c()
# for (i in 1:M_list[[k]]){
# draws <- separate_sampling$draws(paste("label_test_pred[",k,",",i,"]", sep=""), format = "matrix")
# predicted_test <- c(predicted_test, as.vector(draws[1, ]))
# }
true_test = label_test_list[[k]][1:M_list[[k]]]
js_len_truncated <- js_len_test_list[[k]][1:M_list[[k]]]
js_obf_len_truncated <- js_obf_len_test_list[[k]][1:M_list[[k]]]
https_truncated <- https_test_list[[k]][1:M_list[[k]]]
whois_truncated <- whois_test_list[[k]][1:M_list[[k]]]
intercept <- mean(as.vector(separate_sampling$draws(paste("intercept[",k,"]",sep=""), format = "matrix")[, 1]))
js_len_coeff <- mean(as.vector(separate_sampling$draws(paste("js_len_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
js_obf_len_coeff <- mean(as.vector(separate_sampling$draws(paste("js_obf_len_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
https_coeff <- mean(as.vector(separate_sampling$draws(paste("https_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
whois_coeff <- mean(as.vector(separate_sampling$draws(paste("whois_coeff[",k,"]",sep=""), format = "matrix")[, 1]))
predicted_test <- bernoulli_logit(intercept, js_len_coeff, js_obf_len_coeff, https_coeff, whois_coeff, js_len_truncated, js_obf_len_truncated, https_truncated, whois_truncated)
confusion_matrix <- table(predicted_test, true_test)
listTrue <- c(listTrue, true_test)
listPred <- c(listPred, predicted_test)
TP <- confusion_matrix[2,2]
TN <- confusion_matrix[1,1]
FP <-confusion_matrix[1,2]
FN <-confusion_matrix[2,1]
sumTP <- sumTP + TP
sumTN <- sumTN + TN
sumFP <- sumFP + FP
sumFN <- sumFN + FN
accuracy <- (TP+TN)/(TP+FP+FN+TN)
precision <- TP/(TP+FP)
recall <- TP/(TP+FN)
f1 <- 2*(precision*recall)/(precision+recall)
metricsSummary[,countries[k]] <- c(accuracy, precision,recall,f1)
}
accuracy <- (sumTP+sumTN)/(sumTP+sumFP+sumFN+sumTN)
prevalence <- (sumTP+sumFN)/(sumTP+sumFP+sumFN+sumTN)
sensitivity <- sumTP/(sumTP+sumFN)
specificity <- sumFN/(sumFN+sumFP)
precision <- sumTP/(sumTP+sumFP)
recall <- sumTP/(sumTP+sumFN)
f1 <- 2*(precision*recall)/(precision+recall)
metricsSummary[,"All countries"] <- c(accuracy, precision,recall,f1)
metricsSummary
```
```{r, echo=FALSE}
confusion_matrix <- tibble("actual" = listTrue,
"prediction" = listPred)
basic_table <- table(confusion_matrix)
cfm <- as_tibble(basic_table)
plot_confusion_matrix(cfm,
target_col = "actual",
prediction_col = "prediction",
counts_col = "n", palette = "Oranges")
```
## Prior sensitivity analysis
```{r, echo=FALSE}
# Compiling the separate Stan model
file_separate_prior_sensitivity1 <- file.path("models/model_separate_prior_sensitivity_1.stan")
model_separate_prior_sensitivity1 <- cmdstan_model(file_separate_prior_sensitivity1)
model_separate_prior_sensitivity1$compile(quiet = FALSE)
file_separate_prior_sensitivity2 <- file.path("models/model_separate_prior_sensitivity_2.stan")
model_separate_prior_sensitivity2 <- cmdstan_model(file_separate_prior_sensitivity2)
model_separate_prior_sensitivity2$compile(quiet = FALSE)
```
The sampling running options
```{r}
separate_sampling_prior_sensitivity1 <- model_separate_prior_sensitivity1$sample(data = stan_data, chains=3, iter_warmup = 1000, iter_sampling = 500, refresh=0)
separate_sampling_prior_sensitivity2 <- model_separate_prior_sensitivity2$sample(data = stan_data, chains=3, iter_warmup = 1000, iter_sampling = 500, refresh=0)
```
```{r, warning=FALSE, echo=FALSE}
loo_loglike_separate0 <- separate_sampling$loo(variables="log_likelihood",r_eff=TRUE)
loo_loglike_separate1 <- separate_sampling_prior_sensitivity1$loo(variables="log_likelihood",r_eff=TRUE)
loo_loglike_separate2 <- separate_sampling_prior_sensitivity2$loo(variables="log_likelihood",r_eff=TRUE)
cat("The elpd value of the separate model 0 is\n")
print(loo_loglike_separate0$estimates[1][1])
cat("The elpd value of the separate model 1 is\n")
print(loo_loglike_separate1$estimates[1][1])
cat("The elpd value of the separate model 2 is\n")
print(loo_loglike_separate2$estimates[1][1])
```
```{r}
loo_compare_separate <- loo_compare(x = list(loo_loglike_separate0=loo_loglike_separate0, loo_loglike_separate1=loo_loglike_separate1, loo_loglike_separate2=loo_loglike_separate2))
print(loo_compare_separate)
```
We know that elpd_loo is the Bayesian LOO estimate of the expected log pointwise predictive density and is a sum of N individual pointwise log predictive densities. From the comparison table, elpd_diff is the difference in elpd_loo for two models. If more than two models are compared, the difference is computed relative to the model with highest elpd_loo, which is true in this case, as I am comparing three models
The standard error of component-wise differences of elpd_loo (Eq 24 in VGG2017) between two models. This SE is smaller than the SE for individual models due to correlation (i.e., if some observations are easier and some more difficult to predict for all models)
As quick rule: If elpd difference (elpd_diff in loo package) is less than 4, the difference is small. If elpd difference (elpd_diff in loo package) is larger than 4, then compare that difference to standard error of elpd_diff. When the difference (elpd_diff) is larger than 4, the number of observations is larger than 100 and the model is not badly misspecified then normal approximation and SE are quite reliable description of the uncertainty in the difference. Differences smaller than 4 are small and then the models have very similar predictive performance and it doesn’t matter if the normal approximation fails or SE is underestimated @akiLoo.
SE assumes that normal approximation describes well the uncertainty related to the expected difference. Due to cross-validation folds not being independent, SE tends to be underestimated especially if the number of observations is small or the models are badly misspecified. The whole normal approximation tends to fail if the models are very similar or the models are badly misspecified.
# Pooled model
## Model description
## Prior choice and justifications
Default protocol https is used by 81.5% of all the websites. @https://w3techs.com/technologies/details/ce-httpsdefault
There are 1.24 billion with complete WHOIS registration, while there are currently 1.7 billion websites. So the ratio of complete WHOIS website is 0.73.
## Stan code and running options
The Stan model code:
```{r, echo = TRUE, results = 'hide'}
"
"
```
```{r, echo=FALSE}
# Compiling the separate Stan model
file_pooled <- file.path("models/model_pooled.stan")
model_pooled <- cmdstan_model(file_pooled)
model_pooled$compile(quiet = FALSE)
```
The sampling running options
```{r}
pooled_sampling <- model_pooled$sample(data = stan_data, chains=3,
iter_warmup = 500, iter_sampling = 1000, refresh=0)
```
```{r, echo=FALSE}
# Set up the plotting grid
par(mfrow = c(3,3))
row_labels <- c(" θ(JS length)","θ(JS obf. length)","θ(https)","θ(whois)", "intercept", "HTTPS coefficient", "WHOIS coefficient","JS length coefficient","JS obf. length coefficient")
row_names <- c("theta_js_len","theta_js_obf_len","theta_https","theta_whois","intercept","https_coeff","whois_coeff","js_len_coeff","js_obf_len_coeff")
#pooled_sampling$summary()
# Loop through the countries
for(i in 1:9){
# Create the subplot
hist(pooled_sampling$draws(row_names[i]), main=row_labels[i], xlab=row_labels[i])
}
```
## Convergence diagnostics
MCMC convergence chains visualization
```{r, echo=FALSE}
plotConvergence <- function (draws, paramName){
chain1 <- as.vector(draws[1:500, 1])
chain2 <- as.vector(draws[501:1000, 1])
chain3 <- as.vector(draws[1001:1500, 1])
iters = length(chain1)
indices <- 1:iters
data <- data.frame(indices, chain1, chain2,
chain3)
p <- ggplot(data, aes(x=indices)) +
ggtitle(paste("Pooled model - Convergence of",paramName,"\n",iters,"sampling iterations, no warm-up")) +
xlab("iteration") +
ylab(paramName) +
theme(plot.title = element_text(hjust = 0.5, size=10), legend.position = "right") +
geom_line(aes(y = chain1, color = "chain1")) +
geom_line(aes(y = chain2, color = "chain2")) +
geom_line(aes(y = chain3, color = "chain3")) +
#geom_line(aes(y = chain4, color = "chain4")) +
scale_color_manual(name = "MCMC", values = c("chain1" = "red", "chain2" = "blue", "chain3" = "black"))
return(p)
}
intercept_draws <- pooled_sampling$draws("intercept", format = "matrix")