Last active
May 10, 2020 03:28
-
-
Save blha303/b751c5fed24f7cfdb206e220023fe053 to your computer and use it in GitHub Desktop.
Encoding and decoding files with txt records
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# txtrecord_decode.py [-h] output.png logo.example.com | |
import dns.resolver # pip3 install dnspython | |
import base64 | |
import sys | |
from argparse import ArgumentParser | |
def parse_records(name): | |
_next = "" | |
print("{}".format(name), file=sys.stderr) | |
q = dns.resolver.query(name,"TXT").response.answer[0] | |
out = b"" | |
for line in sorted(_.strings[0] for _ in q): | |
if chr(line[0]) == "{": | |
if chr(line[1]) == "n": | |
_next = line.split(b"}",1)[1].decode("utf-8") | |
continue | |
try: | |
out += line.split(b"}",1)[1] | |
except IndexError: | |
pass | |
return out, _next | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument("output", help="Output filename (or - for stdout)") | |
parser.add_argument("domain", help="Target domain") | |
args = parser.parse_args() | |
name = args.domain | |
out = b"" | |
while True: | |
next_out, name = parse_records(name) | |
out += next_out | |
if not name: | |
break | |
if args.output == "-": | |
sys.stdout.buffer.write(base64.b64decode(out)) | |
sys.stdout.flush() | |
else: | |
with open(args.output, "wb") as f: | |
f.write(base64.b64decode(out)) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# txtrecord_encode.py [-h] [--format '{0} IN TXT "{1}"'] input.png logo.example.com | |
import base64 | |
import sys | |
from argparse import ArgumentParser | |
from textwrap import wrap | |
def main(): | |
parser = ArgumentParser() | |
parser.add_argument("input", help="Input filename (or - for stdin)") | |
parser.add_argument("domain", help="Target domain") | |
parser.add_argument("--format", help="DNS record format, where {0} is the target domain (without trailing dot), and {1} is the record text (defaults to bind/named \"{0}. IN TXT '{1}'\")", default="{0}. IN TXT '{1}'") | |
args = parser.parse_args() | |
if args.input == "-": | |
inp = sys.stdin.buffer.read() | |
else: | |
with open(args.input,"rb") as f: | |
inp = f.read() | |
enc = base64.b64encode(inp).decode("utf-8") | |
tot = len(enc) | |
tmp = 0 | |
for n,block in enumerate(wrap(enc,65000)): | |
tmp += len(block) | |
print("\n".join(args.format.format((str(n) if n else "") + sys.argv[2], "{}{}".format("{%03d}" % _n, s)) for _n, s in enumerate(wrap(block,249)))) | |
if tmp != tot: | |
print(args.format.format((str(n) if n else "") + sys.argv[2], "{}{}".format("{nxt}", str(n+1) + sys.argv[2] + "."))) | |
return 0 | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment