import os
import json
import uuid
import requests
from datetime import datetime
import time
ZAP_BASE_URL = "http://localhost:8888"
class ajaxSpider:
def startScan(self, ZAP_BASE_URL, target_url):
"""
Starts the Ajax Spider scan on the target URL.
"""
access_endpoint = f"{ZAP_BASE_URL}/JSON/core/action/accessUrl/"
access_params = {
"url": target_url,
"followRedirects": "true"
}
try:
access_response = requests.get(access_endpoint, params=access_params)
if access_response.status_code == 200:
print("Successfully accessed the URL via ZAP API!")
else:
print(f"Failed to access URL: HTTP {access_response.status_code} - {access_response.text}")
except requests.RequestException as e:
print(f"An error occurred while accessing the URL: {e}")
endpoint = f"{ZAP_BASE_URL}/JSON/ajaxSpider/action/scan/"
params = {
"url": target_url
}
print("Starting Ajax Spider scan...")
response = requests.get(endpoint, params=params)
if response.status_code == 200:
response_data = response.json()
if "Result" in response_data:
return f"Ajax Spider Scan started: {response_data['Result']}"
else:
return "Error starting Ajax Spider scan:", response_data
else:
return f"Failed to start Ajax Spider scan. HTTP {response.status_code}: {response.text}"
def checkStatus(self, ZAP_BASE_URL):
"""
Checks the current status of the Ajax Spider scan.
"""
endpoint = f"{ZAP_BASE_URL}/JSON/ajaxSpider/view/status/"
response = requests.get(endpoint)
if response.status_code == 200:
response_data = response.json()
status = response_data.get("status")
if status == "stopped":
return "The Current Ajax Spider Scan is stopped or completed!"
elif status == "running":
return "The Current Ajax Spider Scan is running!"
else:
return "The Current Ajax Spider Scan is in an unknown state!"
else:
print(f"Failed to fetch Ajax Spider status. HTTP {response.status_code}: {response.text}")
return None
def scanResults(self, ZAP_BASE_URL):
"""
Fetches the results of the completed Ajax Spider scan.
"""
endpoint = f"{ZAP_BASE_URL}/JSON/ajaxSpider/view/results/"
response = requests.get(endpoint)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to fetch Ajax Spider results. HTTP {response.status_code}: {response.text}")
return None
def save_results(self, results, tool_name):
ts = datetime.now().strftime("%Y%m%d%H%M%S")
uid = uuid.uuid4().hex
filename = f"{ts}_{uid}.json"
try:
output_dir = os.path.join(os.getcwd(), "ZAP_Ajax_Scan_output")
os.makedirs(output_dir, exist_ok=True)
filepath = os.path.join(output_dir, filename)
with open(filepath, "w") as file:
json.dump(results, file, indent=4)
return filepath
except FileNotFoundError:
return f"Error: Directory {output_dir} does not exist."
except PermissionError:
return f"Error: Permission denied to write to {output_dir}."
except Exception as e:
print(f"Error saving results: {e}")