Skip to content

Commit 28e7c8f

Browse files
committed
Refactoring KeyboardInterrupt mess
1 parent c497aa9 commit 28e7c8f

File tree

3 files changed

+154
-172
lines changed

3 files changed

+154
-172
lines changed

lib/core/settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from lib.core.enums import OS
2020

2121
# sqlmap version (<major>.<minor>.<month>.<monthly commit>)
22-
VERSION = "1.2.12.21"
22+
VERSION = "1.2.12.22"
2323
TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable"
2424
TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34}
2525
VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE)

sqlmap.py

Lines changed: 151 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,7 @@ def main():
172172

173173
except SqlmapUserQuitException:
174174
errMsg = "user quit"
175-
try:
176-
logger.error(errMsg)
177-
except KeyboardInterrupt:
178-
pass
175+
logger.error(errMsg)
179176

180177
except (SqlmapSilentQuitException, bdb.BdbQuit):
181178
pass
@@ -185,23 +182,18 @@ def main():
185182

186183
except SqlmapBaseException as ex:
187184
errMsg = getSafeExString(ex)
188-
try:
189-
logger.critical(errMsg)
190-
except KeyboardInterrupt:
191-
pass
185+
logger.critical(errMsg)
186+
192187
raise SystemExit
193188

194189
except KeyboardInterrupt:
195190
print
196191

197192
except EOFError:
198193
print
199-
errMsg = "exit"
200194

201-
try:
202-
logger.error(errMsg)
203-
except KeyboardInterrupt:
204-
pass
195+
errMsg = "exit"
196+
logger.error(errMsg)
205197

206198
except SystemExit:
207199
pass
@@ -212,140 +204,136 @@ def main():
212204
excMsg = traceback.format_exc()
213205
valid = checkIntegrity()
214206

