-
Notifications
You must be signed in to change notification settings - Fork 553
Expand file tree
/
Copy pathsend_notify.py
More file actions
executable file
·84 lines (73 loc) · 2.41 KB
/
send_notify.py
File metadata and controls
executable file
·84 lines (73 loc) · 2.41 KB
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
#!/usr/bin/python
"""
Minimalistic RFC 1996-like NOTIFY sender.
"""
import argparse
import ipaddress
import socket
import dns.flags
import dns.inet
import dns.message
import dns.name
import dns.opcode
import dns.query
import dns.rdataclass
import dns.rdatatype
import dns.rrset
def main():
"""Also prints all inputs and intermediate values"""
parser = argparse.ArgumentParser(
description="Send DNS NOTIFY mesage via UDP, optionally with synthetized SOA RR "
"in ANSWER section. No checks. It's not RFC 1996 sect 3.6 compliant sender."
)
parser.add_argument("--source", type=ipaddress.ip_address, help="source IP address")
parser.add_argument("--port", type=int, help="target port", default=53)
parser.add_argument("target", type=ipaddress.ip_address, help="target IP address")
parser.add_argument("zone", type=dns.name.from_text)
parser.add_argument(
"serial",
nargs="?",
type=int,
help="optional serial - adds SOA RR into ANSWER section",
)
parser.add_argument(
"--rdclass",
default=dns.rdataclass.IN,
type=dns.rdataclass.from_text,
help="DNS class, defaults to IN",
)
args = parser.parse_args()
if args.source:
if dns.inet.af_for_address(str(args.target)) != dns.inet.af_for_address(
str(args.source)
):
parser.error("address family for source and target must be the same")
print(args)
msg = construct_msg(args)
print(msg)
udp_send(msg, args)
def construct_msg(args):
"""if args.serial is specified it creates fake SOA RR with given serial"""
msg = dns.message.make_query(
args.zone, dns.rdatatype.SOA, rdclass=args.rdclass, flags=dns.flags.AA
)
msg.set_opcode(dns.opcode.NOTIFY)
if args.serial:
soa = dns.rrset.from_text_list(
name=args.zone,
ttl=0,
rdclass=args.rdclass,
rdtype=dns.rdatatype.SOA,
text_rdatas=[f". . {args.serial} 0 0 0 0"],
)
msg.answer.append(soa)
return msg
def udp_send(msg, args):
"""ignores checks prescribed by RFC 1996 sect 3.6"""
afam = dns.inet.af_for_address(str(args.target))
sock = socket.socket(afam, socket.SOCK_DGRAM)
if args.source:
sock.bind((str(args.source), 0))
dns.query.send_udp(sock, what=msg, destination=(str(args.target), args.port))
if __name__ == "__main__":
main()