Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/lib/parse/configfile.py
2989 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
from lib.core.common import checkFile
9
from lib.core.common import getSafeExString
10
from lib.core.common import openFile
11
from lib.core.common import unArrayizeValue
12
from lib.core.common import UnicodeRawConfigParser
13
from lib.core.convert import getUnicode
14
from lib.core.data import cmdLineOptions
15
from lib.core.data import conf
16
from lib.core.data import logger
17
from lib.core.enums import OPTION_TYPE
18
from lib.core.exception import SqlmapMissingMandatoryOptionException
19
from lib.core.exception import SqlmapSyntaxException
20
from lib.core.optiondict import optDict
21
22
config = None
23
24
def configFileProxy(section, option, datatype):
25
"""
26
Parse configuration file and save settings into the configuration
27
advanced dictionary.
28
"""
29
30
if config.has_option(section, option):
31
try:
32
if datatype == OPTION_TYPE.BOOLEAN:
33
value = config.getboolean(section, option) if config.get(section, option) else False
34
elif datatype == OPTION_TYPE.INTEGER:
35
value = config.getint(section, option) if config.get(section, option) else 0
36
elif datatype == OPTION_TYPE.FLOAT:
37
value = config.getfloat(section, option) if config.get(section, option) else 0.0
38
else:
39
value = config.get(section, option)
40
except ValueError as ex:
41
errMsg = "error occurred while processing the option "
42
errMsg += "'%s' in provided configuration file ('%s')" % (option, getUnicode(ex))
43
raise SqlmapSyntaxException(errMsg)
44
45
if value:
46
conf[option] = value
47
else:
48
conf[option] = None
49
else:
50
debugMsg = "missing requested option '%s' (section " % option
51
debugMsg += "'%s') into the configuration file, " % section
52
debugMsg += "ignoring. Skipping to next."
53
logger.debug(debugMsg)
54
55
def configFileParser(configFile):
56
"""
57
Parse configuration file and save settings into the configuration
58
advanced dictionary.
59
"""
60
61
global config
62
63
debugMsg = "parsing configuration file"
64
logger.debug(debugMsg)
65
66
checkFile(configFile)
67
configFP = openFile(configFile, "rb")
68
69
try:
70
config = UnicodeRawConfigParser()
71
if hasattr(config, "read_file"):
72
config.read_file(configFP)
73
else:
74
config.readfp(configFP)
75
except Exception as ex:
76
errMsg = "you have provided an invalid and/or unreadable configuration file ('%s')" % getSafeExString(ex)
77
raise SqlmapSyntaxException(errMsg)
78
79
if not config.has_section("Target"):
80
errMsg = "missing a mandatory section 'Target' in the configuration file"
81
raise SqlmapMissingMandatoryOptionException(errMsg)
82
83
mandatory = False
84
85
for option in ("direct", "url", "logFile", "bulkFile", "googleDork", "requestFile", "wizard"):
86
if config.has_option("Target", option) and config.get("Target", option) or cmdLineOptions.get(option):
87
mandatory = True
88
break
89
90
if not mandatory:
91
errMsg = "missing a mandatory option in the configuration file "
92
errMsg += "(direct, url, logFile, bulkFile, googleDork, requestFile or wizard)"
93
raise SqlmapMissingMandatoryOptionException(errMsg)
94
95
for family, optionData in optDict.items():
96
for option, datatype in optionData.items():
97
datatype = unArrayizeValue(datatype)
98
configFileProxy(family, option, datatype)
99
100