sqlmap/lib/request/comparison.py

105 lines
3.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
"""
import re
from difflib import SequenceMatcher
from lib.core.common import getFilteredPageContent
2010-12-04 13:13:18 +03:00
from lib.core.common import removeDynamicContent
from lib.core.common import wasLastRequestDBMSError
2010-12-26 16:20:52 +03:00
from lib.core.common import wasLastRequestHTTPError
from lib.core.data import conf
2010-09-13 17:31:01 +04:00
from lib.core.data import kb
from lib.core.data import logger
2011-02-22 16:18:47 +03:00
from lib.core.exception import sqlmapNoneDataException
2010-11-10 02:35:37 +03:00
from lib.core.settings import CONSTANT_RATIO
2010-11-10 01:49:31 +03:00
from lib.core.settings import DIFF_TOLERANCE
2011-02-04 02:25:56 +03:00
from lib.core.settings import MIN_RATIO
from lib.core.settings import MAX_RATIO
2010-12-24 14:06:57 +03:00
from lib.core.settings import LOWER_RATIO_BOUND
from lib.core.settings import UPPER_RATIO_BOUND
from lib.core.threads import getCurrentThreadData
2011-02-04 02:25:56 +03:00
def comparison(page, getRatioValue=False, pageLength=None):
2010-11-04 00:51:36 +03:00
if page is None and pageLength is None:
return None
regExpResults = None
seqMatcher = getCurrentThreadData().seqMatcher
seqMatcher.set_seq1(kb.pageTemplate)
2010-11-04 00:51:36 +03:00
if page:
# String to match in page when the query is valid
if conf.string:
2011-02-04 02:25:56 +03:00
condition = conf.string in page
return condition if not getRatioValue else (MAX_RATIO if condition else MIN_RATIO)
2010-11-04 00:51:36 +03:00
# Regular expression to match in page when the query is valid
if conf.regexp:
2011-02-04 02:25:56 +03:00
condition = re.search(conf.regexp, page, re.I | re.M) is not None
return condition if not getRatioValue else (MAX_RATIO if condition else MIN_RATIO)
2010-11-04 00:51:36 +03:00
# In case of an DBMS error page return None
2010-12-26 16:20:52 +03:00
if kb.errorIsNone and (wasLastRequestDBMSError() or wasLastRequestHTTPError()):
return None
2010-11-07 03:12:00 +03:00
# Dynamic content lines to be excluded before comparison
if not kb.nullConnection:
2010-12-04 13:13:18 +03:00
page = removeDynamicContent(page)
seqMatcher.set_seq1(removeDynamicContent(kb.pageTemplate))
2010-11-04 00:51:36 +03:00
if not pageLength:
pageLength = len(page)
if kb.nullConnection and pageLength:
2011-02-22 16:18:47 +03:00
if not seqMatcher.a:
errMsg = "problem occured while retrieving original page content "
errMsg += "which prevents sqlmap from continuation. please rerun, "
errMsg += "and if problem persists please turn off optimization switches"
raise sqlmapNoneDataException, errMsg
ratio = 1. * pageLength / len(seqMatcher.a)
2010-11-07 19:23:03 +03:00
2010-09-16 13:32:09 +04:00
if ratio > 1.:
ratio = 1. / ratio
else:
seqMatcher.set_seq1(getFilteredPageContent(seqMatcher.a, True) if conf.textOnly else seqMatcher.a)
seqMatcher.set_seq2(getFilteredPageContent(page, True) if conf.textOnly else page)
ratio = round(seqMatcher.quick_ratio(), 3)
2010-03-10 17:14:27 +03:00
# If the url is stable and we did not set yet the match ratio and the
# current injected value changes the url page content
2010-12-18 12:51:34 +03:00
if kb.matchRatio is None:
if kb.pageStable and ratio >= LOWER_RATIO_BOUND and ratio <= UPPER_RATIO_BOUND:
2010-12-18 12:51:34 +03:00
kb.matchRatio = ratio
logger.debug("setting match ratio for current parameter to %.3f" % kb.matchRatio)
elif not kb.pageStable:
2010-12-18 12:51:34 +03:00
kb.matchRatio = CONSTANT_RATIO
logger.debug("setting match ratio for current parameter to default value 0.900")
# If it has been requested to return the ratio and not a comparison
# response
2011-02-04 02:25:56 +03:00
if getRatioValue:
return ratio
2010-12-27 21:27:42 +03:00
elif ratio > UPPER_RATIO_BOUND:
return True
2010-12-18 12:51:34 +03:00
elif kb.matchRatio is None:
return None
else:
if kb.matchRatio == CONSTANT_RATIO:
2010-12-18 12:51:34 +03:00
return ratio > kb.matchRatio
else:
2010-12-18 12:51:34 +03:00
return (ratio - kb.matchRatio) > DIFF_TOLERANCE