Reduce multiple protocol prefixed banner.

This commit is contained in:
Andris Raugulis 2016-09-07 12:58:03 +03:00
parent 673b88b2b1
commit 2ae93b1934

View File

@ -210,6 +210,9 @@ class SSH(object):
MSG_KEXDH_REPLY = 32 MSG_KEXDH_REPLY = 32
class Banner(object): class Banner(object):
_RXP, _RXR = r'SSH-(\d)\.\s*?(\d+)', r'(|-([^\s]*)(\s+(.*))?)'
RX_BANNER = re.compile(r'^({0}((-{0})*)){1}$'.format(_RXP, _RXR))
def __init__(self, protocol, software, comments): def __init__(self, protocol, software, comments):
self.__protocol = protocol self.__protocol = protocol
self.__software = software self.__software = software
@ -246,20 +249,14 @@ class SSH(object):
@classmethod @classmethod
def parse(cls, banner): def parse(cls, banner):
mx = re.match(r'^SSH-(\d)\.\s*?(\d+)-SSH-(\d)\.\s*?(\d+)(|-.*)$', banner) mx = cls.RX_BANNER.match(banner)
if mx is not None:
p1 = (int(mx.group(1)), int(mx.group(2)))
p2 = (int(mx.group(3)), int(mx.group(4)))
protocol = p1 if p1 < p2 else p2
banner = 'SSH-{0}.{1}{2}'.format(protocol[0], protocol[1], mx.group(5))
mx = re.match(r'^SSH-(\d)\.\s*?(\d+)(|-([^\s]*)(\s+(.*))?)$', banner)
if mx is None: if mx is None:
return None return None
protocol = (int(mx.group(1)), int(mx.group(2))) protocol = min(re.findall(cls._RXP, mx.group(1)))
software = (mx.group(4) or '').strip() or None software = (mx.group(9) or '').strip() or None
if software is None and mx.group(3).startswith('-'): if software is None and mx.group(8).startswith('-'):
software = '' software = ''
comments = (mx.group(6) or '').strip() or None comments = (mx.group(11) or '').strip() or None
return cls(protocol, software, comments) return cls(protocol, software, comments)
class Socket(ReadBuf, WriteBuf): class Socket(ReadBuf, WriteBuf):