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
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
473
474
475
import os
os.system('pip install -q dnspython python-whois bs4 requests pandas pyOpenSSL')


import dns.resolver, dns.rdatatype
import requests
from bs4 import BeautifulSoup
from collections import Counter
import whois
from datetime import datetime
import time
import csv
import ssl
import socket
from urllib.request import urlparse
import OpenSSL.crypto
import pandas as pd
import random


def generate_user_agent() -> str:
    a = random.randint(63, 89)
    b = random.randint(1, 3200)
    c = random.randint(1, 140)
    user_agent = f'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{a}.0.{b}.{c} Safari/537.3'
    return user_agent


headers = {
    'User-Agent': generate_user_agent()
}


def count_domain_occurrences(soup: BeautifulSoup, domain: str) -> int:
    """
    Returns the number of occurrences of the domain in the website's page source.
    """
    try:
        domain_count = soup.prettify().count(domain)
        return domain_count
    except Exception as e:
        print(f"count_domain_occurrences: {str(e)}")
        return 0


def get_certificate_info(url: str) -> tuple[str, int]:
    """
    Returns the issuer and age of the certificate if found. None, None otherwise
    """

    try:
        if not url.startswith("https://"):
            raise ValueError("URL must use HTTPS protocol")

        hostname = url.split("https://")[1].split("/")[0]
        ip_addresses = socket.getaddrinfo(hostname, 443)
        ip_address = ip_addresses[0][4][0]

        context = ssl.create_default_context()
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        ssl_conn = context.wrap_socket(sock, server_hostname=hostname)
        ssl_conn.connect((ip_address, 443))
        cert = ssl_conn.getpeercert()

        if 'notAfter' not in cert:
            raise ValueError("Certificate information not found")

        issuer = cert['issuer'][0][0][1]
        not_after = cert['notAfter']

        not_after_date = datetime.strptime(not_after, '%b %d %H:%M:%S %Y %Z')
        certificate_age = (datetime.now() - not_after_date).days

        return issuer, certificate_age

    except Exception as e:
        print(f"get_certificate_info error: {str(e)}")

    return None, None


def check_sfh(soup: BeautifulSoup, domain: str) -> float:
    """
    Return 1 if SFH is "about: blank" or is empty, 0.5 if SFH refers to a different domain, and 0 otherwise
    """
    try:
        form = soup.find('form', {'method': 'post'})

        if not form:
            return 0

        sfh = form.get('action')

        if not sfh or sfh == 'about:blank':
            return 1
        
        sfh_domain = urlparse(sfh).netloc

        if sfh_domain != domain:
            return 0.5
    except Exception as e:
        print(f"check_sfh: {str(e)}")
        pass

    return 0


def age_of_domain(w: whois.WhoisEntry) -> int:
    """
    Returns the age of domain in days, None if error
    """
    try:
        creation_date = w.creation_date
        
        if creation_date is None:
            # Domain creation date is not available, try using updated_date as a fallback
            updated_date = w.updated_date
            if updated_date is None:
                return -1
            if type(updated_date) == list:
                creation_date = min(updated_date)
            else:
                creation_date = updated_date
        
        if type(creation_date) == list:
            creation_date = min(creation_date)
        
        num_days = (datetime.now() - creation_date).days
        
        return num_days
    except Exception as e:
        print('age_of_domain error: ' + str(e))
        return None


def use_iframe(soup: BeautifulSoup) -> int:
    """
    Returns 1 if iframe is present, 0 otherwise
    """
    iframes = soup.find_all('iframe')
    if len(iframes) > 0:
        return 1
    
    return 0


def popup_window_has_text_field(soup: BeautifulSoup) -> int:
    """
    Returns 1 if a pop-up window with text field exists, 0 otherwise
    """
    popups = soup.find_all('div', {'class': 'popup'})
    for popup in popups:
        if popup.find('input', {'type': 'text'}):
            return 1
    
    return 0


def abnormal_url(url: str, w: whois.WhoisEntry) -> int:
    """
    Returns 1 if the hostname is not in the URL, 0 otherwise.
    """
    host_name = w.domain.split('.')[0]
    if host_name not in url:
        return 1
    else:
        return 0


