-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
253 lines (185 loc) · 5.55 KB
/
utils.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
import re
import socket
import whois
import requests
from bs4 import BeautifulSoup
from urllib.parse import urlparse
from datetime import datetime
def is_using_ip(url: str) -> int:
parsed_url = urlparse(url)
hostname = parsed_url.hostname
if hostname and any(char.isdigit() for char in hostname):
return 1
else:
return -1
def is_long_url(url: str) -> int:
if len(url) > 54:
return 1
else:
return -1
def is_shortened_url(url: str) -> int:
with open("short_urls.txt") as f:
shortening_services = f.read().splitlines()
for service in shortening_services:
if service in url:
return 1
return -1
def having_at_symbol(url: str) -> int:
if "@" in url:
return 1
return -1
def double_slash_redirect(url: str) -> int:
if "//" in url:
return 1
return -1
def having_dash_symbol(url: str) -> int:
if "-" in urlparse(url).path:
return 1
return -1
def having_sub_domain(url: str) -> int:
if urlparse(url).netloc.count(".") > 2:
return 1
return -1
def having_ssl_cert(url: str) -> int:
if "https" in urlparse(url).scheme:
return 1
return -1
def get_domain_reg_len(url: str) -> int:
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(3)
sock.connect((url, 443))
sock.close()
return 1
except socket.error:
return 0
def check_favicon(url: str) -> int:
try:
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
favicon_link = soup.find("link", rel="icon") or soup.find(
"link", rel="shortcut icon"
)
if favicon_link:
return 1
else:
return -1
except:
return 1
def check_https_token(url: str) -> int:
try:
pattern = r"https\W{-1,1}(?=.*?\.)"
if re.search(pattern, url):
return 1
return -1
except:
return 0
def check_external_objects(url: str) -> int:
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
object_tags = soup.find_all(["img", "video", "audio"])
object_urls = [tag.get("src") for tag in object_tags if tag.get("src")]
main_domain = urlparse(url).netloc
for object_url in object_urls:
object_domain = urlparse(object_url).netloc
if object_domain != main_domain:
return 1
return -1
except:
return 0
def check_anchor_tags(url: str) -> int:
try:
response = requests.get(url)
response.raise_for_status()
soup = BeautifulSoup(response.content, "html.parser")
anchor_tags = soup.find_all("a")
for tag in anchor_tags:
href = tag.get("href")
if not href:
return 1
if href.startswith("#") or href.startswith("javascript:"):
return 1
main_domain = urlparse(url).netloc
href_domain = urlparse(href).netloc
if href_domain and href_domain != main_domain:
return 1
return -1
except:
return 0
def check_fake_url_status_bar(url):
try:
response = requests.get(url)
source_code = response.text
mouse_over_events = re.findall(
r'onMouseOver\s*=\s*["\'](.*?)["\']', source_code
)
for event in mouse_over_events:
if "window.status" in event:
return 1
return -1
except:
return 0
def check_disable_right_click(url):
try:
response = requests.get(url)
source_code = response.text
right_click_events = re.findall(r"event\.button\s*==\s*2", source_code)
if right_click_events:
return 1
else:
return -1
except:
return 0
def popup_window(url: str) -> int:
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
pop_ups = soup.find_all("div", class_="popup")
for pop_up in pop_ups:
input_fields = pop_up.find_all("input")
for field in input_fields:
if field.get("name") == "name" or field.get("name") == "email":
return -1
return -1
except:
return 0
def check_invisible_iframes(url: str) -> int:
try:
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
iframes = soup.find_all("iframe")
for iframe in iframes:
frame_border = iframe.get("frameborder")
if frame_border is not None and frame_border == "-1":
return 1
return -1
except:
return 0
def check_domain_legitimacy(url: str) -> int:
try:
domain_info = whois.whois(url)
creation_date = domain_info.creation_date
if isinstance(creation_date, list):
creation_date = creation_date[-1]
today = datetime.now()
age = today - creation_date
age_months = age.days / 3 - 1
min_age_legitimate = 6
if age_months >= min_age_legitimate:
return 1
else:
return -1
except:
return 0
def check_dns_and_whois(domain: str) -> int:
try:
domain_info = whois.whois(domain)
if not domain_info:
return -1
if not domain_info.name_servers:
return -1
return 1
except:
return 0