-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
201 lines (167 loc) · 5.72 KB
/
main.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
from flask import Flask, render_template, request
import pickle as pk
from urllib.parse import urlparse
import whois
import socket
import ssl
import requests
import tldextract
import datetime
import dns.resolver
application = Flask(__name__)
model = pk.load(open('mymodel.pkl', 'rb'))
def count_slash_url(url):
return url.count('/')
def count_underline_directory(url):
return url.count('_')
def file_length(url):
parsed_url = urlparse(url)
return len(parsed_url.path.split('/')[-1])
def length_url(url):
return len(url)
def time_domain_activation(domain):
try:
domain_info = whois.whois(domain)
creation_date = domain_info.creation_date
if isinstance(creation_date, list):
creation_date = creation_date[0]
activation_duration = datetime.datetime.now() - creation_date
return activation_duration.days
except:
return None
def count_dot_domain(domain):
return domain.count('.')
def asn_ip(domain):
try:
ip_address = socket.gethostbyname(domain)
asn_info = requests.get(f'https://api.iptoasn.com/v1/as/ip/{ip_address}')
return asn_info.json()['as_number']
except:
return None
def ttl_hostname(domain):
try:
ttl = dns.resolver.resolve(domain, 'A').rrset.ttl
return ttl
except:
return None
def time_domain_expiration(domain):
try:
domain_info = whois.whois(domain)
expiration_date = domain_info.expiration_date
if isinstance(expiration_date, list):
expiration_date = expiration_date[0]
expiration_duration = expiration_date - datetime.datetime.now()
return expiration_duration.days
except:
return None
def time_response(url):
try:
response = requests.get(url)
return response.elapsed.total_seconds()
except:
return None
def count_hyphen_url(url):
return url.count('-')
def count_percent_params(url):
return url.count('%')
def count_vowels_domain(domain):
vowels = 'aeiou'
return sum(1 for char in domain if char in vowels)
def count_hyphen_params(url):
return urlparse(url).query.count('-')
def count_dot_url(url):
return urlparse(url).netloc.count('.')
def count_equal_url(url):
return urlparse(url).query.count('=')
def count_nameservers(domain):
try:
return len(dns.resolver.resolve(domain, 'NS'))
except:
return None
def count_mx_servers(domain):
try:
return len(dns.resolver.resolve(domain, 'MX'))
except:
return None
def count_ip_resolved(domain):
try:
return len(socket.gethostbyname_ex(domain)[-1])
except:
return None
def count_slash_params(url):
return urlparse(url).query.count('/')
def count_redirects(url):
try:
response = requests.get(url)
return len(response.history)
except:
return None
def count_tld_url(url):
extracted = tldextract.extract(url)
# Sum the lengths of subdomain, domain, and suffix parts
return len(extracted.subdomain) + len(extracted.domain) + len(extracted.suffix)
def tls_ssl_certification(domain):
try:
context = ssl.create_default_context()
with socket.create_connection((domain, 443)) as sock:
with context.wrap_socket(sock, server_hostname=domain) as ssock:
cert = ssock.getpeercert()
return ssl.DER_cert_to_PEM_cert(cert)
except:
return None
def count_underline_url(url):
return url.count('_')
def domain_spf(domain):
try:
records = dns.resolver.resolve(domain, 'TXT')
for record in records:
if 'v=spf1' in record.strings[0]:
return True
return False
except:
return False
@application.route('/')
def index():
return render_template('index.html')
@application.route('/predict', methods=['GET','POST'])
def predict():
if request.method == 'POST':
url = request.form.get('url')
domain = urlparse(url).netloc
features = {
"qty_slash_url": count_slash_url(url),
"qty_underline_directory": count_underline_directory(url),
"file_length": file_length(url),
"length_url": length_url(url),
"time_domain_activation": time_domain_activation(domain),
"qty_dot_domain": count_dot_domain(domain),
"asn_ip": asn_ip(domain),
"ttl_hostname": ttl_hostname(domain),
"time_domain_expiration": time_domain_expiration(domain),
"time_response": time_response(url),
"qty_hyphen_url": count_hyphen_url(url),
"qty_percent_params": count_percent_params(url),
"qty_vowels_domain": count_vowels_domain(domain),
"qty_hyphen_params": count_hyphen_params(url),
"qty_dot_url": count_dot_url(url),
"qty_equal_url": count_equal_url(url),
"qty_nameservers": count_nameservers(domain),
"qty_mx_servers": count_mx_servers(domain),
"qty_ip_resolved": count_ip_resolved(domain),
"qty_slash_params": count_slash_params(url),
"qty_redirects": count_redirects(url),
"qty_tld_url": count_tld_url(url),
"tls_ssl_certification": tls_ssl_certification(domain),
"qty_underline_url": count_underline_url(url),
"domain_spf": domain_spf(domain),
}
import numpy as np
feature_values = [value for value in features.values()]
feature_values_reshaped = np.array(feature_values).reshape(1, -1)
prediction =model.predict(feature_values_reshaped)[0]
if prediction==1:
return render_template('index.html', prediction_text=f'The website is malicious')
elif prediction==0:
return render_template('index.html', prediction_text=f'The website is malicious')
if __name__ == '__main__':
application.run(debug=True)