Spaces:
Sleeping
Sleeping
Create url_analyzer.py
Browse files- url_analyzer.py +57 -0
url_analyzer.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
# You can store API keys as environment variables
|
| 5 |
+
SAFE_BROWSING_API_KEY = os.getenv("SAFE_BROWSING_API_KEY")
|
| 6 |
+
|
| 7 |
+
def analyze_urls(urls):
|
| 8 |
+
findings = []
|
| 9 |
+
|
| 10 |
+
for url in urls:
|
| 11 |
+
# --- 1. PhishTank ---
|
| 12 |
+
try:
|
| 13 |
+
res = requests.get(f"https://checkurl.phishtank.com/checkurl/{url}?format=json")
|
| 14 |
+
data = res.json()
|
| 15 |
+
if data.get("results", {}).get("in_database"):
|
| 16 |
+
findings.append(f"URL: {url} is flagged as phishing (PhishTank)")
|
| 17 |
+
else:
|
| 18 |
+
findings.append(f"URL: {url} not flagged (PhishTank)")
|
| 19 |
+
except:
|
| 20 |
+
findings.append(f"URL: {url} could not be checked (PhishTank)")
|
| 21 |
+
|
| 22 |
+
# --- 2. URLHaus ---
|
| 23 |
+
try:
|
| 24 |
+
res = requests.post("https://urlhaus-api.abuse.ch/v1/url/", data={"url": url})
|
| 25 |
+
data = res.json()
|
| 26 |
+
if data.get("query_status") == "ok":
|
| 27 |
+
findings.append(f"URL: {url} is flagged as {data['url_status']} (URLHaus)")
|
| 28 |
+
else:
|
| 29 |
+
findings.append(f"URL: {url} not found in URLHaus")
|
| 30 |
+
except:
|
| 31 |
+
findings.append(f"URL: {url} could not be checked (URLHaus)")
|
| 32 |
+
|
| 33 |
+
# --- 3. Google Safe Browsing ---
|
| 34 |
+
if SAFE_BROWSING_API_KEY:
|
| 35 |
+
try:
|
| 36 |
+
payload = {
|
| 37 |
+
"client": {"clientId": "email-analysis-tool", "clientVersion": "1.0"},
|
| 38 |
+
"threatInfo": {
|
| 39 |
+
"threatTypes": ["MALWARE", "SOCIAL_ENGINEERING", "UNWANTED_SOFTWARE"],
|
| 40 |
+
"platformTypes": ["ANY_PLATFORM"],
|
| 41 |
+
"threatEntryTypes": ["URL"],
|
| 42 |
+
"threatEntries": [{"url": url}],
|
| 43 |
+
},
|
| 44 |
+
}
|
| 45 |
+
res = requests.post(
|
| 46 |
+
f"https://safebrowsing.googleapis.com/v4/threatMatches:find?key={SAFE_BROWSING_API_KEY}",
|
| 47 |
+
json=payload,
|
| 48 |
+
)
|
| 49 |
+
data = res.json()
|
| 50 |
+
if "matches" in data:
|
| 51 |
+
findings.append(f"URL: {url} flagged by Google Safe Browsing")
|
| 52 |
+
else:
|
| 53 |
+
findings.append(f"URL: {url} not flagged (Google Safe Browsing)")
|
| 54 |
+
except:
|
| 55 |
+
findings.append(f"URL: {url} could not be checked (Google Safe Browsing)")
|
| 56 |
+
|
| 57 |
+
return findings
|