> For the complete documentation index, see [llms.txt](https://open-advanced-windows-exploitati.gitbook.io/open-advanced-windows-exploitation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://open-advanced-windows-exploitati.gitbook.io/open-advanced-windows-exploitation/custom-shellcode/shellcode-workflow/netwide-assembler.md).

# Netwide Assembler

The Netwide Assembler (NASM) is a popular assembler program used for writing x86 and x86-64 assembly language code. It is known for its portability, flexibility, and compatibility.

## Download and Install

The NASM assembler will be used to compile our shellcode. The version I am using is 2.16.01 and can be downloaded from <https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/win64/>.

Download the 64-bit installer and run it.

Once the installer has completed make sure that the path for NASM has been added to the system path variable:

<div align="left"><figure><img src="https://1133556953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6fSnuRpvF32xyL2X75m5%2Fuploads%2F0s7WF77YHLVXjbcqoHMo%2Fpath_vars.png?alt=media&amp;token=823cf806-208f-43b1-acd8-63df060f0d41" alt="" width="563"><figcaption><p>System Path variable</p></figcaption></figure></div>

## Batch File

Now is a good time to show how I compile my assembly. I have created two Windows batch files. One to compile in to an executable, and one to compile to a raw binary file.

We can use the executable to debug shellcode in Windbg Preview. The batch file is shown below:

{% code title="compile.bat" lineNumbers="true" %}

```batch
del shellcode.obj
del shellcode.exe
nasm -f win64 shellcode.asm -o shellcode.obj
link /ENTRY:main /MACHINE:X64 /NODEFAULTLIB /SUBSYSTEM:CONSOLE shellcode.obj
```

{% endcode %}

The second batch file is used to compile shellcode into a raw `.bin` file. This is useful for writing shellcode that can be put in C exploit code:

{% code title="hex-encode.bat" lineNumbers="true" %}

```batch
nasm -f bin -o shellcode.bin shellcode.asm
Hex2.exe .\shellcode.bin
```

{% endcode %}

The sharp-eyed will notice that the batch file also runs an executable called `Hex2.exe`.

## Hex2

Like all other tools and workflows this is completely optional. There are plenty of hex viewers out there but I wrote my own in C#:

{% code title="Hex2.cs" lineNumbers="true" %}

```csharp
static void Main(string[] args)
{
    byte[] file = System.IO.File.ReadAllBytes(args[0]);

    StringBuilder sb = new StringBuilder();
    sb.Append("const unsigned char shellcode[] = {");

    string hex = BitConverter.ToString(file).Replace("-", ", 0x");
    hex = "0x" + hex;

    sb.Append(hex);
    sb.Append("};");

    Console.WriteLine(sb.ToString());
 }
```

{% endcode %}

This makes my shellcode workflow for writing exploits in C quick and clean.

The image below shows the output, which can easily be copied and pasted into C code:

<figure><img src="https://1133556953-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6fSnuRpvF32xyL2X75m5%2Fuploads%2FcKTnSKpSgce8iHyUpUqv%2Fencode-hex.png?alt=media&amp;token=d0c848b0-09b2-4e52-9cc8-130b24e32c68" alt=""><figcaption><p>Hex2 formatting for C</p></figcaption></figure>
