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

Skip to content

Commit 422b1a6

Browse files
committed
Minor patches and updates
1 parent eec048d commit 422b1a6

9 files changed

Lines changed: 106 additions & 45 deletions

File tree

lib/core/common.py

Lines changed: 79 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
from lib.core.data import kb
6464
from lib.core.data import logger
6565
from lib.core.data import paths
66+
from lib.core.datatype import OrderedSet
6667
from lib.core.decorators import cachedmethod
6768
from lib.core.defaults import defaults
6869
from lib.core.dicts import DBMS_DICT
@@ -843,7 +844,15 @@ def getManualDirectories():
843844
return directories
844845

845846
def getAutoDirectories():
846-
retVal = set()
847+
"""
848+
>>> pushValue(kb.absFilePaths)
849+
>>> kb.absFilePaths = ["C:\\inetpub\\wwwroot\\index.asp", "/var/www/html"]
850+
>>> getAutoDirectories()
851+
['C:/inetpub/wwwroot', '/var/www/html']
852+
>>> kb.absFilePaths = popValue()
853+
"""
854+
855+
retVal = OrderedSet()
847856

848857
if kb.absFilePaths:
849858
infoMsg = "retrieved web server absolute paths: "
@@ -1370,7 +1379,16 @@ def weAreFrozen():
13701379

13711380
def parseTargetDirect():
13721381
"""
1373-
Parse target dbms and set some attributes into the configuration singleton.
1382+
Parse target dbms and set some attributes into the configuration singleton
1383+
1384+
>>> pushValue(conf.direct)
1385+
>>> conf.direct = "mysql://root:[email protected]:3306/testdb"
1386+
>>> parseTargetDirect()
1387+
>>> conf.dbmsDb
1388+
'testdb'
1389+
>>> conf.dbmsPass
1390+
'testpass'
1391+
>>> conf.direct = popValue()
13741392
"""
13751393

13761394
if not conf.direct:
@@ -1411,6 +1429,9 @@ def parseTargetDirect():
14111429

14121430
break
14131431

1432+
if kb.smokeMode:
1433+
return
1434+
14141435
if not details:
14151436
errMsg = "invalid target details, valid syntax is for instance "
14161437
errMsg += "'mysql://USER:PASSWORD@DBMS_IP:DBMS_PORT/DATABASE_NAME' "
@@ -1475,7 +1496,16 @@ def parseTargetDirect():
14751496

14761497
def parseTargetUrl():
14771498
"""
1478-
Parse target URL and set some attributes into the configuration singleton.
1499+
Parse target URL and set some attributes into the configuration singleton
1500+
1501+
>>> pushValue(conf.url)
1502+
>>> conf.url = "https://www.test.com/?id=1"
1503+
>>> parseTargetUrl()
1504+
>>> conf.hostname
1505+
'www.test.com'
1506+
>>> conf.scheme
1507+
'https'
1508+
>>> conf.url = popValue()
14791509
"""
14801510

