From f774d8fea0834ab1a5125d56db2410b6ec9e483f Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 24 May 2011 11:06:58 +0000 Subject: [PATCH] proper Tor settings (reverted r3915 and implemented it the right way) --- lib/core/enums.py | 1 - lib/core/option.py | 47 +++++++++++++++++++++++++++++++++++------- lib/core/settings.py | 6 ++++++ lib/request/connect.py | 22 +++++--------------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/lib/core/enums.py b/lib/core/enums.py index 440b1ca0d..becabddc3 100644 --- a/lib/core/enums.py +++ b/lib/core/enums.py @@ -99,7 +99,6 @@ class WARNFLAGS: RANDOM_AGENT = 'randomAgent' DATA_TO_STDOUT = 'dataToStdout' THREADS = 'threads' - TOR = 'tor' class EXPECTED: BOOL = "bool" diff --git a/lib/core/option.py b/lib/core/option.py index 3d15e7932..1e3b8a313 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -62,6 +62,7 @@ from lib.core.enums import HTTPMETHOD from lib.core.enums import MOBILES from lib.core.enums import PAYLOAD from lib.core.enums import PRIORITY +from lib.core.exception import sqlmapConnectionException from lib.core.exception import sqlmapFilePathException from lib.core.exception import sqlmapGenericException from lib.core.exception import sqlmapMissingDependence @@ -74,6 +75,7 @@ from lib.core.exception import sqlmapUserQuitException from lib.core.optiondict import optDict from lib.core.settings import CODECS_LIST_PAGE from lib.core.settings import DEFAULT_PAGE_ENCODING +from lib.core.settings import DEFAULT_TOR_PORTS from lib.core.settings import IS_WIN from lib.core.settings import PLATFORM from lib.core.settings import PYVERSION @@ -92,6 +94,7 @@ from lib.core.settings import FIREBIRD_ALIASES from lib.core.settings import MAXDB_ALIASES from lib.core.settings import SYBASE_ALIASES from lib.core.settings import BURP_SPLITTER +from lib.core.settings import LOCALHOST from lib.core.settings import MAX_NUMBER_OF_THREADS from lib.core.settings import TIME_DEFAULT_DELAY from lib.core.settings import TIME_DELAY_CANDIDATES @@ -1241,13 +1244,6 @@ def __cleanupOptions(): conf.nullConnection = not conf.textOnly conf.threads = 3 if conf.threads < 3 else conf.threads - if conf.tor: - infoMsg = "setting Tor socks settings" - logger.info(infoMsg) - - socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, 'localhost', 9050) - socks.wrapmodule(urllib2) - if conf.data: conf.data = urldecode(conf.data) @@ -1575,6 +1571,42 @@ def __setTrafficOutputFP(): if conf.trafficFile: conf.trafficFP = openFile(conf.trafficFile, "w+") +def __setTorProxySettings(): + if not conf.tor: + return + + infoMsg = "setting Tor proxy settings" + logger.info(infoMsg) + + found = None + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + + for port in DEFAULT_TOR_PORTS: + try: + s.connect((LOCALHOST, port)) + found = port + break + except socket.error: + pass + + s.close() + + if found: + conf.proxy = "http://%s:%d" % (LOCALHOST, found) + else: + errMsg = "can't establish connection with the Tor proxy. " + errMsg += "please make sure that you have " + errMsg += "some kind of Vidalia/Privoxy/Polipo " + errMsg += "Tor proxy bundle installed for " + errMsg += "you to be able to successfully use " + errMsg += "--tor switch " + if IS_WIN: + errMsg += "(e.g. https://www.torproject.org/projects/vidalia.html.en)" + else: + errMsg += "(e.g. http://www.coresec.org/2011/04/24/sqlmap-with-tor/)" + + raise sqlmapConnectionException, errMsg + def __basicOptionValidation(): if conf.limitStart is not None and not (isinstance(conf.limitStart, int) and conf.limitStart > 0): errMsg = "value for --start (limitStart) option must be an integer value greater than zero (>0)" @@ -1670,6 +1702,7 @@ def init(inputOptions=advancedDict(), overrideOptions=False): __setRequestFromFile() __cleanupOptions() __basicOptionValidation() + __setTorProxySettings() __setMultipleTargets() __setTamperingFunctions() __setTrafficOutputFP() diff --git a/lib/core/settings.py b/lib/core/settings.py index 69402d759..d97c90178 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -333,3 +333,9 @@ CODECS_LIST_PAGE = 'http://docs.python.org/library/codecs.html#standard-encoding # Simple regular expression used to distinguish scalar from multiple-row commands (not sole condition) SQL_SCALAR_REGEX = r"\A(SELECT(?!\s+DISTINCT\(?))?\s*\w*\(" + +# IP address of the localhost +LOCALHOST = "127.0.0.1" + +# Default ports used in Tor proxy bundles +DEFAULT_TOR_PORTS = (8118, 8123) diff --git a/lib/request/connect.py b/lib/request/connect.py index 8724cb2fd..d34a006b0 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -17,7 +17,6 @@ import urlparse import traceback from extra.multipart import multipartpost -from extra.socks.socks import GeneralProxyError from lib.core.agent import agent from lib.core.common import average from lib.core.common import calculateDeltaSeconds @@ -362,7 +361,7 @@ class Connect: page = processResponse(page, responseHeaders) return page, responseHeaders - except (urllib2.URLError, socket.error, socket.timeout, httplib.BadStatusLine, httplib.IncompleteRead, GeneralProxyError), e: + except (urllib2.URLError, socket.error, socket.timeout, httplib.BadStatusLine, httplib.IncompleteRead), e: tbMsg = traceback.format_exc() if "no host given" in tbMsg: @@ -399,21 +398,10 @@ class Connect: logger.critical(warnMsg) if kb.originalPage is None: - if conf.tor: - warnMsg = "please make sure that you have " - warnMsg += "Tor installed and running for " - warnMsg += "you to be able to successfully use " - warnMsg += "--tor switch " - if IS_WIN: - warnMsg += "(e.g. https://www.torproject.org/download/download.html.en)" - else: - warnMsg += "(e.g. https://help.ubuntu.com/community/Tor)" - singleTimeLogMessage(warnMsg, logging.WARN, WARNFLAGS.TOR) - else: - warnMsg = "if the problem persists please try to rerun " - warnMsg += "with the --random-agent switch turned on " - warnMsg += "and/or try to use proxy switches (--ignore-proxy, --proxy,...)" - singleTimeLogMessage(warnMsg, logging.WARN, WARNFLAGS.RANDOM_AGENT) + warnMsg = "if the problem persists please try to rerun " + warnMsg += "with the --random-agent switch turned on " + warnMsg += "and/or try to use proxy switches (--ignore-proxy, --proxy,...)" + singleTimeLogMessage(warnMsg, logging.WARN, WARNFLAGS.RANDOM_AGENT) elif conf.threads > 1: warnMsg = "if the problem persists please try to lower " warnMsg += "the number of used threads (--threads)"