Open Advanced Windows Exploitation
  • Introduction
    • Welcome
      • Subscribe
      • Contents
      • Intended Audience
      • Required Software and Tools
      • Thank You and Support
  • Custom Shellcode
    • 64-bit Architecture
      • 64-bit Enhancements
      • Calling Conventions
    • Shellcode Workflow
      • Visual Studio Code
      • Netwide Assembler
      • Windbg Preview
      • Workflow
    • Writing Shellcode
      • NULL-Free Position-Independent Shellcode
      • Finding kernel32.dll
      • Resolving Symbols
      • Finding VMAs
      • MessageBox Shellcode
      • Avoiding NULL
      • GetLastError
    • Reverse Shell
      • Exercise
      • Solution
  • Exploit Mitigations
    • Understanding the Battlefield
      • Memory Corruption
      • Vulnerability Primitives
      • Overview of Mitigations
    • Our Old Foes
      • DEP
      • ASLR
  • Memory Management
    • Memory Basics
      • Pages
      • Shared Memory
    • Memory Management
      • The Stack
      • The Heap
        • Heap Grooming and Overflow
        • Virtual Functions in C++
        • The Heap Continued
        • Kernel Mode Heap
      • Managed Memory
  • The Kernel
    • Kernel Basics
      • Kernel Structures
      • Kernel Debugging Options
      • Navigating the Kernel
      • Analysing the Kernel
    • Access Tokens
      • Access Token Basics
      • Token Theft
  • Drivers
    • Driver Basics
      • Implementing a Driver
      • Reversing Our Driver
      • A Basic User Mode Application
  • First Kernel Exploit
    • A Kernel Exploit
      • CVE 2020-17382
      • IDA Free
      • Writing A Basic Fuzzer
      • Controlling RIP
      • Meet SMEP
      • ROP to the Rescue
      • kASLR
      • Priv Esc Shellcode
    • Exploit Code
  • References
    • References
Powered by GitBook
On this page
  • Download and Install
  • Batch File
  • Hex2
  1. Custom Shellcode
  2. Shellcode Workflow

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.

PreviousVisual Studio CodeNextWindbg Preview

Last updated 1 year ago

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 .

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:

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:

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

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:

hex-encode.bat
nasm -f bin -o shellcode.bin shellcode.asm
Hex2.exe .\shellcode.bin

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#:

Hex2.cs
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());
 }

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:

https://www.nasm.us/pub/nasm/releasebuilds/2.16.01/win64/
System Path variable
Hex2 formatting for C