Skip to content

Python development

Joachim Metz edited this page Jul 14, 2022 · 1 revision

libwrc comes with Python-bindings named pywrc.

Below are examples how use pywrc. They assume you have a working version of pywrc on your system. To build pywrc see Building.

Import

To be able to use pywrc in your Python scripts add the following import:

import pywrc

Get version

The get_version() module function can be used to retrieve the version of the pywrc.

pywrc.get_version()

This will return a textual string (Unicode) that contains the libwrc version. Since pywrc is a wrapper around libwrc it does not have a separate version.

Open a .rsrc section from an exe file

import pyexe
import pywrc

exe_file = pyexe.file()
exe_file.open("cmd.exe")
exe_section = exe_file.get_section_by_name('.rsrc')

wrc_stream = pywrc.stream()

wrc_stream.set_virtual_address(exe_section.virtual_address)
wrc_stream.open_file_object(exe_section)

wrc_stream.close()
exe_file.close()

Note that the explicit calls to close are not required.

Resources

A pywrc.resource is the base object for the different resources stored in a PE/COFF executable stream.

Get number of resources

The number of resources can be retrieved by either calling the get_number_of_resources() function, e.g.

number_of_resources = wrc_stream.get_number_of_resources()

or reading the number_of_resources property:

number_of_resources = wrc_stream.number_of_resources

Get a resource by index

A resource can be retrieved by index:

wrc_resource = wrc_stream.get_resource(0)

The function will return the most specific item object type. This will either be pywrc.item, pywrc.manifest, pywrc.message_table, pywrc.string. Where pywrc.resource is the base type for the other resource types.

Get a resource by identifier

A resource can be retrieved by its numeric identifier:

RESOURCE_IDENTIFIER_MESSAGE_TABLE = 0x0b

wrc_resource = wrc_stream.get_resource(RESOURCE_IDENTIFIER_MESSAGE_TABLE)

Also see

import pywrc

help(pywrc)
help(pywrc.stream)
help(pywrc.resource)
help(pywrc.manifest)
help(pywrc.message_table)
help(pywrc.string)