14811511
if not conf.url:
@@ -1826,11 +1856,13 @@ def directoryPath(filepath):
18261856
18271857
>>> directoryPath('/var/log/apache.log')
18281858
'/var/log'
1859+
>>> directoryPath('/var/log')
1860+
'/var/log'
18291861
"""
18301862

18311863
retVal = filepath
18321864

1833-
if filepath:
1865+
if filepath and os.path.splitext(filepath)[-1]:
18341866
retVal = ntpath.dirname(filepath) if isWindowsDriveLetterPath(filepath) else posixpath.dirname(filepath)
18351867

18361868
return retVal
@@ -3029,8 +3061,7 @@ def filterNone(values):
30293061

30303062
def isDBMSVersionAtLeast(version):
30313063
"""
3032-
Checks if the recognized DBMS version is at least the version
3033-
specified
3064+
Checks if the recognized DBMS version is at least the version specified
30343065
"""
30353066

30363067
retVal = None
@@ -3065,6 +3096,12 @@ def isDBMSVersionAtLeast(version):
30653096
def parseSqliteTableSchema(value):
30663097
"""
30673098
Parses table column names and types from specified SQLite table schema
3099+
3100+
>>> kb.data.cachedColumns = {}
3101+
>>> parseSqliteTableSchema("CREATE TABLE users\\n\\t\\tid INTEGER\\n\\t\\tname TEXT\\n);")
3102+
True
3103+
>>> repr(kb.data.cachedColumns).count(',') == 1
3104+
True
30683105
"""
30693106

30703107
retVal = False
@@ -3091,8 +3128,13 @@ def getTechniqueData(technique=None):
30913128

30923129
def isTechniqueAvailable(technique):
30933130
"""
3094-
Returns True if there is injection data which sqlmap could use for
3095-
technique specified
3131+
Returns True if there is injection data which sqlmap could use for technique specified
3132+
3133+
>>> pushValue(kb.injection.data)
3134+
>>> kb.injection.data[PAYLOAD.TECHNIQUE.ERROR] = [test for test in getSortedInjectionTests() if "error" in test["title"].lower()][0]
3135+
>>> isTechniqueAvailable(PAYLOAD.TECHNIQUE.ERROR)
3136+
True
3137+
>>> kb.injection.data = popValue()
30963138
"""
30973139

30983140
if conf.tech and isinstance(conf.tech, list) and technique not in conf.tech:
@@ -3103,6 +3145,12 @@ def isTechniqueAvailable(technique):
31033145
def isStackingAvailable():
31043146
"""
31053147
Returns True whether techniques using stacking are available
3148+
3149+
>>> pushValue(kb.injection.data)
3150+
>>> kb.injection.data[PAYLOAD.TECHNIQUE.STACKED] = [test for test in getSortedInjectionTests() if "stacked" in test["title"].lower()][0]
3151+
>>> isStackingAvailable()
3152+
True
3153+
>>> kb.injection.data = popValue()
31063154
"""
31073155

31083156
retVal = False
@@ -3121,6 +3169,12 @@ def isStackingAvailable():
31213169
def isInferenceAvailable():
31223170
"""
31233171
Returns True whether techniques using inference technique are available
3172+
3173+
>>> pushValue(kb.injection.data)
3174+
>>> kb.injection.data[PAYLOAD.TECHNIQUE.BOOLEAN] = getSortedInjectionTests()[0]
3175+
>>> isInferenceAvailable()
3176+
True
3177+
>>> kb.injection.data = popValue()
31243178
"""
31253179

31263180
return any(isTechniqueAvailable(_) for _ in (PAYLOAD.TECHNIQUE.BOOLEAN, PAYLOAD.TECHNIQUE.STACKED, PAYLOAD.TECHNIQUE.TIME))
@@ -3290,8 +3344,13 @@ def isListLike(value):
32903344

32913345
def getSortedInjectionTests():
32923346
"""
3293-
Returns prioritized test list by eventually detected DBMS from error
3294-
messages
3347+
Returns prioritized test list by eventually detected DBMS from error messages
3348+
3349+
>>> pushValue(kb.forcedDbms)
3350+
>>> kb.forcedDbms = DBMS.SQLITE
3351+
>>> [test for test in getSortedInjectionTests() if hasattr(test, "details") and hasattr(test.details, "dbms")][0].details.dbms == kb.forcedDbms
3352+
True
3353+
>>> kb.forcedDbms = popValue()
32953354
"""
32963355

32973356
retVal = copy.deepcopy(conf.tests)
@@ -3317,8 +3376,7 @@ def priorityFunction(test):
33173376

33183377
def filterListValue(value, regex):
33193378
"""
3320-
Returns list with items that have parts satisfying given regular
3321-
expression
3379+
Returns list with items that have parts satisfying given regular expression
33223380
33233381
>>> filterListValue(['users', 'admins', 'logs'], r'(users|admins)')
33243382
['users', 'admins']
@@ -3348,6 +3406,9 @@ def showHttpErrorCodes():
33483406
def openFile(filename, mode='r', encoding=UNICODE_ENCODING, errors="reversible", buffering=1): # "buffering=1" means line buffered (Reference: http://stackoverflow.com/a/3168436)
33493407
"""
33503408
Returns file handle of a given filename
3409+
3410+
>>> "openFile" in openFile(__file__).read()
3411+
True
33513412
"""
33523413

33533414
if filename == STDIN_PIPE_DASH:
@@ -3399,22 +3460,6 @@ def decodeIntToUnicode(value):
33993460

34003461
return retVal
34013462

