Pedro Bento
commited on
Commit
·
11d55de
1
Parent(s):
40dc7ac
fixed Virus total dependencies
Browse files- tdagent/tools/virus_total.py +25 -25
tdagent/tools/virus_total.py
CHANGED
|
@@ -5,13 +5,11 @@ from datetime import datetime
|
|
| 5 |
from functools import lru_cache
|
| 6 |
from typing import Optional
|
| 7 |
import time
|
|
|
|
| 8 |
|
| 9 |
# Get API key from environment variable
|
| 10 |
API_KEY = os.getenv('VT_API_KEY')
|
| 11 |
|
| 12 |
-
# Initialize the VT client globally
|
| 13 |
-
client = vt.Client(API_KEY)
|
| 14 |
-
|
| 15 |
|
| 16 |
@lru_cache(maxsize=100)
|
| 17 |
def get_url_info_cached(url: str, timestamp: Optional[int] = None) -> str:
|
|
@@ -20,26 +18,28 @@ def get_url_info_cached(url: str, timestamp: Optional[int] = None) -> str:
|
|
| 20 |
timestamp parameter is used to force cache invalidation when needed
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
if
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
URL: {url}
|
| 44 |
Last Analysis Date: {date_str}
|
| 45 |
|
|
@@ -54,9 +54,9 @@ Reputation Score: {url_analysis.reputation}
|
|
| 54 |
Times Submitted: {url_analysis.times_submitted}
|
| 55 |
|
| 56 |
Cache Status: Hit
|
| 57 |
-
|
| 58 |
|
| 59 |
-
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error: {str(e)}"
|
|
|
|
| 5 |
from functools import lru_cache
|
| 6 |
from typing import Optional
|
| 7 |
import time
|
| 8 |
+
import asyncio
|
| 9 |
|
| 10 |
# Get API key from environment variable
|
| 11 |
API_KEY = os.getenv('VT_API_KEY')
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@lru_cache(maxsize=100)
|
| 15 |
def get_url_info_cached(url: str, timestamp: Optional[int] = None) -> str:
|
|
|
|
| 18 |
timestamp parameter is used to force cache invalidation when needed
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
+
# Create a new client for each request (thread-safe)
|
| 22 |
+
with vt.Client(API_KEY) as client:
|
| 23 |
+
# URL ID is created by computing the base64-encoded SHA-256 of the URL
|
| 24 |
+
url_id = vt.url_id(url)
|
| 25 |
+
|
| 26 |
+
# Get the URL analysis
|
| 27 |
+
url_analysis = client.get_object(f"/urls/{url_id}")
|
| 28 |
+
|
| 29 |
+
# Format the results
|
| 30 |
+
last_analysis_stats = url_analysis.last_analysis_stats
|
| 31 |
+
|
| 32 |
+
# Fix: Convert the datetime attribute properly
|
| 33 |
+
last_analysis_date = url_analysis.last_analysis_date
|
| 34 |
+
if last_analysis_date:
|
| 35 |
+
# Convert to datetime if it's a timestamp
|
| 36 |
+
if isinstance(last_analysis_date, (int, float)):
|
| 37 |
+
last_analysis_date = datetime.utcfromtimestamp(last_analysis_date)
|
| 38 |
+
date_str = last_analysis_date.strftime('%Y-%m-%d %H:%M:%S UTC')
|
| 39 |
+
else:
|
| 40 |
+
date_str = "Not available"
|
| 41 |
+
|
| 42 |
+
result = f"""
|
| 43 |
URL: {url}
|
| 44 |
Last Analysis Date: {date_str}
|
| 45 |
|
|
|
|
| 54 |
Times Submitted: {url_analysis.times_submitted}
|
| 55 |
|
| 56 |
Cache Status: Hit
|
| 57 |
+
"""
|
| 58 |
|
| 59 |
+
return result
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error: {str(e)}"
|