# Netwide Assembler

## 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="/files/Dt6sMf54OzgvlaT9aTwU" 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="/files/2JTv3WRR1mqzvmfBAd8g" alt=""><figcaption><p>Hex2 formatting for C</p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://open-advanced-windows-exploitati.gitbook.io/open-advanced-windows-exploitation/custom-shellcode/shellcode-workflow/netwide-assembler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
