Resources Modification

PE Resources Overview

LIEF allows to modify (or create) PE resources at different levels:

Binary Tree Modifications

import lief

pe: lief.PE.Binary = ...

rsrc: lief.PE.ResourceNode = pe.resources

print(rsrc)
rsrc: lief.PE.ResourceNode = pe.resources

dir_node = lief.PE.ResourceDirectory(100)
data_node = lief.PE.ResourceData([1, 2, 3])

rsrc.add_child(dir_node).add_child(data_node)

This low-level API can be used to modify the tree or changing the data of a specific node.

Pretty Printing

One can also print a node to get a pretty representation of the resources tree:

pe: lief.PE.Binary = ...
tree = pe.resources
print(tree)
├── Directory ID: 0000 (0x0000)
│  ├── Directory ID: 0016 (0x0010) type: VERSION
│  │  └── Directory ID: 0001 (0x0001)
│  │      └── Data ID: 0000 (0x0000) Lang: 0x00 / Sublang: 0x00 length=772 (0x000304), offset: 0x1ca0
│  │          ├── Hex: 04:03:34:00:00:00:56:00:53:00:5f:00:56:00:45:00:52:00:53:00
│  │          └── Str: ..4...V.S._.V.E.R.S.
│  └── Directory ID: 0024 (0x0018) type: MANIFEST
│      └── Directory ID: 0001 (0x0001)
│          └── Data ID: 1033 (0x0409) Lang: 0x09 / Sublang: 0x01 length=1900 (0x00076c), offset: 0x1fa4
│              ├── Hex: 3c:61:73:73:65:6d:62:6c:79:20:78:6d:6c:6e:73:3d:22:75:72:6e
│              └── Str: <assembly xmlns="urn

Resources Manager

The is used to expose a higher-level API on the resource tree. It can also be used to set or change resource elements such as the manifest:
import lief

pe: lief.PE.Binary = ...

manager: lief.PE.ResourcesManager = pe.resources_manager

manager.manifest = """
<?xml version="1.0" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1"
          manifestVersion="1.0">
  <trustInfo>
    <security>
      <requestedPrivileges>
         <requestedExecutionLevel level='asInvoker' uiAccess='false'/>
      </requestedPrivileges>
    </security>
  </trustInfo>
</assembly>
"""

pe.write("new.exe")

Resource Tree Transfer between Binaries

LIEF can transfer the resource tree from one binary to another. This operation can be achieved by using the function:
import lief

from_pe: lief.PE.Binary = ...
to_pe: lief.PE.Binary = ...

to_pe.set_resources(from_pe.resources)
to_pe.write("new.exe")