-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_cleaning.Rmd
420 lines (322 loc) · 14.3 KB
/
data_cleaning.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
---
title: "BDA Project: Malicious and Benign Website URL detections"
author: "Nguyen Xuan Binh, Duong Le"
date: "January 2023"
output:
pdf_document:
toc: yes
toc_depth: 3
fig_caption: yes
bibliography: bibliography.bib
---
```{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)
options(dplyr.summarise.inform = FALSE)
```
```{r}
# Load the dataframe
train_websites <- read.csv("websites/Webpages_Classification_train_data.csv")
test_websites <- read.csv("websites/Webpages_Classification_test_data.csv")
train_websites <- na.omit(train_websites)
test_websites <- na.omit(test_websites)
```
```{r}
# Filter the test_websites to keep only rows where label is "bad"
bad_rows <- train_websites %>% filter(label == "bad")
# Randomly select 300 rows from bad rows
bad_sample_rows <- bad_rows %>% sample_n(30)
# Filter the test_websites to keep only rows where label is "good"
good_rows <- train_websites %>% filter(label == "good")
# Randomly select 1200 rows from good_rows
good_sample_rows <- good_rows %>% sample_n(90)
# Concatenate bad_rows and sample_rows to create the new train_websites
train_websites <- rbind(bad_sample_rows, good_sample_rows)
```
```{r}
# Filter the test_websites to keep only rows where label is "bad"
bad_rows <- test_websites %>% filter(label == "bad")
# Randomly select 300 rows from bad rows
bad_sample_rows <- bad_rows %>% sample_n(50)
# Filter the test_websites to keep only rows where label is "good"
good_rows <- test_websites %>% filter(label == "good")
# Randomly select 1200 rows from good_rows
good_sample_rows <- good_rows %>% sample_n(200)
# Concatenate bad_rows and sample_rows to create the new test_websites
test_websites <- rbind(bad_sample_rows, good_sample_rows)
```
```{r}
print(sum(train_websites$label=="bad"))
print(sum(train_websites$label=="good"))
print(sum(test_websites$label=="bad"))
print(sum(test_websites$label=="good"))
```
```{r}
# Define the special characters you want to count
special_chars <- c("/","%", "#", "&”, “." , "," ,"=")
# Create a new column "num_special" and populate it with the number of special characters in each URL
train_websites$num_special <- sapply(train_websites$url, function(x) sum(str_count(x, paste(special_chars, collapse="|"))))
test_websites$num_special <- sapply(test_websites$url, function(x) sum(str_count(x, paste(special_chars, collapse="|"))))
```
```{r}
print(colnames(train_websites))
keptColNames = c("label", "url_len", "geo_loc", "https", "js_len", "js_obf_len", "who_is", "num_special")
train_websites <- train_websites[, keptColNames]
keptColNames = c("label", "url_len", "geo_loc", "https", "js_len", "js_obf_len", "who_is", "num_special")
test_websites <- test_websites[, keptColNames]
print(colnames(train_websites))
```
```{r}
print(head(train_websites))
```
```{r}
write.csv(train_websites, "websites/train_websites.csv")
write.csv(test_websites, "websites/test_websites.csv")
```
```{r}
train_websites <- read.csv("websites/train_websites.csv")
test_websites <- read.csv("websites/test_websites.csv")
```
```{r}
# 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)) +
ggtitle(paste("HTTPS in",numberOfMaliciousURLs,"benign URLs\n(yes/no for HTTPS)")) +
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)) +
ggtitle(paste("WHOIS in",numberOfBenignURLs,"benign URLs\n(complete/incomplete for WHOIS)")) +
guides(fill=guide_legend(title=""))
grid.arrange(p1, p2, ncol = 2)
```
```{r}
# 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(https, who_is) %>%
summarize(count = n())
# Create a pie chart with a legend
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)) +
ggtitle(paste("(HTTPS.WHOIS) pair combination in",numberOfBenignURLs,"benign URLs\n(yes/no for HTTPS and complete/incomplete for WHOIS)")) +
guides(fill=guide_legend(title=""))
```
```{r}
# 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)) +
ggtitle(paste("HTTPS in",numberOfMaliciousURLs,"malicious URLs\n(yes/no for HTTPS)")) +
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)) +
ggtitle(paste("WHOIS in",numberOfMaliciousURLs,"malicious URLs\n(complete/incomplete for WHOIS)")) +
guides(fill=guide_legend(title=""))
grid.arrange(p1, p2, ncol = 2)
```
```{r}
# 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
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)) +
ggtitle(paste("(HTTPS.WHOIS) pair combination in",numberOfMaliciousURLs,"malicious URLs\n(yes/no for HTTPS and complete/incomplete for WHOIS)")) +
guides(fill=guide_legend(title=""))
```
```{r}
ggplot(data = train_websites %>% filter(label == "good") , aes(x = js_len, y = js_obf_len)) +
geom_point() +
ggtitle("num_special vs url_len") +
xlab("js_len") +
ylab("js_obf_len") +
theme(plot.title = element_text(hjust = 0.5))
```
```{r}
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}
hist(train_websites$js_len, main = "JS length histogram of the recorded URLs", breaks = 100)
hist(train_websites$js_obf_len, main ="Obfuscated JS length histogram of the recorded URLs", breaks = 100)
```
```{r}
# 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 of top 5 recorded countries") +
guides(fill = guide_legend(title = "Label")) +
theme(axis.text.x = element_text(angle = 30, hjust = 0.5, vjust = 0.5, size = 10,
margin = margin(r = -20, unit = "pt"),
family = "serif",
lineheight = 0.9, color = "black"))
```
```{r}
train_websites_top_3 <- train_websites %>%
filter(geo_loc %in% train_websites_count$geo_loc)
test_websites_top_3 <- test_websites %>%
filter(geo_loc %in% train_websites_count$geo_loc)
print(nrow(train_websites_top_3))
print(nrow(test_websites_top_3))
```
```{r}
#train_websites_top_3$safety <- ifelse(train_websites_top_3$https == "yes" &
# train_websites_top_3$who_is == "complete", 1,
# ifelse(train_websites_top_3$https == "no"
# & train_websites_top_3$who_is == "complete", 2,
# ifelse(train_websites_top_3$https == "yes" &
# train_websites_top_3$who_is == "incomplete", 3, 4)))
#test_websites_top_3$safety <- ifelse(test_websites_top_3$https == "yes" &
# test_websites_top_3$who_is == "complete", 1,
# ifelse(test_websites_top_3$https == "no"
# & test_websites_top_3$who_is == "complete", 2,
# ifelse(test_websites_top_3$https == "yes" &
# test_websites_top_3$who_is == "incomplete", 3, 4)))
train_websites_top_3 <- rename(train_websites_top_3, whois = who_is)
test_websites_top_3 <- rename(test_websites_top_3, whois = who_is)
train_websites_top_3$https_bin <- ifelse(train_websites_top_3$https == "yes", 0, 1)
train_websites_top_3$whois_bin <- ifelse(train_websites_top_3$whois == "complete", 0, 1)
test_websites_top_3$https_bin <- ifelse(test_websites_top_3$https == "yes", 0, 1)
test_websites_top_3$whois_bin <- ifelse(test_websites_top_3$whois == "complete", 0, 1)
keptColNames = c("label", "geo_loc", "js_len", "js_obf_len", "https", "https_bin", "whois", "whois_bin")
train_websites_top_3 <- train_websites_top_3[, keptColNames]
test_websites_top_3 <- test_websites_top_3[, keptColNames]
print(train_websites_top_3)
print(test_websites_top_3)
```
```{r}
#train_websites_top_3$js <- ifelse(train_websites_top_3$js_len < 250 & train_websites_top_3$js_obf_len >= 100, 0,
# ifelse(train_websites_top_3$js_len < 250 & #train_websites_top_3$js_obf_len < 100, 1, 2))
#test_websites_top_3$js <- ifelse(test_websites_top_3$js_len < 250 & test_websites_top_3$js_obf_len >= 100, 0,
# ifelse(test_websites_top_3$js_len < 250 & #test_websites_top_3$js_obf_len < 100, 1, 2))
train_websites_top_3$js_len_bin <- ifelse(train_websites_top_3$js_len < 250, 0, 1)
train_websites_top_3$js_obf_len_bin <- ifelse(train_websites_top_3$js_obf_len < 100, 0, 1)
test_websites_top_3$js_len_bin <- ifelse(test_websites_top_3$js_len < 250, 0, 1)
test_websites_top_3$js_obf_len_bin <- ifelse(test_websites_top_3$js_obf_len < 100, 0, 1)
print(train_websites_top_3)
print(test_websites_top_3)
```
```{r}
print(head(train_websites_top_3))
print(head(test_websites_top_3))
```
```{r}
#train_websites_top_3 <- train_websites_top_3 %>% mutate(label = ifelse(label == "good", 0, 1))
#test_websites_top_3 <- test_websites_top_3 %>% mutate(label = ifelse(label == "good", 0, 1))
train_websites_top_3$label_bin <- ifelse(train_websites_top_3$label == "good", 0, 1)
test_websites_top_3$label_bin <- ifelse(test_websites_top_3$label == "good", 0, 1)
print(head(test_websites_top_3))
```
```{r}
write.csv(train_websites_top_3, "websites/train_websites_top_3.csv")
write.csv(test_websites_top_3, "websites/test_websites_top_3.csv")
```
```{r}
train_websites_top_3 <- read.csv("websites/train_websites_top_3.csv")
test_websites_top_3 <- read.csv("websites/test_websites_top_3.csv")
```