215-
try:
216-
if valid is False:
217-
errMsg = "code integrity check failed (turning off automatic issue creation). "
218-
errMsg += "You should retrieve the latest development version from official GitHub "
219-
errMsg += "repository at '%s'" % GIT_PAGE
220-
logger.critical(errMsg)
221-
print
222-
dataToStdout(excMsg)
223-
raise SystemExit
224-
225-
elif any(_ in excMsg for _ in ("tamper/", "waf/")):
226-
logger.critical(errMsg)
227-
print
228-
dataToStdout(excMsg)
229-
raise SystemExit
230-
231-
elif any(_ in excMsg for _ in ("ImportError", "Can't find file for module")):
232-
errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip()
233-
logger.critical(errMsg)
234-
raise SystemExit
235-
236-
elif "MemoryError" in excMsg:
237-
errMsg = "memory exhaustion detected"
238-
logger.critical(errMsg)
239-
raise SystemExit
240-
241-
elif any(_ in excMsg for _ in ("No space left", "Disk quota exceeded")):
242-
errMsg = "no space left on output device"
243-
logger.critical(errMsg)
244-
raise SystemExit
245-
246-
elif all(_ in excMsg for _ in ("No such file", "_'", "self.get_prog_name()")):
247-
errMsg = "corrupted installation detected ('%s'). " % excMsg.strip().split('\n')[-1]
248-
errMsg += "You should retrieve the latest development version from official GitHub "
249-
errMsg += "repository at '%s'" % GIT_PAGE
250-
logger.critical(errMsg)
251-
raise SystemExit
252-
253-
elif "Read-only file system" in excMsg:
254-
errMsg = "output device is mounted as read-only"
255-
logger.critical(errMsg)
256-
raise SystemExit
257-
258-
elif "OperationalError: disk I/O error" in excMsg:
259-
errMsg = "I/O error on output device"
260-
logger.critical(errMsg)
261-
raise SystemExit
262-
263-
elif "Violation of BIDI" in excMsg:
264-
errMsg = "invalid URL (violation of Bidi IDNA rule - RFC 5893)"
265-
logger.critical(errMsg)
266-
raise SystemExit
267-
268-
elif "_mkstemp_inner" in excMsg:
269-
errMsg = "there has been a problem while accessing temporary files"
270-
logger.critical(errMsg)
271-
raise SystemExit
272-
273-
elif all(_ in excMsg for _ in ("twophase", "sqlalchemy")):
274-
errMsg = "please update the 'sqlalchemy' package (>= 1.1.11) "
275-
errMsg += "(Reference: https://qiita.com/tkprof/items/7d7b2d00df9c5f16fffe)"
276-
logger.critical(errMsg)
277-
raise SystemExit
278-
279-
elif all(_ in excMsg for _ in ("scramble_caching_sha2", "TypeError")):
280-
errMsg = "please downgrade the 'PyMySQL' package (=< 0.8.1) "
281-
errMsg += "(Reference: https://github.com/PyMySQL/PyMySQL/issues/700)"
282-
logger.critical(errMsg)
283-
raise SystemExit
284-
285-
elif "must be pinned buffer, not bytearray" in excMsg:
286-
errMsg = "error occurred at Python interpreter which "
287-
errMsg += "is fixed in 2.7.x. Please update accordingly "
288-
errMsg += "(Reference: https://bugs.python.org/issue8104)"
289-
logger.critical(errMsg)
290-
raise SystemExit
291-
292-
elif "can't start new thread" in excMsg:
293-
errMsg = "there has been a problem while creating new thread instance. "
294-
errMsg += "Please make sure that you are not running too many processes"
295-
if not IS_WIN:
296-
errMsg += " (or increase the 'ulimit -u' value)"
297-
logger.critical(errMsg)
298-
raise SystemExit
299-
300-
elif "'DictObject' object has no attribute '" in excMsg and all(_ in errMsg for _ in ("(fingerprinted)", "(identified)")):
301-
errMsg = "there has been a problem in enumeration. "
302-
errMsg += "Because of a considerable chance of false-positive case "
303-
errMsg += "you are advised to rerun with switch '--flush-session'"
304-
logger.critical(errMsg)
305-
raise SystemExit
306-
307-
elif all(_ in excMsg for _ in ("pymysql", "configparser")):
308-
errMsg = "wrong initialization of pymsql detected (using Python3 dependencies)"
309-
logger.critical(errMsg)
310-
raise SystemExit
311-
312-
elif "bad marshal data (unknown type code)" in excMsg:
313-
match = re.search(r"\s*(.+)\s+ValueError", excMsg)
314-
errMsg = "one of your .pyc files are corrupted%s" % (" ('%s')" % match.group(1) if match else "")
315-
errMsg += ". Please delete .pyc files on your system to fix the problem"
316-
logger.critical(errMsg)
317-
raise SystemExit
318-
319-
elif kb.get("dumpKeyboardInterrupt"):
320-
raise SystemExit
321-
322-
elif any(_ in excMsg for _ in ("Broken pipe",)):
323-
raise SystemExit
324-
325-
for match in re.finditer(r'File "(.+?)", line', excMsg):
326-
file_ = match.group(1)
327-
file_ = os.path.relpath(file_, os.path.dirname(__file__))
328-
file_ = file_.replace("\\", '/')
329-
if "../" in file_:
330-
file_ = re.sub(r"(\.\./)+", '/', file_)
331-
else:
332-
file_ = file_.lstrip('/')
333-
file_ = re.sub(r"/{2,}", '/', file_)
334-
excMsg = excMsg.replace(match.group(1), file_)
207+
if valid is False:
208+
errMsg = "code integrity check failed (turning off automatic issue creation). "
209+
errMsg += "You should retrieve the latest development version from official GitHub "
210+
errMsg += "repository at '%s'" % GIT_PAGE
211+
logger.critical(errMsg)
212+
print
213+
dataToStdout(excMsg)
214+
raise SystemExit
215+
216+
elif any(_ in excMsg for _ in ("tamper/", "waf/")):
217+
logger.critical(errMsg)
218+
print
219+
dataToStdout(excMsg)
220+
raise SystemExit
221+
222+
elif any(_ in excMsg for _ in ("ImportError", "Can't find file for module")):
223+
errMsg = "invalid runtime environment ('%s')" % excMsg.split("Error: ")[-1].strip()
224+
logger.critical(errMsg)
225+
raise SystemExit
226+
227+
elif "MemoryError" in excMsg:
228+
errMsg = "memory exhaustion detected"
229+
logger.critical(errMsg)
230+
raise SystemExit
231+
232+
elif any(_ in excMsg for _ in ("No space left", "Disk quota exceeded")):
233+
errMsg = "no space left on output device"
234+
logger.critical(errMsg)
235+
raise SystemExit
236+
237+
elif all(_ in excMsg for _ in ("No such file", "_'", "self.get_prog_name()")):
238+
errMsg = "corrupted installation detected ('%s'). " % excMsg.strip().split('\n')[-1]
239+
errMsg += "You should retrieve the latest development version from official GitHub "
240+
errMsg += "repository at '%s'" % GIT_PAGE
241+
logger.critical(errMsg)
242+
raise SystemExit
243+
244+
elif "Read-only file system" in excMsg:
245+
errMsg = "output device is mounted as read-only"
246+
logger.critical(errMsg)
247+
raise SystemExit
248+
249+
elif "OperationalError: disk I/O error" in excMsg:
250+
errMsg = "I/O error on output device"
251+
logger.critical(errMsg)
252+
raise SystemExit
335253

336-
errMsg = maskSensitiveData(errMsg)
337-
excMsg = maskSensitiveData(excMsg)
254+
elif "Violation of BIDI" in excMsg:
255+
errMsg = "invalid URL (violation of Bidi IDNA rule - RFC 5893)"
256+
logger.critical(errMsg)
257+
raise SystemExit
258+
259+
elif "_mkstemp_inner" in excMsg:
260+
errMsg = "there has been a problem while accessing temporary files"
261+
logger.critical(errMsg)
262+
raise SystemExit
338263

339-
if conf.get("api") or not valid:
340-
logger.critical("%s\n%s" % (errMsg, excMsg))
264+
elif all(_ in excMsg for _ in ("twophase", "sqlalchemy")):
265+
errMsg = "please update the 'sqlalchemy' package (>= 1.1.11) "
266+
errMsg += "(Reference: https://qiita.com/tkprof/items/7d7b2d00df9c5f16fffe)"
267+
logger.critical(errMsg)
268+
raise SystemExit
269+
270+
elif all(_ in excMsg for _ in ("scramble_caching_sha2", "TypeError")):
271+
errMsg = "please downgrade the 'PyMySQL' package (=< 0.8.1) "
272+
errMsg += "(Reference: https://github.com/PyMySQL/PyMySQL/issues/700)"
273+
logger.critical(errMsg)
274+
raise SystemExit
275+
276+
elif "must be pinned buffer, not bytearray" in excMsg:
277+
errMsg = "error occurred at Python interpreter which "
278+
errMsg += "is fixed in 2.7.x. Please update accordingly "
279+
errMsg += "(Reference: https://bugs.python.org/issue8104)"
280+
logger.critical(errMsg)
281+
raise SystemExit
282+
283+
elif "can't start new thread" in excMsg:
284+
errMsg = "there has been a problem while creating new thread instance. "
285+
errMsg += "Please make sure that you are not running too many processes"
286+
if not IS_WIN:
287+
errMsg += " (or increase the 'ulimit -u' value)"
288+
logger.critical(errMsg)
289+
raise SystemExit
290+
291+
elif "'DictObject' object has no attribute '" in excMsg and all(_ in errMsg for _ in ("(fingerprinted)", "(identified)")):
292+
errMsg = "there has been a problem in enumeration. "
293+
errMsg += "Because of a considerable chance of false-positive case "
294+
errMsg += "you are advised to rerun with switch '--flush-session'"
295+
logger.critical(errMsg)
296+
raise SystemExit
297+
298+
elif all(_ in excMsg for _ in ("pymysql", "configparser")):
299+
errMsg = "wrong initialization of pymsql detected (using Python3 dependencies)"
300+
logger.critical(errMsg)
301+
raise SystemExit
302+
303+
elif "bad marshal data (unknown type code)" in excMsg:
304+
match = re.search(r"\s*(.+)\s+ValueError", excMsg)
305+
errMsg = "one of your .pyc files are corrupted%s" % (" ('%s')" % match.group(1) if match else "")
306+
errMsg += ". Please delete .pyc files on your system to fix the problem"
307+
logger.critical(errMsg)
308+
raise SystemExit
309+
310+
elif kb.get("dumpKeyboardInterrupt"):
311+
raise SystemExit
312+
313+
elif any(_ in excMsg for _ in ("Broken pipe",)):
314+
raise SystemExit
315+
316+
for match in re.finditer(r'File "(.+?)", line', excMsg):
317+
file_ = match.group(1)
318+
file_ = os.path.relpath(file_, os.path.dirname(__file__))
319+
file_ = file_.replace("\\", '/')
320+
if "../" in file_:
321+
file_ = re.sub(r"(\.\./)+", '/', file_)
341322
else:
342-
logger.critical(errMsg)
343-
kb.stickyLevel = logging.CRITICAL
344-
dataToStdout(excMsg)
345-
createGithubIssue(errMsg, excMsg)
323+
file_ = file_.lstrip('/')
324+
file_ = re.sub(r"/{2,}", '/', file_)
325+
excMsg = excMsg.replace(match.group(1), file_)
326+
327+
errMsg = maskSensitiveData(errMsg)
328+
excMsg = maskSensitiveData(excMsg)
346329

347-
except KeyboardInterrupt:
348-
pass
330+
if conf.get("api") or not valid:
331+
logger.critical("%s\n%s" % (errMsg, excMsg))
332+
else:
333+
logger.critical(errMsg)
334+
kb.stickyLevel = logging.CRITICAL
335+
dataToStdout(excMsg)
336+
createGithubIssue(errMsg, excMsg)
349337

350338
finally:
351339
kb.threadContinue = False
@@ -366,45 +354,39 @@ def main():
366354
shutil.rmtree(kb.tempDir, ignore_errors=True)
367355

368356
if conf.get("hashDB"):
369-
try:
370-
conf.hashDB.flush(True)
371-
except KeyboardInterrupt:
372-
pass
357+
conf.hashDB.flush(True)
373358

374359
if conf.get("harFile"):
375360
with openFile(conf.harFile, "w+b") as f:
376361
json.dump(conf.httpCollector.obtain(), fp=f, indent=4, separators=(',', ': '))
377362

378363
if conf.get("api"):
379-
try:
380-
conf.databaseCursor.disconnect()
381-
except KeyboardInterrupt:
382-
pass
364+
conf.databaseCursor.disconnect()
383365

384366
if conf.get("dumper"):
385367
conf.dumper.flush()
386368

387369
# short delay for thread finalization
388-
try:
389-
_ = time.time()
390-
while threading.activeCount() > 1 and (time.time() - _) > THREAD_FINALIZATION_TIMEOUT:
391-
time.sleep(0.01)
392-
393-
if cmdLineOptions.get("sqlmapShell"):
394-
cmdLineOptions.clear()
395-
conf.clear()
396-
kb.clear()
397-
conf.disableBanner = True
398-
main()
399-
except KeyboardInterrupt:
400-
pass
401-
finally:
402-
# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
403-
if threading.activeCount() > 1:
404-
os._exit(0)
370+
_ = time.time()
371+
while threading.activeCount() > 1 and (time.time() - _) > THREAD_FINALIZATION_TIMEOUT:
372+
time.sleep(0.01)
373+
374+
if cmdLineOptions.get("sqlmapShell"):
375+
cmdLineOptions.clear()
376+
conf.clear()
377+
kb.clear()
378+
conf.disableBanner = True
379+
main()
405380

406381
if __name__ == "__main__":
407-
main()
382+
try:
383+
main()
384+
except KeyboardInterrupt:
385+
pass
386+
finally:
387+
# Reference: http://stackoverflow.com/questions/1635080/terminate-a-multi-thread-python-program
388+
if threading.activeCount() > 1:
389+
os._exit(0)
408390
else:
409391
# cancelling postponed imports (because of Travis CI checks)
410392
from lib.controller.controller import start

txt/checksum.md5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ c8c386d644d57c659d74542f5f57f632 lib/core/patch.py
4949
0c3eef46bdbf87e29a3f95f90240d192 lib/core/replication.py
5050
a7db43859b61569b601b97f187dd31c5 lib/core/revision.py
5151
fcb74fcc9577523524659ec49e2e964b lib/core/session.py
52-
8e45f357b6d73d128267f3b66fe5e081 lib/core/settings.py
52+
7d446786d9c7f49c591f382079d39787 lib/core/settings.py
5353
a971ce157d04de96ba6e710d3d38a9a8 lib/core/shell.py
5454
a7edc9250d13af36ac0108f259859c19 lib/core/subprocessng.py
5555
1581be48127a3a7a9fd703359b6e7567 lib/core/target.py
@@ -234,7 +234,7 @@ ec2ba8c757ac96425dcd2b97970edd3a shell/stagers/stager.asp_
234234
0c48ddb1feb7e38a951ef05a0d48e032 shell/stagers/stager.jsp_
235235
2f9e459a4cf6a58680978cdce5ff7971 shell/stagers/stager.php_
236236
cd90da0474d7b1a67d7b40d208493375 sqlmapapi.py
237-
55ba3999ab8819e0d34ca075d46fa9dd sqlmap.py
237+
82f2326ec9cc8719859266f72c19f9b3 sqlmap.py
238238
523dab9e1093eb59264c6beb366b255a tamper/0x2char.py
239239
3a1697585ae4e7bf315e9dda97d6f321 tamper/apostrophemask.py
240240
d7a119a74be9b385ee3884fb5e6af041 tamper/apostrophenullencode.py

0 commit comments

Comments
 (0)