Skip to content

Commit c8aac19

Browse files
committed
Merge branch 'RicterZ-master'
2 parents 5b8df79 + 9010e15 commit c8aac19

5 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎lib/core/common.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,8 @@ def parseTargetUrl():
12481248
errMsg += "on this platform"
12491249
raise SqlmapGenericException(errMsg)
12501250

1251-
if not re.search("^http[s]*://", conf.url, re.I):
1251+
if not re.search("^http[s]*://", conf.url, re.I) and \
1252+
not re.search("^ws[s]*://", conf.url, re.I):
12521253
if ":443/" in conf.url:
12531254
conf.url = "https://" + conf.url
12541255
else:

‎lib/core/option.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2180,6 +2180,16 @@ def _setTorSocksProxySettings():
21802180
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5 if conf.torType == PROXY_TYPE.SOCKS5 else socks.PROXY_TYPE_SOCKS4, LOCALHOST, conf.torPort or DEFAULT_TOR_SOCKS_PORT)
21812181
socks.wrapmodule(urllib2)
21822182

2183+
def _checkWebSocket():
2184+
infoMsg = "checking URL is WebSocket or not"
2185+
logger.debug(infoMsg)
2186+
if conf.url and (conf.url.startswith("ws:/") or conf.url.startswith("wss:/")):
2187+
try:
2188+
from websocket import ABNF
2189+
except ImportError:
2190+
errMsg = "it seems that python 'websocket-client' third-party library not be installed. "
2191+
raise SqlmapMissingDependence(errMsg)
2192+
21832193
def _checkTor():
21842194
if not conf.checkTor:
21852195
return
@@ -2449,6 +2459,7 @@ def init():
24492459
_setWafFunctions()
24502460
_setTrafficOutputFP()
24512461
_resolveCrossReferences()
2462+
_checkWebSocket()
24522463

24532464
parseTargetUrl()
24542465
parseTargetDirect()

‎lib/core/target.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def process(match, repl):
211211

212212
kb.processUserMarks = True if (kb.postHint and CUSTOM_INJECTION_MARK_CHAR in conf.data) else kb.processUserMarks
213213

214-
if re.search(URI_INJECTABLE_REGEX, conf.url, re.I) and not any(place in conf.parameters for place in (PLACE.GET, PLACE.POST)) and not kb.postHint and not CUSTOM_INJECTION_MARK_CHAR in (conf.data or ""):
214+
if re.search(URI_INJECTABLE_REGEX, conf.url, re.I) and not any(place in conf.parameters for place in (PLACE.GET, PLACE.POST)) and not kb.postHint and not CUSTOM_INJECTION_MARK_CHAR in (conf.data or "") and conf.url.startswith("http"):
215215
warnMsg = "you've provided target URL without any GET "
216216
warnMsg += "parameters (e.g. www.site.com/article.php?id=1) "
217217
warnMsg += "and without providing any POST parameters "

‎lib/request/connect.py‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
import urllib2
2020
import urlparse
2121

22+
try:
23+
import websocket
24+
from websocket import WebSocketException
25+
except ImportError:
26+
class WebSocketException(Exception):
27+
pass
28+
2229
from extra.safe2bin.safe2bin import safecharencode
2330
from lib.core.agent import agent
2431
from lib.core.common import asciifyUrl
@@ -232,6 +239,7 @@ def getPage(**kwargs):
232239
retrying = kwargs.get("retrying", False)
233240
crawling = kwargs.get("crawling", False)
234241
skipRead = kwargs.get("skipRead", False)
242+
is_websocket = conf.url.startswith("ws")
235243

236244
if not urlparse.urlsplit(url).netloc:
237245
url = urlparse.urljoin(conf.url, url)
@@ -364,7 +372,18 @@ def getPage(**kwargs):
364372
url = unicodeencode(url)
365373
post = unicodeencode(post, kb.pageEncoding)
366374

367-
if method and method not in (HTTPMETHOD.GET, HTTPMETHOD.POST):
375+
if is_websocket:
376+
# WebSocket will add Host field of headers automatically
377+
disallowed_headers = ['Host']
378+
ws = websocket.WebSocket()
379+
ws.connect(url, header=["%s: %s" % _ for _ in headers.items() if _[0] not in disallowed_headers], cookie=cookie)
380+
ws.send(urldecode(post) if post else '')
381+
response = ws.recv()
382+
ws.close()
383+
# WebSocket class does not have response headers
384+
return response, {}, 101
385+
386+
elif method and method not in (HTTPMETHOD.GET, HTTPMETHOD.POST):
368387
method = unicodeencode(method)
369388
req = MethodRequest(url, post, headers)
370389
req.set_method(method)
@@ -538,13 +557,13 @@ def getPage(**kwargs):
538557
debugMsg = "got HTTP error code: %d (%s)" % (code, status)
539558
logger.debug(debugMsg)
540559

541-
except (urllib2.URLError, socket.error, socket.timeout, httplib.BadStatusLine, httplib.IncompleteRead, httplib.ResponseNotReady, struct.error, ProxyError, SqlmapCompressionException), e:
560+
except (urllib2.URLError, socket.error, socket.timeout, httplib.BadStatusLine, httplib.IncompleteRead, httplib.ResponseNotReady, struct.error, ProxyError, SqlmapCompressionException, WebSocketException), e:
542561
tbMsg = traceback.format_exc()
543562

544563
if "no host given" in tbMsg:
545564
warnMsg = "invalid URL address used (%s)" % repr(url)
546565
raise SqlmapSyntaxException(warnMsg)
547-
elif "forcibly closed" in tbMsg:
566+
elif "forcibly closed" in tbMsg or "Connection is already closed" in tbMsg:
548567
warnMsg = "connection was forcibly closed by the target URL"
549568
elif "timed out" in tbMsg:
550569
if kb.testMode and kb.testType not in (None, PAYLOAD.TECHNIQUE.TIME, PAYLOAD.TECHNIQUE.STACKED):
@@ -563,6 +582,10 @@ def getPage(**kwargs):
563582
elif "IncompleteRead" in tbMsg:
564583
warnMsg = "there was an incomplete read error while retrieving data "
565584
warnMsg += "from the target URL"
585+
elif "Handshake status" in tbMsg:
586+
status = re.search("Handshake status ([\d]{3})", tbMsg)
587+
errMsg = "websocket handshake status %s" % status.group(1) if status else 'unknown'
588+
raise SqlmapConnectionException(errMsg)
566589
else:
567590
warnMsg = "unable to connect to the target URL"
568591

‎lib/utils/deps.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ def checkDependencies():
7878
logger.warn(warnMsg)
7979
missing_libraries.add('python-ntlm')
8080

81+
try:
82+
from websocket import ABNF
83+
debugMsg = "'python websocket-client' library is found"
84+
logger.debug(debugMsg)
85+
except ImportError:
86+
warnMsg = "sqlmap requires 'python websocket-client' third-party library for "
87+
warnMsg += "if you plan to attack a web application behind websocket. "
88+
warnMsg += "Download from https://pypi.python.org/pypi/websocket-client/"
89+
logger.warn(warnMsg)
90+
missing_libraries.add('websocket-client')
91+
8192
if IS_WIN:
8293
try:
8394
import pyreadline

0 commit comments

Comments
 (0)