pwnypack.asm – (Dis)assembler

This module contains functions to assemble and disassemble code for a given target platform.

Currently, the only supported architecture is x86 (both 32 and 64 bits variants). Assembly is performed by the nasm assembler (only supports nasm syntax). Disassembly is performed by ndisasm (nasm syntax) or capstone (intel & att syntax).

class pwnypack.asm.AsmSyntax

Bases: enum.IntEnum

This enumeration is used to specify the assembler syntax.

pwnypack.asm.asm(code, addr=0, syntax=AsmSyntax.nasm, target=None)

Assemble statements into machine readable code.

Parameters:
  • code (str) – The statements to assemble.
  • addr (int) – The memory address where the code will run.
  • syntax (AsmSyntax) – The input assembler syntax.
  • target (Target) – The target architecture. The global target is used if this argument is None.
Returns:

The assembled machine code.

Return type:

bytes

Raises:
  • SyntaxError – If the assembler statements are invalid.
  • NotImplementedError – In an unsupported target platform is specified.

Example

>>> from pwny import *
>>> asm('''
...     pop rdi
...     ret
... ''', target=Target(arch=Target.Arch.x86, bits=64))
b'_\xc3'
pwnypack.asm.disasm(code, addr=0, syntax=AsmSyntax.nasm, target=None)

Disassemble machine readable code into human readable statements.

Parameters:
  • code (bytes) – The machine code that is to be disassembled.
  • addr (int) – The memory address of the code (used for relative references).
  • syntax (AsmSyntax) – The output assembler syntax.
  • target (Target) – The architecture for which the code was written. The global target is used if this argument is None.
Returns:

The disassembled machine code.

Return type:

list of str

Raises:
  • NotImplementedError – In an unsupported target platform is specified.
  • RuntimeError – If ndisasm encounters an error.

Example

>>> from pwny import *
>>> disasm(b'_\xc3', target=Target(arch=Target.Arch.x86, bits=64))
['pop rdi', 'ret']