Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37d78ff

Browse files
committed
minor optimization
1 parent 6c49af0 commit 37d78ff

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

_sqlmap.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from lib.core.data import kb
3636
from lib.core.data import logger
3737
from lib.core.data import paths
38+
from lib.core.dump import dumper
3839
from lib.core.common import unhandledExceptionMessage
3940
from lib.core.exception import exceptionsTuple
4041
from lib.core.exception import sqlmapSilentQuitException
@@ -44,7 +45,6 @@
4445
from lib.core.settings import LEGAL_DISCLAIMER
4546
from lib.core.testing import smokeTest
4647
from lib.core.testing import liveTest
47-
from lib.core.xmldump import closeDumper
4848
from lib.parse.cmdline import cmdLineParser
4949

5050
def modulePath():
@@ -85,27 +85,23 @@ def main():
8585
except sqlmapUserQuitException:
8686
errMsg = "user quit"
8787
logger.error(errMsg)
88-
closeDumper(False, errMsg)
8988

9089
except sqlmapSilentQuitException:
91-
closeDumper(False)
90+
pass
9291

9392
except exceptionsTuple, e:
9493
e = getUnicode(e)
9594
logger.critical(e)
96-
closeDumper(False, e)
9795

9896
except KeyboardInterrupt:
9997
print
10098
errMsg = "user aborted"
10199
logger.error(errMsg)
102-
closeDumper(False, errMsg)
103100

104101
except EOFError:
105102
print
106103
errMsg = "exit"
107104
logger.error(errMsg)
108-
closeDumper(False, errMsg)
109105

110106
except SystemExit:
111107
pass
@@ -115,23 +111,21 @@ def main():
115111
errMsg = unhandledExceptionMessage()
116112
logger.critical(errMsg)
117113
traceback.print_exc()
118-
closeDumper(False, errMsg)
119-
120-
else:
121-
closeDumper(True)
122114

123115
finally:
124116
dataToStdout("\n[*] shutting down at %s\n\n" % time.strftime("%X"), forceOutput=True)
125117

126118
kb.threadContinue = False
127119
kb.threadException = True
128120

129-
if conf.get('hashDB', None):
121+
if conf.get("hashDB", None):
130122
try:
131123
conf.hashDB.flush(True)
132124
except KeyboardInterrupt:
133125
pass
134126

127+
dumper.flush()
128+
135129
# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
136130
if hasattr(conf, "threads") and conf.threads > 1:
137131
os._exit(0)

lib/core/dump.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
"""
99

1010
import codecs
11+
import cStringIO
1112
import re
1213
import os
14+
import threading
1315

1416
from lib.core.common import Backend
1517
from lib.core.common import dataToDumpFile
@@ -26,6 +28,7 @@
2628
from lib.core.enums import DBMS
2729
from lib.core.exception import sqlmapValueException
2830
from lib.core.replication import Replication
31+
from lib.core.settings import BUFFERED_LOG_SIZE
2932
from lib.core.settings import TRIM_STDOUT_DUMP_SIZE
3033
from lib.core.settings import UNICODE_ENCODING
3134

@@ -39,23 +42,40 @@ class Dump:
3942
def __init__(self):
4043
self.__outputFile = None
4144
self.__outputFP = None
45+
self.__outputBP = None
46+
self.__lock = threading.Lock()
4247

4348
def __write(self, data, n=True, console=True):
4449
text = "%s%s" % (data, "\n" if n else " ")
4550
if console:
4651
dataToStdout(text)
4752

48-
self.__outputFP.write(text)
49-
self.__outputFP.flush()
53+
if kb.get("multiThreadMode"):
54+
self.__lock.acquire()
55+
56+
self.__outputBP.write(text)
57+
58+
if self.__outputBP.tell() > BUFFERED_LOG_SIZE:
59+
self.flush()
60+
61+
if kb.get("multiThreadMode"):
62+
self.__lock.release()
5063

5164
kb.dataOutputFlag = True
5265

66+
def flush(self):
67+
if self.__outputBP and self.__outputFP and self.__outputBP.tell() > 0:
68+
_ = self.__outputBP.getvalue()
69+
self.__outputBP.reset()
70+
self.__outputFP.write(_)
71+
5372
def __formatString(self, inpStr):
5473
return restoreDumpMarkedChars(getUnicode(inpStr))
5574

5675
def setOutputFile(self):
5776
self.__outputFile = "%s%slog" % (conf.outputPath, os.sep)
5877
self.__outputFP = codecs.open(self.__outputFile, "ab", UNICODE_ENCODING)
78+
self.__outputBP = cStringIO.StringIO()
5979

6080
def getOutputFile(self):
6181
return self.__outputFile

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,6 @@
432432

433433
# Give up on hash recognition if nothing was found in first given number of rows
434434
HASH_RECOGNITION_QUIT_THRESHOLD = 10000
435+
436+
# Size of a buffer used for log file output
437+
BUFFERED_LOG_SIZE = 10000

0 commit comments

Comments
 (0)