|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +Copyright (c) 2006-2018 sqlmap developers (http://sqlmap.org/) |
| 5 | +See the file 'LICENSE' for copying permission |
| 6 | +""" |
| 7 | + |
| 8 | +import cookielib |
| 9 | +import glob |
| 10 | +import httplib |
| 11 | +import inspect |
| 12 | +import os |
| 13 | +import re |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import urllib |
| 17 | +import urllib2 |
| 18 | +import urlparse |
| 19 | + |
| 20 | +sys.dont_write_bytecode = True |
| 21 | + |
| 22 | +NAME, VERSION, AUTHOR = "WAF Detectify", "0.1", "Miroslav Stampar (@stamparm)" |
| 23 | +TIMEOUT = 10 |
| 24 | +HEADERS = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:61.0) Gecko/20100101 Firefox/61.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Cache-Control": "max-age=0"} |
| 25 | +SQLMAP_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) |
| 26 | +SCRIPTS_DIR = os.path.join(SQLMAP_DIR, "waf") |
| 27 | +CACHE = {} |
| 28 | +WAF_FUNCTIONS = [] |
| 29 | + |
| 30 | +def get_page(get=None, url=None, host=None, data=None): |
| 31 | + key = (get, url, host, data) |
| 32 | + |
| 33 | + if key in CACHE: |
| 34 | + return CACHE[key] |
| 35 | + |
| 36 | + page, headers, code = None, {}, httplib.OK |
| 37 | + |
| 38 | + url = url or ("%s%s%s" % (sys.argv[1], '?' if '?' not in sys.argv[1] else '&', get) if get else sys.argv[1]) |
| 39 | + if not url.startswith("http"): |
| 40 | + url = "http://%s" % url |
| 41 | + |
| 42 | + try: |
| 43 | + req = urllib2.Request("".join(url[_].replace(' ', "%20") if _ > url.find('?') else url[_] for _ in xrange(len(url))), data, HEADERS) |
| 44 | + page = urllib2.urlopen(req, timeout=TIMEOUT).read() |
| 45 | + except Exception, ex: |
| 46 | + code = getattr(ex, "code", None) |
| 47 | + page = ex.read() if hasattr(ex, "read") else getattr(ex, "msg", "") |
| 48 | + |
| 49 | + result = CACHE[key] = page, headers, code |
| 50 | + |
| 51 | + return result |
| 52 | + |
| 53 | +def main(): |
| 54 | + global WAF_FUNCTIONS |
| 55 | + |
| 56 | + print "%s #v%s\n by: %s\n" % (NAME, VERSION, AUTHOR) |
| 57 | + |
| 58 | + if len(sys.argv) < 2: |
| 59 | + exit("[x] usage: python %s <hostname>" % os.path.split(__file__)[-1]) |
| 60 | + |
| 61 | + cookie_jar = cookielib.CookieJar() |
| 62 | + opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) |
| 63 | + urllib2.install_opener(opener) |
| 64 | + |
| 65 | + sys.path.insert(0, SQLMAP_DIR) |
| 66 | + |
| 67 | + for found in glob.glob(os.path.join(SCRIPTS_DIR, "*.py")): |
| 68 | + dirname, filename = os.path.split(found) |
| 69 | + dirname = os.path.abspath(dirname) |
| 70 | + |
| 71 | + if filename == "__init__.py": |
| 72 | + continue |
| 73 | + |
| 74 | + if dirname not in sys.path: |
| 75 | + sys.path.insert(0, dirname) |
| 76 | + |
| 77 | + try: |
| 78 | + if filename[:-3] in sys.modules: |
| 79 | + del sys.modules[filename[:-3]] |
| 80 | + module = __import__(filename[:-3].encode(sys.getfilesystemencoding() or "utf8")) |
| 81 | + except ImportError, msg: |
| 82 | + exit("[x] cannot import WAF script '%s' (%s)" % (filename[:-3], msg)) |
| 83 | + |
| 84 | + _ = dict(inspect.getmembers(module)) |
| 85 | + if "detect" not in _: |
| 86 | + exit("[x] missing function 'detect(get_page)' in WAF script '%s'" % found) |
| 87 | + else: |
| 88 | + WAF_FUNCTIONS.append((_["detect"], _.get("__product__", filename[:-3]))) |
| 89 | + |
| 90 | + WAF_FUNCTIONS = sorted(WAF_FUNCTIONS, key=lambda _: "generic" in _[1].lower()) |
| 91 | + |
| 92 | + print "[i] %d (sqlmap's) WAF scripts loaded" % len(WAF_FUNCTIONS) |
| 93 | + |
| 94 | + found = False |
| 95 | + for function, product in WAF_FUNCTIONS: |
| 96 | + if found and "unknown" in product.lower(): |
| 97 | + continue |
| 98 | + |
| 99 | + if function(get_page): |
| 100 | + print "[!] WAF/IPS/IDS identified as '%s'" % product |
| 101 | + found = True |
| 102 | + |
| 103 | + if not found: |
| 104 | + print "[o] nothing found" |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + main() |
0 commit comments