From 5de1825d0c94dc5dc2093acadf74fd1474248c1e Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 15 Sep 2015 10:48:23 +0200 Subject: [PATCH 1/4] Fixes #1412 --- lib/request/connect.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/request/connect.py b/lib/request/connect.py index b1ecf8f23..19ae69301 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -630,7 +630,7 @@ class Connect(object): raise SqlmapConnectionException(warnMsg) finally: - if not isinstance(page, unicode): + if isinstance(page, basestring) and not isinstance(page, unicode): if HTTP_HEADER.CONTENT_TYPE in (responseHeaders or {}) and not re.search(TEXT_CONTENT_TYPE_REGEX, responseHeaders[HTTP_HEADER.CONTENT_TYPE]): page = unicode(page, errors="ignore") else: From ee3857444906354f4c596b2bd38a3ddf833b9bea Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 15 Sep 2015 13:26:25 +0200 Subject: [PATCH 2/4] Fixes #1411 --- lib/core/common.py | 4 ++-- lib/core/replication.py | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index fc91fff44..78a64892b 100755 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -3979,7 +3979,7 @@ def pollProcess(process, suppress_errors=False): break -def getSafeExString(ex): +def getSafeExString(ex, encoding=None): """ Safe way how to get the proper exception represtation as a string (Note: errors to be avoided: 1) "%s" % Exception(u'\u0161') and 2) "%s" % str(Exception(u'\u0161')) @@ -3992,4 +3992,4 @@ def getSafeExString(ex): elif getattr(ex, "msg", None): retVal = ex.msg - return getUnicode(retVal) + return getUnicode(retVal, encoding=encoding) diff --git a/lib/core/replication.py b/lib/core/replication.py index c5bbd24cc..476604598 100644 --- a/lib/core/replication.py +++ b/lib/core/replication.py @@ -8,9 +8,11 @@ See the file 'doc/COPYING' for copying permission import sqlite3 from extra.safe2bin.safe2bin import safechardecode +from lib.core.common import getSafeExString from lib.core.common import unsafeSQLIdentificatorNaming from lib.core.exception import SqlmapGenericException from lib.core.exception import SqlmapValueException +from lib.core.settings import UNICODE_ENCODING class Replication(object): """ @@ -49,11 +51,16 @@ class Replication(object): self.name = unsafeSQLIdentificatorNaming(name) self.columns = columns if create: - self.execute('DROP TABLE IF EXISTS "%s"' % self.name) - if not typeless: - self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s" %s' % (unsafeSQLIdentificatorNaming(colname), coltype) for colname, coltype in self.columns))) - else: - self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s"' % unsafeSQLIdentificatorNaming(colname) for colname in self.columns))) + try: + self.execute('DROP TABLE IF EXISTS "%s"' % self.name) + if not typeless: + self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s" %s' % (unsafeSQLIdentificatorNaming(colname), coltype) for colname, coltype in self.columns))) + else: + self.execute('CREATE TABLE "%s" (%s)' % (self.name, ','.join('"%s"' % unsafeSQLIdentificatorNaming(colname) for colname in self.columns))) + except Exception, ex: + errMsg = "problem occurred ('%s') while initializing the sqlite database " % getSafeExString(ex, UNICODE_ENCODING) + errMsg += "located at '%s'" % self.parent.dbpath + raise SqlmapGenericException(errMsg) def insert(self, values): """ @@ -70,7 +77,7 @@ class Replication(object): try: self.parent.cursor.execute(sql, parameters) except sqlite3.OperationalError, ex: - errMsg = "problem occurred ('%s') while accessing sqlite database " % unicode(ex) + errMsg = "problem occurred ('%s') while accessing sqlite database " % getSafeExString(ex, UNICODE_ENCODING) errMsg += "located at '%s'. Please make sure that " % self.parent.dbpath errMsg += "it's not used by some other program" raise SqlmapGenericException(errMsg) From 058870635b38b5dc762fb99e530d70b397a39b97 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 15 Sep 2015 14:37:30 +0200 Subject: [PATCH 3/4] Update for an #1414 --- lib/utils/api.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/utils/api.py b/lib/utils/api.py index 45eb46be8..21068bb96 100644 --- a/lib/utils/api.py +++ b/lib/utils/api.py @@ -116,7 +116,8 @@ class Database(object): class Task(object): - def __init__(self, taskid): + def __init__(self, taskid, remote_addr): + self.remote_addr = remote_addr self.process = None self.output_directory = None self.options = None @@ -343,7 +344,9 @@ def task_new(): Create new task ID """ taskid = hexencode(os.urandom(8)) - DataStore.tasks[taskid] = Task(taskid) + remote_addr = request.remote_addr + + DataStore.tasks[taskid] = Task(taskid, remote_addr) logger.debug("Created new task: '%s'" % taskid) return jsonize({"success": True, "taskid": taskid}) @@ -374,13 +377,15 @@ def task_list(taskid): List task pull """ if is_admin(taskid): - logger.debug("[%s] Listed task pool" % taskid) tasks = list(DataStore.tasks) - return jsonize({"success": True, "tasks": tasks, "tasks_num": len(tasks)}) else: - logger.warning("[%s] Unauthorized call to task_list()" % taskid) - return jsonize({"success": False, "message": "Unauthorized"}) + tasks = [] + for key in DataStore.tasks: + if DataStore.tasks[key].remote_addr == request.remote_addr: + tasks.append(key) + logger.debug("[%s] Listed task pool (%s)" % (taskid, "admin" if is_admin(taskid) else request.remote_addr)) + return jsonize({"success": True, "tasks": tasks, "tasks_num": len(tasks)}) @get("/admin//flush") def task_flush(taskid): @@ -389,11 +394,13 @@ def task_flush(taskid): """ if is_admin(taskid): DataStore.tasks = dict() - logger.debug("[%s] Flushed task pool" % taskid) - return jsonize({"success": True}) else: - logger.warning("[%s] Unauthorized call to task_flush()" % taskid) - return jsonize({"success": False, "message": "Unauthorized"}) + for key in list(DataStore.tasks): + if DataStore.tasks[key].remote_addr == request.remote_addr: + del DataStore.tasks[key] + + logger.debug("[%s] Flushed task pool (%s)" % (taskid, "admin" if is_admin(taskid) else request.remote_addr)) + return jsonize({"success": True}) ################################## # sqlmap core interact functions # From c59ead36cee6f792f3fe6b5d2bcc4729eba3dffb Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 15 Sep 2015 17:23:59 +0200 Subject: [PATCH 4/4] Patch for Python 2.6 (SyntaxError) --- lib/utils/api.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/utils/api.py b/lib/utils/api.py index 21068bb96..c73adaec8 100644 --- a/lib/utils/api.py +++ b/lib/utils/api.py @@ -721,7 +721,9 @@ def client(host=RESTAPI_SERVER_HOST, port=RESTAPI_SERVER_PORT): taskid = None continue - cmdLineOptions = { k: v for k, v in cmdLineOptions.iteritems() if v is not None } + for key in list(cmdLineOptions): + if cmdLineOptions[key] is None: + del cmdLineOptions[key] raw = _client(addr + "/task/new") res = dejsonize(raw)