-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
472 lines (417 loc) · 13.2 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
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
import ipaddress
import re
import urllib.request
from bs4 import BeautifulSoup
import requests
from googlesearch import search
import whois
from datetime import date, datetime
from urllib.parse import urlparse
import numpy as np
import pickle
model = pickle.load(open('TrainedModel_SurfGuard','rb'))
from fastapi import FastAPI
app = FastAPI()
# [1] UsingIp
def UsingIp(url):
try:
ipaddress.ip_address(url)
return -1
except:
return 1
# [2] LongUrl
def LongUrl(url):
if len(url) < 54:
return -1
elif len(url) >= 54 and len(url) <= 75:
return 0
else:
return 1
# [3] ShortiningService
def shortUrl(url):
match = re.search('bit\.ly|goo\.gl|shorte\.st|go2l\.ink|x\.co|ow\.ly|t\.co|tinyurl|tr\.im|is\.gd|cli\.gs|'
'yfrog\.com|migre\.me|ff\.im|tiny\.cc|url4\.eu|twit\.ac|su\.pr|twurl\.nl|snipurl\.com|'
'short\.to|BudURL\.com|ping\.fm|post\.ly|Just\.as|bkite\.com|snipr\.com|fic\.kr|loopt\.us|'
'doiop\.com|short\.ie|kl\.am|wp\.me|rubyurl\.com|om\.ly|to\.ly|bit\.do|t\.co|lnkd\.in|'
'db\.tt|qr\.ae|adf\.ly|goo\.gl|bitly\.com|cur\.lv|tinyurl\.com|ow\.ly|bit\.ly|ity\.im|'
'q\.gs|is\.gd|po\.st|bc\.vc|twitthis\.com|u\.to|j\.mp|buzurl\.com|cutt\.us|u\.bb|yourls\.org|'
'x\.co|prettylinkpro\.com|scrnch\.me|filoops\.info|vzturl\.com|qr\.net|1url\.com|tweez\.me|v\.gd|tr\.im|link\.zip\.net', url)
if match:
return -1
return 1
# [4] Having@Symbol
def HavingAtSymbol(url):
if "@" in url:
return 1
return -1
# [5] DoubleSlashRedirecting
def DoubleSlashRedirecting(url):
if url.rfind('//')>6:
return -1
return 1
# [6] PrefixSuffix
def PrefixSuffix(url):
if '-' in urlparse(url).netloc:
return 1
else:
return -1
# [7] HavingSubDomain
def HavingSubDomain(url):
if url.count('.') == 1:
return 1
elif url.count('.') == 2:
return 0
else:
return -1
# [8] Https
def HttpsToken(url):
if urlparse(url).scheme == "https":
return 1
return -1
# [9] DomainRegistrationLength
def DomainRegistrationLength(url):
try:
w = whois.whois(url)
creation = w.creation_date
exp = w.expiration_date
length = (exp[0]-creation[0]).days
if length <= 365:
return 1
else:
return -1
except:
return -1
# [10] Favicon
def Favicon(url):
try:
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
icon_link = soup.find("link", rel="shortcut icon")
if icon_link != None:
return 1
else:
return -1
except:
return -1
# [11] UsingNonStandardPort
def UsingNonStandardPort(url):
try:
domain = urlparse(url).netloc
port = domain.split(':')
if len(port)>1:
return 1
else:
return -1
except:
return -1
# [12] UsingHttpsDomain
def UsingHttpsDomain(url):
try:
domain = urlparse(url).netloc
if 'https' in domain:
return -1
return 1
except:
return -1
# [13] RequestUrl
def RequestUrl(url):
try:
domain = urlparse(url).netloc
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
for img in soup.find_all('img', src=True):
dots = [x.start(0) for x in re.finditer('\.', img['src'])]
if url 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(0) for x in re.finditer('\.', audio['src'])]
if url 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(0) for x in re.finditer('\.', embed['src'])]
if url in embed['src'] or domain in embed['src'] or len(dots) == 1:
success = success + 1
i = i+1
for iframe in soup.find_all('iframe', src=True):
dots = [x.start(0) for x in re.finditer('\.', iframe['src'])]
if url in iframe['src'] or domain in iframe['src'] or len(dots) == 1:
success = success + 1
i = i+1
try:
percentage = success/float(i) * 100
if percentage < 22.0:
return 1
elif((percentage >= 22.0) and (percentage < 61.0)):
return 0
else:
return -1
except:
return 0
except:
return -1
# [14] AnchorUrl
def AnchorURL(url):
try:
domain = urlparse(url).netloc
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
i,unsafe = 0,0
for a in soup.find_all('a', href=True):
if "#" in a['href'] or "javascript" in a['href'].lower() or "mailto" in a['href'].lower() or not (url in a['href'] or domain in a['href']):
unsafe = unsafe + 1
i = i + 1
try:
percentage = unsafe / float(i) * 100
if percentage < 31.0:
return 1
elif ((percentage >= 31.0) and (percentage < 67.0)):
return 0
else:
return -1
except:
return -1
except:
return -1
# [15] Links In Script Tags
def LinksInScriptTags(url):
try:
domain = urlparse(url).netloc
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
i,success = 0,0
for link in soup.find_all('link', href=True):
dots = [x.start(0) for x in re.finditer('\.', link['href'])]
if url 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(0) for x in re.finditer('\.', script['src'])]
if url in script['src'] or domain in script['src'] or len(dots) == 1:
success = success + 1
i = i+1
try:
percentage = success / float(i) * 100
if percentage < 17.0:
return 1
elif((percentage >= 17.0) and (percentage < 81.0)):
return 0
else:
return -1
except:
return 0
except:
return -1
# [16] ServerFormHandler
def ServerFormHandler(url):
try:
domain = urlparse(url).netloc
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
if len(soup.find_all('form', action=True))==0:
return 1
else :
for form in soup.find_all('form', action=True):
if form['action'] == "" or form['action'] == "about:blank":
return -1
elif url not in form['action'] and domain not in form['action']:
return 0
else:
return 1
except:
return -1
# [17] SubmittingToEmail
def SubmittingToEmail(url):
try:
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
if re.findall(r"[mail\(\)|mailto:?]", soup):
return -1
else:
return 1
except:
return -1
# [18] AbnormalUrl
def AbnormalUrl(url):
try:
domain = urlparse(url).netloc
r = requests.get(url)
whois_r = whois.whois(domain)
domain = urlparse(url).netloc
if r.text == whois_r:
return 1
else:
return -1
except:
return -1
# [19] Website Forwarding
def WebsiteForwarding(url):
try:
r = requests.get(url)
if len(r.history) <= 1:
return 1
elif len(r.history) <= 4:
return 0
else:
return -1
except:
return -1
# [20] Status Bar Customization
def StatusBarCustomization(url):
try:
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
if re.findall("<script>.+onmouseover.+</script>", r.text):
return 1
else:
return -1
except:
return -1
# [21] Disabling Right Click
def DisablingRightClick(url):
try:
r = requests.get(url)
if re.findall(r"event.button ?== ?2", r.text):
return 1
else:
return -1
except:
return -1
# [22] Using Pop-up Window
def UsingPopupWindow(url):
try:
r = requests.get(url)
if re.findall(r"alert\(", r.text):
return 1
else:
return -1
except:
return -1
# [23] Iframe Redirection
def IframeRedirection(url):
try:
r = requests.get(url)
if re.findall(r"[<iframe>|<frameBorder>]", r.text):
return 1
else:
return -1
except:
return -1
# [24] Age of Domain
def AgeOfDomain(url):
try:
domain = urlparse(url).netloc
whois_domain = whois.whois(domain)
start_date = whois_domain.creation_date
current_date = datetime.now()
age =(current_date-start_date[0]).days
if age >= 180:
return 1
return -1
except:
return -1
# [25] DNS Record
def DNSRecord(url):
try:
domain = urlparse(url).netloc
dns = whois.whois(domain)
if dns == None:
return -1
return 1
except:
return -1
# [26] Web Traffic
def WebTraffic(url):
try:
rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url="+url).read(), "xml").find("REACH")['RANK']
rank = int(rank)
if rank < 100000:
return 1
return 0
except:
return -1
# [27] PageRank
def PageRank(url):
try:
domain = urlparse(url).netloc
pageRank = requests.post("https://www.checkpagerank.net/index.php", {"name": domain})
globalRank = int(re.findall(r"Global Rank: ([0-9]+)", pageRank.text)[0])
if globalRank > 0 and globalRank < 100000:
return 1
return -1
except:
return -1
# [28] Google Index
def GoogleIndex(url):
try:
site = search(url, 5)
if site:
return 1
return -1
except:
return -1
# [29] Number of Links Pointing to Page
def LinksPointingToPage(url):
try:
r = requests.get(url)
pointers = len(re.findall(r"<a href=", r.text))
if pointers == 0:
return 1
elif pointers <= 2:
return 0
else:
return -1
except:
return -1
# [30] Statistical Reports Based Feature
def StatisticalReportsBasedFeature(url):
try:
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
if re.findall(r"click|here|link|free|cash|video|visit|website|limited|time|offer|instant|download|win|winner|award|reward|prize|won|claim|now", soup.text):
return -1
return 1
except:
return 1
@app.get("/")
def home():
return "SurfGuardAPI is live and is working perfectly"
@app.get("/getStatus")
def extract(url: str):
urlFeatures = []
urlFeatures.append(UsingIp(url))
urlFeatures.append(LongUrl(url))
urlFeatures.append(shortUrl(url))
urlFeatures.append(HavingAtSymbol(url))
urlFeatures.append(DoubleSlashRedirecting(url))
urlFeatures.append(PrefixSuffix(url))
urlFeatures.append(HavingSubDomain(url))
urlFeatures.append(HttpsToken(url))
urlFeatures.append(DomainRegistrationLength(url))
urlFeatures.append(Favicon(url))
urlFeatures.append(UsingNonStandardPort(url))
urlFeatures.append(UsingHttpsDomain(url))
urlFeatures.append(RequestUrl(url))
urlFeatures.append(AnchorURL(url))
urlFeatures.append(LinksInScriptTags(url))
urlFeatures.append(ServerFormHandler(url))
urlFeatures.append(SubmittingToEmail(url))
urlFeatures.append(AbnormalUrl(url))
urlFeatures.append(WebsiteForwarding(url))
urlFeatures.append(StatusBarCustomization(url))
urlFeatures.append(DisablingRightClick(url))
urlFeatures.append(UsingPopupWindow(url))
urlFeatures.append(IframeRedirection(url))
urlFeatures.append(AgeOfDomain(url))
urlFeatures.append(DNSRecord(url))
urlFeatures.append(WebTraffic(url))
urlFeatures.append(PageRank(url))
urlFeatures.append(GoogleIndex(url))
urlFeatures.append(LinksPointingToPage(url))
urlFeatures.append(StatisticalReportsBasedFeature(url))
inputArray = np.asarray(urlFeatures).reshape(1,-1)
prediction = model.predict(inputArray)
print(urlFeatures)
print(str(prediction))
return str(prediction[0])