Compile assembly directly to shellcode

For some reason, there’s no really easy way to compile assembly directly to shellcode. The closest thing I’ve found to convert binary to shellcode is this bash one-liner from commandlinefu that parses the output from objdump:

nasm2shell, as2shell and bin2shell convert NASM, GNU as and binary files directly to shellcode, respectively. Get the tarballĀ for all three here.

I put this together because I wasn’t able to find a way to convert NASM or GNU assembly directly to shellcode, and the process of manually compiling assembly and parsing the binary file into a c-escaped hex string was a waste of time between iterations. This package accelerates the process of creating shellcode from assembly files.

Using nasm2shell, you can write shellcode routines in NASM and parse them directly from the assembly source:

as2shell converts GNU assembly to shellcode:

bin2shell does the same thing for any raw binary file, in case you’re not using NASM or GAS:

To install, get the tarball, unzip it and type
make
. To install the files to your system, type
sudo make install
. Then the commands can be invoked from any directory on your system.

Convert binary to hex in C

nasm2shell and as2shell are wrappers that compile assembly to machine code and pass the resulting binary file to bin2shell. bin2shell converts binary to c-escaped shellcode with the following function (from bin2shell.c):

This seems to be a pretty sought-after method, so maybe this function will be helpful to someone else. Converting a char to its hex equivalent is as simple as
printf("\x%x", char);

Incoming search terms:

2 thoughts on “Compile assembly directly to shellcode

  1. Thanks Mark! I use an almost similar bash onliner but your tool provides a much cleaner and portable solution. Keep up the good work.
    BTW, do you have a GitHub repo?

    • Glad it helped. I have a seriously neglected github because most of my coding (recently) has been kind of proprietary, but there are some cool jquery widgets on there…

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">