#!/usr/bin/python
import sys
import os
import time
import subprocess
from struct import *

args = {}
obj = {}
asm = {}
version = "1.3"
is_shellcode = False

obj["32"] = "\x7f\x45\x4c\x46\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x34\x00\x00\x00\x00\x00\x28\x00\x05\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x10\x01\x00\x00[SIZE]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x01\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x50\x01\x00\x00\x40\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x10\x00\x00\x00\x19\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90\x01\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00[OPCODE]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2e\x74\x65\x78\x74\x00\x2e\x73\x68\x73\x74\x72\x74\x61\x62\x00\x2e\x73\x79\x6d\x74\x61\x62\x00\x2e\x73\x74\x72\x74\x61\x62\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\xf1\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x01\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x01\x00\x00\x61\x73\x6d\x33\x32\x2e\x61\x73\x6d\x00\x5f\x73\x74\x61\x72\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
obj["64"] = "\x7f\x45\x4c\x46\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x3e\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x40\x00\x00\x00\x00\x00\x40\x00\x05\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00\x00\x00[SIZE]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x90\x01\x00\x00\x00\x00\x00\x00\x21\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x01\x00\x00\x00\x00\x00\x00\x60\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x18\x00\x00\x00\x00\x00\x00\x00\x19\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x02\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00[OPCODE]\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x2e\x74\x65\x78\x74\x00\x2e\x73\x68\x73\x74\x72\x74\x61\x62\x00\x2e\x73\x79\x6d\x74\x61\x62\x00\x2e\x73\x74\x72\x74\x61\x62\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x04\x00\xf1\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x61\x73\x6d\x33\x32\x2e\x61\x73\x6d\x00\x5f\x73\x74\x61\x72\x74\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
asm["32"] = "BITS 32\nglobal _start\nsection .text\n_global:\n"
asm["64"] = "BITS 64\nglobal _start\nsection .text\n_global:\n"

def write_file(filename, code):
	open(filename, "w+").writelines(code)

def format_output(buffer, delimiter):
	try:	
		print "\nASSEMBLY OUTPUT _____________________________________"
		buffer = "".join(buffer).split(delimiter)[1]	
	except:
		pass
	try:
		buffer = buffer.split("BFD:")[0]
	except:
		pass	
	if not len(buffer) <= 2:
		if is_shellcode:
			output = ""
			for item in buffer.split("\n"):
				try:
					tmp = item.split("\t")[1].replace(" ", "")
					t = iter(tmp)
					output += "\\x" + "\\x".join(a + b for a,b in zip(t, t))
				except:
					pass
			buffer = output + "\n\nShellcode length is (" + str(len(output) / 4) + ") bytes\n"
		print buffer

def display_error(message):
	print "Error: " + message
	sys.exit(0)

if __name__ == "__main__":
	print "\nOpAsm Tools v" + version + " / Mr.Un1k0d3r RingZer0 Team\n"

	if not len(sys.argv) >= 4:
		print "Usage: %s [32/64] [opcode/asm] [data] [optional]\n" % sys.argv[0]
		print "\t32/64 choose between 32 or 64 bits" 
		print "\topcode/asm choose between asm to opcode or opcode to asm"
		print "\tdata your opcode/asm code"
		print "\tusable-shellcode convert asm to opcode and format the output as usable shellcode\n"
		print "\tNote*** You can specified multiple asm line by using \";\" as line delimiter\n"
		
		sys.exit(0)

	# check if nasm is available
	if not os.path.exists("/usr/bin/nasm"):
		print "\033[1;31mnasm is missing. Please install: sudo apt-get install nasm\033[1;m\n"
		exit(0)

	if len(sys.argv) > 4:
		if "usable-shellcode" in sys.argv:
			is_shellcode = True
			print "\033[1;31mShellcode mode was set.\033[1;m\n"		

	args["bits"] = "32"
	if sys.argv[1] == "64":
		args["bits"] = "64"

	if sys.argv[2] == "asm":
		args["data"] = sys.argv[3]
		filename = "asm." + str(time.time())
		write_file(filename, asm[args["bits"]] + args["data"].replace(";", "\n"))
		os.system("nasm -f elf" + args["bits"] + " " + filename + " -o " + filename + ".o")
		c = subprocess.Popen(["objdump", "-D", filename + ".o", "-M", "intel"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		c = c.communicate()

		try:
			os.remove(filename)
			os.remove(filename + ".o")
		except:
			pass	
		format_output(c, "<_global>:\n")	
	else:
		try:
			args["data"] = sys.argv[3].replace("\\x", "").decode("hex")
		except:
			display_error("Invalid opcode string.")
		filename = "opcode." + str(time.time())
		write_file(filename, obj[args["bits"]].replace("[OPCODE]", args["data"]).replace("[SIZE]", str(pack("h", len(args["data"][::-1])))))
		c = subprocess.Popen(["objdump", "-D", filename, "-M", "intel"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
		c = c.communicate()
		try:
			os.remove(filename)
			os.remove(filename + ".o")
		except:
			pass
		format_output(c, ">:\n")

