|
76 | 76 | from lib.core.enums import DUMP_FORMAT |
77 | 77 | from lib.core.enums import HTTP_HEADER |
78 | 78 | from lib.core.enums import HTTPMETHOD |
| 79 | +from lib.core.enums import MKSTEMP_PREFIX |
79 | 80 | from lib.core.enums import MOBILES |
80 | 81 | from lib.core.enums import OPTION_TYPE |
81 | 82 | from lib.core.enums import PAYLOAD |
@@ -825,6 +826,80 @@ def _setTamperingFunctions(): |
825 | 826 | for _, function in priorities: |
826 | 827 | kb.tamperFunctions.append(function) |
827 | 828 |
|
| 829 | +def _setPreprocessFunctions(): |
| 830 | + """ |
| 831 | + Loads preprocess functions from given script(s) |
| 832 | + """ |
| 833 | + |
| 834 | + if conf.preprocess: |
| 835 | + for script in re.split(PARAMETER_SPLITTING_REGEX, conf.preprocess): |
| 836 | + found = False |
| 837 | + |
| 838 | + script = script.strip().encode(sys.getfilesystemencoding() or UNICODE_ENCODING) |
| 839 | + |
| 840 | + try: |
| 841 | + if not script: |
| 842 | + continue |
| 843 | + |
| 844 | + if not os.path.exists(script): |
| 845 | + errMsg = "preprocess script '%s' does not exist" % script |
| 846 | + raise SqlmapFilePathException(errMsg) |
| 847 | + |
| 848 | + elif not script.endswith(".py"): |
| 849 | + errMsg = "preprocess script '%s' should have an extension '.py'" % script |
| 850 | + raise SqlmapSyntaxException(errMsg) |
| 851 | + except UnicodeDecodeError: |
| 852 | + errMsg = "invalid character provided in option '--preprocess'" |
| 853 | + raise SqlmapSyntaxException(errMsg) |
| 854 | + |
| 855 | + dirname, filename = os.path.split(script) |
| 856 | + dirname = os.path.abspath(dirname) |
| 857 | + |
| 858 | + infoMsg = "loading preprocess module '%s'" % filename[:-3] |
| 859 | + logger.info(infoMsg) |
| 860 | + |
| 861 | + if not os.path.exists(os.path.join(dirname, "__init__.py")): |
| 862 | + errMsg = "make sure that there is an empty file '__init__.py' " |
| 863 | + errMsg += "inside of preprocess scripts directory '%s'" % dirname |
| 864 | + raise SqlmapGenericException(errMsg) |
| 865 | + |
| 866 | + if dirname not in sys.path: |
| 867 | + sys.path.insert(0, dirname) |
| 868 | + |
| 869 | + try: |
| 870 | + module = __import__(filename[:-3].encode(sys.getfilesystemencoding() or UNICODE_ENCODING)) |
| 871 | + except Exception as ex: |
| 872 | + raise SqlmapSyntaxException("cannot import preprocess module '%s' (%s)" % (filename[:-3], getSafeExString(ex))) |
| 873 | + |
| 874 | + for name, function in inspect.getmembers(module, inspect.isfunction): |
| 875 | + if name == "preprocess" and inspect.getargspec(function).args and all(_ in inspect.getargspec(function).args for _ in ("page", "headers", "code")): |
| 876 | + found = True |
| 877 | + |
| 878 | + kb.preprocessFunctions.append(function) |
| 879 | + function.func_name = module.__name__ |
| 880 | + |
| 881 | + break |
| 882 | + |
| 883 | + if not found: |
| 884 | + errMsg = "missing function 'preprocess(page, headers=None, code=None)' " |
| 885 | + errMsg += "in preprocess script '%s'" % script |
| 886 | + raise SqlmapGenericException(errMsg) |
| 887 | + else: |
| 888 | + try: |
| 889 | + _, _, _ = function("", {}, None) |
| 890 | + except: |
| 891 | + handle, filename = tempfile.mkstemp(prefix=MKSTEMP_PREFIX.PREPROCESS, suffix=".py") |
| 892 | + os.close(handle) |
| 893 | + |
| 894 | + open(filename, "w+b").write("#!/usr/bin/env\n\ndef preprocess(page, headers=None, code=None):\n return page, headers, code\n") |
| 895 | + open(os.path.join(os.path.dirname(filename), "__init__.py"), "w+b").write("pass") |
| 896 | + |
| 897 | + errMsg = "function 'preprocess(page, headers=None, code=None)' " |
| 898 | + errMsg += "in preprocess script '%s' " % script |
| 899 | + errMsg += "should return a tuple '(page, headers, code)' " |
| 900 | + errMsg += "(Note: find template script at '%s')" % filename |
| 901 | + raise SqlmapGenericException(errMsg) |
| 902 | + |
828 | 903 | def _setWafFunctions(): |
829 | 904 | """ |
830 | 905 | Loads WAF/IPS detecting functions from script(s) |
@@ -1937,6 +2012,7 @@ def _setKnowledgeBaseAttributes(flushAll=True): |
1937 | 2012 | kb.headerPaths = {} |
1938 | 2013 | kb.keywords = set(getFileItems(paths.SQL_KEYWORDS)) |
1939 | 2014 | kb.passwordMgr = None |
| 2015 | + kb.preprocessFunctions = [] |
1940 | 2016 | kb.skipVulnHost = None |
1941 | 2017 | kb.tamperFunctions = [] |
1942 | 2018 | kb.targets = oset() |
@@ -2549,6 +2625,7 @@ def init(): |
2549 | 2625 | _setMultipleTargets() |
2550 | 2626 | _listTamperingFunctions() |
2551 | 2627 | _setTamperingFunctions() |
| 2628 | + _setPreprocessFunctions() |
2552 | 2629 | _setWafFunctions() |
2553 | 2630 | _setTrafficOutputFP() |
2554 | 2631 | _setupHTTPCollector() |
|
0 commit comments