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

Skip to content

Commit 19aed90

Browse files
committed
Implementation for an Issue #874
1 parent 6448d3c commit 19aed90

3 files changed

Lines changed: 50 additions & 3 deletions

File tree

lib/core/common.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import contextlib
1010
import cookielib
1111
import copy
12+
import hashlib
1213
import httplib
1314
import inspect
15+
import json
1416
import logging
1517
import ntpath
1618
import os
@@ -23,6 +25,7 @@
2325
import tempfile
2426
import time
2527
import urllib
28+
import urllib2
2629
import urlparse
2730
import unicodedata
2831

@@ -99,6 +102,7 @@
99102
from lib.core.settings import FORM_SEARCH_REGEX
100103
from lib.core.settings import GENERIC_DOC_ROOT_DIRECTORY_NAMES
101104
from lib.core.settings import GIT_PAGE
105+
from lib.core.settings import GITHUB_REPORT_OAUTH_TOKEN
102106
from lib.core.settings import GOOGLE_ANALYTICS_COOKIE_PREFIX
103107
from lib.core.settings import HASHDB_MILESTONE_VALUE
104108
from lib.core.settings import HOST_ALIASES
@@ -876,7 +880,7 @@ def readInput(message, default=None, checkBatch=True):
876880
message = "\n%s" % message
877881
kb.prependFlag = False
878882

879-
if conf.answers:
883+
if conf.get("answers"):
880884
for item in conf.answers.split(','):
881885
question = item.split('=')[0].strip()
882886
answer = item.split('=')[1] if len(item.split('=')) > 1 else None
@@ -892,7 +896,7 @@ def readInput(message, default=None, checkBatch=True):
892896
break
893897

894898
if retVal is None:
895-
if checkBatch and conf.batch:
899+
if checkBatch and conf.get("batch"):
896900
if isListLike(default):
897901
options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default)
898902
elif default:
@@ -2843,6 +2847,43 @@ def unhandledExceptionMessage():
28432847

28442848
return maskSensitiveData(errMsg)
28452849

2850+
def createGithubIssue(errMsg, excMsg):
2851+
"""
2852+
Automatically create a Github issue with unhandled exception information
2853+
"""
2854+
2855+
msg = "\ndo you want to automatically create a new (anonymized) issue "
2856+
msg += "with the unhandled exception information at "
2857+
msg += "the official Github repository? [y/N] "
2858+
test = readInput(msg, default="N")
2859+
if test[0] in ("y", "Y"):
2860+
ex = None
2861+
errMsg = errMsg[errMsg.find("\n"):]
2862+
2863+
for match in re.finditer(r'File "(.+?)", line', excMsg):
2864+
file = match.group(1).replace('\\', "/")
2865+
file = file[file.find("sqlmap"):].replace("sqlmap/", "", 1)
2866+
excMsg = excMsg.replace(match.group(1), file)
2867+
2868+
data = {"title": "Unhandled exception (#%s)" % hashlib.md5(excMsg).hexdigest()[:8], "body": "```%s\n```\n```\n%s```" % (errMsg, excMsg)}
2869+
req = urllib2.Request(url="https://api.github.com/repos/sqlmapproject/sqlmap/issues", data=json.dumps(data), headers={"Authorization": "token %s" % GITHUB_REPORT_OAUTH_TOKEN})
2870+
2871+
try:
2872+
f = urllib2.urlopen(req)
2873+
content = f.read()
2874+
except Exception, ex:
2875+
content = None
2876+
2877+
issueUrl = re.search(r"https://github.com/sqlmapproject/sqlmap/issues/\d+", content or "")
2878+
if issueUrl:
2879+
infoMsg = "created Github issue can been found at the address '%s'" % issueUrl.group(0)
2880+
logger.info(infoMsg)
2881+
else:
2882+
warnMsg = "something went wrong while creating a Github issue"
2883+
if ex:
2884+
warnMsg += " ('%s')" % ex
2885+
logger.warn(warnMsg)
2886+
28462887
def maskSensitiveData(msg):
28472888
"""
28482889
Masks sensitive data in the supplied message

lib/core/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@
474474
# Unix timestamp used for forcing cookie expiration when provided with --load-cookies
475475
FORCE_COOKIE_EXPIRATION_TIME = "9999999999"
476476

477+
# Github OAuth token used for creating an automatic Issue for unhandled exceptions
478+
GITHUB_REPORT_OAUTH_TOKEN = "d6c0c7bf3f2298a7b85f82176c46d2f8d494fcc5"
479+
477480
# Skip unforced HashDB flush requests below the threshold number of cached items
478481
HASHDB_FLUSH_THRESHOLD = 32
479482

sqlmap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from lib.controller.controller import start
2323
from lib.core.common import banner
24+
from lib.core.common import createGithubIssue
2425
from lib.core.common import dataToStdout
2526
from lib.core.common import getUnicode
2627
from lib.core.common import setColor
@@ -127,9 +128,11 @@ def main():
127128
except:
128129
print
129130
errMsg = unhandledExceptionMessage()
131+
excMsg = traceback.format_exc()
130132
logger.critical(errMsg)
131133
kb.stickyLevel = logging.CRITICAL
132-
dataToStdout(setColor(traceback.format_exc()))
134+
dataToStdout(excMsg)
135+
createGithubIssue(errMsg, excMsg)
133136

134137
finally:
135138
if conf.get("showTime"):

0 commit comments

Comments
 (0)