robpot891
10/9/2018 - 5:57 PM

formatConverter.py

#!/usr/bin/python
# -*- coding: utf8 -*-
#
# Author: Arno0x0x, Twitter: @Arno0x0x
#
import argparse

#======================================================================================================
#											HELPERS FUNCTIONS
#======================================================================================================

#------------------------------------------------------------------------
def chunks(s, n):
	for i in xrange(0, len(s), n):
		yield s[i:i+n]

#------------------------------------------------------------------------
def color(string, color=None):
    attr = []
    # bold
    attr.append('1')
    
    if color:
        if color.lower() == "red":
            attr.append('31')
        elif color.lower() == "green":
            attr.append('32')
        elif color.lower() == "blue":
            attr.append('34')
        return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

    else:
        if string.strip().startswith("[!]"):
            attr.append('31')
            return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
        elif string.strip().startswith("[+]"):
            attr.append('32')
            return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
        elif string.strip().startswith("[?]"):
            attr.append('33')
            return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
        elif string.strip().startswith("[*]"):
            attr.append('34')
            return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
        else:
            return string

		
#======================================================================================================
#											MAIN FUNCTION
#======================================================================================================
if __name__ == '__main__':

	hexFormater = {
		'vba': "&H",
		'cs': ",0x",
		'cpp': "\\x",
		'py': "\\x"
	}
	
	#------------------------------------------------------------------------
	# Parse arguments
	parser = argparse.ArgumentParser()
	parser.add_argument("filename", help="Name of the file to be converted")
	parser.add_argument("type", help="Output type", choices=['vba', 'cs', 'cpp', 'py'])
	args = parser.parse_args() 
	
	try:
		with open (args.filename) as fileHandle:
			fileBytes = bytearray(fileHandle.read())
			fileHandle.close()
			print color("[*] File [{}] successfully loaded".format(args.filename))
	except IOError:
		print color("[!] Could not open or read file [{}]".format(args.filename))
		quit()
	
	# Get the hex formater for the selected output type
	formater = hexFormater[args.type]
		
	# Convert the file bytes to the hex representation in the selected output type
	converted = formater	
	converted += formater.join(format(ord(b),'02x') for b in bytes(fileBytes))

	# Print the representation
	if args.type == 'vba':
		# VBA will not support retrieval of paragraph text in a variable if it's too large
		chunks = list(chunks(converted, 8000))
		for chunk in chunks:
			print chunk
	else:
		print converted