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

Skip to content

Commit fb9d7cd

Browse files
committed
refactoring, code clearing and removal of obsolete switch --longest-common
1 parent 534f51f commit fb9d7cd

11 files changed

Lines changed: 31 additions & 131 deletions

File tree

lib/controller/checks.py

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
from lib.core.exception import sqlmapNoneDataException
4949
from lib.core.exception import sqlmapUserQuitException
5050
from lib.core.session import setDynamicMarkings
51-
from lib.core.session import setString
52-
from lib.core.session import setRegexp
53-
from lib.core.session import setTextOnly
5451
from lib.core.settings import CONSTANT_RATIO
5552
from lib.core.settings import UPPER_RATIO_BOUND
5653
from lib.core.unescaper import unescaper
@@ -424,9 +421,14 @@ def checkSqlInjection(place, parameter, value):
424421
injection.data[stype].where = where
425422
injection.data[stype].vector = vector
426423
injection.data[stype].comment = comment
427-
injection.data[stype].matchRatio = kb.matchRatio
428424
injection.data[stype].templatePayload = templatePayload
429425

426+
injection.data[stype].conf = advancedDict()
427+
injection.data[stype].conf.matchRatio = kb.matchRatio
428+
injection.data[stype].conf.textOnly = conf.textOnly
429+
injection.data[stype].conf.string = conf.string
430+
injection.data[stype].conf.regexp = conf.regexp
431+
430432
if hasattr(test, "details"):
431433
for detailKey, detailValue in test.details.items():
432434
if detailKey == "dbms" and injection.dbms is None:
@@ -585,12 +587,6 @@ def checkDynamicContent(firstPage, secondPage):
585587
logger.debug(debugMsg)
586588
return
587589

588-
if conf.longestCommon:
589-
debugMsg = "dynamic content checking skipped "
590-
debugMsg += "because longest common comparison used"
591-
logger.debug(debugMsg)
592-
return
593-
594590
conf.seqMatcher.set_seq1(firstPage)
595591
conf.seqMatcher.set_seq2(secondPage)
596592

@@ -608,7 +604,6 @@ def checkDynamicContent(firstPage, secondPage):
608604
logger.warn(warnMsg)
609605

610606
conf.textOnly = True
611-
setTextOnly()
612607
return
613608

614609
warnMsg = "target url is heavily dynamic"
@@ -677,7 +672,6 @@ def checkStability():
677672

678673
if test:
679674
conf.string = test
680-
setString()
681675

682676
if kb.nullConnection:
683677
debugMsg = "turning off NULL connection "
@@ -695,7 +689,6 @@ def checkStability():
695689

696690
if test:
697691
conf.regex = test
698-
setRegexp()
699692

700693
if kb.nullConnection:
701694
debugMsg = "turning off NULL connection "
@@ -709,7 +702,6 @@ def checkStability():
709702

710703
elif test and test[0] in ("t", "T"):
711704
conf.textOnly = True
712-
setTextOnly()
713705

714706
if kb.nullConnection:
715707
debugMsg = "turning off NULL connection "
@@ -727,24 +719,13 @@ def checkString():
727719
if not conf.string:
728720
return True
729721

730-
condition = (
731-
kb.resumedQueries.has_key(conf.url) and
732-
kb.resumedQueries[conf.url].has_key("String") and
733-
kb.resumedQueries[conf.url]["String"][:-1] == conf.string
734-
)
735-
736-
if condition:
737-
return True
738-
739722
infoMsg = "testing if the provided string is within the "
740723
infoMsg += "target URL page content"
741724
logger.info(infoMsg)
742725

743726
page, _ = Request.queryPage(content=True)
744727

745-
if conf.string in page:
746-
setString()
747-
else:
728+
if conf.string not in page:
748729
warnMsg = "you provided '%s' as the string to " % conf.string
749730
warnMsg += "match, but such a string is not within the target "
750731
warnMsg += "URL page content original request, sqlmap will "
@@ -757,24 +738,13 @@ def checkRegexp():
757738
if not conf.regexp:
758739
return True
759740

760-
condition = (
761-
kb.resumedQueries.has_key(conf.url) and
762-
kb.resumedQueries[conf.url].has_key("Regular expression") and
763-
kb.resumedQueries[conf.url]["Regular expression"][:-1] == conf.regexp
764-
)
765-
766-
if condition:
767-
return True
768-
769741
infoMsg = "testing if the provided regular expression matches within "
770742
infoMsg += "the target URL page content"
771743
logger.info(infoMsg)
772744

773745
page, _ = Request.queryPage(content=True)
774746

775-
if re.search(conf.regexp, page, re.I | re.M):
776-
setRegexp()
777-
else:
747+
if not re.search(conf.regexp, page, re.I | re.M):
778748
warnMsg = "you provided '%s' as the regular expression to " % conf.regexp
779749
warnMsg += "match, but such a regular expression does not have any "
780750
warnMsg += "match within the target URL page content, sqlmap "

lib/core/common.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,20 @@ def initTechnique(technique=None):
19431943

19441944
if data:
19451945
kb.pageTemplate, kb.errorIsNone = getPageTemplate(data.templatePayload, kb.injection.place)
1946-
kb.matchRatio = data.matchRatio
1946+
1947+
kb.matchRatio = data.conf.matchRatio
1948+
if data.conf.textOnly:
1949+
conf.textOnly = True
1950+
debugMsg = "restoring switch --text-only"
1951+
logger.debug(debugMsg)
1952+
if data.conf.string:
1953+
conf.string = data.conf.string
1954+
debugMsg = "restoring option --string '%s'" % data.conf.string
1955+
logger.debug(debugMsg)
1956+
if data.conf.regexp:
1957+
conf.regexp = data.conf.regexp
1958+
debugMsg = "restoring option --regexp '%s'" % data.conf.regexp
1959+
logger.debug(debugMsg)
19471960
else:
19481961
warnMsg = "there is no injection data available for technique "
19491962
warnMsg += "'%s'" % enumValueToNameLookup(PAYLOAD.TECHNIQUE, technique)

lib/core/datatype.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,4 @@ def __init__(self):
8080
self.dbms = None
8181
self.dbms_version = None
8282
self.os = None
83+

lib/core/option.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def __cleanupOptions():
10451045
if conf.optimize:
10461046
#conf.predictOutput = True
10471047
conf.keepAlive = True
1048-
conf.nullConnection = not (conf.textOnly or conf.longestCommon)
1048+
conf.nullConnection = not conf.textOnly
10491049
conf.threads = 4 if conf.threads < 2 else conf.threads
10501050

10511051
if conf.realTest:
@@ -1320,10 +1320,6 @@ def __basicOptionValidation():
13201320
errMsg = "switch --text-only is incompatible with switch --null-connection"
13211321
raise sqlmapSyntaxException, errMsg
13221322

1323-
if conf.longestCommon and conf.nullConnection:
1324-
errMsg = "switch --longest-common is incompatible with switch --null-connection"
1325-
raise sqlmapSyntaxException, errMsg
1326-
13271323
if conf.data and conf.nullConnection:
13281324
errMsg = "switch --data is incompatible with switch --null-connection"
13291325
raise sqlmapSyntaxException, errMsg

lib/core/optiondict.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@
7070
"eString": "string",
7171
"eRegexp": "string",
7272
"thold": "float",
73-
"textOnly": "boolean",
74-
"longestCommon": "boolean"
73+
"textOnly": "boolean"
7574
},
7675

7776
"Techniques": {

lib/core/session.py

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -42,48 +42,6 @@ def unSafeFormatString(value):
4242
retVal = retVal.replace("__LEFT_SQUARE_BRACKET__", "[").replace("__RIGHT_SQUARE_BRACKET__", "]")
4343
return retVal
4444

45-
def setTextOnly():
46-
"""
47-
Save text only option to session file.
48-
"""
49-
50-
condition = (
51-
not kb.resumedQueries or ( kb.resumedQueries.has_key(conf.url) and
52-
not kb.resumedQueries[conf.url].has_key("Text only") )
53-
)
54-
55-
if condition:
56-
dataToSessionFile("[%s][None][None][Text only][True]\n" % conf.url)
57-
58-
kb.originalPage = getFilteredPageContent(kb.originalPage)
59-
kb.pageTemplates.clear()
60-
61-
def setString():
62-
"""
63-
Save string to match in session file.
64-
"""
65-
66-
condition = (
67-
not kb.resumedQueries or ( kb.resumedQueries.has_key(conf.url) and
68-
not kb.resumedQueries[conf.url].has_key("String") )
69-
)
70-
71-
if condition:
72-
dataToSessionFile("[%s][None][None][String][%s]\n" % (conf.url, safeFormatString(conf.string)))
73-
74-
def setRegexp():
75-
"""
76-
Save regular expression to match in session file.
77-
"""
78-
79-
condition = (
80-
not kb.resumedQueries or ( kb.resumedQueries.has_key(conf.url) and
81-
not kb.resumedQueries[conf.url].has_key("Regular expression") )
82-
)
83-
84-
if condition:
85-
dataToSessionFile("[%s][None][None][Regular expression][%s]\n" % (conf.url, safeFormatString(conf.regexp)))
86-
8745
def setInjection(inj):
8846
"""
8947
Save information retrieved about injection place and parameter in the
@@ -202,23 +160,7 @@ def setRemoteTempPath():
202160
dataToSessionFile("[%s][%s][%s][Remote temp path][%s]\n" % (conf.url, kb.injection.place, safeFormatString(conf.parameters[kb.injection.place]), safeFormatString(conf.tmpPath)))
203161

204162
def resumeConfKb(expression, url, value):
205-
if expression == "Text only" and url == conf.url:
206-
value = unSafeFormatString(value[:-1])
207-
208-
logMsg = "resuming text only option '%s' from session file" % value
209-
logger.info(logMsg)
210-
211-
if value and not conf.textOnly:
212-
message = "you did not turned on --text-only switch this time "
213-
message += "which could potentially lead to different "
214-
message += "and/or unstable results. "
215-
message += "Do you want to turn it on? [Y/n] "
216-
test = readInput(message, default="Y")
217-
218-
if not test or test[0] in ("y", "Y"):
219-
conf.textOnly = value
220-
221-
elif expression == "String" and url == conf.url:
163+
if expression == "String" and url == conf.url:
222164
string = unSafeFormatString(value[:-1])
223165

224166
logMsg = "resuming string match '%s' from session file" % string

lib/core/target.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from lib.core.option import __setDBMS
3131
from lib.core.option import __setKnowledgeBaseAttributes
3232
from lib.core.session import resumeConfKb
33-
from lib.core.session import setTextOnly
3433
from lib.core.xmldump import dumper as xmldumper
3534
from lib.request.connect import Connect as Request
3635

@@ -265,13 +264,6 @@ def __createTargetDirs():
265264
__createFilesDir()
266265
__configureDumper()
267266

268-
def __saveSwitches():
269-
"""
270-
Store critical switches to the session file.
271-
"""
272-
if conf.textOnly:
273-
setTextOnly()
274-
275267
def __restoreCmdLineOptions():
276268
"""
277269
Restore command line options that could be possibly
@@ -302,4 +294,3 @@ def setupTargetEnv():
302294
__createTargetDirs()
303295
__setRequestParams()
304296
__setOutputResume()
305-
__saveSwitches()

lib/parse/cmdline.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,6 @@ def cmdLineParser():
214214
action="store_true", default=False,
215215
help="Compare pages based only on their textual content")
216216

217-
detection.add_option("--longest-common", dest="longestCommon",
218-
action="store_true", default=False,
219-
help="Compare pages based on their longest common match")
220-
221217

222218
# Techniques options
223219
techniques = OptionGroup(parser, "Techniques", "These options can "

lib/request/comparison.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from difflib import SequenceMatcher
1313

14+
from lib.core.common import getFilteredPageContent
1415
from lib.core.common import removeDynamicContent
1516
from lib.core.common import wasLastRequestDBMSError
1617
from lib.core.common import wasLastRequestHTTPError
@@ -63,7 +64,7 @@ def comparison(page, headers=None, getSeqMatcher=False, pageLength=None):
6364
return None
6465

6566
# Dynamic content lines to be excluded before comparison
66-
if not kb.nullConnection and not conf.longestCommon:
67+
if not kb.nullConnection:
6768
page = removeDynamicContent(page)
6869
conf.seqMatcher.set_seq1(removeDynamicContent(kb.pageTemplate))
6970

@@ -73,12 +74,10 @@ def comparison(page, headers=None, getSeqMatcher=False, pageLength=None):
7374
if kb.locks.seqLock:
7475
kb.locks.seqLock.acquire()
7576

76-
if conf.longestCommon:
77-
(firstPage, secondPage) = (conf.seqMatcher.a, page)
78-
match = SequenceMatcher(None, firstPage, secondPage).find_longest_match(0, len(firstPage), 0, len(secondPage))
79-
ratio = round(SequenceMatcher(None, firstPage[match[0]:match[0]+match[2]], secondPage[match[1]:match[1]+match[2]]).ratio(), 3)
77+
if conf.textOnly:
78+
(conf.seqMatcher.a, page) = map(getFilteredPageContent, (conf.seqMatcher.a, page))
8079

81-
elif not conf.eRegexp and not conf.eString and kb.nullConnection and pageLength:
80+
if not conf.eRegexp and not conf.eString and kb.nullConnection and pageLength:
8281
ratio = 1. * pageLength / len(conf.seqMatcher.a)
8382

8483
if ratio > 1.:

lib/request/connect.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,9 +458,6 @@ def queryPage(value=None, place=None, content=False, getSeqMatcher=False, silent
458458

459459
threadData.lastQueryDuration = calculateDeltaSeconds(start)
460460

461-
if conf.textOnly:
462-
page = getFilteredPageContent(page)
463-
464461
if kb.testMode:
465462
kb.testQueryCount += 1
466463

0 commit comments

Comments
 (0)