import shutil
import subprocess
import json
import asyncio
async def detect_waf(url: str) -> dict:
"""
Detect WAF using wafw00f
"""
if not shutil.which("wafw00f"):
return {"error": "wafw00f not installed"}
try:
# Run in a separate thread to avoid blocking asyncio loop
cmd = ["wafw00f", url, "-f", "json", "-o", "-"]
process = await asyncio.create_subprocess_exec(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if process.returncode != 0:
return {"error": stderr.decode(), "output": stdout.decode()}
try:
# wafw00f json output parsing
output = stdout.decode()
if not output:
return {"result": "No WAF detected or no output"}
return json.loads(output)
except:
return {"raw_output": stdout.decode()}
except Exception as e:
return {"error": str(e)}