dncil - CIL Disassembler

1.0.2 · active · verified Thu Apr 16

dncil is an open-source Python library developed by the FLARE team to disassemble Common Intermediate Language (CIL) instructions. It supports parsing the header, instructions, and exception handlers of .NET managed methods, exposing the data through an object-oriented API. The library is currently at version 1.0.2 and receives minor bug fixes and improvements with each release.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to disassemble a raw byte stream representing a CIL method body. It initializes a `CilMethodBodyReader` with the CIL bytes and then parses a `CilMethodBody` to iterate through its instructions. For parsing CIL from actual .NET executables, the `dnfile` library is commonly used to extract the raw method bytes first.

from dncil.cil.body import CilMethodBody
from dncil.cil.body.reader import CilMethodBodyReader
from dncil.cil.enums import InstructionPrefix, OpCode

# Example CIL bytes for a simple method that returns 5
# (e.g., ldarg.0, ret or ldc.i4.5, ret)
# This is a simplified example; real CIL bytes often come from a PE file.
# For a full .NET executable, you would typically use `dnfile` to extract method bytes.
simple_cil_bytes = bytes([
    OpCode.LDC_I4_S.value, 0x05, # ldc.i4.s 5
    OpCode.RET.value             # ret
])

# Create a reader for the CIL bytes
reader = CilMethodBodyReader(simple_cil_bytes, 0)

# Parse the method body
try:
    method_body = CilMethodBody(reader)
    print(f"Method has {len(method_body.instructions)} instructions:")
    for i, insn in enumerate(method_body.instructions):
        print(f"  {i:04X} {hex(insn.offset):<8} {insn.opcode.name:<15} {insn.operand}")
except Exception as e:
    print(f"Error disassembling CIL: {e}")

view raw JSON →