_cdn.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of the UFONet project, https://ufonet.03c8.net
  5. Copyright (c) 2013/2026 | psy <epsylon@riseup.net>
  6. You should have received a copy of the GNU General Public License along
  7. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  8. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  9. """
  10. import re, ssl, urllib.request, urllib.error
  11. from urllib.parse import urlparse
  12. CDN_HEADER_SIGNATURES = {
  13. 'Cloudflare': [
  14. ('server', re.compile(r'cloudflare', re.I)),
  15. ('cf-ray', None),
  16. ('cf-cache-status', None),
  17. ('cf-connecting-ip', None),
  18. ],
  19. 'Akamai': [
  20. ('server', re.compile(r'AkamaiGHost|AkamaiNetStorage', re.I)),
  21. ('x-akamai-request-id', None),
  22. ('x-akamai-transformed', None),
  23. ('x-akamai-staging', None),
  24. ],
  25. 'AWS CloudFront': [
  26. ('via', re.compile(r'CloudFront', re.I)),
  27. ('x-amz-cf-id', None),
  28. ('x-amz-cf-pop', None),
  29. ],
  30. 'Fastly': [
  31. ('via', re.compile(r'\bfastly\b', re.I)),
  32. ('x-served-by', re.compile(r'cache-', re.I)),
  33. ('x-fastly-request-id', None),
  34. ('fastly-debug-digest', None),
  35. ],
  36. 'Sucuri': [
  37. ('server', re.compile(r'Sucuri', re.I)),
  38. ('x-sucuri-id', None),
  39. ('x-sucuri-cache', None),
  40. ],
  41. 'Imperva (Incapsula)': [
  42. ('x-iinfo', None),
  43. ('x-cdn', re.compile(r'incapsula|imperva', re.I)),
  44. ('server', re.compile(r'incapsula', re.I)),
  45. ],
  46. 'Google Cloud / GFE': [
  47. ('via', re.compile(r'\bGoogle\b', re.I)),
  48. ('server', re.compile(r'\bGFE/', re.I)),
  49. ],
  50. 'Microsoft Azure': [
  51. ('x-azure-ref', None),
  52. ('x-msedge-ref', None),
  53. ],
  54. 'KeyCDN': [
  55. ('server', re.compile(r'KeyCDN', re.I)),
  56. ('x-edge-location', None),
  57. ],
  58. 'StackPath / MaxCDN': [
  59. ('server', re.compile(r'StackPath|MaxCDN|NetDNA', re.I)),
  60. ('x-hw', None),
  61. ],
  62. 'Bunny.net': [
  63. ('server', re.compile(r'BunnyCDN', re.I)),
  64. ('cdn-pullzone', None),
  65. ],
  66. 'CDN77': [
  67. ('server', re.compile(r'CDN77', re.I)),
  68. ],
  69. 'OVH / EdgeCDN': [
  70. ('server', re.compile(r'OVH', re.I)),
  71. ('x-iplb-instance', None),
  72. ],
  73. 'Section.io': [
  74. ('x-section-cache', None),
  75. ],
  76. 'Reblaze': [
  77. ('server', re.compile(r'rbzid|reblaze', re.I)),
  78. ],
  79. 'F5 BIG-IP': [
  80. ('server', re.compile(r'BigIP|BIG-IP', re.I)),
  81. ],
  82. 'Varnish (front)': [
  83. ('via', re.compile(r'varnish', re.I)),
  84. ('x-varnish', None),
  85. ],
  86. }
  87. def detect_cdn(target, timeout=8, verify=False):
  88. """Return (cdn_name, evidence_header) if target appears behind a CDN/WAF; (None, None) otherwise."""
  89. if not target:
  90. return (None, None)
  91. if '://' not in target:
  92. target = 'http://' + target
  93. parsed = urlparse(target)
  94. url = parsed.scheme + '://' + parsed.netloc + '/'
  95. ctx = ssl.create_default_context()
  96. if not verify:
  97. ctx.check_hostname = False
  98. ctx.verify_mode = ssl.CERT_NONE
  99. headers = {}
  100. try:
  101. req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 UFONet-CDNcheck'}, method='HEAD')
  102. resp = urllib.request.urlopen(req, timeout=timeout, context=ctx)
  103. for k, v in resp.headers.items():
  104. headers[k.lower()] = v
  105. except urllib.error.HTTPError as e:
  106. try:
  107. for k, v in e.headers.items():
  108. headers[k.lower()] = v
  109. except Exception:
  110. pass
  111. except Exception:
  112. return (None, None)
  113. for cdn, sigs in CDN_HEADER_SIGNATURES.items():
  114. for hname, pat in sigs:
  115. if hname in headers:
  116. val = headers[hname]
  117. if pat is None:
  118. return (cdn, hname + ': ' + str(val)[:80])
  119. if pat.search(str(val)):
  120. return (cdn, hname + ': ' + str(val)[:80])
  121. return (None, None)