def dns_record(domain: str) -> tuple[int, int, int]:
    """
    Returns TTL, IP address count and TXT record presence in a tuple of integers.
    Returns None, None, None if dns record not found.
    """
    try:
        answers = dns.resolver.resolve(domain)
        TTL = answers.rrset.ttl
        IP_addresses = len(answers)
        TXT_records = any(answer.rdtype == dns.rdatatype.TXT for answer in answers)
        TXT_records = 1 if TXT_records else 0

        return TTL, IP_addresses, TXT_records
    except dns.resolver.NXDOMAIN:
        return None, None, None
    except Exception as e:
        print(f"dns_record error: {str(e)}")
        return None, None, None



def not_indexed_by_google(url: str) -> int:
    """
    Returns 1 if not indexed by Google, 0 if indexed, -1 if error
    """
    response = make_request(url, headers, timeout=10, retries=3)
    if response is None:
        return -1

    if "did not match any documents" in response.text:
        return 1
    else:
        return 0


def right_click_disabled(soup: BeautifulSoup) -> int:
    """
    Returns 1 if right click is disabled, 0 otherwise.
    """
    for script in soup.find_all('script'):
        if 'event.button==2' in script.text:
            return 1
    return 0


def mouseover_changes(soup: BeautifulSoup) -> int:
    """
    Returns 1 if the mouseover event changes the status bar, 0 otherwise
    """
    onMouseOver_elements = soup.find_all(onmouseover=True)
    for element in onMouseOver_elements:
        if "window.status" in str(element):
            return 1
    return 0


def redirects(response: requests.Response) -> int:
    """
    Returns the number of redirects
    """
    return len(response.history)


def meta_script_link_percentage(soup: BeautifulSoup) -> tuple[float, float, float]:
    """
    Returns the percentage of meta, script, and link tags that have a link
    """
    meta_tags = soup.find_all('meta')
    script_tags = soup.find_all('script')
    link_tags = soup.find_all('link')

    meta_links = sum([1 for tag in meta_tags if tag.has_attr('href')])
    script_links = sum([1 for tag in script_tags if tag.has_attr('src')])
    link_links = sum([1 for tag in link_tags if tag.has_attr('href')])

    total_links = meta_links + script_links + link_links
    if total_links == 0:
        return 0, 0, 0
    meta_percentage = (meta_links / total_links)
    script_percentage = (script_links / total_links)
    link_percentage = (link_links / total_links)

    return meta_percentage, script_percentage, link_percentage


def url_anchor_percentage(soup: BeautifulSoup) -> float:
    """
    Returns the percentage of anchor links on the page with different domain names,
    excluding anchor links with JavaScript or invalid URLs.
    """
    total_links = 0
    anchor_links = 0

    first_a_tag = soup.find('a')
    if first_a_tag is None:
        return 0

    domain = urlparse(first_a_tag.get('href')).netloc
    if not domain:
        return 0

    for a_tag in soup.find_all('a'):
        href = a_tag.get('href')
        if href:
            if href.startswith('javascript:') or href.startswith('#'):
                continue

            link_domain = urlparse(href).netloc
            if link_domain and link_domain != domain:
                anchor_links += 1
            total_links += 1

    if total_links == 0:
        return 0

    return anchor_links / total_links


def request_url_percentage(soup: BeautifulSoup, domain: str) -> float:
    """
    Returns the percentage of external domains in the URL
    """ 
    links = [link.get('href') for link in soup.find_all('a')]
    images = [img.get('src') for img in soup.find_all('img')]
    videos = [video.get('src') for video in soup.find_all('video')]
    sounds = [sound.get('src') for sound in soup.find_all('audio')]
    external_links = []
    
    for link in links + images + videos + sounds:
        if link is None:
            continue
        parsed_domain = urlparse(link).netloc
        if parsed_domain != '' and parsed_domain != domain:
            external_links.append(link)
    
    external_domains = [urlparse(link).netloc for link in external_links]
    domain_counts = Counter(external_domains)
    
    total_links = len(external_domains)
    if total_links == 0:
        return 1
    external_links_count = domain_counts[domain]
    
    return (external_links_count / total_links)


def has_suspicious_port(domain: str) -> int:
    """
    Returns 1 if any of the ports are of the preferred status, 0 otherwise.
    """
    preferred_ports = {
        21: "Close",
        22: "Close",
        23: "Close",
        80: "Open",
        443: "Open",
        445: "Close",
        1433: "Close",
        1521: "Close",
        3306: "Close",
        3389: "Close"
    }
    for port, status in preferred_ports.items():
        try:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.settimeout(0.1)
            result = sock.connect_ex((domain, port))
            if result == 0:
                if status == "Open":
                    return 1
                else:
                    return 0
        except:
            pass
        
    return 0


