sqlmap/lib/request/comparison.py

112 lines
4.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file doc/COPYING for copying permission.
"""
import re
2010-10-12 23:41:29 +04:00
from lib.core.common import getFilteredPageContent
2010-09-13 17:31:01 +04:00
from lib.core.common import preparePageForLineComparison
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
from lib.core.session import setMatchRatio
2010-09-16 13:32:09 +04:00
def comparison(page, headers=None, getSeqMatcher=False, pageLength=None):
regExpResults = None
# 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:
2010-05-14 18:21:13 +04:00
return conf.string in page
# Regular expression to match in page when the query is valid
if conf.regexp:
2010-05-14 18:21:13 +04:00
return re.search(conf.regexp, page, re.I | re.M) is not None
2010-09-13 17:31:01 +04:00
# Dynamic content lines to be excluded before calculating page hash
if kb.dynamicContent:
lines = preparePageForLineComparison(page)
for item in kb.dynamicContent:
if len(lines) == item.pageTotal:
before = item.lineNumber - 1 if isinstance(item.lineNumber, int) else item.lineNumber[0] - 1
after = item.lineNumber + 1 if isinstance(item.lineNumber, int) else item.lineNumber[-1] + 1
if (item.lineContentBefore and lines[before] != item.lineContentBefore) or (item.lineContentAfter and lines[after] != item.lineContentAfter):
continue
if isinstance(item.lineNumber, int):
page = page.replace(lines[item.lineNumber], '')
else:
for i in item.lineNumber:
page = page.replace(lines[i], '')
2010-03-10 17:14:27 +03:00
if conf.seqLock:
conf.seqLock.acquire()
2010-09-16 13:32:09 +04:00
if not conf.eRegexp and not conf.eString and kb.nullConnection:
ratio = 1. * pageLength / len(conf.seqMatcher.a)
if ratio > 1.:
ratio = 1. / ratio
else:
2010-10-12 23:41:29 +04:00
conf.seqMatcher.set_seq2(page if not conf.textOnly else getFilteredPageContent(page))
2010-09-16 13:32:09 +04:00
ratio = round(conf.seqMatcher.ratio(), 3)
2010-03-10 17:14:27 +03:00
if conf.seqLock:
conf.seqLock.release()
# If the url is stable and we did not set yet the match ratio and the
# current injected value changes the url page content
if conf.matchRatio is None:
2010-09-14 14:35:01 +04:00
if conf.thold:
conf.matchRatio = conf.thold
elif conf.md5hash is not None and ratio > 0.6 and ratio < 1:
logger.debug("setting match ratio to %.3f" % ratio)
conf.matchRatio = ratio
elif conf.md5hash is None or ( conf.md5hash is not None and ratio < 0.6 ):
logger.debug("setting match ratio to default value 0.900")
conf.matchRatio = 0.900
2010-09-14 14:35:01 +04:00
if conf.matchRatio is not None:
setMatchRatio()
# If it has been requested to return the ratio and not a comparison
# response
if getSeqMatcher:
return ratio
# If the url is stable it returns True if the page has the same MD5
# hash of the original one
# NOTE: old implementation, it did not handle automatically the fact
# that the url could be not stable (due to VIEWSTATE, counter, etc.)
#elif conf.md5hash is not None:
# return conf.md5hash == md5hash(page)
# If the url is not stable it returns sequence matcher between the
# first untouched HTTP response page content and this content
else:
2010-05-14 18:21:13 +04:00
return ratio > conf.matchRatio