Skip to content

Commit

Permalink
Fix s7 parameter length calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
enen92 committed Oct 6, 2018
1 parent 4dcf46f commit b2d2d51
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions conpot/protocols/s7comm/s7.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def __init__(self, pdu_type=0, reserved=0, request_id=0, result_info=0, paramete
self.pdu_type = pdu_type
self.reserved = reserved
self.request_id = request_id
# sometimes "parameters" happen to be of type int, not str
self.param_length = len(str(parameters))
# sometimes "parameters" happen to be of type int, and not a byte string
self.param_length = len(parameters) if isinstance(parameters, bytes) else len(str(parameters))
self.data_length = len(data)
self.result_info = result_info
self.parameters = parameters
Expand Down
17 changes: 9 additions & 8 deletions conpot/tests/helpers/s7comm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@
import struct
import socket
import string
# from conpot.protocols.s7comm.tpkt import cleanse_byte_string

__FILTER = "".join([' '] + [' ' if chr(x) not in string.printable or chr(x) in string.whitespace else chr(x)
for x in range(1, 256)])


_bytes_to_str = lambda items : (value.decode('ascii') if isinstance(value, bytes) else value for value in items)


def StripUnprintable(msg):
return msg.translate(__FILTER)
return msg.decode('ascii').translate(__FILTER)


class TPKTPacket:
Expand Down Expand Up @@ -315,8 +317,8 @@ def GetIdentity(ip, port, src_tsap, dst_tsap):
7: 'Basic Firmware'
},
'packer': {
(1, 6): lambda packet: "{0:s} v.{2:d}.{3:d}".format(*unpack('!20sHBBH', packet)),
(7,): lambda packet: "{0:s} v.{3:d}.{4:d}.{5:d}".format(*unpack('!20sHBBBB', packet))
(1, 6): lambda packet: "{0:s} v.{2:d}.{3:d}".format(*_bytes_to_str(unpack('!20sHBBH', packet))),
(7,): lambda packet: "{0:s} v.{3:d}.{4:d}.{5:d}".format(*_bytes_to_str(unpack('!20sHBBBB', packet)))
}
},
0x1c: {
Expand All @@ -335,9 +337,9 @@ def GetIdentity(ip, port, src_tsap, dst_tsap):
11: 'Location designation of a module'
},
'packer': {
(1, 2, 5): lambda packet: "%s" % packet[:24],
(3, 7, 8): lambda packet: "%s" % packet[:32],
(4,): lambda packet: "%s" % packet[:26]
(1, 2, 5): lambda packet: "%s" % packet[:24].decode('ascii'),
(3, 7, 8): lambda packet: "%s" % packet[:32].decode('ascii'),
(4,): lambda packet: "%s" % packet[:26].decode('ascii')
}
}
}
Expand All @@ -351,7 +353,6 @@ def GetIdentity(ip, port, src_tsap, dst_tsap):
except S7Error:
continue

indexes = szl_dict[szl_id]['indexes']
packers = szl_dict[szl_id]['packer']

for item in entities:
Expand Down

0 comments on commit b2d2d51

Please sign in to comment.