NASM

Le Netwide Assembler, un assembleur x86/x86-64 largement utilisé pour écrire du shellcode à la main et construire des binaires PoC lors du développement d'exploits. The Netwide Assembler, an x86/x86-64 assembler widely used to hand-craft shellcode and build PoC binaries during exploit development.

↗ https://www.nasm.us

Overview

NASM (Netwide Assembler) is a portable, well-documented x86/x86-64 assembler with clean Intel syntax. Exploit developers reach for it to hand-write and iterate on shellcode, assemble small test harnesses for a target instruction sequence, and understand exactly what bytes an opcode sequence produces before embedding it in a payload.

Common Usage

Assemble a .asm source into an ELF object file

nasm -f elf64 shellcode.asm -o shellcode.o

Link into a standalone executable

ld shellcode.o -o shellcode

Assemble to raw flat binary (no headers) — ideal for extracting shellcode bytes

nasm -f bin shellcode.asm -o shellcode.bin

32-bit target

nasm -f elf32 shellcode.asm -o shellcode.o

Windows PE object

nasm -f win64 payload.asm -o payload.obj

Extracting Shellcode Bytes

# Assemble to flat binary, then dump as a C-style byte string
nasm -f bin shellcode.asm -o shellcode.bin
xxd -i shellcode.bin

Or with objdump for a disassembly sanity-check

objdump -D -b binary -m i386:x86-64 -M intel shellcode.bin

Tips

  • Use -f bin when the goal is a pure opcode stream (shellcode) with no ELF/PE overhead.
  • Pair with objdump -d or a disassembler to verify the exact bytes generated — subtle encoding differences (e.g. short vs near jumps) can break offset-sensitive shellcode.
  • %include and macros make NASM convenient for building reusable shellcode stubs (e.g. egghunters, decoders).
Help / Man page
usage: nasm [-@ response-file] [-o outfile] [-f format] filename
            [options...]

    -f format       select output file format
    -o outfile      write output to file outfile
    -l listfile     write listing to file listfile
    -I path         add a path to the include file search path
    -O               enable optimization (e.g. -Ox for max)
    -D macro[=value] pre-define a macro
    -U macro         undefine a macro
    -w[+-]warning    enable/disable warning class
    -g               generate debugging information
    -v               print version and exit

Available output formats (-f):
    bin    Flat-form binary files (e.g. DOS .COM, .SYS)
    elf32  Linux ELF32 object files
    elf64  Linux ELF64 object files
    win32  Microsoft Win32 object files
    win64  Microsoft Win64 object files
    macho64 MacOS X 64-bit object files

Vue d’ensemble

NASM (Netwide Assembler) est un assembleur x86/x86-64 portable, bien documenté, avec une syntaxe Intel claire. Les développeurs d’exploits l’utilisent pour écrire et itérer à la main sur du shellcode, assembler de petits harnais de test pour une séquence d’instructions cible, et comprendre exactement quels octets une séquence d’opcodes produit avant de l’intégrer dans un payload.

Utilisation courante

# Assembler un source .asm en fichier objet ELF
nasm -f elf64 shellcode.asm -o shellcode.o

# Lier en exécutable autonome
ld shellcode.o -o shellcode

# Assembler en binaire brut (flat, sans en-têtes) : idéal pour extraire les octets du shellcode
nasm -f bin shellcode.asm -o shellcode.bin

# Cible 32 bits
nasm -f elf32 shellcode.asm -o shellcode.o

# Objet PE Windows
nasm -f win64 payload.asm -o payload.obj

Extraction des octets du shellcode

# Assembler en binaire brut, puis extraire sous forme de chaîne d'octets style C
nasm -f bin shellcode.asm -o shellcode.bin
xxd -i shellcode.bin

# Ou avec objdump pour une vérification de désassemblage
objdump -D -b binary -m i386:x86-64 -M intel shellcode.bin

Conseils

  • Utilisez -f bin quand l’objectif est un flux d’opcodes pur (shellcode) sans surcharge ELF/PE.
  • Associez avec objdump -d ou un désassembleur pour vérifier les octets exacts générés : de subtiles différences d’encodage (par exemple sauts courts vs proches) peuvent casser un shellcode sensible aux offsets.
  • %include et les macros rendent NASM pratique pour construire des stubs de shellcode réutilisables (par exemple egghunters, décodeurs).
Aide / Page de manuel
usage: nasm [-@ response-file] [-o outfile] [-f format] filename
            [options...]

    -f format       select output file format
    -o outfile      write output to file outfile
    -l listfile     write listing to file listfile
    -I path         add a path to the include file search path
    -O               enable optimization (e.g. -Ox for max)
    -D macro[=value] pre-define a macro
    -U macro         undefine a macro
    -w[+-]warning    enable/disable warning class
    -g               generate debugging information
    -v               print version and exit

Available output formats (-f):
    bin    Flat-form binary files (e.g. DOS .COM, .SYS)
    elf32  Linux ELF32 object files
    elf64  Linux ELF64 object files
    win32  Microsoft Win32 object files
    win64  Microsoft Win64 object files
    macho64 MacOS X 64-bit object files