Lightweight Vulnerability Scanner for Resourced-constrained Organizations

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import schedule
  2. import subprocess
  3. import time
  4. import os
  5. from datetime import datetime
  6. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  7. ENV_DIR= os.path.join(BASE_DIR, "venv", "bin", "python3")
  8. def log(message):
  9. print(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {message}")
  10. def run_host_discovery():
  11. log(" Running Host Discovery...")
  12. try:
  13. subprocess.run([ENV_DIR, os.path.join(BASE_DIR, "Host_Discovery", "networkdiscovery.py")], check=True)
  14. log(" Host Discovery completed")
  15. except subprocess.CalledProcessError as e:
  16. log(f" Host Discovery failed: {e}")
  17. def run_port_scan():
  18. active_hosts_file = os.path.join(BASE_DIR, "data", "active_hosts.csv")
  19. if not os.path.exists(active_hosts_file) or os.path.getsize(active_hosts_file) == 0:
  20. log(" Skipping Port Scan: active_hosts.csv not found or is empty.")
  21. return
  22. log(" Running Port Scan...")
  23. try:
  24. subprocess.run(["sudo", ENV_DIR, os.path.join(BASE_DIR, "Host_Discovery", "portscanner.py")], check=True)
  25. log("Port Scan completed")
  26. except subprocess.CalledProcessError as e:
  27. log(f" Port Scan failed: {e}")
  28. def run_vulnerability_scan():
  29. active_hosts_file = os.path.join(BASE_DIR, "data", "active_hosts.csv")
  30. if not os.path.exists(active_hosts_file) or os.path.getsize(active_hosts_file) == 0:
  31. log("Skipping Vulnerability Scan: active_hosts.csv not found or is empty.")
  32. return
  33. log(" Running Vulnerability Scan Sequence...")
  34. scripts = [
  35. "createTargets.py",
  36. "taskmaker.py",
  37. "starttask.py",
  38. "getreports.py",
  39. "generate_reports.py"
  40. ]
  41. for script in scripts:
  42. path = os.path.join(BASE_DIR, "Vunerability_Scanner/", script)
  43. try:
  44. subprocess.run([ENV_DIR, path], check=True)
  45. log(f" Finished {script}")
  46. except subprocess.CalledProcessError as e:
  47. log(f" Error in {script}: {e}")
  48. break
  49. # Schedule tasks
  50. schedule.every().hour.at(":00").do(run_host_discovery)
  51. schedule.every().hour.at(":05").do(run_port_scan)
  52. schedule.every(11).hours.at(":10").do(run_vulnerability_scan)
  53. log(" Scheduler is running...")
  54. # Run initial discovery and scan
  55. log(" Running initial startup scan sequence...")
  56. run_host_discovery()
  57. run_port_scan()
  58. run_vulnerability_scan()
  59. while True:
  60. schedule.run_pending()
  61. time.sleep(30)