test.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/env python3
  2. """Dorking pipeline: search_zombies() reaches each engine branch without raising.
  3. We monkey-patch _engine_fetch (HTTP-fetched engines) and the DDGS class (duck)
  4. to return synthetic markup, then call search_zombies() with engine=<each> and
  5. verify it parses at least one href without exceptions. This validates the
  6. regex / decoder paths for all 7 engines without touching the network.
  7. """
  8. import sys, os, types
  9. sys.path.insert(0, os.path.abspath('test'))
  10. from core.main import UFONet
  11. err = []
  12. ENGINES_HTTP = {
  13. "bing": '<a class="tilk" href="https://www.bing.com/ck/a?!&u=a1aHR0cHM6Ly9leGFtcGxlLmNvbS9wcm94eS5waHA_dXJsPQ&p=foo">Example</a>',
  14. "brave": '<a href="https://example.com/proxy.php?url=" rel="noopener">Example</a>',
  15. "mojeek": '<a class="ob" href="https://example.com/proxy.php?url=">Example</a>',
  16. "yahoo": '<a class="d-ib something" href="https://r.search.yahoo.com/_ylt=foo/RU=https%3A%2F%2Fexample.com%2Fproxy.php%3Furl%3D/RK=/foo">Example</a>',
  17. "startpage": '<a class="w-gl__result-title result-link" href="https://example.com/proxy.php?url=">Example</a>',
  18. "ecosia": '<a class="result-title" href="https://example.com/proxy.php?url=">Example</a>',
  19. }
  20. DUCK_RESULTS = [
  21. {"href": "https://example.com/proxy.php?url=foo", "title": "ex", "body": "x"},
  22. {"href": "https://example.org/proxy.php?url=bar", "title": "ex2", "body": "x"},
  23. ]
  24. class _FakeDDGS:
  25. def text(self, q, **kw):
  26. return iter(DUCK_RESULTS)
  27. def make_ufo():
  28. ufo = UFONet()
  29. ufo.create_options(['--sd', 'botnet/dorks.txt'])
  30. ufo.options.verbose = False
  31. ufo.options.num_results = 5
  32. ufo.user_agent = "Mozilla/5.0 test"
  33. ufo.referer = "https://example.com"
  34. ufo.agents = [ufo.user_agent]
  35. ufo.options.proxy = None
  36. ufo.options.forceyes = True
  37. return ufo
  38. for engine, body in ENGINES_HTTP.items():
  39. try:
  40. ufo = make_ufo()
  41. ufo.options.engine = engine
  42. ufo._engine_fetch = lambda url, headers, timeout=10, _body=body: _body
  43. result = ufo.search_zombies('proxy.php?url=', [])
  44. print("[OK] " + engine + " -> parsed (returned object: " + str(type(result).__name__) + ")")
  45. except Exception as e:
  46. err.append(engine + ": " + type(e).__name__ + ": " + str(e)[:200])
  47. try:
  48. ufo = make_ufo()
  49. ufo.options.engine = "duck"
  50. import sys as _s
  51. _fake_mod = types.ModuleType("ddgs")
  52. _fake_mod.DDGS = _FakeDDGS
  53. _s.modules["ddgs"] = _fake_mod
  54. _fake_dds = types.ModuleType("duckduckgo_search")
  55. _fake_dds.DDGS = _FakeDDGS
  56. _s.modules["duckduckgo_search"] = _fake_dds
  57. result = ufo.search_zombies('proxy.php?url=', [])
  58. print("[OK] duck -> parsed (" + str(type(result).__name__) + ")")
  59. except Exception as e:
  60. err.append("duck: " + type(e).__name__ + ": " + str(e)[:200])
  61. for e in err:
  62. print("FAIL:", e)
  63. print()
  64. print(str(7 - len(err)) + "/7 engine code paths OK")
  65. sys.exit(0 if not err else 1)