3402-
def md5File(filename):
3403-
"""
3404-
Calculates MD5 digest of a file
3405-
3406-
# Reference: http://stackoverflow.com/a/3431838
3407-
"""
3408-
3409-
checkFile(filename)
3410-
3411-
digest = hashlib.md5()
3412-
with open(filename, "rb") as f:
3413-
for chunk in iter(lambda: f.read(4096), ""):
3414-
digest.update(chunk)
3415-
3416-
return digest.hexdigest()
3417-
34183463
def checkIntegrity():
34193464
"""
34203465
Checks integrity of code files during the unhandled exceptions
@@ -3441,6 +3486,9 @@ def checkIntegrity():
34413486
def getDaysFromLastUpdate():
34423487
"""
34433488
Get total number of days from last update
3489+
3490+
>>> getDaysFromLastUpdate() >= 0
3491+
True
34443492
"""
34453493

34463494
if not paths:
@@ -3451,6 +3499,9 @@ def getDaysFromLastUpdate():
34513499
def unhandledExceptionMessage():
34523500
"""
34533501
Returns detailed message about occurred unhandled exception
3502+
3503+
>>> all(_ in unhandledExceptionMessage() for _ in ("unhandled exception occurred", "Operating system", "Command line"))
3504+
True
34543505
"""
34553506

34563507
errMsg = "unhandled exception occurred in %s. It is recommended to retry your " % VERSION_STRING

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1987,6 +1987,7 @@ def _setKnowledgeBaseAttributes(flushAll=True):
19871987
kb.serverHeader = None
19881988
kb.singleLogFlags = set()
19891989
kb.skipSeqMatcher = False
1990+
kb.smokeMode = False
19901991
kb.reduceTests = None
19911992
kb.tlsSNI = {}
19921993
kb.stickyDBMS = False

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from thirdparty import six
1919

2020
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
21-
VERSION = "1.3.5.26"
21+
VERSION = "1.3.5.27"
2222
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2323
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2424
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

lib/core/testing.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import codecs
99
import doctest
10+
import logging
1011
import os
1112
import random
1213
import re
@@ -29,6 +30,7 @@
2930
from lib.core.compat import xrange
3031
from lib.core.convert import getUnicode
3132
from lib.core.data import conf
33+
from lib.core.data import kb
3234
from lib.core.data import logger
3335
from lib.core.data import paths
3436
from lib.core.enums import MKSTEMP_PREFIX
@@ -161,9 +163,14 @@ def smokeTest():
161163
errMsg = "smoke test failed at importing module '%s' (%s):\n%s" % (path, os.path.join(root, filename), ex)
162164
logger.error(errMsg)
163165
else:
164-
# Run doc tests
165-
# Reference: http://docs.python.org/library/doctest.html
166+
logger.setLevel(logging.CRITICAL)
167+
kb.smokeMode = True
168+
166169
(failure_count, test_count) = doctest.testmod(module)
170+
171+
kb.smokeMode = False
172+
logger.setLevel(logging.INFO)
173+
167174
if failure_count > 0:
168175
retVal = False
169176

lib/techniques/union/use.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ def unionThread():
352352
key = re.sub(r"[^A-Za-z0-9]", "", item).lower()
353353
if key not in filtered or re.search(r"[^A-Za-z0-9]", item):
354354
filtered[key] = item
355-
items = filtered.values()
355+
items = list(filtered.values())
356356
items = [items]
357357
index = None
358358
for index in xrange(1 + len(threadData.shared.buffered)):

plugins/dbms/maxdb/enumeration.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
See the file 'LICENSE' for copying permission
66
"""
77

8+
from lib.core.common import isListLike
89
from lib.core.common import readInput
910
from lib.core.common import safeSQLIdentificatorNaming
1011
from lib.core.common import unsafeSQLIdentificatorNaming
@@ -48,7 +49,7 @@ def getDbs(self):
4849
retVal = pivotDumpTable("(%s) AS %s" % (query, kb.aliasName), ['%s.schemaname' % kb.aliasName], blind=True)
4950

5051
if retVal:
51-
kb.data.cachedDbs = retVal[0].values()[0]
52+
kb.data.cachedDbs = list(retVal[0].values())[0]
5253

5354
if kb.data.cachedDbs:
5455
kb.data.cachedDbs.sort()
@@ -83,7 +84,7 @@ def getTables(self, bruteForce=None):
8384
retVal = pivotDumpTable("(%s) AS %s" % (query, kb.aliasName), ['%s.tablename' % kb.aliasName], blind=True)
8485

