From 01cf1394a40ef4a24121415a658781ff45c0544e Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Wed, 8 Dec 2010 14:26:40 +0000 Subject: [PATCH] code refactoring --- lib/core/common.py | 12 +++++++++++- lib/request/connect.py | 6 ++---- lib/takeover/xp_cmdshell.py | 12 +++++------- lib/techniques/blind/timebased.py | 26 -------------------------- 4 files changed, 18 insertions(+), 38 deletions(-) delete mode 100644 lib/techniques/blind/timebased.py diff --git a/lib/core/common.py b/lib/core/common.py index 92aaa2e40..37e16b5f9 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1532,7 +1532,17 @@ def wasLastRequestDBMSError(): Returns True if the last web request resulted in a (recognized) DBMS error page """ - return kb.lastErrorPage and kb.lastErrorPage[0]==kb.lastRequestUID + return kb.lastErrorPage and kb.lastErrorPage[0] == kb.lastRequestUID + +def wasLastRequestDelayed(): + """ + Returns True if the last web request resulted in a time-delay + """ + + # 99.9999999997440% of all non time-based sql injection + # affected durations should be inside +-7*stdev(durations) + # (Math reference: http://www.answers.com/topic/standard-deviation) + return (kb.lastQueryDuration >= average(kb.responseTimes) + 7 * stdev(kb.responseTimes)) def extractErrorMessage(page): """ diff --git a/lib/request/connect.py b/lib/request/connect.py index 21f1a2457..fac8401db 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -25,6 +25,7 @@ from lib.core.common import getUnicode from lib.core.common import logHTTPTraffic from lib.core.common import readInput from lib.core.common import stdev +from lib.core.common import wasLastRequestDelayed from lib.core.convert import urlencode from lib.core.common import urlEncodeCookieValues from lib.core.data import conf @@ -420,10 +421,7 @@ class Connect: conf.cj.clear() if timeBasedCompare: - # 99.9999999997440% of all non time-based sql injection - # affected durations should be inside +-7*stdev(durations) - # (Reference: http://www.answers.com/topic/standard-deviation) - return (kb.lastQueryDuration >= average(kb.responseTimes) + 7 * stdev(kb.responseTimes)) + return wasLastRequestDelayed() else: kb.responseTimes.append(kb.lastQueryDuration) diff --git a/lib/takeover/xp_cmdshell.py b/lib/takeover/xp_cmdshell.py index 8d716b750..f8c044492 100644 --- a/lib/takeover/xp_cmdshell.py +++ b/lib/takeover/xp_cmdshell.py @@ -9,13 +9,13 @@ See the file 'doc/COPYING' for copying permission from lib.core.common import randomStr from lib.core.common import readInput +from lib.core.common import wasLastRequestDelayed from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger from lib.core.exception import sqlmapUnsupportedFeatureException from lib.core.unescaper import unescaper from lib.request import inject -from lib.techniques.blind.timebased import timeUse class xp_cmdshell: """ @@ -88,13 +88,11 @@ class xp_cmdshell: inject.goStacked(cmd) def __xpCmdshellCheck(self): - query = self.xpCmdshellForgeCmd("ping -n %d 127.0.0.1" % (conf.timeSec * 2)) - duration = timeUse(query) + cmd = self.xpCmdshellForgeCmd("ping -n %d 127.0.0.1" % (conf.timeSec * 2)) - if duration >= conf.timeSec: - return True - else: - return False + inject.goStacked(cmd) + + return wasLastRequestDelayed() def xpCmdshellForgeCmd(self, cmd): self.__randStr = randomStr(lowercase=True) diff --git a/lib/techniques/blind/timebased.py b/lib/techniques/blind/timebased.py deleted file mode 100644 index 677098235..000000000 --- a/lib/techniques/blind/timebased.py +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env python - -""" -$Id$ - -Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/) -See the file 'doc/COPYING' for copying permission -""" - -import time - -from lib.core.agent import agent -from lib.core.common import calculateDeltaSeconds -from lib.core.common import getDelayQuery -from lib.core.data import conf -from lib.core.data import kb -from lib.core.data import logger -from lib.request import inject -from lib.request.connect import Connect as Request - -def timeUse(query): - start = time.time() - _, _ = inject.goStacked(query) - duration = calculateDeltaSeconds(start) - - return duration