Skip to content

Instantly share code, notes, and snippets.

@BrunoPujos
Created February 27, 2020 17:29
Show Gist options
  • Save BrunoPujos/b29baeeec3cfd8cfb17f7ced626c8bb4 to your computer and use it in GitHub Desktop.
Save BrunoPujos/b29baeeec3cfd8cfb17f7ced626c8bb4 to your computer and use it in GitHub Desktop.
#! /usr/bin/python
## GENERAL
def myhex(val):
h = hex(val)
if h[-1] == "L":
return h[:-1]
return h
## NASM
def nasm(code, path_nasm = 'nasm'):
""" Assemble code using nasm
>>> nasm("int 0x80")
'\xcd\x80'
"""
import subprocess
import tempfile
import os
input_file = tempfile.NamedTemporaryFile(delete=False)
output_file = tempfile.NamedTemporaryFile(delete=False)
input_file.write(code)
input_file.close()
p = subprocess.Popen([path_nasm,
'-fbin',
'-o',
output_file.name,
input_file.name])
p.communicate()
p.wait()
s = output_file.read()
output_file.close()
try:
os.remove(input_file.name)
os.remove(output_file.name)
except Exception:
pass
return s
if __name__ == '__main__':
import sys
if len(sys.argv) < 2:
s = sys.stdin.read()
else:
s = " ".join(sys.argv[1:])
res = nasm("[BITS 64]\n" + s)
print("repr: {}".format(repr(res)))
print("hex: {}".format(res.encode("hex")))
print("len: 0x{:X}".format(len(res)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment