|
15 | 15 | from scapy.asn1.asn1 import ASN1_Class_UNIVERSAL, ASN1_Codecs, ASN1_NULL, \ |
16 | 16 | ASN1_SEQUENCE |
17 | 17 | from scapy.asn1.ber import BERcodec_SEQUENCE |
18 | | -from scapy.sendrecv import sr1 |
| 18 | +from scapy.sendrecv import sr, sr1 |
19 | 19 | from scapy.volatile import RandShort, IntAutoTime |
20 | 20 | from scapy.layers.inet import UDP, IP, ICMP |
21 | 21 |
|
@@ -287,10 +287,51 @@ def answers(self, other): |
287 | 287 | bind_layers(UDP, SNMP, sport=161, dport=161) |
288 | 288 |
|
289 | 289 |
|
| 290 | +def snmpget(dst, oid="1.0.8802.1.1.1.1.1.2.1.2.29", community="public"): |
| 291 | + """ |
| 292 | + SNMP get. |
| 293 | +
|
| 294 | + This can be used to perform a SNMP scan:: |
| 295 | +
|
| 296 | + >>> snmpget("192.168.0.0/16", community="public") |
| 297 | + """ |
| 298 | + ans, _ = sr( |
| 299 | + IP(dst=dst) / UDP(sport=RandShort()) / SNMP( |
| 300 | + community=community, |
| 301 | + PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)]), |
| 302 | + ), |
| 303 | + timeout=2, |
| 304 | + chainCC=1, |
| 305 | + verbose=0, |
| 306 | + retry=2, |
| 307 | + ) |
| 308 | + for r in ans: |
| 309 | + if ICMP in r.answer: |
| 310 | + print(repr(r.answer)) |
| 311 | + return |
| 312 | + print("[%-10s] %-40s: %r" % ( |
| 313 | + r.query.dst, |
| 314 | + r.answer[SNMPvarbind].oid.val, |
| 315 | + r.answer[SNMPvarbind].value, |
| 316 | + )) |
| 317 | + |
| 318 | + |
290 | 319 | def snmpwalk(dst, oid="1", community="public"): |
| 320 | + """ |
| 321 | + SNMP walk |
| 322 | + """ |
291 | 323 | try: |
292 | 324 | while True: |
293 | | - r = sr1(IP(dst=dst) / UDP(sport=RandShort()) / SNMP(community=community, PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)])), timeout=2, chainCC=1, verbose=0, retry=2) # noqa: E501 |
| 325 | + r = sr1( |
| 326 | + IP(dst=dst) / UDP(sport=RandShort()) / SNMP( |
| 327 | + community=community, |
| 328 | + PDU=SNMPnext(varbindlist=[SNMPvarbind(oid=oid)]), |
| 329 | + ), |
| 330 | + timeout=2, |
| 331 | + chainCC=1, |
| 332 | + verbose=0, |
| 333 | + retry=2, |
| 334 | + ) |
294 | 335 | if r is None: |
295 | 336 | print("No answers") |
296 | 337 | break |
|
0 commit comments