|
|
import os |
|
|
from datetime import datetime, timezone |
|
|
|
|
|
import cachetools |
|
|
import gradio as gr |
|
|
import vt |
|
|
|
|
|
|
|
|
_CACHE_MAX_SIZE = 4096 |
|
|
_CACHE_TTL_SECONDS = 3600 |
|
|
|
|
|
|
|
|
API_KEY = os.getenv("VT_API_KEY") |
|
|
|
|
|
|
|
|
@cachetools.cached( |
|
|
cache=cachetools.TTLCache(maxsize=_CACHE_MAX_SIZE, ttl=_CACHE_TTL_SECONDS), |
|
|
) |
|
|
def get_virus_total_url_info(url: str) -> str: |
|
|
"""Get URL Info from VirusTotal URL Scanner. Scan URL is not available.""" |
|
|
if not API_KEY: |
|
|
return "Error: Virus total API key not configured." |
|
|
|
|
|
try: |
|
|
|
|
|
with vt.Client(API_KEY) as client: |
|
|
|
|
|
url_id = vt.url_id(url) |
|
|
|
|
|
|
|
|
url_analysis = client.get_object(f"/urls/{url_id}") |
|
|
|
|
|
|
|
|
last_analysis_stats = url_analysis.last_analysis_stats |
|
|
|
|
|
|
|
|
last_analysis_date = url_analysis.last_analysis_date |
|
|
if last_analysis_date: |
|
|
|
|
|
if isinstance(last_analysis_date, (int, float)): |
|
|
last_analysis_date = datetime.fromtimestamp( |
|
|
last_analysis_date, |
|
|
timezone.utc, |
|
|
) |
|
|
date_str = last_analysis_date.strftime("%Y-%m-%d %H:%M:%S UTC") |
|
|
else: |
|
|
date_str = "Not available" |
|
|
|
|
|
return f""" |
|
|
URL: {url} |
|
|
Last Analysis Date: {date_str} |
|
|
|
|
|
Analysis Statistics: |
|
|
- Harmless: {last_analysis_stats['harmless']} |
|
|
- Malicious: {last_analysis_stats['malicious']} |
|
|
- Suspicious: {last_analysis_stats['suspicious']} |
|
|
- Undetected: {last_analysis_stats['undetected']} |
|
|
- Timeout: {last_analysis_stats['timeout']} |
|
|
|
|
|
Reputation Score: {url_analysis.reputation} |
|
|
Times Submitted: {url_analysis.times_submitted} |
|
|
|
|
|
Cache Status: Hit |
|
|
""".strip() |
|
|
|
|
|
except Exception as err: |
|
|
return f"Error: {err}" |
|
|
|
|
|
|
|
|
gr_virus_total_url_info = gr.Interface( |
|
|
fn=get_virus_total_url_info, |
|
|
inputs=gr.Textbox(label="url"), |
|
|
outputs=gr.Text(label="VirusTotal report"), |
|
|
title="VirusTotal URL Scanner", |
|
|
description="Get URL Info from VirusTotal URL Scanner. Scan URL is not available", |
|
|
examples=["https://advertipros.com//?u=script", "https://google.com"], |
|
|
example_labels=["πΎ Malicious URL", "π§βπ» Benign URL"], |
|
|
) |
|
|
|