Spaces:
Running
Running
Update utils/functions.py
Browse files- utils/functions.py +57 -64
utils/functions.py
CHANGED
@@ -23,8 +23,6 @@ logging.basicConfig(
|
|
23 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
24 |
DATA_DIR = os.path.join(BASE_DIR, '..', 'data')
|
25 |
FAKE_NUMBERS_FILE = os.path.join(DATA_DIR, 'fake_numbers.json')
|
26 |
-
HISTORY_FILE = os.path.join(DATA_DIR, 'history.json') # Jeśli nie używasz historii, możesz usunąć te linie
|
27 |
-
STATS_FILE = os.path.join(DATA_DIR, 'stats.json') # Jeśli nie używasz statystyk, możesz usunąć te linie
|
28 |
|
29 |
# Upewnij się, że katalog 'data' istnieje
|
30 |
os.makedirs(DATA_DIR, exist_ok=True)
|
@@ -112,6 +110,24 @@ def simple_checks(message, language):
|
|
112 |
warnings.append("Wiadomość zawiera prośbę o poufne informacje.")
|
113 |
return warnings
|
114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
def analyze_message(content, sender_info, additional_info, api_key, language):
|
116 |
"""
|
117 |
Analizuje treść wiadomości SMS za pomocą modelu AI, wykorzystując system prompts.
|
@@ -423,38 +439,18 @@ def check_url_with_phishtank(url_to_check, phishtank_api_key):
|
|
423 |
logging.error(f"Błąd podczas sprawdzania URL w PhishTank: {e}")
|
424 |
return False, f"Błąd podczas sprawdzania URL w PhishTank: {e}"
|
425 |
|
426 |
-
def
|
427 |
"""
|
428 |
-
|
429 |
"""
|
430 |
-
|
431 |
-
"
|
432 |
-
"Google Safe Browsing"
|
433 |
-
}
|
434 |
-
|
435 |
-
# Sprawdzenie PhishTank
|
436 |
-
is_phish, error = check_url_with_phishtank(url, phishtank_api_key)
|
437 |
-
if error:
|
438 |
-
logging.error(f"Błąd podczas sprawdzania URL w PhishTank: {error}")
|
439 |
-
threats["PhishTank"] = is_phish
|
440 |
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
else:
|
446 |
-
logging.warning("Brak klucza API Google Safe Browsing.")
|
447 |
-
|
448 |
-
# Pobranie zawartości strony
|
449 |
-
content = get_page_content(url)
|
450 |
-
|
451 |
-
return threats, content
|
452 |
-
|
453 |
-
def check_url_with_google_safe_browsing(url_to_check, google_safe_browsing_api_key):
|
454 |
-
"""
|
455 |
-
Sprawdza, czy podany URL znajduje się w bazie Google Safe Browsing.
|
456 |
-
"""
|
457 |
-
api_url = f"https://safebrowsing.googleapis.com/v4/threatMatches:find?key={google_safe_browsing_api_key}"
|
458 |
payload = {
|
459 |
"client": {
|
460 |
"clientId": "yourcompanyname",
|
@@ -471,35 +467,53 @@ def check_url_with_google_safe_browsing(url_to_check, google_safe_browsing_api_k
|
|
471 |
}
|
472 |
|
473 |
try:
|
474 |
-
response = requests.post(api_url, json=payload)
|
475 |
if response.status_code == 200:
|
476 |
result = response.json()
|
477 |
-
|
478 |
-
|
479 |
-
else:
|
480 |
-
return False
|
481 |
else:
|
482 |
logging.error(f"Błąd podczas sprawdzania URL w Google Safe Browsing: {response.status_code}")
|
483 |
-
return False
|
484 |
except Exception as e:
|
485 |
logging.error(f"Błąd podczas sprawdzania URL w Google Safe Browsing: {e}")
|
486 |
-
return False
|
487 |
|
488 |
-
def
|
489 |
"""
|
490 |
-
|
491 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
492 |
try:
|
493 |
response = requests.get(url)
|
494 |
if response.status_code == 200:
|
|
|
495 |
logging.info(f"Pobrano zawartość strony {url}.")
|
496 |
-
return
|
497 |
else:
|
498 |
logging.error(f"Nie udało się pobrać zawartości strony {url}. Kod statusu HTTP: {response.status_code}")
|
499 |
-
return f"Nie udało się pobrać zawartości strony. Kod statusu HTTP: {response.status_code}"
|
500 |
except Exception as e:
|
501 |
logging.error(f"Błąd podczas pobierania URL {url}: {e}")
|
502 |
-
return f"Błąd podczas pobierania URL: {e}"
|
503 |
|
504 |
def extract_text_from_image(image_file):
|
505 |
"""
|
@@ -513,24 +527,3 @@ def extract_text_from_image(image_file):
|
|
513 |
except Exception as e:
|
514 |
logging.error(f"Błąd podczas ekstrakcji tekstu z obrazu: {e}")
|
515 |
return f"Błąd podczas ekstrakcji tekstu z obrazu: {e}"
|
516 |
-
|
517 |
-
# Funkcje analizy email
|
518 |
-
|
519 |
-
def get_email_info(email_address):
|
520 |
-
"""
|
521 |
-
Pobiera informacje o domenie nadawcy e-mail za pomocą zapytania WHOIS.
|
522 |
-
"""
|
523 |
-
try:
|
524 |
-
domain = email_address.split('@')[-1]
|
525 |
-
w = whois.whois(domain)
|
526 |
-
organization = w.org if w.org else 'Nieznana'
|
527 |
-
country = w.country if w.country else 'Nieznany'
|
528 |
-
logging.info(f"Domena {domain} - Organizacja: {organization}, Kraj: {country}.")
|
529 |
-
return {
|
530 |
-
'domain': domain,
|
531 |
-
'organization': organization,
|
532 |
-
'country': country
|
533 |
-
}
|
534 |
-
except Exception as e:
|
535 |
-
logging.error(f"Nie udało się pobrać informacji WHOIS dla domeny {email_address}: {e}")
|
536 |
-
return None
|
|
|
23 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
24 |
DATA_DIR = os.path.join(BASE_DIR, '..', 'data')
|
25 |
FAKE_NUMBERS_FILE = os.path.join(DATA_DIR, 'fake_numbers.json')
|
|
|
|
|
26 |
|
27 |
# Upewnij się, że katalog 'data' istnieje
|
28 |
os.makedirs(DATA_DIR, exist_ok=True)
|
|
|
110 |
warnings.append("Wiadomość zawiera prośbę o poufne informacje.")
|
111 |
return warnings
|
112 |
|
113 |
+
def get_phone_info(phone_number):
|
114 |
+
"""
|
115 |
+
Weryfikuje numer telefonu i zwraca informacje o kraju i operatorze.
|
116 |
+
"""
|
117 |
+
try:
|
118 |
+
parsed_number = phonenumbers.parse(phone_number, None)
|
119 |
+
country = geocoder.description_for_number(parsed_number, 'pl')
|
120 |
+
operator = carrier.name_for_number(parsed_number, 'pl')
|
121 |
+
if not country:
|
122 |
+
country = "Nieznany"
|
123 |
+
if not operator:
|
124 |
+
operator = "Nieznany"
|
125 |
+
logging.info(f"Numer {phone_number} - Kraj: {country}, Operator: {operator}.")
|
126 |
+
return country, operator
|
127 |
+
except NumberParseException as e:
|
128 |
+
logging.error(f"Nie udało się przetworzyć numeru telefonu {phone_number}: {e}")
|
129 |
+
return "Nieznany", "Nieznany"
|
130 |
+
|
131 |
def analyze_message(content, sender_info, additional_info, api_key, language):
|
132 |
"""
|
133 |
Analizuje treść wiadomości SMS za pomocą modelu AI, wykorzystując system prompts.
|
|
|
439 |
logging.error(f"Błąd podczas sprawdzania URL w PhishTank: {e}")
|
440 |
return False, f"Błąd podczas sprawdzania URL w PhishTank: {e}"
|
441 |
|
442 |
+
def get_google_safe_browsing(url_to_check, google_api_key):
|
443 |
"""
|
444 |
+
Sprawdza, czy URL jest bezpieczny za pomocą Google Safe Browsing API.
|
445 |
"""
|
446 |
+
if not google_api_key:
|
447 |
+
logging.error("Brak klucza API Google Safe Browsing.")
|
448 |
+
return False, "Brak klucza API Google Safe Browsing."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
449 |
|
450 |
+
api_url = "https://safebrowsing.googleapis.com/v4/threatMatches:find"
|
451 |
+
params = {
|
452 |
+
'key': google_api_key
|
453 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
454 |
payload = {
|
455 |
"client": {
|
456 |
"clientId": "yourcompanyname",
|
|
|
467 |
}
|
468 |
|
469 |
try:
|
470 |
+
response = requests.post(api_url, params=params, json=payload)
|
471 |
if response.status_code == 200:
|
472 |
result = response.json()
|
473 |
+
is_threat = bool(result)
|
474 |
+
return is_threat, None
|
|
|
|
|
475 |
else:
|
476 |
logging.error(f"Błąd podczas sprawdzania URL w Google Safe Browsing: {response.status_code}")
|
477 |
+
return False, f"Błąd podczas sprawdzania URL w Google Safe Browsing: {response.status_code}"
|
478 |
except Exception as e:
|
479 |
logging.error(f"Błąd podczas sprawdzania URL w Google Safe Browsing: {e}")
|
480 |
+
return False, f"Błąd podczas sprawdzania URL w Google Safe Browsing: {e}"
|
481 |
|
482 |
+
def analyze_url(url, phishtank_api_key, google_api_key):
|
483 |
"""
|
484 |
+
Analizuje stronę internetową pod kątem phishingu i innych zagrożeń.
|
485 |
"""
|
486 |
+
# Sprawdzenie PhishTank
|
487 |
+
is_phish, error = check_url_with_phishtank(url, phishtank_api_key)
|
488 |
+
if error:
|
489 |
+
phishing_result = error
|
490 |
+
elif is_phish:
|
491 |
+
phishing_result = "Zagrożenie phishingiem wykryte w PhishTank."
|
492 |
+
else:
|
493 |
+
phishing_result = "Brak zagroże�� phishingowych według PhishTank."
|
494 |
+
|
495 |
+
# Sprawdzenie Google Safe Browsing
|
496 |
+
is_threat, error = get_google_safe_browsing(url, google_api_key)
|
497 |
+
if error:
|
498 |
+
google_result = error
|
499 |
+
elif is_threat:
|
500 |
+
google_result = "Zagrożenie wykryte przez Google Safe Browsing."
|
501 |
+
else:
|
502 |
+
google_result = "Brak zagrożeń według Google Safe Browsing."
|
503 |
+
|
504 |
+
# Analiza zawartości strony
|
505 |
try:
|
506 |
response = requests.get(url)
|
507 |
if response.status_code == 200:
|
508 |
+
content = response.text[:500] # Pobierz pierwsze 500 znaków
|
509 |
logging.info(f"Pobrano zawartość strony {url}.")
|
510 |
+
return phishing_result, google_result, content
|
511 |
else:
|
512 |
logging.error(f"Nie udało się pobrać zawartości strony {url}. Kod statusu HTTP: {response.status_code}")
|
513 |
+
return f"Nie udało się pobrać zawartości strony. Kod statusu HTTP: {response.status_code}", google_result, ""
|
514 |
except Exception as e:
|
515 |
logging.error(f"Błąd podczas pobierania URL {url}: {e}")
|
516 |
+
return f"Błąd podczas pobierania URL: {e}", google_result, ""
|
517 |
|
518 |
def extract_text_from_image(image_file):
|
519 |
"""
|
|
|
527 |
except Exception as e:
|
528 |
logging.error(f"Błąd podczas ekstrakcji tekstu z obrazu: {e}")
|
529 |
return f"Błąd podczas ekstrakcji tekstu z obrazu: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|