Exports Modification

PE Resources Overview

LIEF provides extensive support for modifying the PE’s export table giving you the ability to add, remove, or modify export entries or to create the entire export table in a PE Binary.

Creating Export Entries

Creating a can be useful for exposing a “hidden” function based on its address and to leverage its functionality as a regular linker-generated export.

This could be used for code lifting or fuzzing.

import lief

pe: lief.PE.Binary = ...

exp: lief.PE.Export = pe.get_export()

# Remove an entry
exp.remove_entry("my_exported_name")

# Add a new export
exp.add_entry("fuzz_me", 0x10010)

config = lief.PE.Builder.config_t()
config.exports = True
config.export_section = ".myedata" # optional

pe.write("out.dll", config)

Creating an Export Table

This section introduces the API for creating an export table. We’ll explore a scenario where we want to convert a PE executable into a DLL.

Note

The process of converting an executable to a library is also detailed for ELF binary in the tutorial: 08 - Transforming an ELF executable into a library.

First, we need to update PE headers to ensure they are compliant with the DLL format:

import lief

pe: lief.PE.Binary = ...

pe.header.add_characteristic(lief.PE.Header.CHARACTERISTICS.DLL)
pe.optional_header.addressof_entrypoint = 0

Then, we can start creating and filling a new Export Table:

exp = lief.PE.Export("lib_exe2dll.dll", [
    lief.PE.ExportEntry("cbk1", 0x0001000),
    lief.PE.ExportEntry("cbk2", 0x0001010),
])

pe.set_export(exp)

config = lief.PE.Builder.config_t()
config.exports = True

pe.write("lib_exe2dll.dll")

Limitations

This binary-to-library example assumes that the original executable has been compiled to be position-independent which means that it contains relocations.

Within a Python environment, we can check that lib_exe2dll.dll can be loaded as a DLL and that we can call cbk1 and cbk2:

import ctypes

lib = ctypes.windll.LoadLibrary("lib_exe2dll.dll")

assert lib.cbk1() >= 0
assert lib.cbk2() >= 0