5757from lib .core .exception import sqlmapNoneDataException
5858from lib .core .exception import sqlmapMissingDependence
5959from lib .core .exception import sqlmapSyntaxException
60+ from lib .core .optiondict import optDict
6061from lib .core .settings import DESCRIPTION
6162from lib .core .settings import IS_WIN
6263from lib .core .settings import PLATFORM
@@ -417,7 +418,7 @@ def fileToStr(fileName):
417418 @rtype: C{str}
418419 """
419420
420- filePointer = codecs .open (fileName , "r " , conf .dataEncoding )
421+ filePointer = codecs .open (fileName , "rb " , conf .dataEncoding )
421422 fileText = filePointer .read ()
422423
423424 return fileText .replace (" " , "" ).replace ("\t " , "" ).replace ("\r " , "" ).replace ("\n " , " " )
@@ -1106,7 +1107,8 @@ def profile(profileOutputFile=None, dotOutputFile=None, imageOutputFile=None):
11061107 import gtk
11071108 import pydot
11081109 except ImportError , e :
1109- logger .error (e )
1110+ errMsg = "profiling requires third-party libraries (%s)" % str (e )
1111+ logger .error (errMsg )
11101112 return
11111113
11121114 if profileOutputFile is None :
@@ -1209,6 +1211,9 @@ def initCommonOutputs():
12091211 for line in cfile .xreadlines ():
12101212 line = line .strip ()
12111213
1214+ if line .startswith ('#' ):
1215+ continue
1216+
12121217 if len (line ) > 1 :
12131218 if line [0 ] == '[' and line [- 1 ] == ']' :
12141219 key = line [1 :- 1 ]
@@ -1220,20 +1225,27 @@ def initCommonOutputs():
12201225
12211226 cfile .close ()
12221227
1223- def getGoodSamaritanParameters (part , prevValue , originalCharset ):
1228+ def goGoodSamaritan (part , prevValue , originalCharset ):
12241229 """
1225- Function for retrieving parameters needed for good samaritan (common outputs) feature.
1226- Returns singleValue if there is a complete single match (in part of common-outputs.txt set by parameter 'part')
1227- regarding parameter prevValue. If there is no single value match, but multiple, predictedCharset is returned
1228- containing more probable characters (retrieved from matched items in common-outputs.txt) together with the
1229- rest of charset as otherCharset
1230+ Function for retrieving parameters needed for common prediction (good
1231+ samaritan) feature.
1232+
1233+ part is for instance Users, Databases, Tables and corresponds to the
1234+ header (e.g. [Users]) in txt/common-outputs.txt.
1235+
1236+ prevValue: retrieved query output so far (e.g. 'i').
1237+
1238+ Returns singleValue if there is a complete single match (in part of
1239+ txt/common-outputs.txt under 'part') regarding parameter prevValue. If
1240+ there is no single value match, but multiple, commonCharset is
1241+ returned containing more probable characters (retrieved from matched
1242+ values in txt/common-outputs.txt) together with the rest of charset as
1243+ otherCharset.
12301244 """
1245+
12311246 if kb .commonOutputs is None :
12321247 initCommonOutputs ()
12331248
1234- if not part or not prevValue : #is not None and != ""
1235- return None , None , originalCharset
1236-
12371249 predictionSet = set ()
12381250 wildIndexes = []
12391251 singleValue = None
@@ -1249,38 +1261,47 @@ def getGoodSamaritanParameters(part, prevValue, originalCharset):
12491261 charIndex += 1
12501262 findIndex = prevValue .find ('.' , charIndex )
12511263
1264+ # If the header we are looking for has common outputs defined
12521265 if part in kb .commonOutputs :
12531266 for item in kb .commonOutputs [part ]:
1267+ # Check if the common output (item) starts with prevValue
12541268 if re .search ('\A%s' % prevValue , item ):
12551269 singleValue = item
1270+
12561271 for index in wildIndexes :
12571272 char = item [index ]
12581273
12591274 if char not in predictionSet :
12601275 predictionSet .add (char )
12611276
1262- predictedCharset = []
1277+ commonCharset = []
12631278 otherCharset = []
12641279
1280+ # Split the original charset into common chars (commonCharset)
1281+ # and other chars (otherCharset)
12651282 for ordChar in originalCharset :
12661283 if chr (ordChar ) not in predictionSet :
12671284 otherCharset .append (ordChar )
12681285 else :
1269- predictedCharset .append (ordChar )
1286+ commonCharset .append (ordChar )
12701287
1271- predictedCharset .sort ()
1288+ commonCharset .sort ()
12721289
1273- if len (predictedCharset ) > 1 :
1274- return None , predictedCharset , otherCharset
1290+ if len (commonCharset ) > 1 :
1291+ return None , commonCharset , otherCharset
12751292 else :
12761293 return singleValue , None , originalCharset
12771294 else :
12781295 return None , None , originalCharset
12791296
12801297def getCompiledRegex (regex , * args ):
12811298 """
1282- Returns compiled regular expression and stores it in cache for further usage
1299+ Returns compiled regular expression and stores it in cache for further
1300+ usage
12831301 """
1302+
1303+ global __compiledRegularExpressions
1304+
12841305 if (regex , args ) in __compiledRegularExpressions :
12851306 return __compiledRegularExpressions [(regex , args )]
12861307 else :
@@ -1290,15 +1311,23 @@ def getCompiledRegex(regex, *args):
12901311
12911312def getPartRun ():
12921313 """
1293- Goes through call stack and finds constructs matching conf.dmbsHandler.*. Returns it or it's alias used in common-outputs.txt
1314+ Goes through call stack and finds constructs matching conf.dmbsHandler.*.
1315+ Returns it or its alias used in txt/common-outputs.txt
12941316 """
1295- commonPartsDict = { "getTables" : "Tables" , "getColumns" : "Columns" , "getUsers" : "Users" , "getBanner" : "Banners" , "getDbs" : "Databases" }
1317+
12961318 retVal = None
1319+ commonPartsDict = optDict ["Enumeration" ]
12971320 stack = [item [4 ][0 ] if isinstance (item [4 ], list ) else '' for item in inspect .stack ()]
12981321 reobj = getCompiledRegex ('conf\.dbmsHandler\.([^(]+)\(\)' )
1322+
1323+ # Goes backwards through the stack to find the conf.dbmsHandler method
1324+ # calling this function
12991325 for i in xrange (len (stack ) - 1 , 0 , - 1 ):
13001326 match = reobj .search (stack [i ])
1327+
13011328 if match :
1329+ # This is the calling conf.dbmsHandler method (e.g. 'getDbms')
13021330 retVal = match .groups ()[0 ]
13031331 break
1304- return commonPartsDict [retVal ] if retVal in commonPartsDict else retVal
1332+
1333+ return commonPartsDict [retVal ][1 ] if retVal in commonPartsDict else retVal
0 commit comments