Keep WebSocket responses on normal closure (#6173)

Co-authored-by: cuteNBD <206630735+bingdongni@users.noreply.github.com>
This commit is contained in:
cuteNBD 2026-11-04 17:18:29 +08:00 • committed by GitHub
parent 74f50122fb
commit b333a14d8b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 136 additions and 4 deletions

View file

@ -619,17 +619,17 @@ class Connect(object):
warnMsg = "too large websocket response detected. Automatically trimming it"
singleTimeWarnMessage(warnMsg)
break
except websocket.WebSocketTimeoutException:
except (websocket.WebSocketTimeoutException, websocket.WebSocketNormalClosureException):
kb.webSocketRecvCount = len(_page)
break
else:
for i in xrange(max(1, kb.webSocketRecvCount)):
# Note: a later response may carry fewer frames than the calibration one
# (e.g. a FALSE boolean-blind reply); stop on timeout instead of letting it
# bubble up as a failed request, mirroring the initial recv loop above
# (e.g. a FALSE boolean-blind reply); stop on timeout or normal closure,
# mirroring the initial recv loop above
try:
_page.append(ws.recv())
except websocket.WebSocketTimeoutException:
except (websocket.WebSocketTimeoutException, websocket.WebSocketNormalClosureException):
break
page = "\n".join(_page)

View file

@ -39,6 +39,9 @@ class WebSocketTimeoutException(WebSocketException):
class WebSocketConnectionClosedException(WebSocketException):
pass
class WebSocketNormalClosureException(WebSocketConnectionClosedException):
pass
class WebSocket(object):
"""
Minimal RFC 6455 client exposing the websocket-client subset used by sqlmap: settimeout(), connect(),
@ -159,10 +162,14 @@ class WebSocket(object):
def recv(self):
data = bytearray()
receivedFragment = False
while True:
fin, opcode, payload = self._recvFrame()
if opcode == OPCODE_CLOSE:
self._closed = True
normal = not payload or (len(payload) >= 2 and struct.unpack("!H", bytes(payload[:2]))[0] in (1000, 1001))
if normal and not receivedFragment:
raise WebSocketNormalClosureException("WebSocket connection closed")
raise WebSocketConnectionClosedException("WebSocket connection closed")
elif opcode == OPCODE_PING:
self._sendFrame(bytes(payload), OPCODE_PONG)
@ -171,6 +178,7 @@ class WebSocket(object):
continue
data += payload
receivedFragment = True
if fin:
break