test.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/usr/bin/env python3
  2. """Per-mod unit tests for the 18 v2.0 attack modules.
  3. Each mod is invoked with target='127.0.0.1' (the localhost guard inside every
  4. mod returns early without emitting traffic). Tests verify: clean import,
  5. class exists with expected name, .attacking(target, rounds) runs to completion.
  6. """
  7. import sys, os, io, contextlib
  8. NEW_MODS = [
  9. ("memcached", "MEMCACHED"),
  10. ("chargen", "CHARGEN"),
  11. ("cldap", "CLDAP"),
  12. ("ssdp", "SSDP"),
  13. ("qotd", "QOTD"),
  14. ("tftp", "TFTP"),
  15. ("wsdisco", "WSDISCO"),
  16. ("coap", "COAP"),
  17. ("mssql", "MSSQL"),
  18. ("arms", "ARMS"),
  19. ("plex", "PLEX"),
  20. ("netbios", "NETBIOS"),
  21. ("ripv1", "RIPV1"),
  22. ("middlebox", "MIDDLEBOX"),
  23. ("rapidreset", "RAPIDRESET"),
  24. ("slowread", "SLOWREAD"),
  25. ("goldeneye", "GOLDENEYE"),
  26. ("finflood", "FINFLOOD"),
  27. ]
  28. err = []
  29. ok = 0
  30. for modname, classname in NEW_MODS:
  31. try:
  32. mod = __import__("core.mods." + modname, fromlist=[classname])
  33. except Exception as e:
  34. err.append(f"{modname}: import failed -> {type(e).__name__}: {e}")
  35. continue
  36. cls = getattr(mod, classname, None)
  37. if cls is None:
  38. err.append(f"{modname}: missing class {classname}")
  39. continue
  40. try:
  41. inst = cls()
  42. except Exception as e:
  43. err.append(f"{modname}: instantiation failed -> {type(e).__name__}: {e}")
  44. continue
  45. buf = io.StringIO()
  46. try:
  47. with contextlib.redirect_stdout(buf), contextlib.redirect_stderr(buf):
  48. if classname == "GOLDENEYE":
  49. inst.attacking("127.0.0.1", 1, None)
  50. else:
  51. inst.attacking("127.0.0.1", 1)
  52. except SystemExit:
  53. pass
  54. except Exception as e:
  55. err.append(f"{modname}: attacking() raised -> {type(e).__name__}: {e}")
  56. continue
  57. out = buf.getvalue()
  58. if "localhost" not in out.lower() and "Sending message" not in out and "ready to fire" not in out:
  59. if "[Aborting!]" not in out and "[Skipping!]" not in out and "placeholder" not in out.lower():
  60. err.append(f"{modname}: unexpected output (no localhost/placeholder guard hit) -> {out[:120]!r}")
  61. continue
  62. ok += 1
  63. print(f"[OK] {modname} ({classname})")
  64. print(f"\n{ok}/{len(NEW_MODS)} mods passed")
  65. for e in err:
  66. print("FAIL:", e)
  67. sys.exit(0 if not err else 1)