Show / Hide Table of Contents

Class PeFile

This class represents a Portable Executable (PE) file and makes the different header and properties accessible.

Inheritance
object
PeFile
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: PeNet
Assembly: PeNet.dll
Syntax
public class PeFile

Constructors

| Edit this page View Source

PeFile(IRawFile)

Declaration
public PeFile(IRawFile peFile)
Parameters
Type Name Description
IRawFile peFile
| Edit this page View Source

PeFile(byte[])

Create a new PeFile object.

Declaration
public PeFile(byte[] buff)
Parameters
Type Name Description
byte[] buff

A PE file a byte array.

| Edit this page View Source

PeFile(Stream)

Create a new PeFile object.

Declaration
public PeFile(Stream peFile)
Parameters
Type Name Description
Stream peFile

Stream containing a PE file.

| Edit this page View Source

PeFile(string)

Create a new PeFile object.

Declaration
public PeFile(string peFile)
Parameters
Type Name Description
string peFile

Path to a PE file.

Properties

| Edit this page View Source

AuthenticodeInfo

Information about a possible Authenticode binary signature.

Declaration
public AuthenticodeInfo? AuthenticodeInfo { get; }
Property Value
Type Description
AuthenticodeInfo
| Edit this page View Source

ClrComTypeLibId

The COM TypeLib ID of the assembly, if specified, and if the PE is a CLR assembly.

Declaration
public Guid? ClrComTypeLibId { get; }
Property Value
Type Description
Guid?
| Edit this page View Source

ClrModuleVersionIds

The Version ID of each module if the PE is a CLR assembly.

Declaration
public List<Guid>? ClrModuleVersionIds { get; }
Property Value
Type Description
List<Guid>
| Edit this page View Source

DelayImportedFunctions

Access the delay imported functions as an array of parsed objects.

Declaration
public ImportFunction[]? DelayImportedFunctions { get; }
Property Value
Type Description
ImportFunction[]
| Edit this page View Source

ExceptionDirectory

Access the array of RuntimeFunction from the Exception header.

Declaration
public RuntimeFunction[]? ExceptionDirectory { get; }
Property Value
Type Description
RuntimeFunction[]
| Edit this page View Source

ExportedFunctions

Access the exported functions as an array of parsed objects.

Declaration
public ExportFunction[]? ExportedFunctions { get; }
Property Value
Type Description
ExportFunction[]
| Edit this page View Source

FileSize

Returns the file size in bytes.

Declaration
public long FileSize { get; }
Property Value
Type Description
long
| Edit this page View Source

HasValidAuthenticodeSignature

Returns true if the PE file signature is valid. It does not check if the signature is trusted based on the installed certificates on the system.

Declaration
public bool HasValidAuthenticodeSignature { get; }
Property Value
Type Description
bool
| Edit this page View Source

ImageBoundImportDescriptor

Access the IMAGE_BOUND_IMPORT_DESCRIPTOR form the data directory.

Declaration
public ImageBoundImportDescriptor? ImageBoundImportDescriptor { get; }
Property Value
Type Description
ImageBoundImportDescriptor
| Edit this page View Source

ImageComDescriptor

Access the ImageCor20Header (COM Descriptor/CLI) from the data directory.

Declaration
public ImageCor20Header? ImageComDescriptor { get; }
Property Value
Type Description
ImageCor20Header
| Edit this page View Source

ImageDebugDirectory

Access the ImageDebugDirectory of the PE file.

Declaration
public ImageDebugDirectory[]? ImageDebugDirectory { get; }
Property Value
Type Description
ImageDebugDirectory[]
| Edit this page View Source

ImageDelayImportDescriptors

Access the ImageDelayImportDirectory from the data directory.

Declaration
public ImageDelayImportDescriptor[]? ImageDelayImportDescriptors { get; }
Property Value
Type Description
ImageDelayImportDescriptor[]
| Edit this page View Source

ImageDosHeader

Access the ImageDosHeader of the PE file.

Declaration
public ImageDosHeader? ImageDosHeader { get; }
Property Value
Type Description
ImageDosHeader
| Edit this page View Source

ImageExportDirectory

Access the ImageExportDirectory of the PE file.

Declaration
public ImageExportDirectory? ImageExportDirectory { get; }
Property Value
Type Description
ImageExportDirectory
| Edit this page View Source

