Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions conpot/protocols/modbus/modbus_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,26 @@ def handle(self, sock, address):
session.add_event({"type": "CONNECTION_TERMINATED"})
break
_, _, length = struct.unpack(">HHH", request[:6])
previous_request = None
stall_counter = 0
# Implemented fix proposed by Kevalz95, Issue #564
# Adds stall counter to prevent infinite loop on malformed requests
while len(request) < (length + 6):
try:
# Previous request now assigned before recv() called, length check performed after
previous_request = request
new_byte = sock.recv(1)
request += new_byte
if previous_request and len(previous_request) == len(request):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will be always true on the second iteration.
Check request length before and after recv()

stall_counter += 1
if stall_counter >= 1000:
logger.info(
"Modbus client provided data {} but invalid.".format(
session.id
)
)
session.add_event({"type": "CONNECTION_TERMINATED"})
break
except Exception:
break
query = modbus_tcp.TcpQuery()
Expand Down
Loading