File size: 11,902 Bytes
7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 644fcfd 7831b98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 | import os
import base64
import asyncio
import httpx
from urllib.parse import urlparse
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import whois
import datetime
app = FastAPI()
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# VirusTotal API Key (from Hugging Face Secrets)
VT_API_KEY = os.getenv("VT_API_KEY")
class ScanRequest(BaseModel):
target: str
type: str
@app.get("/")
async def root():
return {"message": "Domify Threat Scanner", "status": "running"}
@app.get("/health")
async def health():
return {"status": "ok"}
# ==================== PUBLIC DATA SOURCES (No API Key) ====================
async def get_urlscan_data(domain):
"""Get data from URLScan.io"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://urlscan.io/api/v1/search/?q=domain:{domain}",
timeout=10
)
if response.status_code == 200:
data = response.json()
results = data.get('results', [])
if results:
latest = results[0]
return {
"screenshot": latest.get('screenshot'),
"requests": len(latest.get('requests', [])),
"ips": latest.get('ips', [])[:3],
"asn": latest.get('asn', {}).get('asn'),
"country": latest.get('country', '')
}
except:
pass
return None
async def get_crtsh_data(domain):
"""Get SSL certificate history from crt.sh"""
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://crt.sh/?q={domain}&output=json",
timeout=10
)
if response.status_code == 200:
certs = response.json()
if isinstance(certs, list) and certs:
issuers = list(set([c.get('issuer_name', '') for c in certs[:10] if c.get('issuer_name')]))
return {
"cert_count": len(certs),
"issuers": issuers[:3]
}
except:
pass
return None
async def get_whois_data(domain):
"""Get WHOIS data"""
try:
w = whois.whois(domain)
return {
"registrar": str(w.registrar) if w.registrar else None,
"creation_date": str(w.creation_date) if w.creation_date else None,
"expiration_date": str(w.expiration_date) if w.expiration_date else None,
"name_servers": w.name_servers[:3] if w.name_servers else [],
"org": str(w.org) if w.org else None
}
except:
return None
async def get_threatfox_data(domain):
"""Get threat data from ThreatFox"""
try:
async with httpx.AsyncClient() as client:
response = await client.post(
"https://threatfox-api.abuse.ch/api/v1/",
json={"query": "search_domain", "search_term": domain},
timeout=10
)
if response.status_code == 200:
data = response.json()
if data.get('query_status') == 'ok':
threats = data.get('data', [])
return {
"threat_count": len(threats),
"malware": list(set([t.get('malware') for t in threats[:3] if t.get('malware')]))
}
except:
pass
return None
async def get_ipinfo(ip):
"""Get IP information"""
if not ip:
return None
try:
async with httpx.AsyncClient() as client:
response = await client.get(f"http://ip-api.com/json/{ip}", timeout=10)
if response.status_code == 200:
data = response.json()
if data.get('status') == 'success':
return {
"country": data.get('country'),
"city": data.get('city'),
"isp": data.get('isp'),
"org": data.get('org')
}
except:
pass
return None
# ==================== URL SCAN ====================
@app.post("/api/scan/url")
async def scan_url(request: ScanRequest):
if not VT_API_KEY:
return {"status": "error", "message": "VT_API_KEY not set in secrets"}
try:
async with httpx.AsyncClient() as client:
# Encode URL for VirusTotal
url_id = base64.urlsafe_b64encode(request.target.encode()).decode().strip("=")
# Get VirusTotal report
response = await client.get(
f"https://www.virustotal.com/api/v3/urls/{url_id}",
headers={"x-apikey": VT_API_KEY}
)
if response.status_code == 404:
# URL never scanned - submit for scan
submit = await client.post(
"https://www.virustotal.com/api/v3/urls",
headers={"x-apikey": VT_API_KEY},
data={"url": request.target}
)
await asyncio.sleep(8)
analysis_id = submit.json().get("data", {}).get("id")
analysis = await client.get(
f"https://www.virustotal.com/api/v3/analyses/{analysis_id}",
headers={"x-apikey": VT_API_KEY}
)
attributes = analysis.json().get("data", {}).get("attributes", {})
else:
data = response.json()
attributes = data.get("data", {}).get("attributes", {})
# Extract domain
parsed = urlparse(request.target)
domain = parsed.netloc or request.target
# Get enriched data (run in parallel)
urlscan_data, crtsh_data, whois_data, threatfox_data = await asyncio.gather(
get_urlscan_data(domain),
get_crtsh_data(domain),
get_whois_data(domain),
get_threatfox_data(domain)
)
# Get IP info if available
ip_info = None
if urlscan_data and urlscan_data.get('ips'):
ip_info = await get_ipinfo(urlscan_data['ips'][0])
# Get vendor results
results = attributes.get("last_analysis_results", {})
vendor_results = []
for engine, result in results.items():
vendor_results.append({
"engine": engine,
"category": result.get("category", "undetected"),
"result": result.get("result", "clean")
})
# Sort: malicious first
vendor_results.sort(key=lambda x:
0 if x["category"] == "malicious"
else 1 if x["category"] == "suspicious"
else 2
)
stats = attributes.get("last_analysis_stats", {})
return {
"status": "success",
"stats": {
"malicious": stats.get("malicious", 0),
"suspicious": stats.get("suspicious", 0),
"harmless": stats.get("harmless", 0),
"undetected": stats.get("undetected", 0)
},
"vendor_results": vendor_results[:30],
"total_vendors": len(vendor_results),
"categories": attributes.get("categories", {}),
"tags": attributes.get("tags", [])[:10],
"reputation": attributes.get("reputation", 0),
"times_submitted": attributes.get("times_submitted", 0),
"enriched": {
"urlscan": urlscan_data,
"ssl_certs": crtsh_data,
"whois": whois_data,
"threatfox": threatfox_data,
"ip_info": ip_info
}
}
except Exception as e:
return {"status": "error", "message": str(e)}
# ==================== IP SCAN ====================
@app.post("/api/scan/ip")
async def scan_ip(request: ScanRequest):
if not VT_API_KEY:
return {"status": "error", "message": "VT_API_KEY not set"}
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://www.virustotal.com/api/v3/ip_addresses/{request.target}",
headers={"x-apikey": VT_API_KEY}
)
if response.status_code != 200:
return {"status": "error", "message": "IP not found"}
data = response.json()
attributes = data.get("data", {}).get("attributes", {})
stats = attributes.get("last_analysis_stats", {})
# Get IP info from free source
ip_info = await get_ipinfo(request.target)
return {
"status": "success",
"stats": {
"malicious": stats.get("malicious", 0),
"suspicious": stats.get("suspicious", 0),
"harmless": stats.get("harmless", 0),
"undetected": stats.get("undetected", 0)
},
"country": attributes.get("country", "Unknown"),
"network": attributes.get("network", "Unknown"),
"as_owner": attributes.get("as_owner", "Unknown"),
"ip_info": ip_info
}
except Exception as e:
return {"status": "error", "message": str(e)}
# ==================== DOMAIN SCAN ====================
@app.post("/api/scan/domain")
async def scan_domain(request: ScanRequest):
if not VT_API_KEY:
return {"status": "error", "message": "VT_API_KEY not set"}
try:
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://www.virustotal.com/api/v3/domains/{request.target}",
headers={"x-apikey": VT_API_KEY}
)
if response.status_code != 200:
return {"status": "error", "message": "Domain not found"}
data = response.json()
attributes = data.get("data", {}).get("attributes", {})
stats = attributes.get("last_analysis_stats", {})
# Get enriched data
crtsh_data, whois_data, threatfox_data = await asyncio.gather(
get_crtsh_data(request.target),
get_whois_data(request.target),
get_threatfox_data(request.target)
)
return {
"status": "success",
"stats": {
"malicious": stats.get("malicious", 0),
"suspicious": stats.get("suspicious", 0),
"harmless": stats.get("harmless", 0),
"undetected": stats.get("undetected", 0)
},
"creation_date": attributes.get("creation_date", "Unknown"),
"registrar": attributes.get("registrar", "Unknown"),
"enriched": {
"ssl_certs": crtsh_data,
"whois": whois_data,
"threatfox": threatfox_data
}
}
except Exception as e:
return {"status": "error", "message": str(e)}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=7860) |