-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy-scan-dns-openresolvers.py
executable file
·78 lines (54 loc) · 2.22 KB
/
py-scan-dns-openresolvers.py
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
#!/usr/bin/env python3
from sys import argv, exit as callexit
from time import time
from ipaddress import ip_network
from random import shuffle
import socket as sk
"""
--- Python DNS Open Resolver Scanner ---
Originally written in 2013 in Python2. Refactored in 2021 with Python3.
It still uses Pure Python and the Low-level Python networking interface.
PEP8 compliant
“Readability counts."
“Beautiful is better than ugly.”
— The Zen of Python
"""
''' set packet to use a BSD Socket UDP datagram '''
PAYLOADHEX = 'ff0001000001000000000000016c0c726f6f\
742d73657276657273036e65740000010001' # "l.root-servers.net"
PREFIX = argv[1] if len(argv) > 1 else 0
def iplist():
try: # certify the IP Address format is valid
IP_ADDR_COUNT = ip_network(PREFIX).num_addresses
NETWORK_HOSTS = ip_network(PREFIX).hosts()
if PREFIX == 0:
callexit("Provide a HOST IP Address or an IP/CIDR as argument.")
elif IP_ADDR_COUNT > 1:
for IPADDRESS in NETWORK_HOSTS:
yield IPADDRESS
elif IP_ADDR_COUNT == 1:
if '/32' in PREFIX: # ex: 8.8.8.8/32 will become '8.8.8.8'
yield PREFIX.split('/')[0]
else:
yield PREFIX
except ValueError as error:
print(f'iplist() function Error >>> {error}')
for HOST in iplist():
try:
with sk.socket(sk.AF_INET, sk.SOCK_DGRAM) as packet:
packet.settimeout(0.100)
''' send the 'PAYLOADHEX' to HOSTs in the list (port 53 (DNS)) '''
packet.sendto(bytes.fromhex(PAYLOADHEX), (str(HOST), int(53)))
RESPONSE = packet.recv(128)
''' check if RESPONSE contains a valid DNS RESPONSE with
l.root-servers.net's IP address 199.7.83.42 '''
if (b'\xc0\x0c\x00\x01\x00\x01' and
b'\x00\x04\xc7\x07') in RESPONSE:
UNIXTIME = int(time())
print(f'{HOST},open,{UNIXTIME}')
except sk.timeout: # timeout generated by network conditions
continue
except ValueError as error:
print(f'Error >>> {error}')
except KeyboardInterrupt:
callexit('Program execution interrupted.')