ImageImportDescriptors

Access the ImageImportDescriptor array of the PE file.

Declaration
public ImageImportDescriptor[]? ImageImportDescriptors { get; }
Property Value
Type Description
ImageImportDescriptor[]
| Edit this page View Source

ImageLoadConfigDirectory

Access the ImageLoadConfigDirectory from the data directory.

Declaration
public ImageLoadConfigDirectory? ImageLoadConfigDirectory { get; }
Property Value
Type Description
ImageLoadConfigDirectory
| Edit this page View Source

ImageNtHeaders

Access the ImageNtHeaders of the PE file.

Declaration
public ImageNtHeaders? ImageNtHeaders { get; }
Property Value
Type Description
ImageNtHeaders
| Edit this page View Source

ImageRelocationDirectory

Access the ImageBaseRelocation array of the PE file.

Declaration
public ImageBaseRelocation[]? ImageRelocationDirectory { get; }
Property Value
Type Description
ImageBaseRelocation[]
| Edit this page View Source

ImageResourceDirectory

Access the ImageResourceDirectory of the PE file.

Declaration
public ImageResourceDirectory? ImageResourceDirectory { get; }
Property Value
Type Description
ImageResourceDirectory
| Edit this page View Source

ImageSectionHeaders

Access the ImageSectionHeader of the PE file.

Declaration
public ImageSectionHeader[]? ImageSectionHeaders { get; }
Property Value
Type Description
ImageSectionHeader[]
| Edit this page View Source

ImageTlsDirectory

Access the IMAGE_TLS_DIRECTORY from the data directory.

Declaration
public ImageTlsDirectory? ImageTlsDirectory { get; }
Property Value
Type Description
ImageTlsDirectory
| Edit this page View Source

ImpHash

The Import Hash of the binary if any imports are given else null;

Declaration
public string? ImpHash { get; }
Property Value
Type Description
string
| Edit this page View Source

ImportedFunctions

Access the imported functions as an array of parsed objects.

Declaration
public ImportFunction[]? ImportedFunctions { get; }
Property Value
Type Description
ImportFunction[]
| Edit this page View Source

Is32Bit

Returns true if the PE file is x32.

Declaration
public bool Is32Bit { get; }
Property Value
Type Description
bool
| Edit this page View Source

Is64Bit

Returns true if the PE file is x64.

Declaration
public bool Is64Bit { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsAuthenticodeSigned

Returns true if the PE file is signed. It does not check if the signature is valid!

Declaration
public bool IsAuthenticodeSigned { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsDll

Returns true if the DLL flag in the File Header is set.

Declaration
public bool IsDll { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsDotNet

Returns true if the file is a .NET executable.

Declaration
public bool IsDotNet { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsDriver

Returns true if the PE file is a system driver based on the Subsytem = 0x1 value in the Optional Header.

Declaration
public bool IsDriver { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsExe

Returns true if the Executable flag in the File Header is set.

Declaration
public bool IsExe { get; }
Property Value
Type Description
bool
| Edit this page View Source

IsTrustedAuthenticodeSignature

Returns true of the PE file signature is trusted based on the installed certificates on the system.

Declaration
public bool IsTrustedAuthenticodeSignature { get; }
Property Value
Type Description
bool
| Edit this page View Source

Md5

The MD5 of hash sum of the binary.

Declaration
public string? Md5 { get; }
Property Value
Type Description
string
| Edit this page View Source

MetaDataHdr

Access the MetaDataHdr from the COM/CLI header.

Declaration
public MetaDataHdr? MetaDataHdr { get; }
Property Value
Type Description
MetaDataHdr
| Edit this page View Source

MetaDataStreamBlob

Meta Data Stream #Blob as an byte array.

Declaration
public byte[]? MetaDataStreamBlob { get; }
Property Value
Type Description
byte[]
| Edit this page View Source

MetaDataStreamGuid

Meta Data Stream #GUID.

Declaration
public MetaDataStreamGuid? MetaDataStreamGuid { get; }
Property Value
Type Description
MetaDataStreamGuid
| Edit this page View Source

MetaDataStreamString

Meta Data Stream #String.

Declaration
public MetaDataStreamString? MetaDataStreamString { get; }
Property Value
Type Description
MetaDataStreamString
| Edit this page View Source

MetaDataStreamTablesHeader

Access the Meta Data Stream Tables Header from the list of Meta Data Streams of the .Net header.

Declaration
public MetaDataTablesHdr? MetaDataStreamTablesHeader { get; }
Property Value
Type Description
MetaDataTablesHdr
| Edit this page View Source

MetaDataStreamUs

Meta Data Stream #US (User strings).

Declaration
public MetaDataStreamUs? MetaDataStreamUs { get; }
Property Value
Type Description
MetaDataStreamUs
| Edit this page View Source

RawFile

The PE binary.

Declaration
public IRawFile RawFile { get; }
Property Value
Type Description
IRawFile
| Edit this page View Source

Resources

Access resources of the PE file.

Declaration
public Resources? Resources { get; }
Property Value
Type Description
Resources
| Edit this page View Source

Sha1

The SHA-1 hash sum of the binary.

Declaration
public string? Sha1 { get; }
Property Value
Type Description
string
| Edit this page View Source

Sha256

The SHA-256 hash sum of the binary.

Declaration
public string? Sha256 { get; }
Property Value
Type Description
string
| Edit this page View Source

SigningAuthenticodeCertificate

Signing X509 Authenticode certificate the binary was signed with

Declaration
public X509Certificate2? SigningAuthenticodeCertificate { get; }
Property Value
Type Description
X509Certificate2
| Edit this page View Source

TypeRefHash

Compute the TypeRefHash for .NET PE files. If not available, return null.

Declaration
public string? TypeRefHash { get; }
Property Value
Type Description
string
| Edit this page View Source

WinCertificate

Access the WinCertificate from the Security header.

Declaration
public WinCertificate? WinCertificate { get; }
Property Value
Type Description
WinCertificate

Methods

| Edit this page View Source

AddImport(string, string)

Add a new import to the PE file. If you intend to add multiple imports, use "AddImports" instead.

Declaration
public void AddImport(string module, string function)
Parameters
Type Name Description
string module
string function
| Edit this page View Source

AddImports(List<AdditionalImport>)

Add imports to the PE file.

Declaration
public void AddImports(List<AdditionalImport> additionalImports)
Parameters
Type Name Description
List<AdditionalImport> additionalImports

List with additional imports.

| Edit this page View Source

AddSection(string, byte[], ScnCharacteristicsType)

Add a new section to the PE file.

Declaration
public void AddSection(string name, byte[] unalignedSize, ScnCharacteristicsType characteristics)
Parameters
Type Name Description
string name

Name of the section to add. At max. 8 characters.

byte[] unalignedSize

Size in bytes of the new section.

ScnCharacteristicsType characteristics

Section characteristics.

| Edit this page View Source

AddSection(string, int, ScnCharacteristicsType)

Declaration
public void AddSection(string name, int unalignedSize, ScnCharacteristicsType characteristics)
Parameters
Type Name Description
string name
int unalignedSize
ScnCharacteristicsType characteristics
| Edit this page View Source

GetCrlUrlList()

Get an object which holds information about the Certificate Revocation Lists of the signing certificate if any is present.

Declaration
public CrlUrlList? GetCrlUrlList()
Returns
Type Description
CrlUrlList

Certificate Revocation List information or null if binary is not signed.

| Edit this page View Source

GroupIcons()

Reads the corresponding IDs from GroupIconDirectoryEntry. Collects the Icons corresponding to the IDs as byte array.

Declaration
public IEnumerable<IEnumerable<byte[]>> GroupIcons()
Returns
Type Description
IEnumerable<IEnumerable<byte[]>>

An enumerable of enumerable of byte arrays with icons corresponding to the individual GroupIcons, an empty enumerable if no GroupIcons or only empty GroupIcons are included.

| Edit this page View Source

HasValidAuthenticodeCertChain(bool)

Checks if cert is from a trusted CA with a valid certificate chain.

Declaration
public bool HasValidAuthenticodeCertChain(bool useOnlineCrl)
Parameters
Type Name Description
bool useOnlineCrl

Check certificate chain online or offline.

Returns
Type Description
bool

True if cert chain is valid and from a trusted CA.

| Edit this page View Source

HasValidAuthenticodeCertChain(X509Certificate2?, TimeSpan, bool, bool)

Checks if cert is from a trusted CA with a valid certificate chain.

Declaration
public static bool HasValidAuthenticodeCertChain(X509Certificate2? cert, TimeSpan urlRetrievalTimeout, bool useOnlineCRL = true, bool excludeRoot = true)
Parameters
Type Name Description
X509Certificate2 cert

X509 Certificate

TimeSpan urlRetrievalTimeout

Timeout to validate the certificate online.

bool useOnlineCRL

If true, uses online certificate revocation lists, else on the local CRL.

bool excludeRoot

True if the root certificate should not be validated. False if the whole chain should be validated.

Returns
Type Description
bool

True if cert chain is valid and from a trusted CA.

| Edit this page View Source

Icons()

Reads the location of the Icons from the ResourceDirectory in PeFile, collects the corresponding bytes in an enumerable and adds an ICO-Header to the bytes of each icon. This does not depend on the icon having a group icon directory entry.

Declaration
public IEnumerable<byte[]> Icons()
Returns
Type Description
IEnumerable<byte[]>

An enumerable of icon byte arrays with an ICO-Header, an empty enumerable if no Icons are included.

| Edit this page View Source

IsPeFile(MMFile)

Tests if a file is a PE file based on the MZ header. It is not checked if the PE file is correct in all other parts.

Declaration
public static bool IsPeFile(MMFile file)
Parameters
Type Name Description
MMFile file

MMFile of a possible PE file.

Returns
Type Description
bool

True if the MZ header is set.

| Edit this page View Source

IsPeFile(Stream)

Tests if a file is a PE file based on the MZ header. It is not checked if the PE file is correct in all other parts.

Declaration
public static bool IsPeFile(Stream file)
Parameters
Type Name Description
Stream file

Stream of a possible PE file.

Returns
Type Description
bool

True if the MZ header is set.

| Edit this page View Source

IsPeFile(Span<byte>)

Tests is a buffer is a PE file based on the MZ header. It is not checked if the PE file is correct in all other parts.

Declaration
public static bool IsPeFile(Span<byte> buf)
Parameters
Type Name Description
Span<byte> buf

Byte array containing a possible PE file.

Returns
Type Description
bool

True if the MZ header is set.

| Edit this page View Source

IsPeFile(string)

Tests if a file is a PE file based on the MZ header. It is not checked if the PE file is correct in all other parts.

Declaration
public static bool IsPeFile(string file)
Parameters
Type Name Description
string file

Path to a possible PE file.

Returns
Type Description
bool

True if the MZ header is set.

| Edit this page View Source

RemoveSection(string, bool)

Remove a section from the PE file.

Declaration
public void RemoveSection(string name, bool removeContent = true)
Parameters
Type Name Description
string name

Name of the section to remove.

bool removeContent

Flag if the content should be removed or only the section header entry.

| Edit this page View Source

TryParse(MMFile, out PeFile?)

Try to parse the PE file. Best option for large files, as a memory mapped file is used.

Declaration
public static bool TryParse(MMFile file, out PeFile? peFile)
Parameters
Type Name Description
MMFile file

Memory mapped file containing a possible PE file.

PeFile peFile

Parsed PE file or Null.

Returns
Type Description
bool

True if parable PE file and false if not.

| Edit this page View Source

TryParse(byte[], out PeFile?)

Try to parse the PE file.

Declaration
public static bool TryParse(byte[] buff, out PeFile? peFile)
Parameters
Type Name Description
byte[] buff

Buffer containing a possible PE file.

PeFile peFile

Parsed PE file or Null.

Returns
Type Description
bool

True if parable PE file and false if not.

| Edit this page View Source

TryParse(Stream, out PeFile?)

Try to parse the PE file.

Declaration
public static bool TryParse(Stream file, out PeFile? peFile)
Parameters
Type Name Description
Stream file

Stream containing a possible PE file.

PeFile peFile

Parsed PE file or Null.

Returns
Type Description
bool

True if parable PE file and false if not.

| Edit this page View Source

TryParse(string, out PeFile?)

Try to parse the PE file. Reads the whole file content into memory.

Declaration
public static bool TryParse(string file, out PeFile? peFile)
Parameters
Type Name Description
string file

Path to a possible PE file.

PeFile peFile

Parsed PE file or Null.

Returns
Type Description
bool

True if parable PE file and false if not.

  • Edit this page
  • View Source
In this article
Back to top Generated by DocFX