8586
if retVal:
86-
for table in retVal[0].values()[0]:
87+
for table in list(retVal[0].values())[0]:
8788
if db not in kb.data.cachedTables:
8889
kb.data.cachedTables[db] = [table]
8990
else:
@@ -131,9 +132,9 @@ def getColumns(self, onlyColNames=False, colTuple=None, bruteForce=None, dumpMod
131132
self.getTables()
132133

133134
if len(kb.data.cachedTables) > 0:
134-
tblList = kb.data.cachedTables.values()
135+
tblList = list(kb.data.cachedTables.values())
135136

136-
if isinstance(tblList[0], (set, tuple, list)):
137+
if isListLike(tblList[0]):
137138
tblList = tblList[0]
138139
else:
139140
errMsg = "unable to retrieve the tables "

plugins/dbms/sybase/enumeration.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
from lib.core.common import filterPairValues
9+
from lib.core.common import isListLike
910
from lib.core.common import isTechniqueAvailable
1011
from lib.core.common import readInput
1112
from lib.core.common import safeSQLIdentificatorNaming
@@ -47,7 +48,7 @@ def getUsers(self):
4748
retVal = pivotDumpTable("(%s) AS %s" % (query, kb.aliasName), ['%s.name' % kb.aliasName], blind=blind, alias=kb.aliasName)
4849

4950
if retVal:
50-
kb.data.cachedUsers = retVal[0].values()[0]
51+
kb.data.cachedUsers = list(retVal[0].values())[0]
5152
break
5253

5354
return kb.data.cachedUsers
@@ -102,7 +103,7 @@ def getDbs(self):
102103
retVal = pivotDumpTable("(%s) AS %s" % (query, kb.aliasName), ['%s.name' % kb.aliasName], blind=blind, alias=kb.aliasName)
103104

104105
if retVal:
105-
kb.data.cachedDbs = retVal[0].values()[0]
106+
kb.data.cachedDbs = list(retVal[0].values())[0]
106107
break
107108

108109
if kb.data.cachedDbs:
@@ -146,7 +147,7 @@ def getTables(self, bruteForce=None):
146147
retVal = pivotDumpTable("(%s) AS %s" % (query, kb.aliasName), ['%s.name' % kb.aliasName], blind=blind, alias=kb.aliasName)
147148

148149
if retVal:
149-
for table in retVal[0].values()[0]:
150+
for table in list(retVal[0].values())[0]:
150151
if db not in kb.data.cachedTables:
151152
kb.data.cachedTables[db] = [table]
152153
else:
@@ -195,9 +196,9 @@ def getColumns(self, onlyColNames=False, colTuple=None, bruteForce=None, dumpMod
195196
self.getTables()
196197

197198
if len(kb.data.cachedTables) > 0:
198-
tblList = kb.data.cachedTables.values()
199+
tblList = list(kb.data.cachedTables.values())
199200

200-
if isinstance(tblList[0], (set, tuple, list)):
201+
if isListLike(tblList[0]):
201202
tblList = tblList[0]
202203
else:
203204
errMsg = "unable to retrieve the tables "

plugins/generic/databases.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,9 @@ def getColumns(self, onlyColNames=False, colTuple=None, bruteForce=None, dumpMod
478478
if conf.db in kb.data.cachedTables:
479479
tblList = kb.data.cachedTables[conf.db]
480480
else:
481-
tblList = kb.data.cachedTables.values()
481+
tblList = list(kb.data.cachedTables.values())
482482

483-
if isinstance(tblList[0], (set, tuple, list)):
483+
if isListLike(tblList[0]):
484484
tblList = tblList[0]
485485

486486
tblList = list(tblList)

plugins/generic/entries.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ def dumpTable(self, foundData=None):
9393
self.getTables()
9494

9595
if len(kb.data.cachedTables) > 0:
96-
tblList = kb.data.cachedTables.values()
96+
tblList = list(kb.data.cachedTables.values())
9797

98-
if isinstance(tblList[0], (set, tuple, list)):
98+
if isListLike(tblList[0]):
9999
tblList = tblList[0]
100100
elif not conf.search:
101101
errMsg = "unable to retrieve the tables "

0 commit comments

Comments
 (0)