Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2926c81

Browse files
committed
improved test switch --live-test and minor refactoring
1 parent f40c52c commit 2926c81

5 files changed

Lines changed: 66 additions & 61 deletions

File tree

lib/core/option.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,7 @@ def _saveCmdline():
17311731
infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG
17321732
logger.info(infoMsg)
17331733

1734-
def _setVerbosity():
1734+
def setVerbosity():
17351735
"""
17361736
This function set the verbosity of sqlmap output messages.
17371737
"""
@@ -2057,7 +2057,7 @@ def init(inputOptions=AttribDict(), overrideOptions=False):
20572057
_setKnowledgeBaseAttributes()
20582058
_mergeOptions(inputOptions, overrideOptions)
20592059
_useWizardInterface()
2060-
_setVerbosity()
2060+
setVerbosity()
20612061
_saveCmdline()
20622062
_setRequestFromFile()
20632063
_cleanupOptions()

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@
462462
CHECK_ZERO_COLUMNS_THRESHOLD = 10
463463

464464
# Boldify all logger messages containing these "patterns"
465-
BOLD_PATTERNS = ("' injectable", "might be injectable", "' is vulnerable", "is not injectable")
465+
BOLD_PATTERNS = ("' injectable", "might be injectable", "' is vulnerable", "is not injectable", "test failed", "test passed")
466466

467467
# Generic www root directory names
468468
GENERIC_DOC_ROOT_DIRECTORY_NAMES = ("htdocs", "wwwroot", "www")

lib/core/testing.py

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from lib.core.data import logger
2323
from lib.core.data import paths
2424
from lib.core.option import init
25-
from lib.core.option import _setVerbosity
25+
from lib.core.option import setVerbosity
2626
from lib.core.optiondict import optDict
2727
from lib.parse.cmdline import cmdLineParser
2828

@@ -118,39 +118,42 @@ def liveTest():
118118

119119
for case in livetests.getElementsByTagName("case"):
120120
count += 1
121-
122-
if conf.runCase and conf.runCase != count:
123-
continue
124-
125121
name = None
126-
log = []
122+
parse = []
127123
switches = dict(global_)
128124

129125
if case.hasAttribute("name"):
130126
name = case.getAttribute("name")
131127

128+
if conf.runCase and ((conf.runCase.isdigit() and conf.runCase != count) or not re.search(conf.runCase, name, re.DOTALL)):
129+
continue
130+
132131
if case.getElementsByTagName("switches"):
133132
for child in case.getElementsByTagName("switches")[0].childNodes:
134133
if child.nodeType == child.ELEMENT_NODE and child.hasAttribute("value"):
135134
value = replaceVars(child.getAttribute("value"), vars_)
136135
switches[child.tagName] = adjustValueType(child.tagName, value)
137136

138-
if case.getElementsByTagName("log"):
139-
for item in case.getElementsByTagName("log")[0].getElementsByTagName("item"):
137+
if case.getElementsByTagName("parse"):
138+
for item in case.getElementsByTagName("parse")[0].getElementsByTagName("item"):
140139
if item.hasAttribute("value"):
141-
log.append(replaceVars(item.getAttribute("value"), vars_))
140+
parse.append(replaceVars(item.getAttribute("value"), vars_))
142141

143142
msg = "running live test case '%s' (%d/%d)" % (name, count, length)
144143
logger.info(msg)
145-
result = runCase(switches, log)
144+
145+
result = runCase(switches, parse)
146+
146147
if result:
147148
logger.info("test passed")
148149
else:
149150
logger.error("test failed")
150151
beep()
152+
151153
retVal &= result
152154

153155
dataToStdout("\n")
156+
154157
if retVal:
155158
logger.info("live test final result: PASSED")
156159
else:
@@ -159,9 +162,12 @@ def liveTest():
159162
return retVal
160163

161164
def initCase(switches=None):
162-
paths.SQLMAP_OUTPUT_PATH = tempfile.mkdtemp()
165+
paths.SQLMAP_OUTPUT_PATH = tempfile.mkdtemp(prefix="sqlmaptest-")
163166
paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
164167
paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")
168+
169+
logger.debug("using output directory '%s' for this test case" % paths.SQLMAP_OUTPUT_PATH)
170+
165171
cmdLineOptions = cmdLineParser()
166172
cmdLineOptions.liveTest = cmdLineOptions.smokeTest = False
167173

@@ -171,29 +177,29 @@ def initCase(switches=None):
171177
cmdLineOptions.__dict__[key] = value
172178

173179
init(cmdLineOptions, True)
174-
_setVerbosity()
180+
conf.verbose = 0
181+
setVerbosity()
175182

176183
def cleanCase():
177184
shutil.rmtree(paths.SQLMAP_OUTPUT_PATH, True)
178-
paths.SQLMAP_OUTPUT_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "output")
179-
paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
180-
paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")
181185
conf.verbose = 1
182-
_setVerbosity()
186+
setVerbosity()
183187

184-
def runCase(switches=None, log=None):
188+
def runCase(switches=None, parse=None):
185189
retVal = True
186190
initCase(switches)
187191

188192
result = start()
189-
if result == False: #if None ignore
193+
194+
if result == False: # if None, ignore
195+
logger.error("the test did not run")
190196
retVal = False
191197

192-
if log and retVal:
198+
if parse and retVal:
193199
ifile = open(conf.dumper.getOutputFile(), 'r')
194200
content = ifile.read()
195201
ifile.close()
196-
for item in log:
202+
for item in parse:
197203
if item.startswith("r'") and item.endswith("'"):
198204
if not re.search(item[2:-1], content, re.DOTALL):
199205
retVal = False

lib/parse/cmdline.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,7 @@ def cmdLineParser():
679679
parser.add_option("--live-test", dest="liveTest", action="store_true",
680680
help=SUPPRESS_HELP)
681681

682-
parser.add_option("--run-case", dest="runCase", type="int",
683-
help=SUPPRESS_HELP)
682+
parser.add_option("--run-case", dest="runCase", help=SUPPRESS_HELP)
684683

685684
parser.add_option("--restapi", dest="restApi", action="store_true",
686685
help=SUPPRESS_HELP)

0 commit comments

Comments
 (0)