2008-12-05 18:34:13 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
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
|
2008-12-05 18:34:13 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
2010-11-07 11:52:09 +03:00
|
|
|
from difflib import SequenceMatcher
|
|
|
|
|
2011-01-14 17:37:03 +03:00
|
|
|
from lib.core.common import getFilteredPageContent
|
2010-12-04 13:13:18 +03:00
|
|
|
from lib.core.common import removeDynamicContent
|
2010-11-16 13:42:42 +03:00
|
|
|
from lib.core.common import wasLastRequestDBMSError
|
2010-12-26 16:20:52 +03:00
|
|
|
from lib.core.common import wasLastRequestHTTPError
|
2008-12-05 18:34:13 +03:00
|
|
|
from lib.core.data import conf
|
2010-09-13 17:31:01 +04:00
|
|
|
from lib.core.data import kb
|
2009-02-09 13:28:03 +03:00
|
|
|
from lib.core.data import logger
|
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
|
2010-12-24 14:06:57 +03:00
|
|
|
from lib.core.settings import LOWER_RATIO_BOUND
|
|
|
|
from lib.core.settings import UPPER_RATIO_BOUND
|
2008-12-05 18:34:13 +03:00
|
|
|
|
2010-09-16 13:32:09 +04:00
|
|
|
def comparison(page, headers=None, getSeqMatcher=False, pageLength=None):
|
2010-11-04 00:51:36 +03:00
|
|
|
if page is None and pageLength is None:
|
|
|
|
return None
|
|
|
|
|
2008-12-05 18:34:13 +03:00
|
|
|
regExpResults = None
|
2010-12-04 13:05:18 +03:00
|
|
|
conf.seqMatcher.set_seq1(kb.pageTemplate)
|
2008-12-05 18:34:13 +03:00
|
|
|
|
2010-11-04 00:51:36 +03:00
|
|
|
if page:
|
|
|
|
# String to be excluded before calculating page hash
|
|
|
|
if conf.eString and conf.eString in page:
|
|
|
|
index = page.index(conf.eString)
|
|
|
|
length = len(conf.eString)
|
|
|
|
pageWithoutString = page[:index]
|
|
|
|
pageWithoutString += page[index+length:]
|
|
|
|
page = pageWithoutString
|
|
|
|
|
|
|
|
# Regular expression matches to be excluded before calculating page hash
|
|
|
|
if conf.eRegexp:
|
|
|
|
regExpResults = re.findall(conf.eRegexp, page, re.I | re.M)
|
|
|
|
|
|
|
|
if regExpResults:
|
|
|
|
for regExpResult in regExpResults:
|
|
|
|
index = page.index(regExpResult)
|
|
|
|
length = len(regExpResult)
|
|
|
|
pageWithoutRegExp = page[:index]
|
|
|
|
pageWithoutRegExp += page[index+length:]
|
|
|
|
page = pageWithoutRegExp
|
|
|
|
|
|
|
|
# String to match in page when the query is valid
|
|
|
|
if conf.string:
|
|
|
|
return conf.string in page
|
|
|
|
|
|
|
|
# Regular expression to match in page when the query is valid
|
|
|
|
if conf.regexp:
|
|
|
|
return re.search(conf.regexp, page, re.I | re.M) is not None
|
|
|
|
|
2010-11-13 01:44:15 +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()):
|
2010-11-13 01:44:15 +03:00
|
|
|
return None
|
|
|
|
|
2010-11-07 03:12:00 +03:00
|
|
|
# Dynamic content lines to be excluded before comparison
|
2011-01-14 17:37:03 +03:00
|
|
|
if not kb.nullConnection:
|
2010-12-04 13:13:18 +03:00
|
|
|
page = removeDynamicContent(page)
|
2010-12-04 16:23:28 +03:00
|
|
|
conf.seqMatcher.set_seq1(removeDynamicContent(kb.pageTemplate))
|
2010-11-04 00:51:36 +03:00
|
|
|
|
|
|
|
if not pageLength:
|
|
|
|
pageLength = len(page)
|
2010-11-03 15:40:11 +03:00
|
|
|
|
2010-11-02 12:06:38 +03:00
|
|
|
if kb.locks.seqLock:
|
|
|
|
kb.locks.seqLock.acquire()
|
2010-03-10 17:14:27 +03:00
|
|
|
|
2011-01-14 17:37:03 +03:00
|
|
|
if conf.textOnly:
|
|
|
|
(conf.seqMatcher.a, page) = map(getFilteredPageContent, (conf.seqMatcher.a, page))
|
2010-11-07 11:52:09 +03:00
|
|
|
|
2011-01-14 17:37:03 +03:00
|
|
|
if not conf.eRegexp and not conf.eString and kb.nullConnection and pageLength:
|
2010-09-16 13:32:09 +04:00
|
|
|
ratio = 1. * pageLength / len(conf.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:
|
2010-11-07 03:12:00 +03:00
|
|
|
conf.seqMatcher.set_seq2(page)
|
2010-12-08 02:57:43 +03:00
|
|
|
ratio = round(conf.seqMatcher.quick_ratio(), 3)
|
2009-02-09 13:28:03 +03:00
|
|
|
|
2010-11-02 12:06:38 +03:00
|
|
|
if kb.locks.seqLock:
|
|
|
|
kb.locks.seqLock.release()
|
2010-03-10 17:14:27 +03:00
|
|
|
|
2009-02-09 13:28:03 +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:
|
2010-09-14 14:35:01 +04:00
|
|
|
if conf.thold:
|
2010-12-18 12:51:34 +03:00
|
|
|
kb.matchRatio = conf.thold
|
2010-09-14 14:35:01 +04:00
|
|
|
|
2010-12-24 12:49:19 +03:00
|
|
|
elif 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)
|
2010-01-02 05:02:12 +03:00
|
|
|
|
2010-12-24 12:49:19 +03:00
|
|
|
elif not kb.pageStable:
|
2010-12-18 12:51:34 +03:00
|
|
|
kb.matchRatio = CONSTANT_RATIO
|
2010-11-10 00:39:58 +03:00
|
|
|
logger.debug("setting match ratio for current parameter to default value 0.900")
|
2009-04-22 15:48:07 +04:00
|
|
|
|
2009-02-09 13:28:03 +03:00
|
|
|
# If it has been requested to return the ratio and not a comparison
|
|
|
|
# response
|
2008-12-20 04:54:08 +03:00
|
|
|
if getSeqMatcher:
|
2009-02-09 13:28:03 +03:00
|
|
|
return ratio
|
|
|
|
|
2010-12-27 21:27:42 +03:00
|
|
|
elif ratio > UPPER_RATIO_BOUND:
|
2010-11-05 16:14:12 +03:00
|
|
|
return True
|
|
|
|
|
2010-12-18 12:51:34 +03:00
|
|
|
elif kb.matchRatio is None:
|
2010-11-10 02:38:29 +03:00
|
|
|
return None
|
|
|
|
|
2008-12-20 04:54:08 +03:00
|
|
|
else:
|
2010-12-18 12:51:34 +03:00
|
|
|
if kb.matchRatio == CONSTANT_RATIO or conf.thold:
|
|
|
|
return ratio > kb.matchRatio
|
2010-11-10 02:21:21 +03:00
|
|
|
else:
|
2010-12-18 12:51:34 +03:00
|
|
|
return (ratio - kb.matchRatio) > DIFF_TOLERANCE
|