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.
pip install dnfileVerified import paths — ran on the pinned version, not inferred.
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.
Ensure your project environment uses Python 3.8 or a newer compatible version.
If you were expecting `bytes`, you'll now need to access the `.value` attribute of the returned `HeapItem` (e.g., `item.value`). Be aware that `HeapItemString.value` can be `None` if decoding fails.
When accessing specific fields, be prepared for attributes to be `None` or use the `.struct` attribute (e.g., `pe.net.struct.MajorRuntimeVersion`) to access raw structure values directly. Validate attribute existence and non-`None` values before use.
Always check for the existence and validity of attributes (e.g., `if hasattr(pe, 'net') and pe.net:`) after parsing, especially when dealing with untrusted or potentially malformed input files.
Install the library using pip: `pip install dnfile`
Always check if the `dnPE` object is `None` before attempting to access its attributes: `import dnfile; pe = dnfile.dnPE('path/to/your/file.exe'); if pe and hasattr(pe, 'net'): print(pe.net.struct) else: print('File is not a valid .NET PE or could not be parsed.')`Ensure the input file is a valid .NET executable. While `dnfile` attempts to parse malformed files, extreme corruption can lead to parsing errors. Consider using a `try-except ValueError` block to handle such cases gracefully: `try: pe = dnfile.dnPE('path/to/malformed.exe') # ... process pe object ... except ValueError as e: print(f'Error parsing file: {e}')`