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
|
#!/usr/bin/env python3
## \file
# \brief CNC TXT REC set up script
# Copyright (c) 2019-2022 David Timber <dxdt@dev.snart.me>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
import base64
import prne_txtrec
import getopt
# Error definitions
HOOK_ERRORS = {
"NOT_IMPL": {
"msg": "not implemented",
"ec": 1
},
"AWS_NO_BOTO3": {
"msg": "Please install Boto3 for AWS hook",
"ec": 1
},
"INV_ARG": {
"msg": "invalid argument",
"ec": 2
}
}
BYTES_PER_RR = 189
def mktxtrr (cnt: int, suffix: str):
return "%08u" % (cnt) + suffix
def main_aws (param: dict):
"""AWS hook main function"""
zone_id = param["zone_id"];
if zone_id is None:
sys.stderr.writelines([
"--zone-id required.\n",
"Run '{0} --help' for help.\n".format(sys.argv[0])
])
exit(2)
head_rec = param["head_rec"]
suffix = param["suffix"]
ttl = param["ttl"]
if ttl is None: ttl = 3600
try:
import boto3
except ModuleNotFoundError as e:
prne_txtrec.handle_err(HOOK_ERRORS["AWS_NO_BOTO3"], e)
client = boto3.client("route53")
ins_q = [] # List of RRs to be inserted
cnt = 0
# process the queued request and clear the queue
def flush_q ():
prne_txtrec.change_all(client, zone_id, 'UPSERT', ins_q)
ins_q.clear()
while True:
b = sys.stdin.buffer.read(BYTES_PER_RR)
if not b: # Assume that EOF is reached
break
ins_q.append({
'Name': mktxtrr(cnt, suffix),
'Type': 'TXT',
'TTL': ttl,
'ResourceRecords': [
{ 'Value': '"' + base64.b64encode(b).decode('ascii') + '"' }
]
})
cnt += 1
if len(ins_q) >= prne_txtrec.AWS_MAX_ITEMS:
flush_q()
flush_q()
head_rr = mktxtrr(cnt, suffix)
# insert the head rec
prne_txtrec.change_all(
client,
zone_id,
'UPSERT',
[{
'Name': head_rec,
'Type': 'TXT',
'TTL': ttl,
'ResourceRecords': [
{ 'Value': '"' + head_rr + '"' }
]
}])
def main_dnsmasq (param: dict):
head_rec = param["head_rec"]
suffix = param["suffix"]
ttl = param["ttl"]
if ttl is None: ttl_str = ""
else: ttl_str = ",%us" % ttl
cnt = 0
while True:
b = sys.stdin.buffer.read(BYTES_PER_RR)
if not b: # EOF
break
name = mktxtrr(cnt, suffix)
l = '''txt-record={name},"{val}"{ttl}'''.format(
name = name,
val = base64.b64encode(b).decode('ascii'),
ttl = ttl_str
)
cnt += 1
print(l)
head_rr = mktxtrr(cnt, suffix)
l = '''txt-record={name},"{val}"{ttl}'''.format(
name = head_rec,
val = head_rr,
ttl = ttl_str
)
print(l)
HOOKS = {
"aws": main_aws,
"dnsmasq": main_dnsmasq
}
USAGE_STR = '''Upload or output Proone CNC TXT DNS records
Usage: {arg0} <options>
Options:
-h, --help print this message and exit normally
-V, --version print version info and exit normally
--hook=<str> (required) use the hook. See below for available hooks
--head=<str> (required) set the name of the header CNC TXT record
--suffix=<str> (required) set the suffix of the data CNC TXT record(s)
--zond-id=<str> set the zone id. Required for AWS hook
--ttl=<uint> specify TTL of the records
Hooks:
{hooks}
'''.format(
arg0 = sys.argv[0],
hooks = " ".join(k + "\n" for k in HOOKS.keys())
)
def print_usage (out):
out.write(USAGE_STR)
opts, args = getopt.getopt(
sys.argv[1:],
"hV",
[
"help",
"version",
"hook=",
"head=",
"suffix=",
"zone-id=",
"ttl="
])
opts = dict(opts)
if set(opts.keys()).intersection(set([ "--help", "-h", "--version", "-V" ])):
if "--version" in opts or "-V" in opts:
print("prne-txtrec version: " + prne_txtrec.VERSION)
if "--help" in opts or "-h" in opts:
print_usage(sys.stdout)
exit(0)
# process argv
try:
ARGV_DICT = {}
ARGV_DICT["hook"] = opts["--hook"]
ARGV_DICT["head_rec"] = opts["--head"]
ARGV_DICT["suffix"] = opts["--suffix"]
ARGV_DICT["zone_id"] = opts.get("--zone-id")
ARGV_DICT["ttl"] = opts.get("--ttl")
if ARGV_DICT["ttl"]:
ARGV_DICT["ttl"] = int(ARGV_DICT["ttl"])
if ARGV_DICT["ttl"] not in range(0, 2147483648):
prne_txtrec.handle_err(
HOOK_ERRORS["INV_ARG"],
None,
ARGV_DICT["ttl"])
except KeyError as e:
sys.stderr.writelines([
e.args[0] + " option required.\n",
"Run '{0} --help' for help.\n".format(sys.argv[0])
])
exit(2)
# call the function
try:
HOOKS[ARGV_DICT["hook"]](ARGV_DICT)
except KeyError:
prne_txtrec.handle_err(HOOK_ERRORS["NOT_IMPL"], None, ARGV_DICT["hook"])
exit(0)
|