-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeatures_extraction.py
executable file
·374 lines (298 loc) · 12.7 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
370
371
372
373
374
# Purpose -
# Running this file (stand alone) - For extracting all the features from a web page for testing.
# Notes -
# 1 stands for legitimate
# 0 stands for suspicious
# -1 stands for phishing
from bs4 import BeautifulSoup
import urllib
import bs4
import re
import socket
import whois
from datetime import datetime
import time
# https://breakingcode.wordpress.com/2010/06/29/google-search-python/
# Previous package structure was modified. Import statements according to new structure added. Also code modified.
from googlesearch import search
# This import is needed only when you run this file in isolation.
import sys
from patterns import *
# Path of your local server. Different for different OSs.
LOCALHOST_PATH = "/Library/WebServer/Documents/"
DIRECTORY_NAME = "Malicious-Web-Content-Detection-Using-Machine-Learning"
def having_ip_address(url):
ip_address_pattern = ipv4_pattern + "|" + ipv6_pattern
match = re.search(ip_address_pattern, url)
return -1 if match else 1
def url_length(url):
if len(url) < 54:
return 1
if 54 <= len(url) <= 75:
return 0
return -1
def shortening_service(url):
match = re.search(shortening_services, url)
return -1 if match else 1
def having_at_symbol(url):
match = re.search('@', url)
return -1 if match else 1
def double_slash_redirecting(url):
# since the position starts from 0, we have given 6 and not 7 which is according to the document.
# It is convenient and easier to just use string search here to search the last occurrence instead of re.
last_double_slash = url.rfind('//')
return -1 if last_double_slash > 6 else 1
def prefix_suffix(domain):
match = re.search('-', domain)
return -1 if match else 1
def having_sub_domain(url):
# Here, instead of greater than 1 we will take greater than 3 since the greater than 1 condition is when www and
# country domain dots are skipped
# Accordingly other dots will increase by 1
if having_ip_address(url) == -1:
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]))|(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}',
url)
pos = match.end()
url = url[pos:]
num_dots = [x.start() for x in re.finditer(r'\.', url)]
if len(num_dots) <= 3:
return 1
elif len(num_dots) == 4:
return 0
else:
return -1
def domain_registration_length(domain):
expiration_date = domain.expiration_date
today = time.strftime('%Y-%m-%d')
today = datetime.strptime(today, '%Y-%m-%d')
registration_length = 0
# Some domains do not have expiration dates. This if condition makes sure that the expiration date is used only
# when it is present.
if expiration_date:
registration_length = abs((expiration_date - today).days)
return -1 if registration_length / 365 <= 1 else 1
def favicon(wiki, soup, domain):
for head in soup.find_all('head'):
for head.link in soup.find_all('link', href=True):
dots = [x.start() for x in re.finditer(r'\.', head.link['href'])]
return 1 if wiki in head.link['href'] or len(dots) == 1 or domain in head.link['href'] else -1
return 1
def https_token(url):
match = re.search(http_https, url)
if match and match.start() == 0:
url = url[match.end():]
match = re.search('http|https', url)
return -1 if match else 1
def request_url(wiki, soup, domain):
i = 0
success = 0
for img in soup.find_all('img', src=True):
dots = [x.start() for x in re.finditer(r'\.', img['src'])]
if wiki in img['src'] or domain in img['src'] or len(dots) == 1:
success = success + 1
i = i + 1
for audio in soup.find_all('audio', src=True):
dots = [x.start() for x in re.finditer(r'\.', audio['src'])]
if wiki in audio['src'] or domain in audio['src'] or len(dots) == 1:
success = success + 1
i = i + 1
for embed in soup.find_all('embed', src=True):
dots = [x.start() for x in re.finditer(r'\.', embed['src'])]
if wiki in embed['src'] or domain in embed['src'] or len(dots) == 1:
success = success + 1
i = i + 1
for i_frame in soup.find_all('i_frame', src=True):
dots = [x.start() for x in re.finditer(r'\.', i_frame['src'])]
if wiki in i_frame['src'] or domain in i_frame['src'] or len(dots) == 1:
success = success + 1
i = i + 1
try:
percentage = success / float(i) * 100
except:
return 1
if percentage < 22.0:
return 1
elif 22.0 <= percentage < 61.0:
return 0
else:
return -1
def url_of_anchor(wiki, soup, domain):
i = 0
unsafe = 0
for a in soup.find_all('a', href=True):
# 2nd condition was 'JavaScript ::void(0)' but we put JavaScript because the space between javascript and ::
# might not be
# there in the actual a['href']
if "#" in a['href'] or "javascript" in a['href'].lower() or "mailto" in a['href'].lower() or not (
wiki in a['href'] or domain in a['href']):
unsafe = unsafe + 1
i = i + 1
# print a['href']
try:
percentage = unsafe / float(i) * 100
except:
return 1
if percentage < 31.0:
return 1
# return percentage
elif 31.0 <= percentage < 67.0:
return 0
else:
return -1
# Links in <Script> and <Link> tags
def links_in_tags(wiki, soup, domain):
i = 0
success = 0
for link in soup.find_all('link', href=True):
dots = [x.start() for x in re.finditer(r'\.', link['href'])]
if wiki in link['href'] or domain in link['href'] or len(dots) == 1:
success = success + 1
i = i + 1
for script in soup.find_all('script', src=True):
dots = [x.start() for x in re.finditer(r'\.', script['src'])]
if wiki in script['src'] or domain in script['src'] or len(dots) == 1:
success = success + 1
i = i + 1
try:
percentage = success / float(i) * 100
except:
return 1
if percentage < 17.0:
return 1
elif 17.0 <= percentage < 81.0:
return 0
else:
return -1
# Server Form Handler (SFH)
# Have written conditions directly from word file..as there are no sites to test ######
def sfh(wiki, soup, domain):
for form in soup.find_all('form', action=True):
if form['action'] == "" or form['action'] == "about:blank":
return -1
elif wiki not in form['action'] and domain not in form['action']:
return 0
else:
return 1
return 1
# Mail Function
# PHP mail() function is difficult to retrieve, hence the following function is based on mailto
def submitting_to_email(soup):
for form in soup.find_all('form', action=True):
return -1 if "mailto:" in form['action'] else 1
# In case there is no form in the soup, then it is safe to return 1.
return 1
def abnormal_url(domain, url):
hostname = domain.name
match = re.search(hostname, url)
return 1 if match else -1
# IFrame Redirection
def i_frame(soup):
for i_frame in soup.find_all('i_frame', width=True, height=True, frameBorder=True):
# Even if one iFrame satisfies the below conditions, it is safe to return -1 for this method.
if i_frame['width'] == "0" and i_frame['height'] == "0" and i_frame['frameBorder'] == "0":
return -1
if i_frame['width'] == "0" or i_frame['height'] == "0" or i_frame['frameBorder'] == "0":
return 0
# If none of the iframes have a width or height of zero or a frameBorder of size 0, then it is safe to return 1.
return 1
def age_of_domain(domain):
creation_date = domain.creation_date
expiration_date = domain.expiration_date
ageofdomain = 0
if expiration_date:
ageofdomain = abs((expiration_date - creation_date).days)
return -1 if ageofdomain / 30 < 6 else 1
def web_traffic(url):
try:
rank = \
bs4.BeautifulSoup(urllib.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find(
"REACH")['RANK']
except TypeError:
return -1
rank = int(rank)
return 1 if rank < 100000 else 0
def google_index(url):
site = search(url, 5)
return 1 if site else -1
def statistical_report(url, hostname):
try:
ip_address = socket.gethostbyname(hostname)
except:
return -1
url_match = re.search(
r'at\.ua|usa\.cc|baltazarpresentes\.com\.br|pe\.hu|esy\.es|hol\.es|sweddy\.com|myjino\.ru|96\.lt|ow\.ly', url)
ip_match = re.search(
'146\.112\.61\.108|213\.174\.157\.151|121\.50\.168\.88|192\.185\.217\.116|78\.46\.211\.158|181\.174\.165\.13|46\.242\.145\.103|121\.50\.168\.40|83\.125\.22\.219|46\.242\.145\.98|'
'107\.151\.148\.44|107\.151\.148\.107|64\.70\.19\.203|199\.184\.144\.27|107\.151\.148\.108|107\.151\.148\.109|119\.28\.52\.61|54\.83\.43\.69|52\.69\.166\.231|216\.58\.192\.225|'
'118\.184\.25\.86|67\.208\.74\.71|23\.253\.126\.58|104\.239\.157\.210|175\.126\.123\.219|141\.8\.224\.221|10\.10\.10\.10|43\.229\.108\.32|103\.232\.215\.140|69\.172\.201\.153|'
'216\.218\.185\.162|54\.225\.104\.146|103\.243\.24\.98|199\.59\.243\.120|31\.170\.160\.61|213\.19\.128\.77|62\.113\.226\.131|208\.100\.26\.234|195\.16\.127\.102|195\.16\.127\.157|'
'34\.196\.13\.28|103\.224\.212\.222|172\.217\.4\.225|54\.72\.9\.51|192\.64\.147\.141|198\.200\.56\.183|23\.253\.164\.103|52\.48\.191\.26|52\.214\.197\.72|87\.98\.255\.18|209\.99\.17\.27|'
'216\.38\.62\.18|104\.130\.124\.96|47\.89\.58\.141|78\.46\.211\.158|54\.86\.225\.156|54\.82\.156\.19|37\.157\.192\.102|204\.11\.56\.48|110\.34\.231\.42',
ip_address)
if url_match:
return -1
elif ip_match:
return -1
else:
return 1
def get_hostname_from_url(url):
hostname = url
# TODO: Put this pattern in patterns.py as something like - get_hostname_pattern.
pattern = "https://|http://|www.|https://www.|http://www."
pre_pattern_match = re.search(pattern, hostname)
if pre_pattern_match:
hostname = hostname[pre_pattern_match.end():]
post_pattern_match = re.search("/", hostname)
if post_pattern_match:
hostname = hostname[:post_pattern_match.start()]
return hostname
# TODO: Put the DNS and domain code into a function.
def main(url):
with open(LOCALHOST_PATH + DIRECTORY_NAME + '/markup.txt', 'r') as file:
soup_string = file.read()
soup = BeautifulSoup(soup_string, 'html.parser')
status = []
hostname = get_hostname_from_url(url)
status.append(having_ip_address(url))
status.append(url_length(url))
status.append(shortening_service(url))
status.append(having_at_symbol(url))
status.append(double_slash_redirecting(url))
status.append(prefix_suffix(hostname))
status.append(having_sub_domain(url))
dns = 1
try:
domain = whois.query(hostname)
except:
dns = -1
status.append(-1 if dns == -1 else domain_registration_length(domain))
status.append(favicon(url, soup, hostname))
status.append(https_token(url))
status.append(request_url(url, soup, hostname))
status.append(url_of_anchor(url, soup, hostname))
status.append(links_in_tags(url, soup, hostname))
status.append(sfh(url, soup, hostname))
status.append(submitting_to_email(soup))
status.append(-1 if dns == -1 else abnormal_url(domain, url))
status.append(i_frame(soup))
status.append(-1 if dns == -1 else age_of_domain(domain))
status.append(dns)
status.append(web_traffic(soup))
status.append(google_index(url))
status.append(statistical_report(url, hostname))
print('\n1. Having IP address\n2. URL Length\n3. URL Shortening service\n4. Having @ symbol\n'
'5. Having double slash\n6. Having dash symbol(Prefix Suffix)\n7. Having multiple subdomains\n'
'8. SSL Final State\n8. Domain Registration Length\n9. Favicon\n10. HTTP or HTTPS token in domain name\n'
'11. Request URL\n12. URL of Anchor\n13. Links in tags\n14. SFH\n15. Submitting to email\n16. Abnormal URL\n'
'17. IFrame\n18. Age of Domain\n19. DNS Record\n20. Web Traffic\n21. Google Index\n22. Statistical Reports\n')
print(status)
return status
# Use the below two lines if features_extraction.py is being run as a standalone file. If you are running this file as
# a part of the workflow pipeline starting with the chrome extension, comment out these two lines.
# if __name__ == "__main__":
# if len(sys.argv) != 2:
# print("Please use the following format for the command - `python2 features_extraction.py <url-to-be-tested>`")
# exit(0)
# main(sys.argv[1])