@@ -55,7 +55,7 @@ def usage(err=None):
55
55
uout = Output ()
56
56
p = os .path .basename (sys .argv [0 ])
57
57
uout .
head (
'# {0} {1}, [email protected] \n ' .
format (
p ,
VERSION ))
58
- if err is not None :
58
+ if err is not None and len ( err ) > 0 :
59
59
uout .fail ('\n ' + err )
60
60
uout .info ('usage: {0} [-1246pbnvl] <host>\n ' .format (p ))
61
61
uout .info (' -h, --help print this help' )
@@ -92,10 +92,10 @@ def __setattr__(self, name, value):
92
92
# type: (str, Union[str, int, bool, Sequence[int]]) -> None
93
93
valid = False
94
94
if name in ['ssh1' , 'ssh2' , 'batch' , 'colors' , 'verbose' ]:
95
- valid , value = True , True if value else False
95
+ valid , value = True , True if bool ( value ) else False
96
96
elif name in ['ipv4' , 'ipv6' ]:
97
97
valid = False
98
- value = True if value else False
98
+ value = True if bool ( value ) else False
99
99
ipv = 4 if name == 'ipv4' else 6
100
100
if value :
101
101
value = tuple (list (self .ipvo ) + [ipv ])
@@ -916,7 +916,7 @@ def compare_version(self, other):
916
916
else :
917
917
other = str (other )
918
918
mx = re .match (r'^([\d\.]+\d+)(.*)$' , other )
919
- if mx is not None :
919
+ if bool ( mx ) :
920
920
oversion , opatch = mx .group (1 ), mx .group (2 ).strip ()
921
921
else :
922
922
oversion , opatch = other , ''
@@ -933,10 +933,10 @@ def compare_version(self, other):
933
933
elif self .product == SSH .Product .OpenSSH :
934
934
mx1 = re .match (r'^p\d(.*)' , opatch )
935
935
mx2 = re .match (r'^p\d(.*)' , spatch )
936
- if not (mx1 and mx2 ):
937
- if mx1 is not None :
936
+ if not (bool ( mx1 ) and bool ( mx2 ) ):
937
+ if bool ( mx1 ) :
938
938
opatch = mx1 .group (1 )
939
- if mx2 is not None :
939
+ if bool ( mx2 ) :
940
940
spatch = mx2 .group (1 )
941
941
if spatch < opatch :
942
942
return - 1
@@ -946,28 +946,28 @@ def compare_version(self, other):
946
946
947
947
def between_versions (self , vfrom , vtill ):
948
948
# type: (str, str) -> bool
949
- if vfrom and self .compare_version (vfrom ) < 0 :
949
+ if bool ( vfrom ) and self .compare_version (vfrom ) < 0 :
950
950
return False
951
- if vtill and self .compare_version (vtill ) > 0 :
951
+ if bool ( vtill ) and self .compare_version (vtill ) > 0 :
952
952
return False
953
953
return True
954
954
955
955
def display (self , full = True ):
956
956
# type: (bool) -> str
957
- r = '{0} ' .format (self .vendor ) if self .vendor else ''
957
+ r = '{0} ' .format (self .vendor ) if bool ( self .vendor ) else ''
958
958
r += self .product
959
- if self .version :
959
+ if bool ( self .version ) :
960
960
r += ' {0}' .format (self .version )
961
961
if full :
962
962
patch = self .patch or ''
963
963
if self .product == SSH .Product .OpenSSH :
964
964
mx = re .match (r'^(p\d)(.*)$' , patch )
965
- if mx is not None :
965
+ if bool ( mx ) :
966
966
r += mx .group (1 )
967
967
patch = mx .group (2 ).strip ()
968
- if patch :
968
+ if bool ( patch ) :
969
969
r += ' ({0})' .format (patch )
970
- if self .os :
970
+ if bool ( self .os ) :
971
971
r += ' running on {0}' .format (self .os )
972
972
return r
973
973
@@ -977,16 +977,13 @@ def __str__(self):
977
977
978
978
def __repr__ (self ):
979
979
# type: () -> str
980
- r = 'vendor={0}' .format (self .vendor ) if self .vendor else ''
981
- if self .product :
982
- if self .vendor :
983
- r += ', '
984
- r += 'product={0}' .format (self .product )
985
- if self .version :
980
+ r = 'vendor={0}, ' .format (self .vendor ) if bool (self .vendor ) else ''
981
+ r += 'product={0}' .format (self .product )
982
+ if bool (self .version ):
986
983
r += ', version={0}' .format (self .version )
987
- if self .patch :
984
+ if bool ( self .patch ) :
988
985
r += ', patch={0}' .format (self .patch )
989
- if self .os :
986
+ if bool ( self .os ) :
990
987
r += ', os={0}' .format (self .os )
991
988
return '<{0}({1})>' .format (self .__class__ .__name__ , r )
992
989
@@ -1009,19 +1006,19 @@ def _extract_os_version(cls, c):
1009
1006
if c is None :
1010
1007
return None
1011
1008
mx = re .match (r'^NetBSD(?:_Secure_Shell)?(?:[\s-]+(\d{8})(.*))?$' , c )
1012
- if mx is not None :
1009
+ if bool ( mx ) :
1013
1010
d = cls ._fix_date (mx .group (1 ))
1014
1011
return 'NetBSD' if d is None else 'NetBSD ({0})' .format (d )
1015
1012
mx = re .match (r'^FreeBSD(?:\slocalisations)?[\s-]+(\d{8})(.*)$' , c )
1016
- if mx is None :
1013
+ if not bool ( mx ) :
1017
1014
mx = re .match (r'^[^@]+@FreeBSD\.org[\s-]+(\d{8})(.*)$' , c )
1018
- if mx is not None :
1015
+ if bool ( mx ) :
1019
1016
d = cls ._fix_date (mx .group (1 ))
1020
1017
return 'FreeBSD' if d is None else 'FreeBSD ({0})' .format (d )
1021
1018
w = ['RemotelyAnywhere' , 'DesktopAuthority' , 'RemoteSupportManager' ]
1022
1019
for win_soft in w :
1023
1020
mx = re .match (r'^in ' + win_soft + r' ([\d\.]+\d)$' , c )
1024
- if mx is not None :
1021
+ if bool ( mx ) :
1025
1022
ver = mx .group (1 )
1026
1023
return 'Microsoft Windows ({0} {1})' .format (win_soft , ver )
1027
1024
generic = ['NetBSD' , 'FreeBSD' ]
@@ -1037,35 +1034,35 @@ def parse(cls, banner):
1037
1034
software = str (banner .software )
1038
1035
mx = re .match (r'^dropbear_([\d\.]+\d+)(.*)' , software )
1039
1036
v = None # type: Optional[str]
1040
- if mx is not None :
1037
+ if bool ( mx ) :
1041
1038
patch = cls ._fix_patch (mx .group (2 ))
1042
1039
v , p = 'Matt Johnston' , SSH .Product .DropbearSSH
1043
1040
v = None
1044
1041
return cls (v , p , mx .group (1 ), patch , None )
1045
1042
mx = re .match (r'^OpenSSH[_\.-]+([\d\.]+\d+)(.*)' , software )
1046
- if mx is not None :
1043
+ if bool ( mx ) :
1047
1044
patch = cls ._fix_patch (mx .group (2 ))
1048
1045
v , p = 'OpenBSD' , SSH .Product .OpenSSH
1049
1046
v = None
1050
1047
os_version = cls ._extract_os_version (banner .comments )
1051
1048
return cls (v , p , mx .group (1 ), patch , os_version )
1052
1049
mx = re .match (r'^libssh-([\d\.]+\d+)(.*)' , software )
1053
- if mx is not None :
1050
+ if bool ( mx ) :
1054
1051
patch = cls ._fix_patch (mx .group (2 ))
1055
1052
v , p = None , SSH .Product .LibSSH
1056
1053
os_version = cls ._extract_os_version (banner .comments )
1057
1054
return cls (v , p , mx .group (1 ), patch , os_version )
1058
1055
mx = re .match (r'^RomSShell_([\d\.]+\d+)(.*)' , software )
1059
- if mx is not None :
1056
+ if bool ( mx ) :
1060
1057
patch = cls ._fix_patch (mx .group (2 ))
1061
1058
v , p = 'Allegro Software' , 'RomSShell'
1062
1059
return cls (v , p , mx .group (1 ), patch , None )
1063
1060
mx = re .match (r'^mpSSH_([\d\.]+\d+)' , software )
1064
- if mx is not None :
1061
+ if bool ( mx ) :
1065
1062
v , p = 'HP' , 'iLO (Integrated Lights-Out) sshd'
1066
1063
return cls (v , p , mx .group (1 ), None , None )
1067
1064
mx = re .match (r'^Cisco-([\d\.]+\d+)' , software )
1068
- if mx is not None :
1065
+ if bool ( mx ) :
1069
1066
v , p = 'Cisco' , 'IOS/PIX sshd'
1070
1067
return cls (v , p , mx .group (1 ), None , None )
1071
1068
return None
@@ -1107,27 +1104,27 @@ def __str__(self):
1107
1104
r = 'SSH-{0}.{1}' .format (self .protocol [0 ], self .protocol [1 ])
1108
1105
if self .software is not None :
1109
1106
r += '-{0}' .format (self .software )
1110
- if self .comments :
1107
+ if bool ( self .comments ) :
1111
1108
r += ' {0}' .format (self .comments )
1112
1109
return r
1113
1110
1114
1111
def __repr__ (self ):
1115
1112
# type: () -> str
1116
1113
p = '{0}.{1}' .format (self .protocol [0 ], self .protocol [1 ])
1117
1114
r = 'protocol={0}' .format (p )
1118
- if self .software :
1115
+ if self .software is not None :
1119
1116
r += ', software={0}' .format (self .software )
1120
- if self .comments :
1117
+ if bool ( self .comments ) :
1121
1118
r += ', comments={0}' .format (self .comments )
1122
1119
return '<{0}({1})>' .format (self .__class__ .__name__ , r )
1123
1120
1124
1121
@classmethod
1125
1122
def parse (cls , banner ):
1126
- # type: (text_type) -> SSH.Banner
1123
+ # type: (text_type) -> Optional[ SSH.Banner]
1127
1124
valid_ascii = utils .is_print_ascii (banner )
1128
1125
ascii_banner = utils .to_print_ascii (banner )
1129
1126
mx = cls .RX_BANNER .match (ascii_banner )
1130
- if mx is None :
1127
+ if not bool ( mx ) :
1131
1128
return None
1132
1129
protocol = min (re .findall (cls .RX_PROTOCOL , mx .group (1 )))
1133
1130
protocol = (int (protocol [0 ]), int (protocol [1 ]))
@@ -1814,7 +1811,7 @@ def output_algorithm(alg_db, alg_type, alg_name, alg_max_len=0):
1814
1811
if level == 'info' :
1815
1812
versions = alg_desc [0 ]
1816
1813
since_text = SSH .Algorithm .get_since_text (versions )
1817
- if since_text :
1814
+ if since_text is not None and len ( since_text ) > 0 :
1818
1815
texts .append ((level , since_text ))
1819
1816
idx = idx + 1
1820
1817
if ldesc > idx :
@@ -1951,7 +1948,10 @@ def output_recommendations(algs, software, padlen=0):
1951
1948
fm = '(rec) {0}{1}{2}-- {3} algorithm to {4} {5}'
1952
1949
fn (fm .format (sg , name , p , alg_type , an , b ))
1953
1950
if len (obuf ) > 0 :
1954
- title = '(for {0})' .format (software .display (False )) if software else ''
1951
+ if software is not None :
1952
+ title = '(for {0})' .format (software .display (False ))
1953
+ else :
1954
+ title = ''
1955
1955
out .head ('# algorithm recommendations {0}' .format (title ))
1956
1956
obuf .flush ()
1957
1957
out .sep ()
@@ -2150,7 +2150,10 @@ def audit(aconf, sshv=None):
2150
2150
packet_type , payload = s .read_packet (sshv )
2151
2151
if packet_type < 0 :
2152
2152
try :
2153
- payload_txt = payload .decode ('utf-8' ) if payload else u'empty'
2153
+ if payload is not None and len (payload ) > 0 :
2154
+ payload_txt = payload .decode ('utf-8' )
2155
+ else :
2156
+ payload_txt = u'empty'
2154
2157
except UnicodeDecodeError :
2155
2158
payload_txt = u'"{0}"' .format (repr (payload ).lstrip ('b' )[1 :- 1 ])
2156
2159
if payload_txt == u'Protocol major versions differ.' :
0 commit comments