2010-12-24 15:13:48 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2011-01-31 14:41:28 +03:00
|
|
|
$Id$
|
2010-12-24 15:13:48 +03:00
|
|
|
|
2011-04-15 16:33:18 +04:00
|
|
|
Copyright (c) 2006-2011 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-12-24 15:13:48 +03:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2011-01-16 13:52:42 +03:00
|
|
|
import difflib
|
2010-12-24 15:13:48 +03:00
|
|
|
import threading
|
|
|
|
|
|
|
|
from lib.core.data import kb
|
2011-05-30 03:17:50 +04:00
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.datatype import advancedDict
|
|
|
|
from lib.core.exception import sqlmapThreadException
|
|
|
|
|
|
|
|
shared = advancedDict()
|
2010-12-24 15:13:48 +03:00
|
|
|
|
|
|
|
class ThreadData():
|
|
|
|
"""
|
|
|
|
Represents thread independent data
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2011-05-30 03:17:50 +04:00
|
|
|
global shared
|
|
|
|
|
2011-04-30 17:20:05 +04:00
|
|
|
self.disableStdOut = False
|
|
|
|
self.lastErrorPage = None
|
|
|
|
self.lastHTTPError = None
|
|
|
|
self.lastRedirectMsg = None
|
|
|
|
self.lastQueryDuration = 0
|
|
|
|
self.lastRequestUID = 0
|
2011-05-11 16:54:33 +04:00
|
|
|
self.retriesCount = 0
|
2011-04-30 17:20:05 +04:00
|
|
|
self.seqMatcher = difflib.SequenceMatcher(None)
|
2011-05-30 03:17:50 +04:00
|
|
|
self.shared = shared
|
2011-04-30 17:20:05 +04:00
|
|
|
self.valueStack = []
|
2010-12-24 15:13:48 +03:00
|
|
|
|
|
|
|
def getCurrentThreadUID():
|
|
|
|
return hash(threading.currentThread())
|
|
|
|
|
|
|
|
def getCurrentThreadData():
|
|
|
|
"""
|
|
|
|
Returns current thread's dependent data
|
|
|
|
"""
|
|
|
|
|
|
|
|
threadUID = getCurrentThreadUID()
|
|
|
|
if threadUID not in kb.threadData:
|
|
|
|
kb.threadData[threadUID] = ThreadData()
|
|
|
|
return kb.threadData[threadUID]
|
2011-05-30 03:17:50 +04:00
|
|
|
|
2011-05-30 03:56:41 +04:00
|
|
|
def runThreads(numThreads, threadFunction, cleanupFunction=None, forwardException=True):
|
2011-05-30 03:17:50 +04:00
|
|
|
threads = []
|
|
|
|
|
|
|
|
kb.threadContinue = True
|
|
|
|
kb.threadException = False
|
|
|
|
|
|
|
|
if numThreads > 1:
|
|
|
|
infoMsg = "starting %d threads" % numThreads
|
|
|
|
logger.info(infoMsg)
|
|
|
|
else:
|
|
|
|
threadFunction()
|
|
|
|
return
|
|
|
|
|
|
|
|
# Start the threads
|
|
|
|
for numThread in range(numThreads):
|
|
|
|
thread = threading.Thread(target=threadFunction, name=str(numThread))
|
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
|
|
|
|
|
|
|
# And wait for them to all finish
|
|
|
|
try:
|
|
|
|
alive = True
|
|
|
|
|
|
|
|
while alive:
|
|
|
|
alive = False
|
|
|
|
|
|
|
|
for thread in threads:
|
|
|
|
if thread.isAlive():
|
|
|
|
alive = True
|
|
|
|
thread.join(1)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
kb.threadContinue = False
|
|
|
|
kb.threadException = True
|
|
|
|
|
|
|
|
print
|
|
|
|
logger.debug("waiting for threads to finish")
|
|
|
|
|
|
|
|
try:
|
|
|
|
while (threading.activeCount() > 1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise sqlmapThreadException, "user aborted (Ctrl+C was pressed multiple times)"
|
|
|
|
|
2011-05-30 03:56:41 +04:00
|
|
|
if forwardException:
|
|
|
|
raise
|
|
|
|
|
2011-05-30 03:17:50 +04:00
|
|
|
finally:
|
|
|
|
kb.threadContinue = True
|
|
|
|
kb.threadException = False
|
|
|
|
|
|
|
|
if cleanupFunction:
|
|
|
|
cleanupFunction()
|