Created
February 15, 2017 10:48
-
-
Save Arno0x/783c9d071e45c95a65f6d772642f10e0 to your computer and use it in GitHub Desktop.
This file contains 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/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment