dnfile

0.18.0 · active · verified Wed Apr 15

dnfile is a Python library designed to parse .NET executable files. It aims to parse as much as possible, even if the file is partially malformed, and provides an easy-to-use, object-oriented API developed with IDE autocompletion in mind. The current version is 0.18.0, with a release cadence of several updates per year.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load and parse a .NET executable file using `dnfile`. It shows how to access the CLR header and iterate through some basic metadata streams. For a real use case, `filepath` should point to an actual .NET executable.

import sys
import dnfile

# For demonstration, create a dummy file or use a path to a real .NET exe
# Replace 'dummy.exe' with a path to an actual .NET executable for real parsing
# For a runnable quickstart, we'll simulate a file existence check.
filepath = "path/to/your/dotnet_executable.exe" # Placeholder

try:
    # In a real scenario, you'd ensure filepath points to a valid file.
    # For this example, we'll just demonstrate the parsing attempt.
    # A real application would handle FileNotFoundError or similar.
    pe = dnfile.dnPE(filepath)
    
    # Check if the .NET header exists
    if hasattr(pe, "net") and pe.net:
        print(f"Successfully parsed .NET executable: {filepath}")
        print(f"CLR Runtime Header Version: {pe.net.struct.MajorRuntimeVersion}.{pe.net.struct.MinorRuntimeVersion}")
        
        # Accessing metadata tables (example)
        if hasattr(pe.net, "mdtables") and pe.net.mdtables:
            print(f"Number of Metadata Tables: {len(pe.net.mdtables.tables_list)}")
            # Example: Iterate through some streams
            for s in pe.net.metadata.streams_list:
                print(f"  Stream Name: {s.name}, Size: {s.size}")
    else:
        print(f"File {filepath} does not appear to be a .NET executable or parsing failed.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →