-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures_extraction.py
369 lines (327 loc) · 10.2 KB
/
features_extraction.py
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
#!/usr/bin/env python
# coding: utf-8
import time
from urllib.parse import urlparse
import bs4
import pandas as pd
import re
from bs4 import BeautifulSoup
import whois
import urllib
import numpy as np
import joblib
import csv
from googlesearch import search
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn import metrics
import urllib.request
#import urllib2.urlopen
from urllib.request import urlopen
#from urllib2 import urlopen
import requests
from datetime import datetime
from tld import get_tld, get_fld
import warnings
warnings.filterwarnings("ignore")
urldata = pd.read_csv('finaldata.csv')
def getDomain(url):
domain = urlparse(url).netloc
if re.match(r"^www.",domain):
domain = domain.replace("www.","")
return domain
def havingIP(url):
match = re.search(
'(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.'
'([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\/)|' # IPv4
'((0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\.(0x[0-9a-fA-F]{1,2})\\/)' # IPv4 in hexadecimal
'(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}', url) # Ipv6
if match:
return 1
else:
return 0
def haveAtSign(url):
if "@" in url:
at = 1
else:
at = 0
return at
# 4 Length of URL (URL_Length)
def getLength(url):
if len(url) < 54:
length = 0
else:
length = 1
return length
# 5 Depth of URL - Gives number of '/' in URL (URL_Depth)
def getDepth(url):
s = urlparse(url).path.split('/')
depth = 0
for j in range(len(s)):
if len(s[j]) != 0:
depth = depth+1
return depth
# 6 Redirection "//" in URL (Redirection)
def redirection(url):
pos = url.rfind('//')
if pos > 6:
if pos > 7:
return 1
else:
return 0
else:
return 0
# 7 "http/https" in Domain name (https_Domain)
def httpDomain(url):
domain = urlparse(url).netloc
if 'https' in domain:
return 0
else:
return 1
def prefixSuffix(url):
if '-' in urlparse(url).netloc:
return 1
else:
return 0
# 10 Web Traffic
def web_traffic(url):
try:
#Filling the whitespaces in the URL if any
url = urllib.parse.quote(url,safe='/', encoding=None, errors=None)
#print(url)
#rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find("REACH")['RANK']
rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "lxml-xml").find("REACH")['RANK']
except TypeError:
return 1
if rank <100000:
return 0
else:
return 1
def domainAge(url):
try:
domain_name = whois.whois(urlparse(url).netloc)
except:
return 1
creation_date = domain_name.creation_date
expiration_date = domain_name.expiration_date
if (isinstance(creation_date,str) or isinstance(expiration_date,str)):
try:
creation_date = datetime.strptime(creation_date,'%Y-%m-%d')
expiration_date = datetime.strptime(expiration_date,"%Y-%m-%d")
except:
return 1
if ((expiration_date is None) or (creation_date is None)):
return 1
elif ((type(expiration_date) is list) or (type(creation_date) is list)):
return 1
else:
ageofdomain = abs((expiration_date - creation_date).days)
if ((ageofdomain/30) < 6):
age = 1
else:
age = 0
return age
def domainEnd(url):
try:
domain_name = whois.whois(urlparse(url).netloc)
except:
return 1
expiration_date = domain_name.expiration_date
if isinstance(expiration_date,str):
try:
expiration_date = datetime.strptime(expiration_date,"%Y-%m-%d")
except:
return 1
if (expiration_date is None):
return 1
elif (type(expiration_date) is list):
return 1
else:
today = datetime.now()
end = abs((expiration_date - today).days)
if ((end/30) < 6):
end = 1
else:
end = 0
return end
def iframe(response):
if response == "":
return 1
else:
if re.findall(r"[<iframe>|<frameBorder>]", response.text):
return 0
else:
return 1
def mouseOver(response):
if response == "" :
return 1
else:
if re.findall("<script>.+onmouseover.+</script>", response.text):
return 1
else:
return 0
def rightClick(response):
if response == "":
return 1
else:
if re.findall(r"event.button ?== ?2", response.text):
return 0
else:
return 1
def forwarding(response):
if response == "":
return 1
else:
if len(response.history) <= 2:
return 0
else:
return 1
def get_email(url):
try:
domain_name = whois.whois(urlparse(url).netloc)
email = domain_name.emails
if (email is None):
flag = 1
else:
flag = 0
except:
flag = 1
return flag
def google_index(url):
site = search(url, 5)
return 0 if site else 1
def achieve_subdomain(url):
if havingIP(url)==0:
try:
return get_tld(url,as_object=True).subdomain
except:
return None
def achieve_tld(url):
if havingIP(url)==0:
try:
return get_tld(url,as_object=True).tld
except:
return None
def achieve_fld(url):
if havingIP(url)==0:
try:
return get_fld(url,as_object=True)
except:
return None
def url_length(url):
return len(url)
def path_length(url):
return len(urlparse(url).path)
def hostname_length(url):
return len(urlparse(url).netloc)
def alpha_count(url):
alpha = 0
for i in url:
if i.isalpha():
alpha += 1
return alpha
def digit_count(url):
digits = 0
for i in url:
if i.isdigit():
digits = digits + 1
return digits
def count1(url):
return url.count('.')
def count2(url):
return url.count('@')
def count3(url):
return url.count('-')
def count4(url):
return url.count('%')
def count5(url):
return url.count('?')
def count6(url):
return url.count('=')
def num_of_dir(url):
urldir = urlparse(url).path
return urldir.count('/')
def first_dir_length(url):
urlpath= urlparse(url).path
try:
return len(urlpath.split('/')[1])
except:
return 0
# Function to extract features
def featureExtraction(url):
features = []
features.append(havingIP(url))
features.append(haveAtSign(url))
features.append(getLength(url))
features.append(getDepth(url))
features.append(redirection(url))
features.append(httpDomain(url))
features.append(prefixSuffix(url))
features.append(domainAge(url))
features.append(domainEnd(url))
features.append(get_email(url))
features.append(google_index(url))
features.append(web_traffic(url))
features.append(len(str(achieve_subdomain(url))))
features.append(len(str(achieve_tld(url))))
features.append(len(str(achieve_tld(url))))
features.append(path_length(url))
features.append(hostname_length(url))
features.append(alpha_count(url))
features.append(digit_count(url))
features.append(url_length(url) - (alpha_count(url) + digit_count(url)))
features.append(count1(url))
features.append(count2(url))
features.append(count3(url))
features.append(count4(url))
features.append(count5(url))
features.append(count6(url))
features.append(num_of_dir(url))
features.append(first_dir_length(url))
features.append(alpha_count(url) / url_length(url))
features.append(digit_count(url) / url_length(url))
features.append((url_length(url) - (alpha_count(url) + digit_count(url))) / url_length(url))
return features
# Create the dataframe to save the features
result = pd.DataFrame(columns=['use_of_ip', 'have_@', 'url_length', 'dir_depth', 'is_redirection',
'is_https', 'have_-', 'domain_Age', 'domain_End', 'email',
'google_index', 'web_traffic', 'subdomain_len', 'tld_len', 'fld_len',
'url_path_len', 'hostname_len', 'url_alphas', 'url_digits', 'url_puncs',
'count.', 'count@', 'count-', 'count%', 'count?', 'count=',
'count_dirs', 'first_dir_len', 'pc_alphas', 'pc_digits', 'pc_puncs',
'result'], index=[0])
with open('finaldata.csv',encoding='ISO-8859-1') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
# extract the features of data
for i in range(len(urldata)):
#url = rows[i+1][0]
try:
url = rows[i+1][0]
feature = featureExtraction(url)
result.loc[i-1,['use_of_ip', 'have_@', 'url_length', 'dir_depth', 'is_redirection',
'is_https', 'have_-', 'domain_Age', 'domain_End', 'email',
'google_index', 'web_traffic', 'subdomain_len', 'tld_len', 'fld_len',
'url_path_len', 'hostname_len', 'url_alphas', 'url_digits', 'url_puncs',
'count.', 'count@', 'count-', 'count%', 'count?', 'count=',
'count_dirs', 'first_dir_len', 'pc_alphas', 'pc_digits', 'pc_puncs']]=feature[0:31]
result['result'][i-1] = urldata['result'][i]
time.sleep(1)
except Exception:
pass
# Transform data types
for item in ['use_of_ip', 'have_@', 'url_length', 'dir_depth', 'is_redirection',
'is_https', 'have_-', 'domain_Age', 'domain_End', 'email',
'google_index', 'web_traffic', 'subdomain_len', 'tld_len', 'fld_len',
'url_path_len', 'hostname_len', 'url_alphas', 'url_digits', 'url_puncs',
'count.', 'count@', 'count-', 'count%', 'count?', 'count=',
'count_dirs', 'first_dir_len', 'result']:
result[item]=result[item].astype(np.int64)
for item in ['pc_alphas', 'pc_digits', 'pc_puncs']:
result[item]=result[item].astype(np.float64)
# Save the result
result.to_csv('result.csv',index=False)