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

Skip to content

Commit 0570585

Browse files
committed
Merge branch 'master' of github.com:sqlmapproject/sqlmap
2 parents ca1c0c2 + ca33715 commit 0570585

9 files changed

Lines changed: 312 additions & 97 deletions

File tree

lib/core/common.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,3 +3309,21 @@ def isNumber(value):
33093309
return False
33103310
else:
33113311
return True
3312+
3313+
def pollProcess(process, suppress_errors=False):
3314+
while True:
3315+
dataToStdout(".")
3316+
time.sleep(1)
3317+
3318+
returncode = process.poll()
3319+
3320+
if returncode is not None:
3321+
if not suppress_errors:
3322+
if returncode == 0:
3323+
dataToStdout(" done\n")
3324+
elif returncode < 0:
3325+
dataToStdout(" process terminated by signal %d\n" % returncode)
3326+
elif returncode > 0:
3327+
dataToStdout(" quit unexpectedly with return code %d\n" % returncode)
3328+
3329+
break

lib/core/convert.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import md5
1212
import sha
1313

14+
import json
1415
import pickle
1516
import sys
1617
import struct
@@ -126,3 +127,6 @@ def stdoutencode(data):
126127
retVal = data.encode(UNICODE_ENCODING)
127128

128129
return retVal
130+
131+
def jsonize(data):
132+
return json.dumps(data, sort_keys=False, indent=4)

lib/core/log.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,3 @@
3333
LOGGER_HANDLER.setFormatter(FORMATTER)
3434
LOGGER.addHandler(LOGGER_HANDLER)
3535
LOGGER.setLevel(logging.WARN)
36-
37-
# to handle logger with the RESTful API
38-
LOGGER_OUTPUT = StringIO.StringIO()

lib/core/option.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@
5252
from lib.core.common import UnicodeRawConfigParser
5353
from lib.core.common import urldecode
5454
from lib.core.common import urlencode
55+
from lib.core.convert import base64pickle
5556
from lib.core.convert import base64unpickle
57+
from lib.core.convert import jsonize
5658
from lib.core.data import conf
5759
from lib.core.data import kb
5860
from lib.core.data import logger
@@ -1804,6 +1806,33 @@ def _mergeOptions(inputOptions, overrideOptions):
18041806
if hasattr(conf, key) and conf[key] is None:
18051807
conf[key] = value
18061808

1809+
# Logger recorder object, which keeps the log structure
1810+
class LogRecorder(logging.StreamHandler):
1811+
"""
1812+
Logging handler class which only records CUSTOM_LOGGING.PAYLOAD entries
1813+
to a global list.
1814+
"""
1815+
loghist = []
1816+
1817+
def emit(self, record):
1818+
"""
1819+
Simply record the emitted events.
1820+
"""
1821+
self.loghist.append({'levelname': record.levelname,
1822+
'text': record.msg % record.args if record.args else record.msg,
1823+
'id': len(self.loghist)+1})
1824+
1825+
if conf.fdLog:
1826+
# TODO: this is very heavy operation and slows down a lot the
1827+
# whole execution of the sqlmap engine, find an alternative
1828+
os.write(conf.fdLog, base64pickle(self.loghist))
1829+
1830+
def _setRestAPILog():
1831+
if hasattr(conf, "fdLog") and conf.fdLog:
1832+
logger.removeHandler(LOGGER_HANDLER)
1833+
LOGGER_RECORDER = LogRecorder()
1834+
logger.addHandler(LOGGER_RECORDER)
1835+
18071836
def _setTrafficOutputFP():
18081837
if conf.trafficFile:
18091838
infoMsg = "setting file for logging HTTP traffic"
@@ -2069,14 +2098,13 @@ def init(inputOptions=AttribDict(), overrideOptions=False):
20692098

20702099
if not inputOptions.disableColoring:
20712100
coloramainit()
2072-
elif hasattr(LOGGER_HANDLER, "disable_coloring"):
2073-
LOGGER_HANDLER.disable_coloring = True
20742101

20752102
_setConfAttributes()
20762103
_setKnowledgeBaseAttributes()
20772104
_mergeOptions(inputOptions, overrideOptions)
20782105
_useWizardInterface()
20792106
setVerbosity()
2107+
_setRestAPILog()
20802108
_saveCmdline()
20812109
_setRequestFromFile()
20822110
_cleanupOptions()

lib/core/subprocessng.py

Lines changed: 135 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77

88
import errno
99
import os
10+
import subprocess
1011
import sys
1112
import time
1213

1314
from lib.core.common import dataToStdout
1415
from lib.core.settings import IS_WIN
1516

16-
if not IS_WIN:
17+
if IS_WIN:
18+
try:
19+
from win32file import ReadFile, WriteFile
20+
from win32pipe import PeekNamedPipe
21+
except ImportError:
22+
pass
23+
import msvcrt
24+
else:
25+
import select
1726
import fcntl
1827

1928
if (sys.hexversion >> 16) >= 0x202:
@@ -61,30 +70,132 @@ def blockingWriteToFD(fd, data):
6170

6271
break
6372

64-
def setNonBlocking(fd):
65-
"""
66-
Make a file descriptor non-blocking
67-
"""
68-
69-
if IS_WIN is not True:
70-
flags = fcntl.fcntl(fd, FCNTL.F_GETFL)
71-
flags = flags | os.O_NONBLOCK
72-
fcntl.fcntl(fd, FCNTL.F_SETFL, flags)
73-
74-
def pollProcess(process, suppress_errors=False):
75-
while True:
76-
dataToStdout(".")
77-
time.sleep(1)
73+
# the following code is taken from http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/
74+
class Popen(subprocess.Popen):
75+
def recv(self, maxsize=None):
76+
return self._recv('stdout', maxsize)
77+
78+
def recv_err(self, maxsize=None):
79+
return self._recv('stderr', maxsize)
80+
81+
def send_recv(self, input='', maxsize=None):
82+
return self.send(input), self.recv(maxsize), self.recv_err(maxsize)
83+
84+
def get_conn_maxsize(self, which, maxsize):
85+
if maxsize is None:
86+
maxsize = 1024
87+
elif maxsize < 1:
88+
maxsize = 1
89+
return getattr(self, which), maxsize
90+
91+
def _close(self, which):
92+
getattr(self, which).close()
93+
setattr(self, which, None)
94+
95+
if subprocess.mswindows:
96+
def send(self, input):
97+
if not self.stdin:
98+
return None
99+
100+
try:
101+
x = msvcrt.get_osfhandle(self.stdin.fileno())
102+
(errCode, written) = WriteFile(x, input)
103+
except ValueError:
104+
return self._close('stdin')
105+
except (subprocess.pywintypes.error, Exception), why:
106+
if why[0] in (109, errno.ESHUTDOWN):
107+
return self._close('stdin')
108+
raise
78109

79-
returncode = process.poll()
110+
return written
111+
112+
def _recv(self, which, maxsize):
113+
conn, maxsize = self.get_conn_maxsize(which, maxsize)
114+
if conn is None:
115+
return None
116+
117+
try:
118+
x = msvcrt.get_osfhandle(conn.fileno())
119+
(read, nAvail, nMessage) = PeekNamedPipe(x, 0)
120+
if maxsize < nAvail:
121+
nAvail = maxsize
122+
if nAvail > 0:
123+
(errCode, read) = ReadFile(x, nAvail, None)
124+
except ValueError:
125+
return self._close(which)
126+
except (subprocess.pywintypes.error, Exception), why:
127+
if why[0] in (109, errno.ESHUTDOWN):
128+
return self._close(which)
129+
raise
80130

81-
if returncode is not None:
82-
if not suppress_errors:
83-
if returncode == 0:
84-
dataToStdout(" done\n")
85-
elif returncode < 0:
86-
dataToStdout(" process terminated by signal %d\n" % returncode)
87-
elif returncode > 0:
88-
dataToStdout(" quit unexpectedly with return code %d\n" % returncode)
131+
if self.universal_newlines:
132+
read = self._translate_newlines(read)
133+
return read
134+
else:
135+
def send(self, input):
136+
if not self.stdin:
137+
return None
138+
139+
if not select.select([], [self.stdin], [], 0)[1]:
140+
return 0
141+
142+
try:
143+
written = os.write(self.stdin.fileno(), input)
144+
except OSError, why:
145+
if why[0] == errno.EPIPE: #broken pipe
146+
return self._close('stdin')
147+
raise
89148

149+
return written
150+
151+
def _recv(self, which, maxsize):
152+
conn, maxsize = self.get_conn_maxsize(which, maxsize)
153+
if conn is None:
154+
return None
155+
156+
flags = fcntl.fcntl(conn, fcntl.F_GETFL)
157+
if not conn.closed:
158+
fcntl.fcntl(conn, fcntl.F_SETFL, flags| os.O_NONBLOCK)
159+
160+
try:
161+
if not select.select([conn], [], [], 0)[0]:
162+
return ''
163+
164+
r = conn.read(maxsize)
165+
if not r:
166+
return self._close(which)
167+
168+
if self.universal_newlines:
169+
r = self._translate_newlines(r)
170+
return r
171+
finally:
172+
if not conn.closed:
173+
fcntl.fcntl(conn, fcntl.F_SETFL, flags)
174+
175+
def recv_some(p, t=.1, e=1, tr=5, stderr=0):
176+
if tr < 1:
177+
tr = 1
178+
x = time.time()+t
179+
y = []
180+
r = ''
181+
if stderr:
182+
pr = p.recv_err
183+
else:
184+
pr = p.recv
185+
while time.time() < x or r:
186+
r = pr()
187+
if r is None:
90188
break
189+
elif r:
190+
y.append(r)
191+
else:
192+
time.sleep(max((x-time.time())/tr, 0))
193+
return ''.join(y)
194+
195+
def send_all(p, data):
196+
if not data:
197+
return
198+
199+
while len(data):
200+
sent = p.send(data)
201+
data = buffer(data, sent)

lib/core/update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
from subprocess import Popen as execute
1414

1515
from lib.core.common import dataToStdout
16+
from lib.core.common import pollProcess
1617
from lib.core.data import conf
1718
from lib.core.data import logger
1819
from lib.core.data import paths
1920
from lib.core.revision import getRevisionNumber
2021
from lib.core.settings import GIT_REPOSITORY
2122
from lib.core.settings import IS_WIN
22-
from lib.core.subprocessng import pollProcess
2323

2424
def update():
2525
if not conf.updateAll:

lib/parse/cmdline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ def cmdLineParser():
664664
help="Simple wizard interface for beginner users")
665665

666666
# Hidden and/or experimental options
667-
parser.add_option("--pickle", dest="pickledOptions", help=SUPPRESS_HELP)
667+
parser.add_option("--pickled-options", dest="pickledOptions", help=SUPPRESS_HELP)
668668

669669
parser.add_option("--profile", dest="profile", action="store_true",
670670
help=SUPPRESS_HELP)

0 commit comments

Comments
 (0)