def external_favicons(soup: BeautifulSoup, domain: str) -> int:
    """
    Returns the number of favicons loaded from external domains.
    """
    favicon_links = soup.find_all('link', {'rel': 'icon'})
    external_favicons = 0

    for link in favicon_links:
        href = link.get('href')

        if href:
            href_domain = urlparse(href).netloc

            if href_domain != domain:
                external_favicons += 1

    return external_favicons


def domain_registeration_length(w: whois.WhoisEntry) -> int:
    """"
    Returns the number of days since the domain was registered, None if error
    """
    try:
        domain = w.domain_name
        expiration_date = w.expiration_date
        if type(expiration_date) == list:
            expiration_date = expiration_date[0]
        if expiration_date is not None:
            time_to_expire = (expiration_date - datetime.now()).days
            return time_to_expire
        else:
            return 0
    except Exception as e:
        print('domain_registeration_length error: ' + str(e))
        return None


def check_email_submission(soup: BeautifulSoup) -> int:
    """
    Returns 1 if "mail()" or "mailto:" is used, 0 otherwise.
    """
    try:
        forms = soup.find_all('form')
        for form in forms:
            if 'mail(' in str(form) or 'mailto:' in str(form):
                return 1
        return 0
    except:
        return 0
    

def make_request(url: str, headers: dict, timeout: int, retries: int) -> requests.Response:
    for i in range(retries):
        try:
            response = requests.get(url, headers=headers, timeout=timeout, allow_redirects=True)
            response.raise_for_status()
            return response
        except requests.exceptions.RequestException as e:
            retry_delay = 2**i
            print(f'\033[34mRequestException for {url}: {e}. Retrying in {retry_delay} seconds...\033[0m')
            time.sleep(retry_delay)
        except Exception as e:
            print(f'\033[31mError making request for {url}: {e}\033[0m')
            return None
    print(f'\033[31mFailed to make request after {retries} retries.\033[0m')
    return None


def collect_data(url: str, is_malicious: bool):
    start_time = time.time()

    try:
        response = make_request(url, headers, timeout=10, retries=3)
        if response is None:
            return
        redirects_value = redirects(response)        
    except Exception as e:
        print(f'Error making request: {e}')
        return
    not_indexed_by_google_value = not_indexed_by_google(url)
    issuer, certificate_age = get_certificate_info(url)
    
    try:
        soup = BeautifulSoup(response.content, 'html.parser')
        email_submission_value = check_email_submission(soup)
        url_anchor_percentage_value = url_anchor_percentage(soup)
        meta_percentage, script_percentage, link_percentage = meta_script_link_percentage(soup)
        mouseover_changes_value = mouseover_changes(soup)
        right_click_disabled_value = right_click_disabled(soup)
        popup_window_has_text_field_value = popup_window_has_text_field(soup)
        use_iframe_value = use_iframe(soup)
    except Exception as e:
        print('soup error, double check your code: ' + str(e))
        return
    
    try:
        parsed_url = urlparse(url)
        domain = parsed_url.netloc
        has_suspicious_port_value = has_suspicious_port(domain)
        request_url_percentage_value = request_url_percentage(soup, domain)
        external_favicons_value = external_favicons(soup, domain)
        TTL, ip_address_count, TXT_record = dns_record(domain)
        check_sfh_value = check_sfh(soup, domain)
        count_domain_occurrences_value = count_domain_occurrences(soup, domain)
    except Exception as e:
        print('urlparse error, double check your code: ' + str(e))
        return
    
    try:
        w = whois.whois(domain)
        domain_registeration_length_value = domain_registeration_length(w)
        abnormal_url_value = abnormal_url(url, w)
        age_of_domain_value = age_of_domain(w)
    except Exception as e:
        print('whois error: ' + str(e))
        domain_registeration_length_value = None
        abnormal_url_value = None
        age_of_domain_value = None
    
    
    print(f"{url} took {time.time() - start_time} seconds to complete")

    row = [url, redirects_value, not_indexed_by_google_value, issuer, certificate_age, email_submission_value, request_url_percentage_value, url_anchor_percentage_value, meta_percentage, script_percentage, link_percentage, mouseover_changes_value, right_click_disabled_value, popup_window_has_text_field_value, use_iframe_value, has_suspicious_port_value, external_favicons_value, TTL, ip_address_count, TXT_record, check_sfh_value, count_domain_occurrences_value, domain_registeration_length_value, abnormal_url_value, age_of_domain_value, is_malicious]
    
    with open('phishing_detection_dataset.csv', mode='a', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(row)